Finished the go fish program.
This commit is contained in:
parent
4a608b9edb
commit
c5d797cfbb
2 changed files with 75 additions and 2 deletions
|
|
@ -265,7 +265,7 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
|
|||
human.push_back(cardDeck.back());
|
||||
cardDeck.pop_back();
|
||||
cout << "Opponent does not have " << inputChar << ". You go fish." << endl;
|
||||
cout << "New Card: " << human[human.size() - 1].getFullCard() << endl;
|
||||
cout << "New Card: " << human[human.size()-1].getFullCard() << endl; // also try human.end()???
|
||||
}
|
||||
// check to see if sets exist in the vector, aka is there four of a value.
|
||||
// if there is four of a value, increase the number of sets to return, remove the cards from hand.
|
||||
|
|
@ -324,6 +324,79 @@ int computerTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &card
|
|||
Handles the computer’s turn and it should do the same actions as the human but for the
|
||||
computer. The function should return the number of set that is collected during its turn.
|
||||
*/
|
||||
int numSets = 0;
|
||||
int maxCountCard = 0;
|
||||
char inputChar = computer[0].getCardRawValue();
|
||||
bool foundCard = false;
|
||||
bool foundSet = false;
|
||||
cout << "Opponent's turn. They asked for: " << inputChar << endl;
|
||||
// ask for card rank/number/value
|
||||
int humanHandSize = human.size();
|
||||
// if card is in opposite hand, remove from and give to the other. (loop through human hand looking for card)
|
||||
for(int checkHandIterator = 0; checkHandIterator < humanHandSize; checkHandIterator++)
|
||||
{
|
||||
// checking if value of card is equal to any of the other ones.
|
||||
if(human[checkHandIterator].getCardRawValue() == inputChar)
|
||||
{
|
||||
// copy the card into the human's hand.
|
||||
human.push_back(human[checkHandIterator]);
|
||||
// set the card find to true.
|
||||
foundCard = true;
|
||||
cout << "You have " << human[checkHandIterator].getFullCard();
|
||||
cout << ". They collect all " << inputChar << "s." << endl;
|
||||
}
|
||||
}
|
||||
// remove the cards that had been drawn from the other hand (use remove/remove if and erase with the vector iterators)
|
||||
if(foundCard)
|
||||
{
|
||||
// remove cards if found
|
||||
human.erase(remove_if(human.begin(), human.end(),
|
||||
[&](Card &remCard){return remCard.getCardValue() == inputChar;}), human.end());
|
||||
}
|
||||
// if not, draw a card from the top of the deck.
|
||||
else
|
||||
{
|
||||
// draw a card if not found.
|
||||
computer.push_back(cardDeck.back());
|
||||
cardDeck.pop_back();
|
||||
}
|
||||
// check to see if sets exist in the vector, aka is there four of a value.
|
||||
// if there is four of a value, increase the number of sets to return, remove the cards from hand.
|
||||
// NOTE TO PROGRAMMER: https://stackoverflow.com/questions/39906504/how-to-count-duplicates-in-a-vector-c
|
||||
sort(computer.begin(), computer.end());
|
||||
// had to use the stupid auto keyword here to make the damn iterator move... kept getting bad casting types.
|
||||
for (auto cardIterator = computer.cbegin(); cardIterator != computer.cend();)
|
||||
{
|
||||
maxCountCard = count(cardIterator, computer.cend(), *cardIterator);
|
||||
if(maxCountCard == 4)
|
||||
{
|
||||
// this is where I make sure that there is a set here before removing it,
|
||||
// and set return value here.
|
||||
numSets = numSets + 1;
|
||||
foundSet = true;
|
||||
}
|
||||
// skip around until duplicates are found, using a trick that I researched.
|
||||
for(auto lastChar = *cardIterator; *++cardIterator == lastChar;)
|
||||
{
|
||||
// this above for loop essentially keeps the iterator moving
|
||||
// intentional empty block, since iterator must keep moving, but I don't need to do anything.
|
||||
}
|
||||
}
|
||||
if(foundSet)
|
||||
{
|
||||
// if a valid set is found, remove all four cards from the computer's hand.
|
||||
computer.erase(remove_if(computer.begin(), computer.end(),
|
||||
[&](Card &remCard){return remCard.getCardValue() == inputChar;}), computer.end());
|
||||
}
|
||||
// display hand to computer.
|
||||
int handSize = human.size();
|
||||
cout << "Your Hand: " << endl;
|
||||
cout << "| ";
|
||||
for(int handDisplayIterator = 0; handDisplayIterator < handSize; handDisplayIterator++)
|
||||
{
|
||||
cout << human[handDisplayIterator].getFullCard() << " | ";
|
||||
}
|
||||
cout << endl;
|
||||
// return the number of sets
|
||||
return 0;
|
||||
return numSets;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue