summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-01-30 02:04:36 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-01-30 02:04:36 -0600
commit3ee143e7b0e81b1728fb808da0f42b60b043b063 (patch)
treea330c8acf934e32cf2ec950204fa445467f578ab
parent0529bbb2d56942bf3b88469786a4776d5cf49447 (diff)
downloadpoker-3ee143e7b0e81b1728fb808da0f42b60b043b063.tar.gz
poker-3ee143e7b0e81b1728fb808da0f42b60b043b063.tar.bz2
poker-3ee143e7b0e81b1728fb808da0f42b60b043b063.zip
implement userAI and card discarding method
-rw-r--r--game.cpp142
-rw-r--r--game.h6
-rw-r--r--main.cpp11
-rw-r--r--user.cpp2
4 files changed, 147 insertions, 14 deletions
diff --git a/game.cpp b/game.cpp
index 6f02b86..ce76406 100644
--- a/game.cpp
+++ b/game.cpp
@@ -287,20 +287,26 @@ bool Game::doWeHaveAnAce(Card **cards)
return false;
}
-void Game::discardFromPairOrBetter(Card **cards, int whichOpponent)
+/* returns array which contains number of pairs, three of a kind,
+ * or four of a kind, index that contains 1 should be discarded
+ * since it's a unique card */
+int *Game::whichCardsToDiscard(Card **cards)
{
if (cards == NULL)
{
- PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardFromPairOrBetter(): cards parameter is NULL!\n");
- return;
+ PRINT_COLOR(ANSI_COLOR_RED, "\nwhichCardsToDiscard(): cards parameter is NULL!\n");
+ return NULL;
}
+ int *howManyOfSameCards = new int[5]();
+ howManyOfSameCards[0] = 1;
+ howManyOfSameCards[1] = 1;
+ howManyOfSameCards[2] = 1;
+ howManyOfSameCards[3] = 1;
+ howManyOfSameCards[4] = 1;
- int howManyOfSameCards[5] = { 1,1,1,1,1 };
- int numOfDiscarded = 0;
char c1, c2;
-
- /* goal is to look later at indices that contain zero */
+ /* goal is to look later at indices that contain zero */
int i, j;
for (i = 0; i < 5; i++)
{
@@ -318,7 +324,22 @@ void Game::discardFromPairOrBetter(Card **cards, int whichOpponent)
}
}
- /* indices that contain a zero should be discarded */
+ return howManyOfSameCards;
+}
+
+void Game::oppDiscardFromPairOrBetter(Card **cards, int whichOpponent)
+{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardFromPairOrBetter(): cards parameter is NULL!\n");
+ return;
+ }
+
+ int numOfDiscarded = 0;
+ int *howManyOfSameCards = whichCardsToDiscard(cards);
+
+ int i;
+ /* indices that contain a one should be discarded */
for (i = 0; i < 5; i++)
{
if (howManyOfSameCards[i] == 1)
@@ -363,7 +384,9 @@ bool Game::sanityCheck(Card **cards)
}
-
+/*==============================================================================
+ * Discard and Draw
+ *============================================================================*/
void Game::discardAndDraw(Card **cards, int amount, int offset)
{
if (offset + amount > 5)
@@ -400,7 +423,9 @@ void Game::discardAndDraw(Card **cards, int amount, int offset)
}
-
+/*==============================================================================
+ * Computer AI
+ *============================================================================*/
void Game::opponentAI(int whichOpponent)
{
Card **cards = getOpponentsCards(whichOpponent);
@@ -416,7 +441,7 @@ void Game::opponentAI(int whichOpponent)
<< ", there's a pair or better" << endl << ANSI_COLOR_RESET;
#endif
/* discard some cards*/
- discardFromPairOrBetter(cards, whichOpponent);
+ oppDiscardFromPairOrBetter(cards, whichOpponent);
return;
}
@@ -492,3 +517,98 @@ void Game::opponentAI(int whichOpponent)
#endif
}
+
+/*==============================================================================
+ * User AI
+ *============================================================================*/
+void Game::userDiscardCards(Card **cards, int howManyUserCanDiscard)
+{
+ int whatCard = 0;
+ string *msg = new string("List the card number you wish to discard (0 to skip): ");
+
+ int i;
+ for (i = 0; i < howManyUserCanDiscard; i++)
+ {
+ whatCard = parseInt(msg, 0, 5);
+ if (whatCard == 0)
+ break;
+
+ discardAndDraw(cards, 1, whatCard-1);
+ }
+}
+
+void Game::userAI(Card **cards)
+{
+ struct countAndSuit tmp = numOfSameSuit(cards);
+ int howManyUserCanDiscard = 0;
+ int i;
+
+ if (pairOrBetter(cards))
+ {
+ int *cardsToDiscard = whichCardsToDiscard(cards);
+ howManyUserCanDiscard = 0;
+
+ /* indices that contain a one should be discarded */
+ for (i = 0; i < 5; i++)
+ {
+ if (cardsToDiscard[i] == 1)
+ howManyUserCanDiscard++;
+ }
+
+ cout << "You have a pair or better and can discard "
+ << howManyUserCanDiscard << " card(s)" << endl;
+
+ userDiscardCards(cards, howManyUserCanDiscard);
+ return;
+ }
+
+ else if ( (tmp = numOfSameSuit(cards)).count == 5)
+ {
+ howManyUserCanDiscard = 3;
+
+ cout << "You have 5 cards of the same suit, you should\n"
+ << "keep all of the cards but you can discard "
+ << howManyUserCanDiscard << " card(s)" << endl;
+
+ userDiscardCards(cards, howManyUserCanDiscard);
+ return;
+ }
+
+ else if (tmp.count == 4)
+ {
+ howManyUserCanDiscard = 3;
+
+ cout << "You have 4 cards of the same suit, you should\n"
+ << "discard card of the different suit"
+ << howManyUserCanDiscard << " card(s)" << endl;
+
+ userDiscardCards(cards, howManyUserCanDiscard);
+ return;
+ }
+
+ else if (doWeHaveAnAce(cards))
+ {
+ /* discard 4 cards */
+ howManyUserCanDiscard = 4;
+
+ cout << "Since you have an Ace you can keep the Ace\n"
+ << "and discard 4 other cards" << endl;
+
+ userDiscardCards(cards, howManyUserCanDiscard);
+ return;
+ }
+
+ else
+ {
+ /* discard 3 cards */
+ howManyUserCanDiscard = 3;
+
+ cout << "You have a High Hand, two highest cards should\n"
+ << "be kept and you should discard 3 other cards"
+ << endl;
+
+ userDiscardCards(cards, howManyUserCanDiscard);
+ return;
+ }
+}
+
diff --git a/game.h b/game.h
index 9d79fcc..c8874b8 100644
--- a/game.h
+++ b/game.h
@@ -42,11 +42,15 @@ class Game : virtual public CardPile, public User, public Opponent
struct countAndSuit numOfSameSuit(Card **);
bool doWeHaveAnAce(Card **);
- void discardFromPairOrBetter(Card **, int);
+ int *whichCardsToDiscard(Card **);
+ void oppDiscardFromPairOrBetter(Card **, int);
bool sanityCheck(Card **);
void discardAndDraw(Card **, int, int);
void opponentAI(int);
+ void userDiscardCards(Card **, int);
+ void userAI(Card **);
+
/* variables */
int handsPlayed;
int handsWon;
diff --git a/main.cpp b/main.cpp
index 561efe8..399c11c 100644
--- a/main.cpp
+++ b/main.cpp
@@ -33,18 +33,26 @@ int main(int argc, char *argv[])
pokerGame->printPile();
#endif
+ /* all methods assume that cards are sorted, it is our responsibility to
+ * to meet that, this results in simpler code */
cout << "\n> Dealing cards to the user" << endl;
pokerGame->dealUserCards();
pokerGame->sortUserCards();
cout << "Cards in your hand: ";
pokerGame->printUserCards();
+ pokerGame->userAI(pokerGame->getUserCards());
+ cout << "\nFinal cards in your hand: ";
+ pokerGame->sortUserCards();
+ pokerGame->printUserCards();
+#if 0
cout << "Number of same cards: " <<
pokerGame->numOfSameCards(pokerGame->getUserCards()) << endl;
if (pokerGame->pairOrBetter(pokerGame->getUserCards()))
cout << "User has pair or better" << endl;
+
struct countAndSuit userCountAndSuit = pokerGame->numOfSameSuit(pokerGame->getUserCards());
cout << "Top suit: " << *pokerGame->suits[userCountAndSuit.whatSuit] << ", count: "
<< userCountAndSuit.count << endl;
@@ -55,8 +63,9 @@ int main(int argc, char *argv[])
pokerGame->discardAndDraw( pokerGame->getUserCards(), 2, 3 );
cout << "Cards in your hand after discarding: ";
pokerGame->printUserCards();
+#endif
- cout << "\n> Dealing cards to opponent(s)" << endl;
+ cout << "> Dealing cards to opponent(s)" << endl;
pokerGame->dealOpponentCards();
pokerGame->sortOpponentCards();
diff --git a/user.cpp b/user.cpp
index 679ee9d..6a773ff 100644
--- a/user.cpp
+++ b/user.cpp
@@ -25,7 +25,7 @@ void User::printUserCards(void)
for (i = 0; i < 5; i++)
if (userCards[i])
cout << i+1 << ") " << userCards[i]->getType() << " ";
- cout << endl;
+ cout << endl << endl;
}
void User::sortUserCards(void)