0

So I have a Hand object that is an array of Card objects. Here's my constructor for the Hand:

public Hand(){
    Card[] Hand = new Card[5];
}

And here's my constructor for the Card:

   public Card(int value, char suit){
    if (value < 0 || value > 13)
    {
      System.err.println("Twilight Zone Card");
      System.exit(1);
    }
    this.value = value;

    if (suit == 'c')
      suit = 'C';
    if (suit == 'd')
      suit = 'D';
    if (suit == 'h')
      suit = 'H';
    if (suit == 's')
      suit = 'S';

    if (suit == 'C' || suit == 'D' || suit == 'H' || suit == 'S')
    {
      this.suit = suit;
    }
    else
    { 
      System.err.println("No such suit.");
      System.exit(1);
    } 
 }

The game I have to make is go fish, so sometimes I need to pull the particular card object out of the hand to compare it or print it etc. So once I instantiate a Hand in my main class, it treats it as an object and not an array. So how am I supposed to pull the cards for different locations in the hand? Like I can't do:

Hand Player1 = new Hand();
Hand Player2 = new Hand();
if (Player1[2] == Player2[2])....

So I tried to make a getCard function in the Hand class, but I don't know how to access like, say the second card in the hand, since it won't let me do hand[2] since it doesn't treat it as an array. I'm struggling so hard right now. What should I do?

007
  • 21
  • 3
  • `Player1` is not declared as an array type. You cannot use the `[index]` notation. – Sotirios Delimanolis Oct 16 '13 at 03:10
  • Also, the way you have your constructor currently set up, `Card[] Hand = new Card[5];` is a local variable and basically goes bye-bye as soon as that constructor is done. – Dennis Meng Oct 16 '13 at 03:12
  • 1
    I _love_ the `System.exit()` as exceptional state handling but have you tried throwing an `Exception` instead? – Boris the Spider Oct 16 '13 at 03:14
  • In your code snippet, Hand is a class. Hand objects are supposed to HAVE a field called Hand (bad naming scheme, at least start with lowercase 'h'). A Hand object IS not an array. But you declared the array as a local variable in the constructor, not as a field in the class (`Card[] hand;` `hand = new Card[7]`). – clwhisk Oct 16 '13 at 05:48

2 Answers2

2
public class Hand {
    Card[] hand;

    public Hand() {
        hand = new Card[5];
    }

    public Card getCard(int index) {
        return hand[index];
    }
}

player1.getCard(2).equals(player2.getCard(2)) // avoid using "==" to test equality unless you know what you are doing.

EDIT:

In java, "==" can use to test primitive values, but not objects unless you do want to test if they are the identical objects, you can find tons of great answers about equality test in java.

Java String.equals versus ==

So you have to implement/override the proper methods in Card to test Card equality.

Community
  • 1
  • 1
xiaowl
  • 5,177
  • 3
  • 27
  • 28
0

Firstly you need to ovverride equals and hachCode in your Card class.

public int hashCode(){
   // its simple but just solve the purpose
   return value + suit;
}

public boolean equals(Object other){
   // Check for null, object type...
   Card otherCard = (Card)  other;
   return this.value==otherCard .value && this.suit==otherCard.suit;
}

Now its safe to use the Card type while comparing its two instances and using in collections like List, Set...

In Hand class have accessor method for card at a given index.

class Hand{
    // your code.
    public Card getCardAtIndex(int i){
         // check size, null
          return theCardArray[i];
    }
}
Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86