Human stuff is done, but segfaults. Look into that.
This commit is contained in:
parent
ef2702e3a5
commit
4a608b9edb
2 changed files with 49 additions and 14 deletions
Binary file not shown.
|
|
@ -140,6 +140,8 @@ int main(int argc, char **argv)
|
|||
vector<Card> computerHand;
|
||||
// keep count of the number of sets collected. ends when number is 13.
|
||||
int currentNumSets = 0;
|
||||
int humanNumSets = 0;
|
||||
int computerNumSets = 0;
|
||||
// char array for the setups
|
||||
char suits[] = {'C','D','H','S'};
|
||||
int suitsLen = sizeof(suits)/sizeof(suits[0]);
|
||||
|
|
@ -174,7 +176,7 @@ int main(int argc, char **argv)
|
|||
deck.pop_back();
|
||||
}
|
||||
// tell the user the game has started.
|
||||
cout << "Game Start." << endl;
|
||||
cout << "Game Start." << endl << endl;
|
||||
// output the human's hand to the screen
|
||||
cout << "Here is your starting hand:" << endl << "| ";
|
||||
int handSize = humanHand.size();
|
||||
|
|
@ -187,8 +189,9 @@ int main(int argc, char **argv)
|
|||
while(currentNumSets != 13)
|
||||
{
|
||||
// switch back and forth between human and computer.
|
||||
currentNumSets = currentNumSets + humanTurn(humanHand, computerHand, deck);
|
||||
currentNumSets = currentNumSets + computerTurn(humanHand, computerHand, deck);
|
||||
humanNumSets = humanNumSets + humanTurn(humanHand, computerHand, deck);
|
||||
computerNumSets = computerNumSets + computerTurn(humanHand, computerHand, deck);
|
||||
currentNumSets = humanNumSets + computerNumSets;
|
||||
}
|
||||
// return 0
|
||||
return 0;
|
||||
|
|
@ -228,14 +231,13 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
|
|||
int maxCountCard = 0;
|
||||
char inputChar;
|
||||
bool foundCard = false;
|
||||
bool foundSet = false;
|
||||
// ask for card rank/number/value
|
||||
cout << "Your turn. Ask for a card (2-9, J/Q/K/A): " << endl;
|
||||
cin.ignore();
|
||||
cout << ">";
|
||||
cout << endl << "Your turn. Ask for a card (2-9, J/Q/K/A): ";
|
||||
cin >> inputChar;
|
||||
Card valueOnlyCard(inputChar, 'X');
|
||||
int computerHandSize = computer.size();
|
||||
// if card is in opposite hand, remove from and give to the other. (loop through computer hand looking for card)
|
||||
for(int checkHandIterator = 0; checkHandIterator < computer.size(); checkHandIterator++)
|
||||
for(int checkHandIterator = 0; checkHandIterator < computerHandSize; checkHandIterator++)
|
||||
{
|
||||
// checking if value of card is equal to any of the other ones.
|
||||
if(computer[checkHandIterator].getCardRawValue() == inputChar)
|
||||
|
|
@ -244,6 +246,8 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
|
|||
human.push_back(computer[checkHandIterator]);
|
||||
// set the card find to true.
|
||||
foundCard = true;
|
||||
cout << "Opponent has " << computer[checkHandIterator].getFullCard();
|
||||
cout << ". You 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)
|
||||
|
|
@ -251,7 +255,8 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
|
|||
if(foundCard)
|
||||
{
|
||||
// remove cards if found
|
||||
computer.erase(remove_if(computer.begin(), computer.end(), [&](Card &remCard){return remCard.getCardValue() == inputChar;}), computer.end());
|
||||
computer.erase(remove_if(computer.begin(), computer.end(),
|
||||
[&](Card &remCard){return remCard.getCardValue() == inputChar;}), computer.end());
|
||||
}
|
||||
// if not, draw a card from the top of the deck.
|
||||
else
|
||||
|
|
@ -259,16 +264,46 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
|
|||
// draw a card if not found.
|
||||
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;
|
||||
}
|
||||
// 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: https://stackoverflow.com/questions/39906504/how-to-count-duplicates-in-a-vector-c
|
||||
for (int setCounter = 0; setCounter < human.size(); setCounter++)
|
||||
// NOTE TO PROGRAMMER: https://stackoverflow.com/questions/39906504/how-to-count-duplicates-in-a-vector-c
|
||||
sort(human.begin(), human.end());
|
||||
// had to use the stupid auto keyword here to make the damn iterator move... kept getting bad casting types.
|
||||
for (auto cardIterator = human.cbegin(); cardIterator != human.cend();)
|
||||
{
|
||||
/* code */
|
||||
maxCountCard = count(cardIterator, human.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 human's hand.
|
||||
human.erase(remove_if(human.begin(), human.end(),
|
||||
[&](Card &remCard){return remCard.getCardValue() == inputChar;}), human.end());
|
||||
}
|
||||
// display hand to human.
|
||||
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 numSets;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue