Posted by fengshaun on Wed 5 Mar 19:49
report abuse | download | new post
- //definitions
- #ifndef DEFINITIONS_H
- #define DEFINITIONS_H
- const int totalCards = 52;
- const int deckRows = 4;
- const int deckColumns = 13;
- int deck[ deckRows ][ deckColumns ] = { 0 };
- char *suite[] = { "Hearts", "Clubs", "Diamonds", "Spades" };
- char *face[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
- #endif
- //Class Player definition
- #ifndef PLAYER_H
- #define PLAYER_H
- #include "definitions.h"
- class Player
- {
- public:
- Player( int );
- ~Player();
- void deal( int, int );
- void printCards();
- void rank();
- private:
- char *cards[ 7 ][ 2 ];
- char *hold[ 1 ];
- bool highCards; //DONE
- bool pair; //DONE
- bool twoPair; //DONE
- bool threeOfAKind; //DONE
- bool straight;
- bool flush; //DONE
- bool fullHouse; //DONE
- bool fourOfAKind; //DONE
- bool straightFlush;
- bool royalFlush;
- unsigned int pairCounter;
- unsigned int playerNumber;
- friend class CommCards;
- friend void save( Player *, Player *, Player * );
- friend void printResults( Player *, Player *, Player * );
- friend void makeTrue( Player *, Player *, Player * );
- };
- Player::Player( int pNum ) {
- cards[ 7 ][ 2 ];
- hold[ 1 ];
- highCards = false;
- pair = false;
- twoPair = false;
- threeOfAKind = false;
- straight = false;
- flush = false;
- fullHouse = false;
- fourOfAKind = false;
- straightFlush = false;
- royalFlush = false;
- pairCounter = 0;
- playerNumber = pNum;
- }
- Player::~Player() {}
- void Player::deal( int x, int y ) {
- //first card
- for( int row = 0; row < deckRows; row++ ) {
- for( int column = 0; column < deckColumns; column++ ) {
- if( deck[ row ][ column ] == x ) {
- cards[ 0 ][ 0 ] = face[ column ] + '\0';
- cards[ 0 ][ 1 ] = suite[ row ] + '\0';
- }
- }
- }
- //second card
- for( int row = 0; row < deckRows; row++ ) {
- for( int column = 0; column < deckColumns; column++ ) {
- if( deck[ row ][ column ] == y ) {
- cards[ 1 ][ 0 ] = face[ column ] + '\0';
- cards[ 1 ][ 1 ] = suite[ row ] + '\0';
- }
- }
- }
- }
- void Player::printCards() {
- cout << cards[ 0 ][ 0 ] << " of " << cards[ 0 ][ 1 ] << endl;
- cout << cards[ 1 ][ 0 ] << " of " << cards[ 1 ][ 1 ] << endl;
- }
- void Player::rank() {
- int pairC1;
- int pairC2;
- int pair2C1;
- int pair2C2;
- int threeC1;
- int threeC2;
- int threeC3;
- int straightC1;
- int straightC2;
- int straightC3;
- int straightC4;
- int straightC5;
- int flushC1;
- int flushC2;
- int flushC3;
- int flushC4;
- int flushC5;
- int fourC1;
- int fourC2;
- int fourC3;
- int fourC4;
- int hearts = 0;
- int clubs = 0;
- int spades = 0;
- int diamonds = 0;
- //THREE OF A KIND
- for( threeC1 = 0; threeC1 < 7; threeC1++ ) {
- for( threeC2 = 0; threeC2 < 7; threeC2++ ) {
- if( threeC1 > threeC2 && cards[ threeC1 ][ 0 ] == cards[ threeC2 ][ 0 ] ) {
- hold[ 0 ] = cards[ threeC1 ][ 0 ];
- for( threeC3 = 0; threeC3 < 7; threeC3++ ) {
- if( threeC3 != threeC1 && threeC3 != threeC2 ) {
- if( cards[ threeC3 ][ 0 ] == cards[ threeC1 ][ 0 ] && threeOfAKind == false ) {
- cout << "player " << playerNumber << " has a three of a kind: " << endl;
- cout << cards[ threeC1 ][ 0 ] << " of " << cards[ threeC1 ][ 1 ] << endl;
- cout << cards[ threeC2 ][ 0 ] << " of " << cards[ threeC2 ][ 1 ] << endl;
- cout << cards[ threeC3 ][ 0 ] << " of " << cards[ threeC3 ][ 1 ] << endl << endl;
- threeOfAKind = true;
- }
- }
- }
- }
- }
- }
- //PAIRS
- for( pairC1 = 0; pairC1 < 7; pairC1++ ) {
- for( pairC2 = 0; pairC2 < 7; pairC2++ ) {
- if( pairC1 > pairC2 && cards[ pairC1 ][ 0 ] == cards[ pairC2 ][ 0 ] && pairCounter <= 2 ) {
- cout << "Player " << playerNumber << " has a pair of: " << endl;
- cout << cards[ pairC1 ][ 0 ] << " of " << cards[ pairC1 ][ 1 ] << " AND "
- << cards[ pairC2 ][ 0 ] << " of " << cards[ pairC2 ][ 1 ] << endl;
- pairCounter++;
- pair = true;
- }
- }
- }
- //Two Pair
- if( pairCounter >= 2 ) {
- cout << "Player has two pairs." << endl;
- twoPair = true;
- }
- //FLUSH
- for( int card1 = 0; card1 < 7; card1++ ) {
- if( cards[ card1 ][ 1 ] == suite[ 0 ] ) {
- hearts++;
- }
- if( cards[ card1 ][ 1 ] == suite[ 1 ] ) {
- clubs++;
- }
- if( cards[ card1 ][ 1 ] == suite[ 2 ] ) {
- diamonds++;
- }
- if( cards[ card1 ][ 1 ] == suite[ 3 ] ) {
- spades++;
- }
- }
- if( hearts >= 5 ) {
- cout << "Player " << playerNumber << " has a flush of Hearts" << endl;
- flush = true;
- }
- else if( clubs >= 5 ) {
- cout << "Player " << playerNumber << " has a flush of Clubs" << endl;
- flush = true;
- }
- else if( diamonds >= 5 ) {
- cout << "Player " << playerNumber << " has a flush of Diamonds" << endl;
- flush = true;
- }
- else if( spades >= 5 ) {
- cout << "Player " << playerNumber << " has a flush of Spades" << endl;
- flush = true;
- }
- //Four of a kind
- for( fourC1 = 0; fourC1 < 7; fourC1++ ) {
- for( fourC2 = 0; fourC2 < 7; fourC2++ ) {
- for( fourC3 = 0; fourC3 < 7; fourC3++ ) {
- for( fourC4 = 0; fourC4 < 7; fourC4++ ) {
- if( fourC1 > fourC2 && fourC2 > fourC3 && fourC3 > fourC4 ) {
- if( cards[ fourC1 ][ 0 ] == cards[ fourC2 ][ 0 ] && cards[ fourC2 ][ 0 ] == cards[ fourC3 ][ 0 ] && cards[ fourC3 ][ 0 ] == cards[ fourC4 ][ 0 ] && cards[ fourC4 ][ 0 ] == cards[ fourC1 ][ 0 ] ) {
- cout << "Player " << playerNumber << " has a four of kind of: " << endl;
- cout << cards[ fourC1 ][ 0 ] << " of " << cards[ fourC1 ][ 1 ] << endl
- << cards[ fourC2 ][ 0 ] << " of " << cards[ fourC2 ][ 1 ] << endl
- << cards[ fourC3 ][ 0 ] << " of " << cards[ fourC3 ][ 1 ] << endl
- << cards[ fourC4 ][ 0 ] << " of " << cards[ fourC4 ][ 1 ] << endl << endl;
- fourOfAKind = true;
- }
- }
- }
- }
- }
- }
- //full house
- if( pair && threeOfAKind ) {
- cout << "Player has a Full House." << endl;
- fullHouse = true;
- }
- //High Cards
- if( !( pair || twoPair || threeOfAKind || straight || flush || fullHouse || fourOfAKind || straightFlush || royalFlush) ) {
- cout << "Player has High Cards." << endl;
- highCards = true;
- }
- }
- #endif
- //community cards
- #ifndef COMMUNITY_CARDS_H
- #define COMMUNITY_CARDS_H
- #include "definitions.h"
- #include "player.h"
- class Player;
- class CommCards
- {
- public:
- CommCards();
- ~CommCards();
- void deal( Player *, int, int, int, int, int);
- void print( Player * );
- private:
- int flop1Num;
- int flop2Num;
- int flop3Num;
- int turnNum;
- int riverNum;
- };
- CommCards::CommCards() {
- flop1Num = flop2Num = flop3Num = turnNum = riverNum = 0;
- }
- CommCards::~CommCards() {}
- void CommCards::deal( Player *player, int a, int b, int c, int d, int e ) {
- flop1Num = a;
- flop2Num = b;
- flop3Num = c;
- turnNum = d;
- riverNum = e;
- for( int row = 0; row < deckRows; row++ ) {
- for( int column = 0; column < deckColumns; column++ ) {
- if( deck[ row ][ column ] == flop1Num ) {
- player->cards[ 2 ][ 0 ] = face[ column ] + '\0';
- player->cards[ 2 ][ 1 ] = suite[ row ] + '\0';
- }
- else if( deck[ row ][ column ] == flop2Num ) {
- player->cards[ 3 ][ 0 ] = face[ column ] + '\0';
- player->cards[ 3 ][ 1 ] = suite[ row ] + '\0';
- }
- else if( deck[ row ][ column ] == flop3Num ) {
- player->cards[ 4 ][ 0 ] = face[ column ] + '\0';
- player->cards[ 4 ][ 1 ] = suite[ row ] + '\0';
- }
- else if( deck[ row ][ column ] == turnNum ) {
- player->cards[ 5 ][ 0 ] = face[ column ] + '\0';
- player->cards[ 5 ][ 1 ] = suite[ row ] + '\0';
- }
- else if( deck[ row ][ column ] == riverNum ) {
- player->cards[ 6 ][ 0 ] = face[ column ] + '\0';
- player->cards[ 6 ][ 1 ] = suite[ row ] + '\0';
- }
- }
- }
- }
- void CommCards::print( Player *player ) {
- cout << "Flop cards are: " << endl;
- cout << player->cards[ 2 ][ 0 ] << " of " << player->cards[ 2 ][ 1 ] << endl;
- cout << player->cards[ 3 ][ 0 ] << " of " << player->cards[ 3 ][ 1 ] << endl;
- cout << player->cards[ 4 ][ 0 ] << " of " << player->cards[ 4 ][ 1 ] << endl;
- cout << endl;
- cout << "The turn is: " << endl;
- cout << player->cards[ 5 ][ 0 ] << " of " << player->cards[ 5 ][ 1 ] << endl;
- cout << endl;
- cout << "The river is: " << endl;
- cout << player->cards[ 6 ][ 0 ] << " of " << player->cards[ 6 ][ 1 ] << endl;
- cout << endl;
- }
- #endif
- //A simple, automatic poker game.
- //licenced under GPL 3.0 or above
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <iomanip>
- #include <string>
- #include <fstream>
- using namespace std;
- #include "player.h"
- #include "community_cards.h"
- #include "definitions.h"
- void shuffle();
- void save( Player *, Player *, Player * );
- void printResults( Player *, Player *, Player * );
- int main ()
- {
- cout << endl;
- srand( time( 0 ) );
- cout << "time is: " << time( 0 ) << endl << endl;
- // sleep( 1 );
- shuffle();
- Player player1( 1 );
- Player player2( 2 );
- Player player3( 3 );
- CommCards communityCards;
- communityCards.deal( &player1, 7, 8, 9, 11, 13 );
- communityCards.deal( &player2, 7, 8, 9, 11, 13 );
- communityCards.deal( &player3, 7, 8, 9, 11, 13 );
- player1.deal( 1, 2 );
- cout << "Player 1 has:" << endl;
- player1.printCards();
- cout << endl;
- player2.deal( 3, 4 );
- cout << "Player 2 has:" << endl;
- player2.printCards();
- cout << endl;
- player3.deal( 5, 6 );
- cout << "Player 3 has:" << endl;
- player3.printCards();
- cout << endl;
- communityCards.print( &player1 );
- cout << "checking hands: " << endl;
- // sleep( 2 );
- player1.rank();
- cout << endl;
- // sleep( 1 );
- player2.rank();
- cout << endl;
- // sleep( 1 );
- player3.rank();
- cout << endl;
- // makeTrue( &player1, &player2, &player3 );
- printResults( &player1, &player2, &player3 );
- cout << endl;
- char yn;
- cout << endl << "Game will now exit, do you want to save your game?( y/n ) ";
- cin >> yn;
- switch( yn ) {
- case 'n':
- case 'N':
- exit( 1 );
- case 'y':
- case 'Y':
- save( &player1, &player2, &player3 );
- exit( 1 );
- default:
- cout << "invalid option. Exiting" << endl;
- sleep( 3 );
- exit( 1 );
- }
- }
- void shuffle() {
- for( int card = 1; card <= totalCards; card++ ) {
- int randRow = rand() % deckRows;
- int randColumn = rand() % deckColumns;
- while( deck[ randRow ][ randColumn ] != 0 ) {
- randRow = rand() % deckRows;
- randColumn = rand() % deckColumns;
- }
- deck[ randRow ][ randColumn ] = card;
- }
- }
- void save( Player *player1, Player *player2, Player *player3 ) {
- ofstream ofile;
- ofile.open( "autopoker.sav" );
- if( ofile.is_open() ) {
- ofile << "time is: " << time( 0 ) << endl << endl;
- ofile << "Player 1 has:" << endl;
- ofile << player1->cards[ 0 ][ 0 ] << " of " << player1->cards[ 0 ][ 1 ] << endl;
- ofile << player1->cards[ 1 ][ 0 ] << " of " << player1->cards[ 1 ][ 1 ] << endl;
- ofile << endl;
- ofile << "Player 2 has:" << endl;
- ofile << player2->cards[ 0 ][ 0 ] << " of " << player2->cards[ 0 ][ 1 ] << endl;
- ofile << player2->cards[ 1 ][ 0 ] << " of " << player2->cards[ 1 ][ 1 ] << endl;
- ofile << endl;
- ofile << "Player 3 has:" << endl;
- ofile << player3->cards[ 0 ][ 0 ] << " of " << player3->cards[ 0 ][ 1 ] << endl;
- ofile << player3->cards[ 1 ][ 0 ] << " of " << player3->cards[ 1 ][ 1 ] << endl;
- ofile << endl;
- ofile << "community cards are ( 3 flops, turn, and river ):" << endl;
- ofile << player1->cards[ 2 ][ 0 ] << " of " << player1->cards[ 2 ][ 1 ] << endl;
- ofile << player1->cards[ 3 ][ 0 ] << " of " << player1->cards[ 3 ][ 1 ] << endl;
- ofile << player1->cards[ 4 ][ 0 ] << " of " << player1->cards[ 4 ][ 1 ] << endl;
- ofile << player1->cards[ 5 ][ 0 ] << " of " << player1->cards[ 5 ][ 1 ] << endl;
- ofile << player1->cards[ 6 ][ 0 ] << " of " << player1->cards[ 6 ][ 1 ] << endl;
- ofile << endl;
- {
- if( player1->highCards ) {
- ofile << "Player " << player1->playerNumber << " has high cards." << endl;
- }
- if( player1->pair ) {
- ofile << "Player " << player1->playerNumber << " has a pair." << endl;
- }
- if( player1->twoPair ) {
- ofile << "Player " << player1->playerNumber << " has two pairs." << endl;
- }
- if( player1->threeOfAKind ) {
- ofile << "Player " << player1->playerNumber << " has a three of a kind." << endl;
- }
- if( player1->straight ) {
- ofile << "Player " << player1->playerNumber << " has a straight." << endl;
- }
- if( player1->flush ) {
- ofile << "Player " << player1->playerNumber << " has a flush." << endl;
- }
- if( player1->fullHouse ) {
- ofile << "Player " << player1->playerNumber << " has a flush." << endl;
- }
- if( player1->fourOfAKind ) {
- ofile << "Player " << player1->playerNumber << " has a four of a kind." << endl;
- }
- if( player1->straightFlush ) {
- ofile << "Player " << player1->playerNumber << " has straight flush." << endl;
- }
- if( player1->royalFlush ) {
- ofile << "Player " << player1->playerNumber << " has royal flush." << endl;
- }
- }
- {
- if( player2->highCards ) {
- ofile << "Player " << player2->playerNumber << " has high cards." << endl;
- }
- if( player2->pair ) {
- ofile << "Player " << player2->playerNumber << " has a pair." << endl;
- }
- if( player2->twoPair ) {
- ofile << "Player " << player2->playerNumber << " has two pairs." << endl;
- }
- if( player2->threeOfAKind ) {
- ofile << "Player " << player2->playerNumber << " has a three of a kind." << endl;
- }
- if( player2->straight ) {
- ofile << "Player " << player2->playerNumber << " has a straight." << endl;
- }
- if( player2->flush ) {
- ofile << "Player " << player2->playerNumber << " has a flush." << endl;
- }
- if( player2->fullHouse ) {
- ofile << "Player " << player2->playerNumber << " has a flush." << endl;
- }
- if( player2->fourOfAKind ) {
- ofile << "Player " << player2->playerNumber << " has a four of a kind." << endl;
- }
- if( player2->straightFlush ) {
- ofile << "Player " << player2->playerNumber << " has straight flush." << endl;
- }
- if( player2->royalFlush ) {
- ofile << "Player " << player2->playerNumber << " has royal flush." << endl;
- }
- }
- {
- if( player3->highCards ) {
- ofile << "Player " << player3->playerNumber << " has high cards." << endl;
- }
- if( player3->pair ) {
- ofile << "Player " << player3->playerNumber << " has a pair." << endl;
- }
- if( player3->twoPair ) {
- ofile << "Player " << player3->playerNumber << " has two pairs." << endl;
- }
- if( player3->threeOfAKind ) {
- ofile << "Player " << player3->playerNumber << " has a three of a kind." << endl;
- }
- if( player3->straight ) {
- ofile << "Player " << player3->playerNumber << " has a straight." << endl;
- }
- if( player3->flush ) {
- ofile << "Player " << player3->playerNumber << " has a flush." << endl;
- }
- if( player3->fullHouse ) {
- ofile << "Player " << player3->playerNumber << " has a flush." << endl;
- }
- if( player3->fourOfAKind ) {
- ofile << "Player " << player3->playerNumber << " has a four of a kind." << endl;
- }
- if( player3->straightFlush ) {
- ofile << "Player " << player3->playerNumber << " has straight flush." << endl;
- }
- if( player3->royalFlush ) {
- ofile << "Player " << player3->playerNumber << " has royal flush." << endl;
- }
- }
- sleep( 3 );
- cout << "game successfully saved." << endl;
- }
- else {
- sleep( 2 );
- cout << "ERROR: autopoker.sav could not be opened" << endl;
- cout << "Please make sure that the file is present in the same directory as the game." << endl;
- }
- }
- void printResults( Player *player1, Player *player2, Player *player3 ) {
- cout << setw( 30 ) << "Player 1" << setw( 14 ) << "Player 2" << setw( 14 ) << "Player 3" << endl;
- cout << "==============================================================" << endl;
- cout << setiosflags( ios::right );
- cout << setw( 20 ) << "Pair |";
- cout << setw( 4 );
- if( player1->pair ) cout << "X";
- cout << setw( 14 );
- if( player2->pair ) cout << "X";
- cout << setw( 14 );
- if( player3->pair ) cout << "X";
- cout << endl << setw( 20 ) << "Two pairs |";
- cout << setw( 4 );
- if( player1->twoPair ) cout << "X";
- cout << setw( 14 );
- if( player2->twoPair ) cout << "X";
- cout << setw( 14 );
- if( player3->twoPair ) cout << "X";
- cout << endl << setw( 20 ) << "Three of a kind |";
- cout << setw( 4 );
- if( player1->threeOfAKind ) cout << "X";
- cout << setw( 14 );
- if( player2->threeOfAKind ) cout << "X";
- cout << setw( 14 );
- if( player3->threeOfAKind ) cout << "X";
- cout << endl << setw( 20 ) << "Straight |";
- cout << setw( 4 );
- if( player1->straight ) cout << "X";
- cout << setw( 14 );
- if( player2->straight ) cout << "X";
- cout << setw( 14 );
- if( player3->straight ) cout << "X";
- cout << endl << setw( 20 ) << "Flush |";
- cout << setw( 4 );
- if( player1->flush ) cout << "X";
- cout << setw( 14 );
- if( player2->flush ) cout << "X";
- cout << setw( 14 );
- if( player3->flush ) cout << "X";
- cout << endl << setw( 20 ) << "Full house |";
- cout << setw( 4 );
- if( player1->fullHouse ) cout << "X";
- cout << setw( 14 );
- if( player2->fullHouse ) cout << "X";
- cout << setw( 14 );
- if( player3->fullHouse ) cout << "X";
- cout << endl << setw( 20 ) << "Four of a kind |";
- cout << setw( 4 );
- if( player1->fourOfAKind ) cout << "X";
- cout << setw( 14 );
- if( player2->fourOfAKind ) cout << "X";
- cout << setw( 14 );
- if( player3->fourOfAKind ) cout << "X";
- cout << endl << setw( 20 ) << "Straight flush |";
- cout << setw( 4 );
- if( player1->straightFlush ) cout << "X";
- cout << setw( 14 );
- if( player2->straightFlush ) cout << "X";
- cout << setw( 14 );
- if( player3->straightFlush ) cout << "X";
- cout << endl << setw( 20 ) << "Royal flush |";
- cout << setw( 4 );
- if( player1->royalFlush ) cout << "X";
- cout << setw( 14 );
- if( player2->royalFlush ) cout << "X";
- cout << setw( 14 );
- if( player3->royalFlush ) cout << "X";
- }
- void makeTrue( Player *player1, Player *player2, Player *player3 ) { //for testing the table
- player1->pair = true;
- player1->twoPair = true;
- player1->threeOfAKind = true;
- player1->straight = true;
- player1->flush = true;
- player1->fullHouse = true;
- player1->fourOfAKind = true;
- player1->straightFlush = true;
- player1->royalFlush = true;
- player2->pair = true;
- player2->twoPair = true;
- player2->threeOfAKind = true;
- player2->straight = true;
- player2->flush = true;
- player2->fullHouse = true;
- player2->fourOfAKind = true;
- player2->straightFlush = true;
- player2->royalFlush = true;
- player3->pair = true;
- player3->twoPair = true;
- player3->threeOfAKind = true;
- player3->straight = true;
- player3->flush = true;
- player3->fullHouse = true;
- player3->fourOfAKind = true;
- player3->straightFlush = true;
- player3->royalFlush = true;
- }
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.