Relatively new to programming in general... I need help:
I'm trying to make a program with a deck of cards (note, program has a lot of stuff to go that is unfinished but I'm just trying to work bit by bit and make things work)... and there are two lines that I can't figure out, with the compiler telling me:
Driver.java:35: cannot find symbol
symbol : variable length
location: class Hand
for (int count = 0; count < theHand.length; count++){
and
Driver.java:36: array required, but Hand found
System.out.println("The "+ theHand[count] +".");
Basically for some reason I think the program is treating theHand, which is a "Hand" class, which is an array, as something else, but I dunno why or how.
So I guess I'll post most of my code, I dunno what else to do:
Drive with the problems (some stuff cut out):
public class Driver {
//----------------------------------------
// Runs estimates of the probability of
// given card combinations by dealing
// hands of 7 cards and checking for card
// combinations (repeating many times).
//----------------------------------------
public static void main(String[] args) {
// Create a Hand, and declare variables
// to count card combinations and keep
// track of trials.
Hand theHand = new Hand();
int threeOfAKind = 0;
int fourOfAKind = 0;
int runOfFour = 0;
final int MAX_SHUFFLE = 5;
final int MAX_TRIALS = 100000;
// Testing shuffle method:
for (int trials = 1; trials <= MAX_SHUFFLE; trials++) {
theHand.reset();
theHand.fillHand();
System.out.println("Here is a hand after shuffle # "+trials+":");
for (int count = 0; count < theHand.length; count++){
System.out.println("The "+ theHand[count] +".");
}
}
}
The relevant methods of my Hand class:
public class Hand {
private Card[] hand;
private Deck theDeck;
//----------------------------------
// Creates a new instance of Hand,
// with no cards in it.
//----------------------------------
public Hand() {
hand = new Card[7];
theDeck = new Deck();
}
//----------------------------------
// Fills hand with 7 cards from the
// top of the deck.
//----------------------------------
public void fillHand() {
int index = 0;
while (index < 7) {
hand[index] = theDeck.deal();
index++;
}
}
//----------------------------------
// Empties the hand/resets the hand
// and reshuffles the deck.
//----------------------------------
public void reset() {
hand = new Card[7];
theDeck.shuffle();
}
}
Deck:
public class Deck {
private Card[] deck;
private int dealIndex;
//----------------------------------
// Creates a new instance of Deck,
// and shuffles it.
//----------------------------------
public Deck() {
deck = new Card[52];
dealIndex = 0;
int index = 0;
for (int suit = 1; suit < 5; suit++)
for (int rank = 1; rank <14; rank++){
deck [index] = new Card(rank, suit);
index++;
}
shuffle();
}
//-----------------------------------
// Shuffles the deck so the order of
// cards is random.
//-----------------------------------
public void shuffle() {
int count = 0;
while (count <= 250) {
// Picks 2 random index numbers from 0 to 51
int randomIndexOne = (int) (52*Math.random());
int randomIndexTwo = (int) (52*Math.random());
// Switch the two cards at the random indices.
Card tempCard = deck[randomIndexOne];
deck[randomIndexOne] = deck [randomIndexTwo];
deck[randomIndexTwo] = tempCard;
count++;
// Set the dealIndex to 0 so that when you
// deal you deal from the top of the reshuffled
// deck.
dealIndex = 0;
}
}
//-----------------------------------
// Deals a card from the deck, from
// the "top" down.
//-----------------------------------
public Card deal() {
// Ensure you're not at the bottom of the deck
if (dealIndex == 51) {
shuffle();
}
// Return the card in the deck that corresponds
// to the dealIndex, and up the dealIndex so the
// next time you deal it is the next card.
dealIndex++;
return deck[dealIndex-1];
}
}
Card (maybe the problem has to do with how I pass the card to string)?
public class Card {
// Declare the two variables that define
// each card.
private int suit;
private int rank;
// Assign numerical values to the
//names of suits for organization purposes.
private final int CLUBS = 1,
DIAMONDS = 2,
HEARTS = 3,
SPADES = 4;
//----------------------------------------------
// Creates a new instance of Card, with
// the specified integer input values for
// rank (1-13) and suit (0-3).
// (Ace = 1, Jack = 11, Queen = 12, King = 13)
//----------------------------------------------
public Card(int cardRank, int cardSuit) {
suit = cardSuit;
rank = cardRank;
}
//-------------------------------------------
// Returns integer value of rank.
//-------------------------------------------
public int getRank() {
return rank;
}
//-------------------------------------------
// Returns integer value of suit.
//-------------------------------------------
public int getSuit() {
return suit;
}
//---------------------------
// Returns suit as string.
//---------------------------
public String getSuitString() {
switch (suit) {
case 1: return "Ace";
case 2: return "two";
case 3: return "three";
case 4: return "four";
case 5: return "five";
case 6: return "six";
case 7: return "seven";
case 8: return "eight";
case 9: return "nine";
case 10: return "ten";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "Invalid Rank";
}
}
//---------------------------
// Returns rank as string.
//---------------------------
public String getRankString() {
switch (suit) {
case CLUBS: return "Clubs";
case DIAMONDS: return "Diamonds";
case HEARTS: return "Hearts";
case SPADES: return "Spades";
default: return "Invalid Suit";
}
}
//---------------------------------
// Returns a string for Card.
//---------------------------------
public String toString() {
return getRankString() + "of" + getSuitString();
}
}
Yeah, I don't really understand.... nor do I expect any of you to read all that goddamn code, but I didn't know what part is causing the problem!
Also, just for learning purposes, is this a bad way to organize my program and classes and stuff?
EDIT: SHIT why the hell did it format ridiculously stupidly in the post like that?!?!?!!?
Posts
1.
.length isn't an attribute of hand
2.
Driver.java:36: array required, but Hand found
System.out.println("The "+ theHand[count] +".");
theHand[count] will return a Hand, not a string. So I don't think you can use it like that.
took out her barrettes and her hair spilled out like rootbeer
I was confused about the theHand[number] thing, because I assume that a card would be in that spot...? I tried to make a new card like Card tempCard = theHand[number] but that doesn't work...
EDIT: Well, I believe I fixed this part by making a method in the hand class that passes through the card value given an input index value. Whee ?!
Are you maybe confusing yourself by having a class called Hand which has a class variable called hand? You can't refer to a class variable from outside the class if the class variable is declared private. If you had declared it public, you would refer to the array class variable hand like theHand.hand[someNumber], and theHand.hand.length.
http://www.thelostworlds.net/
I'm a moron. Well, thanks. I see my sillyness.
EDIT: Actually, which is better, using a method like I did in my work around to my error, to pass the object through, or making the variable public? For the most part our teacher was telling us to use methods as accessors and mutators for instance/class variables, and keep them private.
Correct. Thats called data-hiding, and is a very good practice to keep. I tend to use the format getX() and setX() in my coding, gets being accessor, which would look like:
And likewise for the setX():
Keep your shit hidden so people can't just fuck with your data through some random fuck up, but allow for methods to be able to change that data.
See how many books I've read so far in 2010
My understanding is that it's not really about people, it's about other parts of your program code. If you're going to change a variable in an object, it's a good idea to do it through a method rather than a public variable because you may need to take some action immediately when that value is changed.
It also lets you only allow variables to be read, not changed. That's mainly what I use the C# built-in equivalent (Properties) for.
http://www.thelostworlds.net/
It looks like you get the basic idea of OOP. When I took intro programming classes, a lot of people had trouble wrapping their heads around that.
Even if getVar() and setVar() may just be simple wrappers for a variable, you can change that variable without changing the rest of the program so long as the public interface stays the same.
See how many books I've read so far in 2010
The hardest part of learning OO based programming is learning how you can use Objects and treat them as things instead of primitive types. Many of my classmates struggled to find ways to figure out my ComS class's assignments. Something as large as a competition of players playing against each other seems difficult if you try to look at the smallest components it'll need, but is easy when you learn how to build classes efficiently. I wish I copied down my instructor's notes when he did a refresher for our ComS228 class with a top-down structure of different card games, i'd give them to you. All my notes is over generics, which you don't want to deal with yet.