It took me quite a while but that's what I got so far for the Video poker game. Right now I'm working on the run game fuction. Any suggestions?
Code:
// I N C L U D E S ////////////////////////////////////////////////////////////////
#include <stdlib.h> // for itoa(), rand(), and srand()
#include <time.h> // tine() needed to seed srand()
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// D A T A T Y P E S /////////////////////////////////////////////////////////////
// enumerate our suits
enum Suit {SPADE, CLUB, HEART, DIAMOND};
// Basic card structure
struct Card
{
int value; // Card value
Suit suit; // Card suit
bool used; // Was the card used already?
};
// G L O B A L S //////////////////////////////////////////////////////////////////
struct Card DECK[52]; // Our deck of cards
// P R O T O T Y P E S ////////////////////////////////////////////////////////////
// Game related functions
void print_hand(int hand[]);
void generate_deck();
void deal(int hand[]);
void run_game(void);
// Utility functions
int x_of_a_kind( int hand[], int rank);
void reset_deck();
int get_card();
// Test hands
bool is_royal_flush(int hand[]);
bool is_straight_flush(int hand[]);
bool is_flush(int hand[]);
bool is_straight(int hand[]);
bool is_full_house(int hand[]);
bool is_four_of_a_kind(int hand[]);
bool is_three_of_a_kind(int hand[]);
bool is_two_pair(int hand[]);
bool is_pair(int hand[]);
// F U N C T I O N S //////////////////////////////////////////////////////////////
bool is_royal_flush(int hand[])
{
int i,
high = 0;
if( is_straight_flush(hand) )
{
// Test to see if high card is a king
for(i = 0; i < 5; i++)
if( DECK[ hand[i] ].value > high)
high = DECK[ hand[i] ].value;
if(high == 13)
return true;
} // end if
return false;
} // end is_royal_flush
///////////////////////////////////////////////////////////////////////////////////
bool is_straight_flush(int hand[])
{
if( is_flush(hand) && is_straight(hand) )
return true;
return false;
} // end is_straight_flush
///////////////////////////////////////////////////////////////////////////////////
bool is_flush(int hand[])
{
Suit s = DECK[ hand[0] ].suit;
for(int i = 1; i < 5; i++)
if( DECK[ hand[i] ].suit != s)
return false;
return true;
} // end is_flush()
///////////////////////////////////////////////////////////////////////////////////
bool is_straight(int hand[])
{
int values[5],
i,
j,
low=15;
bool king=false,
next;
// PASS 1 - stock array of values and check for a king
for(i = 0, king = false; i < 5; i++)
{
if( DECK[ hand[i] ].value == 13)
king = true;
values[i] = DECK[ hand[i] ].value;
} // end for
// PASS 2 - Find low value and convert any aces to high value if there is a king in the hand
for(i = 0; i < 5; i++)
{
if(values[i] == 1 && king == true)
values[i] = 14;
if(values[i] < low)
low = values[i];
} // end for
// PASS 3 - Check ordering
for(i = 0; i < 4; i++)
{
// Test to see if there is one card higher than low
for(j = 0, next = false; j < 5; j++)
{
if(values[j] == low + 1)
{
low = values[j];
next = true;
break;
} // end if
} // end for
// if it didn't find the next card
if(next == false)
return false;
} // end for
return true;
} // end is_straight()
///////////////////////////////////////////////////////////////////////////////////
int x_of_a_kind( int hand[], int rank)
{
// TODO: Lab
int matches[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
max_element,
max;
for(int i = 0; i < 5; i++)
matches[ DECK[ hand[i] ].value - 1]++;
for(int j = 0; j < rank; j++)
{
max = 0;
for(int i = 0; i < 13; i++)
{
if( matches[i] > max )
{
max_element = i;
max = matches[i];
} // end if
} // end for
matches[max_element] = 0;
} // end for
return max;
} // end x_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_full_house(int hand[])
{
if( x_of_a_kind(hand, 1) == 3 && x_of_a_kind(hand,2) == 2 )
return true;
else
return false;
} // end is_full_house()
///////////////////////////////////////////////////////////////////////////////////
bool is_four_of_a_kind(int hand[])
{
if( x_of_a_kind(hand, 1) == 4)
return true;
else
return false;
} // end is_four_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_three_of_a_kind(int hand[])
{
if( x_of_a_kind(hand, 1) == 3)
return true;
else
return false;
} // end is_three_of_a_kind()
///////////////////////////////////////////////////////////////////////////////////
bool is_two_pair(int hand[])
{
if( x_of_a_kind(hand, 1) == 2 && x_of_a_kind(hand,2) == 2 )
return true;
else
return false;
} // end is_two_pair()
///////////////////////////////////////////////////////////////////////////////////
bool is_pair(int hand[])
{
if( x_of_a_kind(hand, 1) == 2)
return true;
else
return false;
} // end is_pair()
///////////////////////////////////////////////////////////////////////////////////
/* Prints cards in array hand. hand[] is expected to be an array of
* 5 integers that corospond to elements in the deck. Numbers are also
* printed below each card.
*/
void print_hand(int hand[])
{
string output;
char c[5]; // buffer to hold integer conversions
// Print out the hand
for(int i = 0; i < 5; i++)
{
// Output the card value
switch( DECK[hand[i]].value )
{
case 1:
output = "A";
break;
case 11:
output = "J";
break;
case 12:
output = "Q";
break;
case 13:
output = "K";
break;
default:
// Convert integer value to ascii
itoa(DECK[hand[i]].value, c, 10);
output = c;
} // end switch
// Now output the suit
switch(DECK[hand[i]].suit)
{
case SPADE:
output = output + "S";
break;
case CLUB:
output = output + "C";
break;
case HEART:
output = output + "H";
break;
case DIAMOND:
output = output + "D";
break;
} // end switch
// Output the card
cout << setw(4) << output;
} // end for
cout << endl;
// print out card numbers below the hand
for(int i = 1; i <= 5; i++)
cout << setw(4) << i;
cout << endl;
} // end print_hand()
///////////////////////////////////////////////////////////////////////////////////
/* Sets up the deck of cards to be used for the video poker program.
* This function should be called at the beginning of the program
*/
void generate_deck()
{
// cycle through each suit...
for(Suit current_suit = SPADE; current_suit <= DIAMOND;
current_suit = static_cast<Suit>( current_suit + 1) )
{
// cycle through suit values
for(int index = 0; index < 13; index++)
{
DECK[ (current_suit*13) + index ].value = index + 1;
DECK[ (current_suit*13) + index ].suit = current_suit;
} // end suit values
} // end for suit
} // end generate_deck
///////////////////////////////////////////////////////////////////////////////////
/* Reset the deck's used status. This should be done before
* the deal()
*/
void reset_deck()
{
for(int i = 0; i < 52; i++)
DECK[i].used = false;
} // end reset_deck()
///////////////////////////////////////////////////////////////////////////////////
/* Gets a random card that is not currently used. sets used status to true
* once a card has been selected.
*/
int get_card()
{
int element;
// pick out random card
do
{
element = rand()%52;
} while( DECK[element].used == true );
// Mark the card as used
DECK[element].used = true;
return element;
} // end get_card
///////////////////////////////////////////////////////////////////////////////////
/* Deals 5 random cards and placed their index in hand[]. Since deal()
* uses get_card, the card is marked used.
*/
void deal(int hand[])
{
// Get 5 random cards
for(int i = 0; i < 5; i++)
hand[i] = get_card();
} // end deal
///////////////////////////////////////////////////////////////////////////////////
// Start playing video poker!
void run_game(void)
{
// deposit $
// deal 5 random cards
// option to throw out up to 3 cards and replace them
// with new cards drawn randomly from the deck
// winnings are calculated
// If the player has a winning hand then appropriate credits are awarded
// if credits are left -- option: a button to cash out or continue playing
} // end run_game
// M A I N ////////////////////////////////////////////////////////////////////////
int main(void)
{
// run the game!
run_game();
return 0;
} // end main