Newyo
Newyo

Reputation: 187

Java data structure for counting card in players hand

the player always have 4 card, and they are stored in an array, the player needs to select the most unwanted card and return the index of the unwanted card(from 0 to 3), and the rule follows

  1. If a single rank appears the most in the player’s hand, then a card of differing rank is selected randomly.
  2. Otherwise, a card is selected randomly from the player’s entire hand.

I have spend two hours on it, the problem is that I need to know occurrence of each card rank and their position in the array, so I used

Map<Rank, ArrayList<Integer>>

and the decision making part is extremely messy, is there a better way to do this?

example:

if my hand is A,10,10,A then I randomly select one. if my hand is 3,3,3,A then I select A if my hand is 2,2,4,5 then I randomly select 4,or 5. if my hand is A,2,3,4 then I randomly select one.

Upvotes: 0

Views: 217

Answers (1)

Gilbert Le Blanc
Gilbert Le Blanc

Reputation: 51445

You need to define model classes, instead of trying to keep track of everything in maps of array lists.

public class Card {
    private String rank;
    private String value:
}

.

public class Hand {
    private List<Card> cards;
}

.

public class Player {
    private Hand hand;
}

.

public class CardGame {
    private List<Player> players;
}

You can fill out these model classes with the rest of the game information.

Upvotes: 1

Related Questions