summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle K <kylek389@gmail.com>2011-01-31 15:40:30 -0600
committerKamil Kaminski <kamilkss@gmail.com>2011-01-31 15:40:30 -0600
commit1458bf374f2070b3879a3ead7a3af5c5651e824b (patch)
treec0086a60a6178d0730b46b5bb5dc861454d8f7c0
parent53851a3b1af720a58360a5019a1707eba74199d3 (diff)
downloadpoker-1458bf374f2070b3879a3ead7a3af5c5651e824b.tar.gz
poker-1458bf374f2070b3879a3ead7a3af5c5651e824b.tar.bz2
poker-1458bf374f2070b3879a3ead7a3af5c5651e824b.zip
do some more little work
-rw-r--r--Makefile5
-rw-r--r--card.cpp43
-rw-r--r--card.h2
-rw-r--r--cardpile.h12
-rw-r--r--game.cpp23
-rw-r--r--game.h13
-rw-r--r--sorthand.cpp38
-rw-r--r--sorthand.h19
8 files changed, 138 insertions, 17 deletions
diff --git a/Makefile b/Makefile
index 468cd4c..6a63de7 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@
# Makefile
PROG = poker
-OBJS = main.o cardpile.o card.o game.o user.o opponent.o
+OBJS = main.o cardpile.o card.o game.o user.o opponent.o sorthand.o
CC = g++
DBGFLAGS = -g -O0
ifdef DEBUG
@@ -38,6 +38,9 @@ user.o: %.o: %.cpp %.h
opponent.o: %.o: %.cpp %.h
$(CC) -c $(CFLAGS) $<
+sorthand.o: %.o: %.cpp %.h
+ $(CC) -c $(CFLAGS) $<
+
.PHONY: clean
clean:
diff --git a/card.cpp b/card.cpp
index 5942c91..f100e1c 100644
--- a/card.cpp
+++ b/card.cpp
@@ -11,6 +11,12 @@ Card::Card(void) : type(""), rank(0)
}
+Card::Card(Card *card)
+{
+ type = card->type;
+ rank = card->rank;
+}
+
Card::Card(string &str)
{
type = str;
@@ -69,7 +75,6 @@ void Card::rankCard(void)
break;
default:
std::cerr << "something went wrong while ranking a card" << endl;
- break;
}
}
@@ -126,3 +131,39 @@ void Card::sortCards(Card **cards)
}
}
+char Card::giveRank(char type)
+{
+ switch (type)
+ {
+ case 'A':
+ return 13;
+ case 'K':
+ return 12;
+ case 'Q':
+ return 11;
+ case 'J':
+ return 10;
+ case 'T':
+ return 9;
+ case '9':
+ return 8;
+ case '8':
+ return 7;
+ case '7':
+ return 6;
+ case '6':
+ return 5;
+ case '5':
+ return 4;
+ case '4':
+ return 3;
+ case '3':
+ return 2;
+ case '2':
+ return 1;
+ default:
+ std::cerr << "something went wrong while giving a card" << endl;
+ return 0;
+ }
+}
+
diff --git a/card.h b/card.h
index 77a3804..ad5c238 100644
--- a/card.h
+++ b/card.h
@@ -30,6 +30,7 @@ class Card
public:
Card(void);
Card(string &);
+ Card(Card *);
int getRank(void);
enum Suit getSuit(void);
@@ -38,6 +39,7 @@ class Card
string getType(void);
void rankCard(void);
void sortCards(Card **);
+ char giveRank(char);
};
#endif
diff --git a/cardpile.h b/cardpile.h
index a1e2469..25f0132 100644
--- a/cardpile.h
+++ b/cardpile.h
@@ -9,6 +9,18 @@ using std::cout;
using std::endl;
using std::string;
+struct countAndSuit
+{
+ enum Suit whatSuit;
+ int count;
+};
+
+struct countAndType
+{
+ char type;
+ int count;
+};
+
class CardPile : public Card
{
private:
diff --git a/game.cpp b/game.cpp
index ef263c4..c0a0dd0 100644
--- a/game.cpp
+++ b/game.cpp
@@ -362,7 +362,7 @@ struct countAndType Game::numOfSameType(Card **cards)
#ifdef DEBUG
cout << ANSI_COLOR_CYAN << "\ndebug: \"" << same[whereMaxWasFound].type
<< "\" has highest occurence, occurs " << same[whereMaxWasFound].count
- << " times" << ANSI_COLOR_RESET;
+ << " time(s)" << ANSI_COLOR_RESET;
#endif
return same[whereMaxWasFound];
@@ -785,6 +785,8 @@ void Game::evaluateHand(Card **cards, int who)
struct countAndSuit tmp = numOfSameSuit(cards);
struct countAndType tmp2 = numOfSameType(cards);
+ /* XXX: Some hands need to be sorted */
+
/* royal flush */
if (haveAce && inOrderByOne && (tmp.count == 5))
whatHand = 0;
@@ -877,12 +879,29 @@ void Game::determineWinner(void)
int whatIndex = -1;
for (i = 0; i < numOfPlayers; i++)
- if (playerHands[i] < min)
+ if (playerHands[i] <= min)
{
min = playerHands[i];
whatIndex = i;
}
+ /* deal with ties */
+ int numOfTies = 0;
+ for (i = 0; i < numOfPlayers; i++)
+ if (playerHands[i] == min)
+ {
+ ties[i] = true;
+ numOfTies++;
+ }
+
+#ifdef DEBUG
+ if (numOfTies > 1)
+ cout << ANSI_COLOR_CYAN << "\ndebug: there's a tie between " << numOfTies
+ << " players" << endl << ANSI_COLOR_RESET;
+#endif
+
+ /* XXX: UNFINISHED */
+
cout << ANSI_COLOR_CYAN << endl << *winmsg[whatIndex] << endl
<< ANSI_COLOR_RESET;
diff --git a/game.h b/game.h
index ceac0dc..10e401f 100644
--- a/game.h
+++ b/game.h
@@ -16,18 +16,6 @@ using std::cout;
using std::endl;
using std::cin;
-struct countAndSuit
-{
- enum Suit whatSuit;
- int count;
-};
-
-struct countAndType
-{
- char type;
- int count;
-};
-
class Game : virtual public CardPile, public User, public Opponent
{
private:
@@ -40,7 +28,6 @@ class Game : virtual public CardPile, public User, public Opponent
void askForNumberOfOpponents(void);
void repeatGame(bool *);
- /* asks user for a number, param1 = msg, 2 = rangeA, 3 = rangeB */
int parseInt(string *, int, int);
int howManyCardsOfSameSuit(Card **);
bool pairOrBetter(Card **);
diff --git a/sorthand.cpp b/sorthand.cpp
new file mode 100644
index 0000000..8445e7f
--- /dev/null
+++ b/sorthand.cpp
@@ -0,0 +1,38 @@
+/*
+ * sorthand.cpp
+ *
+ *
+ */
+
+#include "sorthand.h"
+
+SortHand::SortHand()
+{
+
+}
+
+void sortFourOfAKind(Card **cards, struct countAndType *maxCount)
+{
+ int i;
+ /* find the card that is different from other four */
+ for (i = 0; i < 5; i++)
+ if (cards[i]->getRank() != maxCount->type)
+ break;
+
+#ifdef DEBUG
+ cout << ANSI_COLOR_CYAN << "\ndebug: sortFourOfAKind(): " << cards[i]->getType()
+ << " is different from other 4 cards" << endl << ANSI_COLOR_RESET;
+#endif
+
+ /* put that card at the end */
+ Card *tmp = cards[i];
+
+ cards[4] = cards[i];
+ cards[i] = tmp;
+}
+
+void sortFullHouse(Card **cards, struct countAndType *threeCount)
+{
+
+}
+
diff --git a/sorthand.h b/sorthand.h
new file mode 100644
index 0000000..3baece7
--- /dev/null
+++ b/sorthand.h
@@ -0,0 +1,19 @@
+#ifndef _SORTHAND_H_
+#define _SORTHAND_H_
+
+#include "cardpile.h"
+
+class SortHand : virtual public CardPile
+{
+ private:
+
+ protected:
+
+ public:
+ SortHand(void);
+
+ void sortFourOfAKind(Card **, struct countAndType *);
+};
+
+#endif
+