(Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card poker hand.
Then write the following additional functions:
a) Determine whether the hand contains two pairs
b) Determine whether the hand contains a full house (i.e., three of a kind with pair).
c) Determinewhetherthehandcontainsastraight flush (i.e.,fivecardsofconsecutivefacevalues).
d) Determine whether the hand contains a flush (i.e., five of the same suit)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
// prototypes
void shuffle(unsigned int wDeck[][FACES]); // shuffling modifies wDeck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[]); // dealing doesn't modify the arrays
int main(void)
{
// initialize deck array
unsigned int deck[SUITS][FACES] = { 0 };
srand(time(NULL)); // seed random-number generator
shuffle(deck); // shuffle the deck
// initialize suit array
const char *suit[SUITS] =
{"Hearts", "Diamonds", "Clubs", "Spades"};
// initialize face array
const char *face[FACES] =
{"Ace", "Deuce", "Three", "Four",
"Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"};
deal(deck, face, suit); // deal the deck
}
// shuffle cards in deck
void shuffle(unsigned int wDeck[][FACES])
{
// for each of the cards, choose slot of deck randomly
for (size_t card = 1; card <= CARDS; ++card) {
size_t row; // row number
size_t column; // column number
// choose new random location until unoccupied slot found
do {
row = rand() % SUITS;
column = rand() % FACES;
} while(wDeck[row][column] != 0); // end do...while
// place card number in chosen slot of deck
wDeck[row][column] = card;
}
}
// deal cards in deck
void deal(unsigned int wDeck[][FACES], const char *wFace[],
const char *wSuit[])
{
// deal each of the cards
for (size_t card = 1; card <= CARDS; ++card) {
// loop through rows of wDeck
for (size_t row = 0; row < SUITS; ++row) {
// loop through columns of wDeck for current row
for (size_t column = 0; column < FACES; ++column) {
// if slot contains current card, display card
if (wDeck[row][column] == card) {
printf("%5s of %-8s%c", wFace[column], wSuit[row],
card % 2 == 0 ? '\n' : '\t'); // 2-column format
}
}
}
}
}
Here i am adding some additional feature also for your reference like... Determine whether the hand contains a pairs , Determine whether the hand contains four of a kind ..etc. you can easily remove it if you don't want it as i wrote in a separate function for all type of possibilties regarding this problem.
I also used a file handling to write it into separate file.
I just write for your reference. i hope you like it.
Source code:-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SUITS 4
#define FACES 13
#define CARDS 52
//Function Prototypes
void shuffle(int wDeck[][FACES]);
void deal(const int wDeck[][FACES], const char *wface[], const char
*wSuit[], FILE *p, char *result[], unsigned int rindex);
void display(const char *wfFace[], const char *wfSuit[], const char
*wsFace[], const char *wsSuit[], FILE *p);
int main(void) {
//Print ouput into a text file
FILE *p;
FILE *r;
p = fopen("projectLog.txt", "w");
//Includes all 100 rounds of the poker
game
r = fopen("projectResult.txt", "w");
//Includes the results from eval in an array
//initialize suit array
const char *suit[SUITS] = {"Hearts", "Diamonds",
"Clubs", "Spades"};
//initialize face array
const char *face[FACES] = {"Ace", "Deuce", "Three",
"Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten",
"Jack", "Queen", "King"};
char *result[100];
//initialize deck array
int deck[SUITS][FACES] = {0};
srand(time(0));
//Plays the poker game 100 times
for(unsigned int hand = 1; hand <= 100; hand++)
{
fprintf(p, "ROUND %u: \n", hand);
shuffle(deck);
//Shuffles the deck
deal(deck, face, suit, p, result,
hand-1); //deal the deck
fprintf(r, "%s\n", result[hand-1]);
for(int i = 0; i < SUITS;
i++) {
for(int j = 0; j
< FACES; j++) {
deck[i][j] = 0;
}
}
}
fclose(p);
fclose(r);
}
//shuffle cards in deck
void shuffle(int wDeck[][FACES]) {
int row, column, card;
//for each card, choose slot of deck randomly
for(card = 1; card <= CARDS; card++) {
//choose new random location until
unoccupied slot found
do {
row = rand() %
SUITS;
column = rand()
% FACES;
}
while(wDeck[row][column] != 0);
//place card number in chosen
slot of deck
wDeck[row][column] = card;
}
}
//deal 5-cards poker hand in deck
void deal(const int wDeck[][FACES], const char *wFace[], const char
*wSuit[], FILE *p, char *result[], unsigned int rindex) {
//operational functions
void pair(const char *wfFace[], const char *wfSuit[],
int result[], FILE *p);
void twoPair(const char *wfFace[], const char
*wfSuit[], int result[], FILE *p);
void threeKind(const char *wfFace[], const char
*wfSuit[], int result[], FILE *p);
void fourKind(const char *wfFace[], const char
*wfSuit[], int result[], FILE *p);
void flush(const char *wfFace[], const char *wfSuit[],
int result[], FILE *p);
void straight(const char *wfFace[], const char
*wfSuit[], const char *wFace[], int result[], FILE *p);
void eval(const int firstResult[], const int
secondResult[], FILE *p, char *result[], unsigned int rindex);
int card, row, column, i, j = 0;
/*Storage for the two five-hand cards
Boolean Result:
0 = Pair
1 = Two Pair
2 = Three of a
kind
3 = Four of a
kind
4 = Flush
5 = Straight
*/
//FIRST HAND
char *fSuit[5];
char *fFace[5];
int fResult[6] = {0};
//SECOND HAND
char *sSuit[5];
char *sFace[5];
int sResult[6] = {0};
//deal 5-card poker hand
for(card = 1, i = 0; card <= 10; card++,
i++){
for(row = 0; row < 4;
row++){
for(column = 0;
column < 13; column++){
if(wDeck[ row ][ column ] == card && i
< 5){
fSuit[ i ] = wSuit[ row
];
fFace[ i ] = wFace[ column ];
}else if(wDeck[ row ][ column ] == card
&& i > 4){
sSuit[ j ] = wSuit[ row
];
sFace[ j ] = wFace[ column ];
j++;
}
}
}
}
//display 5-card poker hand
display(fFace, fSuit, sFace, sSuit, p);
//EVALUATE
fprintf(p,"\n******************\nEvaluate both
hand\n******************\n\nFirst hand\n**********\n\n");
pair( fFace, fSuit, fResult, p);
//fprintf(p,"\n fResult[ 0 ] = %d\n", fResult[ 0 ]
);
twoPair( fFace, fSuit, fResult, p);
//fprintf(p,"\n fResult[ 1 ] = %d\n", fResult[ 1 ]
);
threeKind( fFace, fSuit, fResult, p);
//fprintf(p,"\n fResult[ 2 ] = %d\n", fResult[ 2 ]
);
fourKind( fFace, fSuit, fResult, p);
//fprintf(p,"\n fResult[ 3 ] = %d\n", fResult[ 3 ]
);
flush( fFace, fSuit, fResult, p);
//fprintf(p,"\n fResult[ 4 ] = %d\n", fResult[ 4 ]
);
straight( fFace, fSuit, wFace, fResult, p);
//fprintf(p,"\n fResult[ 5 ] = %d\n", fResult[ 5 ]
);
fprintf(p,"\nSecond hand\n***********\n\n");
pair( sFace, sSuit, sResult, p);
//fprintf(p,"\n sResult[ 0 ] = %d\n", sResult[ 0 ]
);
twoPair( sFace, sSuit, sResult, p);
//fprintf(p,"\n sResult[ 1 ] = %d\n", sResult[ 1 ]
);
threeKind( sFace, sSuit, sResult, p);
//fprintf(p,"\n sResult[ 2 ] = %d\n", sResult[ 2 ]
);
fourKind( sFace, sSuit, sResult, p);
//fprintf(p,"\n sResult[ 3 ] = %d\n", sResult[ 3 ]
);
flush( sFace, sSuit, sResult, p);
//fprintf(p,"\n sResult[ 4 ] = %d\n", sResult[ 4 ]
);
straight( sFace, sSuit, wFace, sResult, p);
//fprintf(p,"\n sResult[ 5 ] = %d\n", sResult[ 5 ]
);
eval(fResult, sResult, p, result, rindex);
}
//Displays the two hands
void display(const char *wfFace[], const char *wfSuit[], const char
*wsFace[], const char *wsSuit[], FILE *p) {
int i;
fprintf(p,"*************************\nYour five-card poker hand\n*************************\n\nFirst hand %28s\n********** %28s\n\n", "Second hand", "***********");
for(i = 0; i < 5; i++) {
fprintf(p,"%10s of %-8s %10s of
%-8s\n", wfFace[i], wfSuit[i], wsFace[i], wsSuit[i]);
}
}
//Determine whether the hand contains a pair
void pair(const char *wfFace[], const char *wfSuit[], int result[],
FILE *p) {
int card;
int i, j = 0, k;
int pair[5] = {0};
fprintf(p,"\nPair...\n");
for(card = 0; card < 5; card++) {
for(i = card + 1; i < 5; i++)
{
if(wfFace[card]
== wfFace[i]) {
j++;
pair[card] = i;
}
}
}
if(j == 1) {
for(card = 0; card < 5; card++)
{
if(pair[card] !=
0) {
k = pair[card];
fprintf(p,"\n%10s of %-8s and %5s of %-8s\n",
wfFace[card], wfSuit[card], wfFace[k], wfSuit[k]);
result[0] = 1;
}
}
} else {
fprintf(p,"\n Your five-hand card
does not contain any pair. \n");
}
}
//Determine whether the hand contains two pairs
void twoPair(const char *wfFace[], const char *wfSuit[], int
result[], FILE *p) {
int card;
int i, j = 0, k;
int pair[5] = {0};
//compute whether the five-hand card contains
two pair
fprintf(p,"\nTwo-pair...\n");
for(card = 0; card < 5; card++) {
for(i = card + 1; i <
5; i++) {
if(wfFace[card] == wfFace[i]){
j++;
pair[card] = i;
}
}
}
//display result
if(j == 2) {
for(card = 0; card < 5;
card++) {
if(pair[card] != 0) {
k = pair[card];
fprintf(p,"\n%10s of %-8s and %5s of %-8s\n", wfFace[card],
wfSuit[card], wfFace[k], wfSuit[k]);
}
}
} else {
fprintf(p,"\n Your five-hand
card does not contains two pair.\n");
}
}
//Determine whether the hand contains three of a kind
void threeKind(const char *wfFace[], const char *wfSuit[], int
result[], FILE *p) {
int card;
int i, j = 0, k, x;
int pair[3] = {0};
fprintf(p,"\nThree of a kind...\n");
for(card = 0; card < 3;
card++) {
for(i
= card + 1; i < 5; i++) {
if(wfFace[card] == wfFace[i]) {
for(x = i + 1; x < 5; x++){
if(wfFace[i] == wfFace[x]) {
j++;
pair[0] = card;
pair[1] = i;
pair[2] = x;
}
}
}
}
}
if(j == 1) {
for(card =
0; card < 3; card++) {
k = pair[card];
fprintf(p,"\n%10s of %-8s\n", wfFace[k], wfSuit[k]);
}
} else {
fprintf(p,"\n Your five-hand card doesn't contains three of a
kind.\n");
}
}
//Determine whether the hand contains four of a kind
void fourKind(const char *wfFace[], const char *wfSuit[], int
result[], FILE *p) {
int card;
int i, index = 0;
int pair[5] = {0};
fprintf(p,"\nFour of a kind...\n");
for(card = 0; card < 2; ++card) {
for(i = card + 1; i < 5; i++)
{
if(wfFace[card] == wfFace[i]) {
index++;
if(index == 1)
pair[i -
1] = 1;
pair[i] = 1;
}
}
if(index == 3)
break;
else if(index > 3)
break;
else {
index = 0;
for(i = 0; i
< 5; i++)
pair[i] = 0;
}
}
if(index == 3) {
for(i = 0; i < 5; i++) {
if(pair[1] ==
1)
fprintf(p,"\n%5s of %-9s\n", wfFace[i],
wfSuit[i]);
}
fprintf(p,"\n");
} else {
fprintf(p,"\n Your five-hand card
doesn't contains four-of-a-kind.\n");
}
}
//Determines if the hand is a flush
void flush (const char *wfFace[], const char *wfSuit[], int
result[], FILE *p) {
int i, j = 0;
//compute whether your five-hand card contains a
flush
fprintf(p,"\nFlush...\n");
for(i = 0; i < 5; i++) {
if(wfSuit[0] == wfSuit[i])
j++;
}
if(j == 4) {
result[4] = 1;
for(i = 0; i < 5; i++) {
fprintf(p,"\n
%5s of %-8s\n", wfFace[i], wfSuit[i]);
}
}
else {
fprintf(p,"\n Not flush\n");
}
}
//Determines if the hand is a straight
void straight(const char *wfFace[], const char *wfSuit[], const
char *wFace[], int result[], FILE *p) {
int i, j, k;
int pass, count, hold, check = 1;
int faceValue[5] = {0};
fprintf(p,"\nStraight...\n");
//locate face value and store in an array
for(i = 0 ; i < 5; i++) {
for(j = 0 ; j < 13; j++) {
if(wfFace[i] ==
wFace[ j ]) {
faceValue[i] = j;
}
}
}
//sort face value in ascending order using bubble
sort
for(pass = 0; pass < 4; pass++) {
for(count = 0; count < 4;
count++){
if(faceValue[
count ] > faceValue[ count + 1 ]) {
//swap
hold = faceValue[ count ];
faceValue[count] = faceValue[count + 1];
faceValue[count + 1] = hold;
}
}
}
//check if the hand contains a straight
for(i = 0; i < 4; i++) {
if(faceValue[i] + 1 != faceValue[i +
1]) {
check = 0;
}
}
if(check == 1) {
result[5] = 1;
for(i = 0; i < 5; i++) {
fprintf(p,"\n
%5s of %-8s\n", wfFace[i], wfSuit[i]);
}
}
else {
fprintf(p,"\n Not a
straight\n");
}
}
//Evaluates the hands to determine which is the better
hand
void eval(const int firstResult[], const int secondResult[], FILE
*p, char *result[], unsigned int rindex) {
int i, x = 0, y = 0;
fprintf(p,"\n******\nResult\n******\n\n");
for(i = 0; i < 6; ++i) {
if(firstResult[i] == 1)
x = i + 1;
if(secondResult[i] == 1)
y = i + 1;
}
fprintf(p,"x = %d y = %5d\n\n", x, y);
if(x > y){
fprintf(p, "First hand
wins\n\n");
result[rindex] = "First hand
wins";
}
else if(y > x){
fprintf(p, "Second hand
wins\n\n");
result[rindex] = "Second hand
wins";
}
else if(x == y) {
fprintf(p, "Tie\n\n");
result[rindex] = "Tie";
}
else {
fprintf(p, "Error, could not
evaluate\n\n");
result[rindex] = "Error, could not
evaluate";
}
}
***********************************End of
program***********************************************************
(Card Shuffling and Dealing) Modify the program below so that the card-dealing function deals a five-card...
7.12 (Card Shuffling and Dealing) Modify the program in Fig. 7.24 so that the card-dealing function deals a five-card poker hand. Then write the following additional functions: a) Determine whether the hand contains a pair. b) Determinewhetherthehandcontainstwopairs. c) Determine whether the hand contains three of a kind (e.g., three jacks). d) Determinewhetherthehandcontainsfourofakind(e.g.,fouraces). e) Determine whether the hand contains a flush (i.e., all five cards of the same suit). f) Determine whether the hand contains a straight (i.e., five cards of...
The following program simulates shuffling a deck of cards. (The line numbers are not ) part of the code). Without changing its functionality, rewrite the program by replacing the 2D array card [52] [2] with a double pointer char **card and replacing the code between line 12 and line 26 with a function. The function prototype could be void shuffleCards (char **, char [, char [) 1 7/Shuffling cards 2 4 using namespace std; 6 main() #include #include <cstdlib> <iostream>...
NEED HELP TO CREATE A BLACKJACK GAME WITH THE UML DIAGRAM AND PROBLEM SOLVING TO GET CODE TO RUN!! THANKS Extend the DeckofCards and the Card class in the book to implement a card game application such as BlackJack, Texas poker or others. Your game should support multiple players (up to 5 for BlackJack). You must build your game based on the Cards and DeckofCards class from the book. You need to implement the logic of the game. You can...
Given these three classes: Card, DeckOfCards, and DeckOfCardsTest. Extend the DeckofCards class to implement a BlackJack class, which implements a BlackJack game. Please do not use any java applet on the coding. Hint: Use a test class to test above classes. Pulic class Card { private final String face; // face of card ("Ace", "Deuce", ...) private final String suit; // suit of card ("Hearts", "Diamonds", ...) // two-argument constructor initializes card's face and suit public...
C Programming The following code creates a deck of cards, shuffles it, and deals to players. This program receives command line input [1-13] for number of players and command line input [1-13] for number of cards. Please modify this code to deal cards in a poker game. Command line input must accept [2-10] players and [5] cards only per player. Please validate input. Then, display the sorted hands, and then display the sorted hands - labeling each hand with its...
I wrote a card shuffle program and I am trying to see how I can add a sorting function to the program to sort the players hands before printing them. How would I do this? #include <iostream> #include <iomanip> #include <vector> #include <algorithm> using namespace std; void printHand(int []); void deal(vector<int> &, int[], int[], int[], int[]); void unwrapDeck(vector<int> &deck) { for(int i=0; i<=51; i++) deck.push_back(i); } void shuffleDeck(vector<int> &deck) { //Status of cards before shuffling cout << "Before shuffling: "...
//main.cpp
#include <iostream>
#include <iomanip>
#include "deck-of-cards.hpp"
void RunAllTests() {
int count;
std::cin >> count;
DeckOfCards myDeckOfCards;
for (int i = 0; myDeckOfCards.moreCards() && i < count;
++i) {
std::cout << std::left << std::setw(19)
<< myDeckOfCards.dealCard().toString();
if (i % 4 == 3)
std::cout << std::endl;
}
}
int main() {
RunAllTests();
return 0;
}
//card.hpp
#ifndef CARD_HPP_
#define CARD_HPP_
#include <string>
class Card {
public:
static const int totalFaces = 13;
static const int totalSuits = 4;
Card(int cardFace, int...
S12 Five Crowns – Part 2 This week you will continue your work on the Five Crowns program. 1. You will create a class Deck that consists of the following: a. The vector of 116 cards from last week’s program. b. a method to deal a card from the deck 2. Create overloaded operators: a. bool operator == that overloads the == operator to check if two cards are equal. b. bool operator < that overload the < operator and...
Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler { /** * The number of consecutive shuffle steps to be performed in each call * to each sorting...
Write a Java program that deals a five-card poker hand and then determines which of the following hands are contained in it: high card, pairs, two pair, three of a kind, flush. Test only for those hands. Use the numbers from 0 to 51 to represent the cards of the poker deck. 1. To deal the cards, your main method should call a secondary method that selects a random card. The secondary method should accept 5 integers that represent the...