summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-01-29 18:38:20 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-01-29 18:38:20 -0600
commit597f9a05c1cde173c1475ddba4830eb51e96a533 (patch)
tree3fe807a50311e042d434e5e16c362add50e65c42
parent62cf7e0d2fdd07fa87e7dd2fd4c969af7ce0ef54 (diff)
downloadpoker-597f9a05c1cde173c1475ddba4830eb51e96a533.tar.gz
poker-597f9a05c1cde173c1475ddba4830eb51e96a533.tar.bz2
poker-597f9a05c1cde173c1475ddba4830eb51e96a533.zip
implement discardAndDraw function
-rw-r--r--card.cpp2
-rw-r--r--card.h12
-rw-r--r--cardpile.cpp3
-rw-r--r--game.cpp38
-rw-r--r--game.h11
-rw-r--r--main.cpp11
6 files changed, 62 insertions, 15 deletions
diff --git a/card.cpp b/card.cpp
index 3c4854d..0f4fc3f 100644
--- a/card.cpp
+++ b/card.cpp
@@ -101,7 +101,7 @@ enum Suit Card::getSuit(void)
void Card::sortCards(Card **cards)
{
- /* bubble sort */
+ /* reverse bubble sort */
int i, j;
Card *tmp;
diff --git a/card.h b/card.h
index 8703d05..f282173 100644
--- a/card.h
+++ b/card.h
@@ -1,9 +1,19 @@
#ifndef _CARD_H_
#define _CARD_H_
+#include <cstdio>
#include <iostream>
#include <string>
+#define ANSI_COLOR_RED "\x1b[31m"
+#define ANSI_COLOR_GREEN "\x1b[32m"
+#define ANSI_COLOR_YELLOW "\x1b[33m"
+#define ANSI_COLOR_BLUE "\x1b[34m"
+#define ANSI_COLOR_MAGENTA "\x1b[35m"
+#define ANSI_COLOR_CYAN "\x1b[36m"
+#define ANSI_COLOR_RESET "\x1b[0m"
+#define PRINT_COLOR(C, S) printf("%s%s%s", C, S, ANSI_COLOR_RESET)
+
using std::string;
using std::cout;
using std::endl;
@@ -28,8 +38,6 @@ class Card
string getType(void);
void rankCard(void);
void sortCards(Card **);
-
-
};
#endif
diff --git a/cardpile.cpp b/cardpile.cpp
index 786f3dc..824e689 100644
--- a/cardpile.cpp
+++ b/cardpile.cpp
@@ -78,7 +78,10 @@ Card *CardPile::PopCardFromDeck(void)
if (deckCurrIndex >= 0)
return deck[deckCurrIndex--];
else
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "deck is empty!\n");
return NULL;
+ }
}
void CardPile::resetDeck(void)
diff --git a/game.cpp b/game.cpp
index 2bfedcd..7e6e866 100644
--- a/game.cpp
+++ b/game.cpp
@@ -256,6 +256,36 @@ bool Game::doWeHaveAnAce(Card **cards)
return false;
}
+void Game::discardAndDraw(Card **cards, int amount, int offset)
+{
+ if (offset + amount > 5)
+ {
+ PRINT_COLOR(ANSI_COLOR_RED, "amount of cards to be discarded is out of bounds!\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;
+#endif
+ putCardToDiscardPile(cards[offset+i]);
+ }
+
+ /* draw cards from the deck into starting at cards[offset] */
+ for (i = 0; i < amount; i++)
+ {
+ cards[offset+i] = PopCardFromDeck();
+#ifdef DEBUG
+ cout << "debug: drawing card " << i+1 << ": " << cards[offset+i]->getType()
+ << " at cards[" << offset << "]" << endl;
+#endif
+ }
+}
+
void Game::opponentAI(Card **cards)
{
struct countAndSuit tmp;
@@ -275,5 +305,13 @@ void Game::opponentAI(Card **cards)
/* keep 4 cards of the same suit and discard 1 of different suit */
return;
}
+ else if (doWeHaveAnAce(cards))
+ {
+ /* discard 4 cards */
+ }
+ else
+ {
+ /* discard 3 cards */
+ }
}
diff --git a/game.h b/game.h
index ed2807b..cb1c528 100644
--- a/game.h
+++ b/game.h
@@ -2,7 +2,6 @@
#define _GAME_H_
#include <iostream>
-#include <cstdio>
#include <string>
#include <cstdlib>
#include <ctime>
@@ -13,15 +12,6 @@
#include "user.h"
#include "opponent.h"
-#define ANSI_COLOR_RED "\x1b[31m"
-#define ANSI_COLOR_GREEN "\x1b[32m"
-#define ANSI_COLOR_YELLOW "\x1b[33m"
-#define ANSI_COLOR_BLUE "\x1b[34m"
-#define ANSI_COLOR_MAGENTA "\x1b[35m"
-#define ANSI_COLOR_CYAN "\x1b[36m"
-#define ANSI_COLOR_RESET "\x1b[0m"
-#define PRINT_COLOR(C, S) printf("%s%s%s", C, S, ANSI_COLOR_RESET)
-
using std::cout;
using std::endl;
using std::cin;
@@ -52,6 +42,7 @@ class Game : virtual public CardPile, public User, public Opponent
struct countAndSuit numOfSameSuit(Card **);
bool doWeHaveAnAce(Card **);
+ void discardAndDraw(Card **, int, int);
void opponentAI(Card **);
/* variables */
diff --git a/main.cpp b/main.cpp
index 710d7a2..9fa9d51 100644
--- a/main.cpp
+++ b/main.cpp
@@ -29,7 +29,7 @@ int main(int argc, char *argv[])
pokerGame->shufflePile();
#ifdef DEBUG
- cout << "Deck after shuffle" << endl;
+ PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after shuffle\n");
pokerGame->printPile();
#endif
@@ -49,6 +49,13 @@ int main(int argc, char *argv[])
cout << "Top suit: " << *pokerGame->suits[userCountAndSuit.whatSuit] << ", count: "
<< userCountAndSuit.count << endl;
+ if ( pokerGame->doWeHaveAnAce(pokerGame->getUserCards()) )
+ cout << "You have an Ace!" << endl;
+
+ pokerGame->discardAndDraw( pokerGame->getUserCards(), 2, 3 );
+ cout << "Cards in your hand after discarding: ";
+ pokerGame->printUserCards();
+
cout << "\nDealing cards to opponent(s)" << endl;
pokerGame->dealOpponentCards(pokerGame->numOfOpponents);
pokerGame->sortOpponentCards(pokerGame->numOfOpponents);
@@ -56,7 +63,7 @@ int main(int argc, char *argv[])
pokerGame->printOpponentCards(pokerGame->numOfOpponents);
#ifdef DEBUG
- cout << "\nDeck after being delt to opponents and user" << endl;
+ PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n");
pokerGame->printPile();
#endif