void GenericPlayer::CalculateLegalPlays(std::vector<Card> CardsOnTable, bool StarterPlayer){ int MinValues[4] = {-1,-1,-1,-1}; //need to be -1 by default so that when onTable is empty ACE is not added to Legal Plays int MaxValues[4] = {-1,-1,-1,-1}; int Ranks[13] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; Hand* LegalPlays = new Hand(); for(int i = 0; i < CardsOnTable.size(); i++){ for(int j = 0; j < 4; j++){ if(CardsOnTable.at(i).getSuit() == (Suit)j){ int rank = (Rank)CardsOnTable.at(i).getRank() + 1; //cout<<"Rank value"<<CardsOnTable.at(i).GetRankValue()<<endl; // this returns something really insane if(MinValues[j] == -1){ MinValues[j] =rank; }else if(rank< MinValues[j]) MinValues[j] =rank; if(MaxValues[j] == -1) MaxValues[j] = rank; else if(rank> MaxValues[j]) MaxValues[j] = rank; } } } std::vector<Card> PlayerCards = GetCurrentDeck()->GetCards(); for(int i = 0 ; i < GetCurrentDeck()->GetNumCards();i++) { if(PlayerCards.at(i).getRank()==SEVEN) { if(StarterPlayer) // if he is the starter player, his ONLY play available should be 7S { if(PlayerCards.at(i).getSuit()==SPADE) LegalPlays->AddCard(PlayerCards.at(i)); } else { LegalPlays->AddCard(PlayerCards.at(i)); } } else { for(int j = 0; j < 4; j++) { if(PlayerCards.at(i).getSuit() == (Suit)j){ int value = PlayerCards.at(i).GetRankValue(); if(value == MinValues[j] -1 || value == MaxValues[j] + 1) { LegalPlays->AddCard(PlayerCards.at(i)); } } } } } LegalCards = LegalPlays; }
int main(int argc, char** argv) { //string sampleString = "Two of Diamonds, Three of Diamonds, Four of Diamonds, Seven of Diamonds, Seven of Clubs, Seven of Hearts, Jack of Hearts"; //string newCard = "Five of Diamonds"; string sampleString, newCard; cout << "What is the initial hand?: "; getline(cin, sampleString); cout << "What is the added hand?: "; getline(cin, newCard); Hand myHand; //transform everything to lower case transform(sampleString.begin(), sampleString.end(), sampleString.begin(), ::tolower); transform(newCard.begin(), newCard.end(), newCard.begin(), ::tolower); vector<string> hand = tokenizeStrings(sampleString, ",:", 1); // For each of the cards, we need to create cards out of them for (vector<string>::iterator _card = hand.begin(); _card != hand.end(); _card++) myHand.AddCard(new Card(*_card)); myHand.sortHand(); Card* swappableCard = new Card(newCard); cout << "Checking for a run with length " << RunLength << "\n"; Card* swappedCard = myHand.CheckForRun(*swappableCard); outputWin(swappedCard); cout << "Checking for a set with length " << SetLength << "\n"; swappedCard = myHand.CheckForSet(*swappableCard); outputWin(swappedCard); RunLength = 4; cout << "Checking for a run with length " << RunLength << "\n"; swappedCard = myHand.CheckForRun(*swappableCard); outputWin(swappedCard); SetLength = 4; cout << "Checking for a set with length " << SetLength << "\n"; swappedCard = myHand.CheckForSet(*swappableCard); outputWin(swappedCard); //Clean up delete swappableCard; myHand.cleanUp(); return 0; }