Problem Statement
A deck of cards contains 52 cards, representing all combinations of 13 different ranks (in descending order: Ace, King, Queen, Jack, Ten, 9, 8, 7, 6, 5, 4, 3, 2) and 4 different suits (clubs, diamonds, hearts, spades). One of the four suits is chosen as the trump suit.
Four players are playing a card game. We will be analyzing a single trick. In one trick, every player gets to play one card from his hand. The first player may play any card he wishes. The other players must play by the following rules:
- If a player has a card of the same suit as the first player's
card, he must play it.
If the trick hasn't been broken (see rule 2) and he has a card of higher rank than the highest played so far (considering only cards of the required suit), then he must play such a card (he may play any card of higher rank than the highest so far).
If the trick has been broken or he doesn't have a card strong enough, he may play any card of the required suit. - If a player doesn't have a card of the required suit, then
he must play a card of the trump suit if he has one. When the
first trump card in a trick is played, the trick
is broken.
If the trick was already broken, then the player has to play a trump of higher rank if he has one. - Otherwise (the player has no cards the same suit as the first card played, and has no trumps), he may play any card.
One of the players is too lazy to think so he wants help in determining which cards he may play.
You will be given a String trump and two String[]s, cardsPlayed and hand. trump represents the chosen trump suit and will be "clubs", "diamonds", "hearts" or "spades" (quotes for clarity). cardsPlayed contains the cards played so far in the trick, in the order in which they were played. hand contains the cards the lazy player has in his hand.
Each card in the input will be represented as a string of 2 characters, the rank (one of the characters 'A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2') followed by the suit ('C', 'D', 'H' or 'S').
Return a String[] containing all cards the lazy player may play. The cards should be in the same relative order as in hand.
Definition
- Class:
- LazyCardPlayer
- Method:
- whichCards
- Parameters:
- String, String[], String[]
- Returns:
- String[]
- Method signature:
- String[] whichCards(String trump, String[] cardsPlayed, String[] hand)
- (be sure your method is public)
Constraints
- trump will be "clubs", "diamonds", "hearts" or "spades".
- cardsPlayed will contain between 0 and 3 elements, inclusive.
- hand will contain between 1 and 13 elements, inclusive.
- Each element of cardsPlayed and hand will represent a single card, formatted as described in the problem statement.
- No two cards in all of cardsPlayed and hand will be the same.
Examples
"clubs"
{ }{ "TH", "9C", "8D", "2S", "4D" }Returns: {"TH", "9C", "8D", "2S", "4D" }No cards have been played so far, which means the lazy player is the first to play. The first player may play any card."clubs"
{ "8S" }{ "4H", "5S", "7S" }Returns: {"5S", "7S" }The first player opened spades. The lazy player doesn't have a card stronger than the 8, so he may play either of his cards."diamonds"
{ "4H", "9H" }{ "AH", "5H" }Returns: {"AH" }"hearts"
{ "6S", "9S", "9C" }{ "TC", "AH", "5H" }Returns: {"AH", "5H" }With no spades, the player has to break the trick using one of his cards in the trump suit (hearts)."hearts"
{ "6S", "9S", "KH" }{ "TC", "AH", "5H" }Returns: {"AH" }This time the trick has already been broken by the third player, so the lazy player must play his ace to beat the king."hearts"
{ "6S", "9S", "KH" }{ "JS", "4S", "3S", "2H" }Returns: {"JS", "4S", "3S" }The player must play spades, but doesn't need to throw the strongest card because the trick has been broken."diamonds"
{ "QD" }{ "JD", "KC", "4D" }Returns: {"JD", "4D" }