Mike asked in Computers & InternetSoftware · 7 years ago

Can someone help me with this Poker game C++?

Here's my code so far. Create a struct called card that contains both the suit and the value for a playing card. The suit will range from 0 to 3 and the value will range from 1 to 13 (Ace to King). Also create a two dimensional array as follows:

bool deck[4][14]; to allow you to keep track of which cards have been dealt. Initialize all of the values of this array to false.

Create an array called hand that contains five elements. The type of this array should be card. Deal five cards (using the deck array to make sure that you don’t deal the same card twice) and place these five cards in the hand array.

Now write a function called isFlush that takes an array of cards as a parameter. It should return a bool. In the card game Poker, a hand of five cards where the cards are all the same suit is called a “flush”. Have the isFlush function return true if the hand is a flush and false if it is not a flush.

In main deal a hand of five cards 100,000 times. Each time you deal a hand, call the isFlush function to determine whether the hand is a flush or not. Keep track of how many flushes you get. After you have done this 100,000 times, calculate the percentage of hands that were flushes and print the result to the screen.

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <ctype.h>

#include <string.h>

int main()

{

int deck[52];

int i=0;

char suitnames[5][10]={"spades", "diamonds", "clubs", "hearts"};

char ranknames[13][10]={"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "jack", "queen", "king"};

int swapped=0, acerank=0, currentCardRank;

int R[5];

int S[5];

// create a new deck, with cards in order, but unique

for(i=0; i<52; i++){

deck[i] = i;

}

// shuffle the deck:

for(i=0; i<52; i++){

// generate a random index to swap with the card at index i.

int j = rand() % 52;

int temp = deck[i];

deck[i] = deck[j];

deck[j] = temp;

}

// print all the cards

for(i=0; i<52; i++){

int suitnumber = deck[i] / 13; // 0 - 3

int rank = deck[i] % 13;

printf("%s of %s\n", ranknames[rank], suitnames[suitnumber]);

}

// Get the rank of the first 5 cards

for(i=0; i<5; i++)

{

R[i] = deck[i]%13;

S[i] = deck[i]/13;

}

// Sort the R array

do{

// 1 pass of the bubble sort

swapped = 0;

for(i=0; i<4; i++){

if(R[i] > R[i+1]){

int temp = R[i];

R[i] = R[i+1];

R[i+1] = temp;

swapped = 1;

}

}

}while(swapped);

if(S[0] == S[1] && S[1]==S[2] && S[2]==S[3] && S[3]==S[4]){

printf("Flush\n");

}

else{

printf("Not a flush\n");

}

system("pause");

return 0;

}

2 Answers

Relevance
  • Favorite Answer

    I think you're in my CSIT class...

  • Amber
    Lv 4
    5 years ago

    Im soo happy pokerview accepts US players

Still have questions? Get your answers by asking now.