Human stuff is done, but segfaults. Look into that.

This commit is contained in:
Charles Descamps 2024-09-12 16:24:25 -07:00
parent ef2702e3a5
commit 4a608b9edb
2 changed files with 49 additions and 14 deletions

Binary file not shown.

View file

@ -140,6 +140,8 @@ int main(int argc, char **argv)
vector<Card> computerHand; vector<Card> computerHand;
// keep count of the number of sets collected. ends when number is 13. // keep count of the number of sets collected. ends when number is 13.
int currentNumSets = 0; int currentNumSets = 0;
int humanNumSets = 0;
int computerNumSets = 0;
// char array for the setups // char array for the setups
char suits[] = {'C','D','H','S'}; char suits[] = {'C','D','H','S'};
int suitsLen = sizeof(suits)/sizeof(suits[0]); int suitsLen = sizeof(suits)/sizeof(suits[0]);
@ -174,7 +176,7 @@ int main(int argc, char **argv)
deck.pop_back(); deck.pop_back();
} }
// tell the user the game has started. // tell the user the game has started.
cout << "Game Start." << endl; cout << "Game Start." << endl << endl;
// output the human's hand to the screen // output the human's hand to the screen
cout << "Here is your starting hand:" << endl << "| "; cout << "Here is your starting hand:" << endl << "| ";
int handSize = humanHand.size(); int handSize = humanHand.size();
@ -187,8 +189,9 @@ int main(int argc, char **argv)
while(currentNumSets != 13) while(currentNumSets != 13)
{ {
// switch back and forth between human and computer. // switch back and forth between human and computer.
currentNumSets = currentNumSets + humanTurn(humanHand, computerHand, deck); humanNumSets = humanNumSets + humanTurn(humanHand, computerHand, deck);
currentNumSets = currentNumSets + computerTurn(humanHand, computerHand, deck); computerNumSets = computerNumSets + computerTurn(humanHand, computerHand, deck);
currentNumSets = humanNumSets + computerNumSets;
} }
// return 0 // return 0
return 0; return 0;
@ -228,14 +231,13 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
int maxCountCard = 0; int maxCountCard = 0;
char inputChar; char inputChar;
bool foundCard = false; bool foundCard = false;
bool foundSet = false;
// ask for card rank/number/value // ask for card rank/number/value
cout << "Your turn. Ask for a card (2-9, J/Q/K/A): " << endl; cout << endl << "Your turn. Ask for a card (2-9, J/Q/K/A): ";
cin.ignore();
cout << ">";
cin >> inputChar; 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) // 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. // checking if value of card is equal to any of the other ones.
if(computer[checkHandIterator].getCardRawValue() == inputChar) 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]); human.push_back(computer[checkHandIterator]);
// set the card find to true. // set the card find to true.
foundCard = 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) // 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) if(foundCard)
{ {
// remove cards if found // 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. // if not, draw a card from the top of the deck.
else else
@ -259,16 +264,46 @@ int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDec
// draw a card if not found. // draw a card if not found.
human.push_back(cardDeck.back()); human.push_back(cardDeck.back());
cardDeck.pop_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. // 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. // 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 // NOTE TO PROGRAMMER: https://stackoverflow.com/questions/39906504/how-to-count-duplicates-in-a-vector-c
for (int setCounter = 0; setCounter < human.size(); setCounter++) 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 the number of sets
return numSets; return numSets;
} }