Get the Go Fish human function almost completed, computer turn still needs work.
This commit is contained in:
parent
b34d34a63d
commit
ef2702e3a5
3 changed files with 80 additions and 8 deletions
Binary file not shown.
|
|
@ -90,6 +90,11 @@ class Card
|
||||||
//
|
//
|
||||||
return cardNumericalValue;
|
return cardNumericalValue;
|
||||||
}
|
}
|
||||||
|
char getCardRawValue()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
return cardValue;
|
||||||
|
}
|
||||||
char getCardSuit()
|
char getCardSuit()
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
|
|
@ -99,6 +104,21 @@ class Card
|
||||||
{
|
{
|
||||||
return fullCard;
|
return fullCard;
|
||||||
}
|
}
|
||||||
|
friend bool operator==(const Card &cardOne, const Card &cardTwo)
|
||||||
|
{
|
||||||
|
// compare cardOne's value to cardTwo's value.
|
||||||
|
// THIS WILL ONLY CHECK NUMERICAL VALUE
|
||||||
|
// If I was doing this for a REAL card game, I would add an
|
||||||
|
// && (cardOne.cardSuit == cardTwo.cardSuit)
|
||||||
|
// to make sure that the suit AND value are the same.
|
||||||
|
return (cardOne.cardNumericalValue == cardTwo.cardNumericalValue);
|
||||||
|
}
|
||||||
|
friend bool operator<(const Card &cardOne, const Card &cardTwo)
|
||||||
|
{
|
||||||
|
// compare cardOne's value to cardTwo's value with less than function.
|
||||||
|
// THIS WILL ONLY CHECK NUMERICAL VALUE
|
||||||
|
return (cardOne.cardNumericalValue < cardTwo.cardNumericalValue);
|
||||||
|
}
|
||||||
private:
|
private:
|
||||||
char cardValue;
|
char cardValue;
|
||||||
char cardSuit;
|
char cardSuit;
|
||||||
|
|
@ -107,8 +127,8 @@ class Card
|
||||||
};
|
};
|
||||||
|
|
||||||
// function prototypes
|
// function prototypes
|
||||||
int humanTurn(vector<Card> &human, vector<Card> &computer);
|
int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDeck);
|
||||||
int computerTurn(vector<Card> &human, vector<Card> &computer);
|
int computerTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDeck);
|
||||||
|
|
||||||
// main program
|
// main program
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|
@ -153,12 +173,22 @@ int main(int argc, char **argv)
|
||||||
computerHand.push_back(deck.back());
|
computerHand.push_back(deck.back());
|
||||||
deck.pop_back();
|
deck.pop_back();
|
||||||
}
|
}
|
||||||
|
// tell the user the game has started.
|
||||||
|
cout << "Game Start." << endl;
|
||||||
|
// output the human's hand to the screen
|
||||||
|
cout << "Here is your starting hand:" << endl << "| ";
|
||||||
|
int handSize = humanHand.size();
|
||||||
|
for(int handDisplayIterator = 0; handDisplayIterator < handSize; handDisplayIterator++)
|
||||||
|
{
|
||||||
|
cout << humanHand[handDisplayIterator].getFullCard() << " | ";
|
||||||
|
}
|
||||||
|
cout << endl;
|
||||||
// main gameplay loop (while?)
|
// main gameplay loop (while?)
|
||||||
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);
|
currentNumSets = currentNumSets + humanTurn(humanHand, computerHand, deck);
|
||||||
currentNumSets = currentNumSets + computerTurn(humanHand, computerHand);
|
currentNumSets = currentNumSets + computerTurn(humanHand, computerHand, deck);
|
||||||
}
|
}
|
||||||
// return 0
|
// return 0
|
||||||
return 0;
|
return 0;
|
||||||
|
|
@ -185,7 +215,7 @@ Device Input/Device: ???
|
||||||
Device Output/Monitor: ???
|
Device Output/Monitor: ???
|
||||||
Dependencies: ???
|
Dependencies: ???
|
||||||
*/
|
*/
|
||||||
int humanTurn(vector<Card> &human, vector<Card> &computer)
|
int humanTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDeck)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Handles the human’s turn. It will prompt the player to ask for a card rank/number and
|
Handles the human’s turn. It will prompt the player to ask for a card rank/number and
|
||||||
|
|
@ -194,11 +224,53 @@ int humanTurn(vector<Card> &human, vector<Card> &computer)
|
||||||
card. The function should return the number of sets collected during the turn
|
card. The function should return the number of sets collected during the turn
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
int numSets = 0;
|
||||||
|
int maxCountCard = 0;
|
||||||
|
char inputChar;
|
||||||
|
bool foundCard = false;
|
||||||
// ask for card rank/number/value
|
// ask for card rank/number/value
|
||||||
// if card is in hand,
|
cout << "Your turn. Ask for a card (2-9, J/Q/K/A): " << endl;
|
||||||
|
cin.ignore();
|
||||||
|
cout << ">";
|
||||||
|
cin >> inputChar;
|
||||||
|
Card valueOnlyCard(inputChar, 'X');
|
||||||
|
// 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++)
|
||||||
|
{
|
||||||
|
// checking if value of card is equal to any of the other ones.
|
||||||
|
if(computer[checkHandIterator].getCardRawValue() == inputChar)
|
||||||
|
{
|
||||||
|
// copy the card into the human's hand.
|
||||||
|
human.push_back(computer[checkHandIterator]);
|
||||||
|
// set the card find to true.
|
||||||
|
foundCard = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// remove the cards that had been drawn from the other hand (use remove/remove if and erase with the vector iterators)
|
||||||
|
// NOTE: https://stackoverflow.com/questions/72179264/classes-remove-a-specific-object-from-a-vector-of-objects
|
||||||
|
if(foundCard)
|
||||||
|
{
|
||||||
|
// remove cards if found
|
||||||
|
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
|
||||||
|
{
|
||||||
|
// draw a card if not found.
|
||||||
|
human.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: https://stackoverflow.com/questions/39906504/how-to-count-duplicates-in-a-vector-c
|
||||||
|
for (int setCounter = 0; setCounter < human.size(); setCounter++)
|
||||||
|
{
|
||||||
|
/* code */
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// return the number of sets
|
// return the number of sets
|
||||||
return 0;
|
return numSets;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -211,7 +283,7 @@ Device Input/Device: ???
|
||||||
Device Output/Monitor: ???
|
Device Output/Monitor: ???
|
||||||
Dependencies: ???
|
Dependencies: ???
|
||||||
*/
|
*/
|
||||||
int computerTurn(vector<Card> &human, vector<Card> &computer)
|
int computerTurn(vector<Card> &human, vector<Card> &computer, vector<Card> &cardDeck)
|
||||||
{
|
{
|
||||||
/*
|
/*
|
||||||
Handles the computer’s turn and it should do the same actions as the human but for the
|
Handles the computer’s turn and it should do the same actions as the human but for the
|
||||||
|
|
|
||||||
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue