USING C++: For This project we will be building the framework for 5 card draw po
ID: 3821514 • Letter: U
Question
USING C++:
For This project we will be building the framework for 5 card draw poker. The game will have 1 player and at least 1 virtual player (optionally up to 3). Players will be dealt cards from a deck to their hand. A total of 5 cards will be dealt to each player. The value of each hand will be determined by standard Poker Rules. Players will place bets based in accordance to the rules of 5 card draw. Bets will be placed into a pot. The player with the highest ranked hand will have win the pot for that round of play.
Cards can be of value 1-13 with the “Ace”, “Jack”, “Queen” and “King” being 1, 11, 12 and 13 respectively. All other card values show their value (a “2” is worth 2). There are also four possible suites. These are Clubs, Spade, Diamond and heart. The combination makes for 52 different cards.
Task 1: Create a deck of cards and shuffle them. For this task you will need to create 52 unique cards and place them in the deck (a Stack container object). They should be placed in the deck in random order.
Task 2: A player object should be created. The player should have a container named hand which can hold 5 card objects. These card objects should be dealt (passed) from the deck to the player’s hand. The card objects should be dynamic and passed from the deck to the hand using pointers.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#pragma warn -aus
//type definitions
typedef unsigned char card;
typedef unsigned char pairs;
//message printed after winning
char win_msgs[9][16] = {
{"high card"},
{"one pair"},
{"two pairs"},
{"three of a kind"},
{"straight"},
{"flush"},
{"full house"},
{"four of a kind"},
{"straight flush"}
};
//arrays for the names of things
typedef struct cardz {
char name[6]; //"Queen"
int value; //2 - 14 (Ace's are 14)
char suit[9]; //"Diamonds"
char icon; //ascii char 3 4 5 or 6
char color[6]; //"Black"
}card1;
static char *suits[] = {"Hearts","Diamonds","Clubs","Spades"};
static char *names[]= {"Two","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack","Queen","King","Ace"};
static char *colour[]= {"Black","Red"};
/* function prototypes */
/* compares value of 2 cards, and passed to qsort as the fourth argument */
int compareface(const void *c1,const void *c2);
void deal(card1 cards[52], card1 hands[5][5]);
void filldeck(card1 cards[52]); /* Populates a deck of cards */
//pairs findpairs(card *hand); /* finds any pairs in a hand */
//void printcard(card c); /* Displays the value of a card*/
void printdeck(card1 cards[52]); /* prints an entire deck of cards*/
void shuffle(card1 cards[52]);
int main() {
card1 cards[52];
card deck[52],*deckp;
card1 hands[5][5];
pairs numpairs[5],highest;
int hand,cd,winner;
printf(" ");
/*populate and shuffle the deck */
filldeck(cards); //printdeck(cards);
shuffle(cards); printdeck(cards);
deal(cards, hands);
// determine the winner and print it
printf(" press enter when ready");
hand = getchar();
return 0;
}
void deal(card1 cards[52], card1 hands[5][5]) {
int i, j, cd, hd;
int value, hi, ohi, lo;
card1 temp;
for(cd=0, i=0; cd<5; cd++) {
for(hd=0;hd<5;hd++) {
hands[hd][cd] = cards[i];
++i;
}
}
for(i=0;i<5;i++) { /* sort the hands here */
ohi = 5;
lo = 1;
for(; lo < ohi; lo++) {
temp = hands[i][lo]; //value = A[lo];
hi = lo - 1;
while(hands[i][hi].value > temp.value) {
hands[i][hi + 1] = hands[i][hi];
--hi;
if(hi < 0) break;
}
hands[i][hi+1] = temp;
}
}
for(i = 0; i < 5; i++) {
printf(" hand #%d:", i+1);
for(j = 0; j < 5; j++) {
printf("%2d %c ", hands[i][j].value, hands[i][j].icon);
}
}
}
void filldeck(card1 cards[52]) { /* populate the deck here */
int i, n, v, s, red, black;
int deck[15] = {0};
n = s = 0;
for(i = 0, v = 2; i < 52; i++) {
strcpy(cards[i].name, names[n]); //assign the name: "Queen", etc.
if(++n > 12) n = 0;
cards[i].value = v; //assign the value: Ten = 10, etc
if(++v > 14) v = 2; //Ace = 14
strcpy(cards[i].suit, suits[s]); //etc.
cards[i].icon = s+3;
if(++s > 3) s = 0;
if(i%2== 0)
strcpy(cards[i].color, "black");
else
strcpy(cards[i].color, "red");
}
//check the deck for 26 black and red cards, and 4 of each card type
red = black = 0;
for(i = 0; i < 52; i++) {
deck[cards[i].value]++;
if(strcmp(cards[i].color, "red") == 0) red++;
if(strcmp(cards[i].color, "black") == 0) black++;
}
for(i = 2; i < 15; i++) {
if((deck[i] != 4) || (red != 26) || (black != 26))
printf(" filldeck() reports an error in the deck!");
}
}
void printdeck(card1 cards[52]) {
int i, type; //type is the type of printout we want done
card1 c1;
printf(" ");
for(i = 0, type = 1; i < 52; i++) {
printf("%2d: %2d %c ", i+1, cards[i].value, cards[i].icon);
}
printf(" press enter to continue");
i=getchar();
return;
}
void shuffle(card1 cards[52]) {
int i,j, rnd;
card1 temp;
srand(time(NULL)); /* seed the random number generator */
for(i=0;i<52;i++) {
rnd=rand() % 52; //random number between 0 & 51 */
temp = cards[i];
cards[i] = cards[rnd];
cards[rnd] = temp;
}
}
int compareface(const void* c1, const void *c2)
{
card cd1,cd2;
cd1=*((card*) c1);
cd2=*((card*) c2);
cd1= (cd1&0x3c)>>2;
cd2= (cd2&0x3c)>>2;
if(cd1>cd2)
return 1;
if(cd1==cd2)
return 0;
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.