summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-01-29 19:46:29 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-01-29 19:46:29 -0600
commitda1a7180edee0f108d15314e493faa2e9e067095 (patch)
treed70fc2511861cae9588a136254d8095dedf83332
parent63a098d36fcf53b2cad48d042f6efd3174d93717 (diff)
downloadpoker-da1a7180edee0f108d15314e493faa2e9e067095.tar.gz
poker-da1a7180edee0f108d15314e493faa2e9e067095.tar.bz2
poker-da1a7180edee0f108d15314e493faa2e9e067095.zip
do more error checking
-rw-r--r--card.cpp6
-rw-r--r--game.cpp62
-rw-r--r--game.h1
-rw-r--r--main.cpp6
-rw-r--r--opponent.cpp8
-rw-r--r--opponent.h2
6 files changed, 73 insertions, 12 deletions
diff --git a/card.cpp b/card.cpp
index 0f4fc3f..ffcffbd 100644
--- a/card.cpp
+++ b/card.cpp
@@ -101,6 +101,12 @@ enum Suit Card::getSuit(void)
void Card::sortCards(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nsortCards(): cards parameter is NULL!\n");
+ return;
+ }
+
/* reverse bubble sort */
int i, j;
Card *tmp;
diff --git a/game.cpp b/game.cpp
index 385ff2f..8d91427 100644
--- a/game.cpp
+++ b/game.cpp
@@ -119,6 +119,12 @@ void Game::repeatGame(bool *quit)
int Game::howManyCardsOfSameSuit(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nhowManyCardsOfSameSuit(): cards parameter is NULL!\n");
+ return -1;
+ }
+
char c, l;
l = cards[0]->getType()[1];
@@ -135,6 +141,12 @@ int Game::howManyCardsOfSameSuit(Card **cards)
bool Game::pairOrBetter(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\npairOrBetter(): cards parameter is NULL!\n");
+ return false;
+ }
+
char c1, c2;
int ret = 0;
@@ -160,6 +172,12 @@ bool Game::pairOrBetter(Card **cards)
/* returns max number of same cards, returns 0 when all cards are different */
int Game::numOfSameCards(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameCards(): cards parameter is NULL!\n");
+ return -1;
+ }
+
char c1, c2;
int same = 0;
int max = 0;
@@ -193,17 +211,24 @@ int Game::numOfSameCards(Card **cards)
struct countAndSuit Game::numOfSameSuit(Card **cards)
{
- int max = 0;
- enum Suit tmpSuit = Undef;
-
- struct countAndSuit same[4] =
+ struct countAndSuit same[5] =
{
{ Clubs, 0},
{ Diamond, 0 },
{ Hearts, 0 },
{ Spades, 0 },
+ { Undef, -1 },
};
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameSuit(): cards parameter is NULL!\n");
+ return same[4];
+ }
+
+ int max = 0;
+ enum Suit tmpSuit = Undef;
+
/* go through all cards and count how many there is of each suit */
int i;
for (i = 0; i < 5; i++)
@@ -243,6 +268,12 @@ struct countAndSuit Game::numOfSameSuit(Card **cards)
bool Game::doWeHaveAnAce(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\ndoWeHaveAnAce(): cards parameter is NULL!\n");
+ return false;
+ }
+
char c;
int i;
@@ -263,14 +294,19 @@ void Game::discardAndDraw(Card **cards, int amount, int offset)
PRINT_COLOR(ANSI_COLOR_RED, "amount of cards to be discarded is out of bounds!\n");
return;
}
+ else if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardAndDraw(): cards parameter is NULL!\n");
+ return;
+ }
int i;
/* discard cards starting from offset */
for (i = 0; i < amount; i++)
{
#ifdef DEBUG
- cout << "debug: discarding card " << i+1 << ": " << cards[offset+i]->getType()
- << " at cards[" << offset << "]" << endl;
+ cout << ANSI_COLOR_CYAN << "debug: discarding card " << i+1 << " \"" << cards[offset+i]->getType()
+ << "\" at cards[" << offset+i << "]" << endl << ANSI_COLOR_RESET;
#endif
putCardToDiscardPile(cards[offset+i]);
}
@@ -280,14 +316,20 @@ void Game::discardAndDraw(Card **cards, int amount, int offset)
{
cards[offset+i] = PopCardFromDeck();
#ifdef DEBUG
- cout << "debug: drawing card " << i+1 << ": " << cards[offset+i]->getType()
- << " at cards[" << offset << "]" << endl;
+ cout << ANSI_COLOR_CYAN << "debug: drawing card " << i+1 << " \"" << cards[offset+i]->getType()
+ << "\" into cards[" << offset+i << "]" << endl << ANSI_COLOR_RESET;
#endif
}
}
void Game::opponentAI(Card **cards)
{
+ if (cards == NULL)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\ngetopponentAI(): cards parameter is NULL!\n");
+ return;
+ }
+
struct countAndSuit tmp = numOfSameSuit(cards);
if (pairOrBetter(cards))
@@ -316,8 +358,8 @@ void Game::opponentAI(Card **cards)
if (cards[i]->getSuit() != tmp.whatSuit)
break;
}
- /* no i should be the index of card that we want to discard */
+ /* no i should be the index of card that we want to discard */
discardAndDraw(cards, 1, i);
return;
@@ -337,5 +379,7 @@ void Game::opponentAI(Card **cards)
/* discard 3 cards */
discardAndDraw(cards, 3, 2);
}
+
+ PRINT_COLOR(ANSI_COLOR_RED, "\nopponentAI(): cards did not match any of the criterias!\n");
}
diff --git a/game.h b/game.h
index cb1c528..ec8e8e7 100644
--- a/game.h
+++ b/game.h
@@ -46,7 +46,6 @@ class Game : virtual public CardPile, public User, public Opponent
void opponentAI(Card **);
/* variables */
- int numOfOpponents;
int handsPlayed;
int handsWon;
diff --git a/main.cpp b/main.cpp
index a01b1fb..1dee3e1 100644
--- a/main.cpp
+++ b/main.cpp
@@ -62,7 +62,11 @@ int main(int argc, char *argv[])
cout << "Opponents' cards:" << endl;
pokerGame->printOpponentCards(pokerGame->numOfOpponents);
- //pokerGame->opponentAI
+#ifdef DEBUG
+ cout << "Computer's AI" << endl;
+ pokerGame->opponentAI(pokerGame->getOpponentsCards(1));
+ pokerGame->printOpponentCards(pokerGame->numOfOpponents);
+#endif
#ifdef DEBUG
PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n");
diff --git a/opponent.cpp b/opponent.cpp
index 8c23478..7f8a14b 100644
--- a/opponent.cpp
+++ b/opponent.cpp
@@ -44,6 +44,12 @@ void Opponent::sortOpponentCards(int opponentsAmount)
Card **Opponent::getOpponentsCards(int whichOpponent)
{
- return opponentCards[whichOpponent];
+ if (whichOpponent < 1 || whichOpponent > 3)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "\ngetOpponentsCards(): opponent number out of bounds!\n");
+ return NULL;
+ }
+
+ return opponentCards[whichOpponent-1];
}
diff --git a/opponent.h b/opponent.h
index 71dc792..5376ecf 100644
--- a/opponent.h
+++ b/opponent.h
@@ -17,6 +17,8 @@ class Opponent : virtual public CardPile
void printOpponentCards(int);
void sortOpponentCards(int);
Card **getOpponentsCards(int);
+
+ int numOfOpponents;
};
#endif