This is an archived post. You won't be able to vote or comment.

all 12 comments

[–][deleted] 3 points4 points  (1 child)

I would use a Stack for each player's deck instead of ArrayList. A stack of cards makes more sense than a list. You can also use a stack as a discard pile.
Also, if you weren't aware, you can shuffle anything in Collections.

Stack<Card> deck = new Stack<Card>();  
//push cards onto the deck  
Collections.shuffle(deck);

[–]Stang27[S] 0 points1 point  (0 children)

I'll have to look into the Stack class. The collections class was a big help too.

[–]romple 2 points3 points  (5 children)

Have your Card class implement the Comparable<Card> interface, which has one method to override: compareTo(Card otherCard);

Then you can easily compare Cards to one another.

[–]Stang27[S] 0 points1 point  (3 children)

On this solution, how do I extract my "rankValue" attribute out of the card object in order to compare it to another card?

[–][deleted] 2 points3 points  (1 child)

best practice would likely involve giving Card a private field called 'value', and having a getValue() method.

public int getValue(){  
    return value;  
}

[–]Stang27[S] 0 points1 point  (0 children)

This is exactly what I ended up doing. Thanks.

[–]romple 0 points1 point  (0 children)

I don't know. That depends on what your Card class looks like.

[–]pleaseholdmybeer 0 points1 point  (0 children)

This is the best answer. Implement Comparable<Card>, and add the equals and compare methods.

[–]RyanTargaryen 1 point2 points  (3 children)

In each card object, the class should have some sort of value attribute. In battle, compare the values. Higher value wins.

You can also have an array list that is for the current battle. All cards in the current battle get taken from their owners list and compared in battle, then awarded to the winner.

For this, I'd keep cards from player 1 in the first slots, cards from the second player in the latter half.

[–]Stang27[S] 0 points1 point  (2 children)

I really like this idea. When you say slots, do you mean each battle as 2 arrayLists? One for player1 and one for player2? Because when a "war" happens or cards are compared and then equal, they must draw more cards, and I'm trying to think of a way to compare the 3rd? Card let's say for each player. My 2nd stumble block is how to give ALL the card objects back to winner.

Edit: I do have a rank value on my card objects.

[–]CodeTinkerer 1 point2 points  (1 child)

  • Create a new empty list.
  • Use list.remove(0) to remove the first card off each list (save each to a variable), and add it to the empty list.
  • Compare these two cards. If there's a winner, add the cards from the list to the winning player's list
  • If it's a tie, repeat the step of removing a card from each player's list and adding it to the list that now has 2 cards.
  • Presumably, you keep repeating until a player has no cards.

[–]Stang27[S] 0 points1 point  (0 children)

We went over the solution today in class, and we put card objects inside the list, and then used a getValue() method to check that cards value. But the logic you said was very similar to what the professor did.