//Given the cards already played by all players and a player's hand, determines the legal cards which can be played by the player CardList GameLogic::legalMoves(const CardList& table, const CardList& hand) { CardList legalMoves; // used to store legal cards // In the case the table is empty, this means this play has 7 of spades, so he has to be forced to play it if(table.size() == 0) { // finds the 7S card, so we only got one instance of it int index = hand.find(Card(SPADE, SEVEN)); const Card* sevenSpades = hand[index]; legalMoves.add(sevenSpades); return legalMoves; } // loops through hand to determine which ones are legal to play for (int index = 0; index < hand.size(); index++) { const Card * card = hand[index]; // Stores the index of lower rank and higher rank card to determine whether it exists or not int lowerRankIndex = -1; int higherRankIndex = -1; // Determines whether a lower rank or higher rank card exists if (card->getRank() != ACE) { Card lowerRank = Card(card->getSuit(), (Rank) (card->getRank() - 1)); lowerRankIndex = table.find(lowerRank); } if (card->getRank() != KING) { Card higherRank = Card(card->getSuit(), (Rank)(card->getRank() + 1)); higherRankIndex = table.find(higherRank); } // Determines whether the current card is legal to play if (card->getRank() == SEVEN || lowerRankIndex !=-1 || higherRankIndex != -1) { legalMoves.add(card); } } return legalMoves; }
// Plays the given card in the game void Role::playCard(const Card& card) { const CardList legalCards = legalMoves(); // gets the card which are legal to play // Determines whether the card being played is legal to play, otherwise exception is thrown int cardIndex = legalCards.find(card); if(cardIndex == -1) { throw IllegalPlayException(); } //Removes the card from the hand, and returns it so it can be added to the table const Card* played = player_->hand_.remove(card); player_->getGame()->addToTable(played); triggerPlayerUpdate(true); }