Hi. This program is pretty much working. I just can't figure out how to compare the higher hand when each player get a pair/three a kind.

CardDeck.h

enum Suit {clubs, diamonds, hearts, spades};
//---------------- C L A S S   C A R D -----------------------------------------
class Card  // this class describes a single card
{
public:
	
	Card();
	void Display();		// for displaying a card in a readable fashion
	int GetValue();    	// retrieves the card's value
	void SetValue(int); // sets the card's value to the value provided
	Suit GetSuit();     // retrieves the card's suit
	void SetSuit(Suit); // sets the card's suit to the suit provided

private:                // we keep these private so that only authorized
		                // users of the class can access them directly
	Suit suit;			// each card has a suit
	int value;			// and a value (2..14, representing 2..10,J,Q,K, Ace)
};

//----------------- C L A S S   D E C K --------------------------------------------

class Deck           // This class describes a collection of 52 cards
{


public:
	Deck();			 // the constructor opens a new deck of cards.
	void Shuffle();  // shuffle Deck (mix up order of the cards in deck)
	void ShowDeck(); // Display each card in the deck from topCard to last card
	int ReturnTopCard();  // return the index of the cards dealt
	Card DealACard();   // deal a card from the top
	void ShowCard();  // Display a single card

private:
	int topCard;	 // points to position of current top card of deck
	Card cards[52];	 // a deck is 52 cards.
};

CardDeck.cpp

#include <ctime>
#include <cstdlib>
#include <cassert>      // assertion library
#include <iostream>		// for cout
#include "CardDeck.h"

using namespace std;

//---------------- C L A S S   C A R D   F U N C T I O N S ----------------
Card::Card()
{
}
// preconditions:  Card has valid suit and value
// postconditions: Card's suit and value displayed (screen default)
void Card::Display()
// display the suit and value of an individual card
{
	if ((2 <= value) && (value <= 10))	// for number cards, show value
		cout << value;              
	else						// for face cards and Ace use abbreviation
	{
		switch (value)
		{

		case 11: cout << 'J'; break;
		case 12: cout << 'Q'; break;
		case 13: cout << 'K'; break;
		case 14: cout << 'A'; break;
		}
	}
		
	switch (suit)			// display suit
	{
		case clubs:	    cout << char(5); break; //	cout << " of clubs"; 	break;
		case diamonds:  cout << char(4); break; //	cout << " of diamonds"; break;
		case hearts:	cout << char(3); break; //" of hearts"; 	break;
		case spades:	cout << char(6); break; //" of spades"; 	break;
	};
}
//---------------------------------------------------------------------------------------------
//preconditions: Card has been defined (valid data)
//postconditions: the value of card is returned ( 2 - 14)
int Card::GetValue()
// return the numeric value of a card
{
	return (value);
}
//---------------------------------------------------------------------------------------------
//preconditions: input for value is valid (2 - 14)
//postconditions: if valid card value is changed to input; if invalid program exits with assertion error
void Card::SetValue(int v)
// set the numeric value of a card
{
	assert ( v >= 2 && v <= 14);  //must be 2 to 14 (2 to Ace)
	value = v;
}
//---------------------------------------------------------------------------------------------
//preconditions:Card has been defined (valid data)
//postconditions: The suit of the card is returned ( clubs, diamonds, hearts, or spades)
Suit Card::GetSuit()
// return the suit value of a card
{
	return (suit);
}
//---------------------------------------------------------------------------------------------
//preconditions: input for suit is valid ( clubs, diamonds, hearts, or spades)
//postconditions: suit member of card is changed to input; if invalid, exits with assertion error
void Card::SetSuit(Suit st)
// set the suit of a card
{
	assert (st == clubs || st == diamonds || st == hearts || st == spades);
	// suit must be clubs, diamonds, hearts, or spades
	suit = st;
}

//----------------- C L A S S   D E C K    F U N C T I O N S -------------------------

//---------------------------------------------------------------------------------------------
//preconditions: topcard = 0
//postconditions: Each card is assign with suit and value starting from 
//				  2 of Club to Ace of Spade
Deck::Deck()
// constructor for initializing a new deck of 52 cards
{
	topCard = 0;							// we haven't dealt any cards yet
	
	for (int i = 0; i < 52; i++)	    	// for each card in the deck:
	{
		cards[i].SetValue((i % 13) + 2);	// assign it a numeric value (1 - 13)
		switch (i / 13)	 					// and a suit.
		{
			case 0: cards[i].SetSuit(clubs);	break;
			case 1: cards[i].SetSuit(diamonds);	break;
			case 2: cards[i].SetSuit(hearts);	break;
			case 3: cards[i].SetSuit(spades);	break;
		}
	}
}
//---------------------------------------------------------------------------------------------
//preconditions: topCard = 0
//postconditions: Assign each card with suit and value randomly and 
//				  total of 52 cards. 
void Deck::Shuffle()
// shuffles the deck by switching each card
// with a randomly selected card
{
	srand(time (0) );				// Initialize the random number generator.
	
		
	for (int i = 0; i < 3; i++)		// Shuffle 3 times.
	{
		for (int j = 0; j < 52; j++)	// Rearrange each card (0 - 51).
		{
			int r = rand() % 52;		// Pick a location to swap j-th card with
			Card c = cards[j];
			cards[j] = cards[r];
			cards[r] = c;
		}
	}
	
	topCard = 0;						// no cards dealt yet, set top card to card 0
}
//---------------------------------------------------------------------------------------------
//preconditions: All card must be assigned
//postconditions: Show the deck of card in 4 columns
void Deck::ShowDeck()
{
	for (int i = 0; i < 52; i++)
	{	
		if (i%4 == 0)         // arrange cards 4 columns per line
		   cout << "\n";	
		cards[i].Display();
		cout << "\t";	
	}	
}

//---------------------------------------------------------------------------------------------
//preconditions: Cards must be assigned
//postconditions: Return the the top card
int Deck::ReturnTopCard()
{
	return topCard;
}

//----------------------------------------------------------------------------------------------
//precondition: topcard < 52
//postcondition: Return the card thats on top of the deck
//				 and add one to topcard
Card Deck::DealACard()
{		
	return cards[topCard++];	
}

//precondition: none
//postcondition: show the top card
void Deck::ShowCard()
{	
	cards[topCard].Display();
	cout << "  ";
}

PokerGame.h

#include "Player.h"
#include <iostream>
#include <string>

using namespace std;

class PokerGame
{
public:
	PokerGame();
	void Instructions();      // Displays the instructions to the user(s)
	void PlayGame();          // Runs the game
	
private:
	Deck deck;
	Player player1,
		   player2;
	void DealCards();         // Deals 5 cards to each player alternating between players
	void StartGame();         // Pre-game management: Shuffle, get player names
	int DetermineWinner(); // Determines who wins the hand
	void AnnounceWinner();    // Announces which player won & their hand ranks	
};

PokerGame.cpp

#include <iostream>
#include "PokerGame.h"

using namespace std;

Deck deck;
Card card;

PokerGame::PokerGame()
{}

void PokerGame::Instructions()
{
	cout << "Poker is played with a standard deck of 52 cards. The cards are \n"
		 << "ranked Ace, King, Queen, Joker, 10, 9, 8, 7, 6, 5, 4, 3, 2. \n"
		 << "There are four suits; however, no suit is higher than another.\n"
		 << "All poker hands contain five cards, the biggest hand wins.\n\n";
	cout<<endl;
}

void PokerGame::DealCards()
{   

	for(int i=1;i<11;i++) 
	{	
		card=deck.DealACard();
		if(i%2!=0)
			player1.SetCardInHand(card);
		else
			player2.SetCardInHand(card);
	}
	
}

int PokerGame::DetermineWinner()
{
	if(player1.ReportHand()>player2.ReportHand())
		return 1;
	else if(player2.ReportHand()>player1.ReportHand())
		return 2;
	else		
	{
		if((player1.ReportHighCard()>player2.ReportHighCard()) || (player1.ReportHand()>player2.ReportHand()))
			return 1;
		else if((player2.ReportHighCard()>player1.ReportHighCard()) || (player2.ReportHand()>player1.ReportHand()))
			return 2;
		else 
			return 3;	
	}
}

void PokerGame::StartGame()
{
	Instructions();
	player1.GetPlayerName();
	player2.GetPlayerName();	
	deck.Shuffle();
}

void PokerGame::AnnounceWinner()
{
	cout<<endl;
		cout<<player1.ReportName()<<" has a: "<<endl;
		if(player1.ReportHand()== 9)
			cout<<"Royal Flush."<<endl;
		else if(player1.ReportHand()== 8)
			cout<<"Straight Flush."<<endl;
		else if(player1.ReportHand()== 7)
			cout<<"Four Of A Kind."<<endl;
		else if(player1.ReportHand()== 6)
			cout<<"Full House."<<endl;
		else if(player1.ReportHand()== 5)
			cout<<"Flush."<<endl;
		else if(player1.ReportHand()== 4)
			cout<<"Straight."<<endl;
		else if(player1.ReportHand()== 3)
			cout<<"Three Of A Kind."<<endl;
		else if(player1.ReportHand()== 2)
			cout<<"Two Pair."<<endl;
		else if(player1.ReportHand()== 1)
			cout<<"Pair."<<endl;
		else
		{
			if(player1.ReportHighCard()<11)
			cout<<" High Card of "<<player1.ReportHighCard()<<"."<<endl;
			else if(player1.ReportHighCard()==11)
			cout<<" High Card of J."<<endl;	
			else if(player1.ReportHighCard()==12)
			cout<<" High Card of Q."<<endl;	
			else if(player1.ReportHighCard()==13)
			cout<<" High Card of K."<<endl;	
			else if(player1.ReportHighCard()==14)
			cout<<" High Card of A."<<endl;	
			
		}

		cout<<endl;

		cout<<player2.ReportName()<<" has a: "<<endl;
		if(player2.ReportHand()== 9)
			cout<<"Royal Flush."<<endl;
		else if(player2.ReportHand()== 8)
			cout<<"Straight Flush."<<endl;
		else if(player2.ReportHand()== 7)
			cout<<"Four Of A Kind."<<endl;
		else if(player2.ReportHand()== 6)
			cout<<"Full House."<<endl;
		else if(player2.ReportHand()== 5)
			cout<<"Flush."<<endl;
		else if(player2.ReportHand()== 4)
			cout<<"Straight."<<endl;
		else if(player2.ReportHand()== 3)
			cout<<"Three Of A Kind."<<endl;
		else if(player2.ReportHand()== 2)
			cout<<"Two Pair."<<endl;
		else if(player2.ReportHand()== 1)
			cout<<"Pair."<<endl;
		else
		{
			if(player2.ReportHighCard()<11)
			cout<<" High Card of "<<player2.ReportHighCard()<<"."<<endl;
			else if(player2.ReportHighCard()==11)
			cout<<" High Card of J."<<endl;	
			else if(player2.ReportHighCard()==12)
			cout<<" High Card of Q."<<endl;	
			else if(player2.ReportHighCard()==13)
			cout<<" High Card of K."<<endl;	
			else if(player2.ReportHighCard()==14)
			cout<<" High Card of A."<<endl;	
			
		}

		cout<<endl;

	if(DetermineWinner()==1)
		cout<<player1.ReportName()<<" wins! "<<endl;
	else if(DetermineWinner()==2)
		cout<<player2.ReportName()<<" wins! "<<endl;
	else
		cout<<"It is a tie. "<<endl;
}

void PokerGame::PlayGame()
{
	StartGame();
	DealCards();
	player1.SortHand();
	player2.SortHand();
	player1.ShowHand();
	player2.ShowHand();
	AnnounceWinner();
}

Player.h

#ifndef _HPLAYER     // if not already included as definition
#define _HPLAYER     // in this programming project include it

#include <iostream>
#include <string>
#include <cassert>
#include <iomanip>
#include "Carddeck.h"


using namespace std;

enum HandType { HighCard, Pair, twoPairs, threeOfAKind,
                straight, Flush, fullHouse, fourOfAKind,
				straightFlush, royalFlush};
class Player
{
public:
	Player();
	void ShowHand(); //displays the cards in the players hand 
	HandType ReportHand();
	void SetCardInHand(Card);
	int ReportHighCard();
    string ReportName();
	void GetPlayerName();
	void SortHand();	

private:	
	string name;
	Card hand[5];
	int cardCount;
	int handStrength;
	bool IsOnePair();
	bool IsTwoPairs();
	bool IsTriplets();
	bool IsStraight();
	bool IsFlush();
	bool IsFullHouse();
	bool IsFourOfKind();
	bool IsStraightFlush();
	bool IsRoyalFlush();
	
};
#endif _HPLAYER

Player.cpp

#include <iostream>
#include "Player.h"

using namespace std;

Player::Player()
{
	cardCount=0;
}

void Player::GetPlayerName()
{
	cout << "Please enter your name: ";
	cin >> name;
}


string Player::ReportName()
{
	return name;
}

void Player::SortHand()
{
	for(int j = 0; j < 4; j++)	
	{
		int highCard = j;
		for (int i = j+1; i < 5; i++)
		{
			if(hand[i].GetValue() > hand[highCard].GetValue())
				highCard = i;
		}

		Card c = hand[j];
		hand[j] = hand[highCard];
		hand[highCard] = c;
	}
}

void Player::SetCardInHand(Card card)
{	
	hand[cardCount] = card;
	cardCount++;
}

void Player::ShowHand()
{
	cout << ReportName() << "'s hand is :";

	for(int counter = 0; counter < 5; counter++)
	{
		hand[counter].Display();
		cout << " ";	
	}
	cout<<endl;
}

int Player::ReportHighCard()
{
	return hand[0].GetValue();
}

bool Player::IsOnePair()
{
	if((hand[0].GetValue()==hand[1].GetValue())	|| (hand[1].GetValue()==hand[2].GetValue())	|| (hand[2].GetValue()==hand[3].GetValue())	|| (hand[3].GetValue()==hand[4].GetValue()))//determines if there is any pair
		return true;
	else
		return false;
}

bool Player::IsTwoPairs()
{

	if((hand[0].GetValue()==hand[1].GetValue())	&& (hand[2].GetValue()==hand[3].GetValue()))
		return true;
	else if((hand[1].GetValue()==hand[2].GetValue()) && (hand[3].GetValue()==hand[4].GetValue()))
		return true;
	else if((hand[0].GetValue()==hand[1].GetValue()) && (hand[3].GetValue()==hand[4].GetValue()))
		return true;
	else
		return false;	
}

bool Player::IsTriplets()
{

	if(hand[0].GetValue()==hand[1].GetValue() && hand[1].GetValue()==hand[2].GetValue())//checks for three of a kind
		return true;
	else if(hand[1].GetValue()==hand[2].GetValue() && hand[2].GetValue()==hand[3].GetValue())
		return true;
	else if(hand[2].GetValue()==hand[3].GetValue() && hand[3].GetValue()==hand[4].GetValue())
		return true;
	else
		return false;	
}

bool Player::IsFlush()
{
	bool flush;

	for(int counter=0; counter<4; counter++)//if it's a flush
		if(hand[counter].GetSuit()==hand[counter+1].GetSuit())
			flush=true;
		else
		{
			flush=false;
			break;
		}

	return flush;

}

bool Player::IsStraight()
{
	bool straight;
	for(int counter=0; counter<4;counter++)//if it's a straight
		if((hand[counter].GetValue()-1)==hand[counter+1].GetValue())
			straight=true;
		else
		{
			straight=false;
			break;
		}

	return straight;
}

bool Player::IsFullHouse()
{	
	if((hand[0].GetValue()==hand[1].GetValue() && hand[1].GetValue()==hand[2].GetValue()) && (hand[3].GetValue()==hand[4].GetValue()))//if it's a full house
		return true;
	else if((hand[2].GetValue()==hand[3].GetValue() && hand[3].GetValue()==hand[4].GetValue()) && (hand[0].GetValue()==hand[1].GetValue()))
		return true;
	else
		return false;
}

bool Player::IsFourOfKind()
{
	if(hand[0].GetValue()==hand[1].GetValue() &&  hand[1].GetValue()==hand[2].GetValue() && hand[2].GetValue()==hand[3].GetValue())//if it's a 
		return true;
	else if( hand[1].GetValue()==hand[2].GetValue() &&  hand[2].GetValue()==hand[3].GetValue() && hand[3].GetValue()==hand[4].GetValue())
		return true;
	else
		return false;
}

bool Player::IsStraightFlush()
{
	bool straight, flush;
	for(int counter=0; counter<4;counter++)//if it's a straight
		if((hand[counter].GetValue()-1)==hand[counter+1].GetValue())
			straight=true;
		else
		{
			straight=false;
			break;
		}


	for(int counter=0; counter<4; counter++)//if it's a flush
		if(hand[counter].GetSuit()==hand[counter+1].GetSuit())
			flush=true;
		else
		{
			flush=false;
			break;
		}

	if(straight==true && flush==true)
		return true;
	else
		return false;

}

bool Player::IsRoyalFlush()
{

		bool straight, flush;
	for(int counter=0; counter<4;counter++)//if it's a straight
		if((hand[counter].GetValue()-1)==hand[counter+1].GetValue())
			straight=true;
		else
		{
			straight=false;
			break;
		}


	for(int counter=0; counter<4; counter++)//if it's a flush
		if(hand[counter].GetSuit()==hand[counter+1].GetSuit())
			flush=true;
		else
		{
			flush=false;
			break;
		}

	if(straight==true && flush==true && hand[0].GetValue()== 14)
		return true;
	else
		return false;


}

HandType Player::ReportHand()
{

	if(IsRoyalFlush()==true)
		return royalFlush;
	else if(IsStraightFlush()==true)
		return straightFlush;
	else if(IsFourOfKind()==true)
		return fourOfAKind;
	else if(IsFullHouse()==true)
		return fullHouse;
	else if(IsFlush()==true)
		return Flush;
	else if(IsStraight()==true)
		return straight;
	else if(IsTriplets()==true)
		return threeOfAKind;
	else if(IsTwoPairs()==true)
		return twoPairs;
	else if(IsOnePair()==true)
		return Pair;
	else
		return HighCard;
}

PlayPoker.cpp

#include "Player.h"
#include "PokerGame.h"

using namespace std;


void main()
{
	char con;
	do{
	PokerGame game;	
	game.PlayGame();

	cout<<"Do you want to play again (y for yes, n for no): "<<endl;
	cin>>con;


	}while(con=='y' || con=='Y');
}

There might be some code that I forgot to remove when i try to make it work.

You really want us to look through 650 lines of code to
1) find you compare function?
2) figure out what's wrong?

Sorry, nope. Pinpoint the problem areas and we can see what we can find.

Sorry about the long code. 1st time using this.

Under the function "void PokerGame::AnnounceWinner()" in PokerGame.cpp (might want to take a look in Player.cpp for ReportHand()),
I compare both players' hands with numbers. Lets say both players get a pair, then both player will have a score of 1.
This program only compare the score of 1 from both player, so it announce the game as a tie. I was trying to set the players' hands into comparing high cards of the pair if both players have a pair.

CardDeck.h and CardDeck.cpp have no problem at all. Just included it in case someone wants to use the code and try it out.

It's fine if its too long of confusing to help. Appreciate your afford though.

Perhaps a better way to assign scores would take account of all the possible hands. For example, the fact of getting a pair is 100 points, and then add another value which is the sum of the two cards making the pair. In this way, a pair of threes would score 106, and a pair of eights would score 108.

I suggest 100 as a base value, rather than 1, so that you could go on to assign a score for every possible hand, and just use these scores to see who wins. For example, if you decide to give three-of-a-kind a base score of 500, it's clear that even the very lowest three-of-a-kind will always score higher than the very best pair.

A simple highest-value card hand would have a base score of zero, so the best possible highest-value card will always score less than the very lowest pair score.

Edited 5 Years Ago by Moschops: n/a

Or add another value called HighCard. For a pair, set HighCard to the value of the pair (4 for Fours, 11 for Jacks).

This would be helpful for Pairs, Trips, Straights, etc.

This question has already been answered. Start a new discussion instead.