summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-01-29 22:14:31 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-01-29 22:14:31 -0600
commitea1022bb593ad714e1123951182ab540ab498d24 (patch)
treeb2009c0a9deb4ffa812d1e7d9b47a07eb7562ab4
parent579915ba835cb0a1f0f893881c09e6b59c6a7f0e (diff)
downloadpoker-ea1022bb593ad714e1123951182ab540ab498d24.tar.gz
poker-ea1022bb593ad714e1123951182ab540ab498d24.tar.bz2
poker-ea1022bb593ad714e1123951182ab540ab498d24.zip
mild refactoring
-rw-r--r--game.cpp45
-rw-r--r--game.h2
-rw-r--r--main.cpp22
3 files changed, 44 insertions, 25 deletions
diff --git a/game.cpp b/game.cpp
index 6325731..5f2b3a1 100644
--- a/game.cpp
+++ b/game.cpp
@@ -317,6 +317,8 @@ bool Game::sanityCheck(Card **cards)
return true;
}
+
+
void Game::discardAndDraw(Card **cards, int amount, int offset)
{
if (offset + amount > 5)
@@ -352,22 +354,21 @@ void Game::discardAndDraw(Card **cards, int amount, int offset)
}
}
-void Game::opponentAI(Card **cards)
+
+
+void Game::opponentAI(int whichOpponent)
{
+ Card **cards = getOpponentsCards(whichOpponent);
if (cards == NULL)
- {
-#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_RED, "\ngetopponentAI(): cards parameter is NULL!\n");
-#endif
return;
- }
struct countAndSuit tmp = numOfSameSuit(cards);
if (pairOrBetter(cards))
{
#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, there's a pair or better\n");
+ cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent
+ << ", there's a pair or better" << endl << ANSI_COLOR_RESET;
#endif
/* discard 4 other cards*/
return;
@@ -376,7 +377,8 @@ void Game::opponentAI(Card **cards)
else if ( (tmp = numOfSameSuit(cards)).count == 5)
{
#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, 5 cards are of the same suit!\n");
+ cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent
+ << ", 5 cards are of the same suit!" << endl << ANSI_COLOR_RESET;
#endif
/* keep 5 cards of the same suit */
return;
@@ -385,7 +387,8 @@ void Game::opponentAI(Card **cards)
else if (tmp.count == 4)
{
#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, 4 cards are of the same suit\n");
+ cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent
+ << ", 4 cards are of the same suit" << endl << ANSI_COLOR_RESET;
#endif
/* keep 4 cards of the same suit and discard 1 of different suit */
@@ -405,9 +408,11 @@ void Game::opponentAI(Card **cards)
else if (doWeHaveAnAce(cards))
{
-#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, High Hand with Ace detected\n");
-#endif
+ #ifdef DEBUG
+ cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent
+ << ", High Hand with Ace detected" << endl << ANSI_COLOR_RESET;
+ #endif
+
/* discard 4 cards */
discardAndDraw(cards, 4, 1);
return;
@@ -415,16 +420,20 @@ void Game::opponentAI(Card **cards)
else if (sanityCheck(cards))
{
-#ifdef DEBUG
- PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, High Hand detected\n");
-#endif
+ #ifdef DEBUG
+ cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent
+ << ", High Hand detected" << endl << ANSI_COLOR_RESET;
+ #endif
+
/* discard 3 cards */
discardAndDraw(cards, 3, 2);
}
-#ifdef DEBUG
+ #ifdef DEBUG
else
- PRINT_COLOR(ANSI_COLOR_RED, "\nopponentAI(): FATAL, cards did not match any of the criterias!\n");
-#endif
+ cout << ANSI_COLOR_RED << "\ndebug: opponent " << whichOpponent
+ << ", FATAL: cards did not match any of the criterias!"
+ << endl << ANSI_COLOR_RESET;
+ #endif
}
diff --git a/game.h b/game.h
index eeb9d23..5455309 100644
--- a/game.h
+++ b/game.h
@@ -44,7 +44,7 @@ class Game : virtual public CardPile, public User, public Opponent
bool doWeHaveAnAce(Card **);
bool sanityCheck(Card **);
void discardAndDraw(Card **, int, int);
- void opponentAI(Card **);
+ void opponentAI(int);
/* variables */
int handsPlayed;
diff --git a/main.cpp b/main.cpp
index 7f587ee..561efe8 100644
--- a/main.cpp
+++ b/main.cpp
@@ -36,7 +36,7 @@ int main(int argc, char *argv[])
cout << "\n> Dealing cards to the user" << endl;
pokerGame->dealUserCards();
pokerGame->sortUserCards();
- cout << "> Cards in your hand: ";
+ cout << "Cards in your hand: ";
pokerGame->printUserCards();
cout << "Number of same cards: " <<
@@ -53,7 +53,7 @@ int main(int argc, char *argv[])
cout << "You have an Ace!" << endl;
pokerGame->discardAndDraw( pokerGame->getUserCards(), 2, 3 );
- cout << "> Cards in your hand after discarding: ";
+ cout << "Cards in your hand after discarding: ";
pokerGame->printUserCards();
cout << "\n> Dealing cards to opponent(s)" << endl;
@@ -61,12 +61,22 @@ int main(int argc, char *argv[])
pokerGame->sortOpponentCards();
#ifdef DEBUG
- cout << "Opponents' cards:" << endl;
+ PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponents' cards:\n");
pokerGame->printOpponentCards();
- cout << "\nComputer's AI";
- pokerGame->opponentAI(pokerGame->getOpponentsCards(1));
- cout << "Resorted Opponents' cards:" << endl;
+ PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: \"Computer's AI\"");
+#endif
+
+ /* perform AI for each opponent */
+ int i;
+ for (i = 0; i < pokerGame->numOfOpponents; i++)
+ pokerGame->opponentAI(i+1);
+
+ /* resort the cards after the AI */
+ pokerGame->sortOpponentCards();
+
+#ifdef DEBUG
+ PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: resorted opponents' cards:\n");
pokerGame->printOpponentCards();
PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n");