USE C++ OR JAVA ---- POST SOURCE CODE WILL DOWNVOTE FOR INCORRECT ANSWER WILL REPORT FOR REPEAT ANSWER -------------------------------------------------------------------------------------------------------------------- Create a simple program that plays poker 1000 times with the following rules: Each time the program will: Shuffle the 52-card deck and deal yourself 5 cards. Using the remaining 47 cards, deal other 5 players their 5 cards. Determine if your hand would win or lose compared to the others. (Poker hands ranking listed below for reference) Record how many games out of 1000 you won. For each rank of hand, report the percentage of hands having that rank, and the average winning percentage for each rank (out of those 1000 games).

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

USE C++ OR JAVA ---- POST SOURCE CODE

WILL DOWNVOTE FOR INCORRECT ANSWER

WILL REPORT FOR REPEAT ANSWER

--------------------------------------------------------------------------------------------------------------------

Create a simple program that plays poker 1000 times with the following rules:

Each time the program will:

  • Shuffle the 52-card deck and deal yourself 5 cards.
  • Using the remaining 47 cards, deal other 5 players their 5 cards.
  • Determine if your hand would win or lose compared to the others. (Poker hands ranking listed below for reference)
  • Record how many games out of 1000 you won.
  • For each rank of hand, report the percentage of hands having that rank, and the average winning percentage for each rank (out of those 1000 games).

--------------------------------------------------------------------------------------------------------------------

Your program will produce 2 output files (1000 entries):

  • A session log output as a CSV (comma-separated value) file, with each hand (you and 5 other hands) on a separate line.
    • For each hand: Output the cards in the hand; what the hand was evaluated as; and its winning percentage.
  • A summary showing the percentage of hands falling into each rank, and the overall win percentage for each rank, as a ‘normal’ text file.
    • Don’t just list the percentages; add enough text to make it reader-friendly.

--------------------------------------------------------------------------------------------------------------------

The system random-number generator (random() and related functions) for the most common development environments will be more than adequate for this project.

 

Picture reference for card rankings:

 

Flush: All 5 cards are the same suit and they do not form a sequence. Ties broken by comparing
card ranks, as for no-pair hands; there is no relative ranking of the suits themselves.
Full House: Three of a kind plus a pair: three cards of one rank, and 2 cards of another rank.
Ties broken by comparing the values of the three cards.
• Four of a Kind: 4 cards one one rank, and 1 unrelated card. Ties broken by comparing values of
the 4 of a kind.
Straight Flush: A straight, and a flush: 5 cards in an unbroken sequence, all the same suit. A
Straight Flush headed by the Ace as high card (A-K-Q-J-10) is a royal flush. The likelihood of a
royal flush in 5 cards is about 1 in 650,000 (the exact odds are 649,739:1). In a best-5 game like
Texas Hold 'Em, the odds are 'only' about 39,000 to 1 against.
Transcribed Image Text:Flush: All 5 cards are the same suit and they do not form a sequence. Ties broken by comparing card ranks, as for no-pair hands; there is no relative ranking of the suits themselves. Full House: Three of a kind plus a pair: three cards of one rank, and 2 cards of another rank. Ties broken by comparing the values of the three cards. • Four of a Kind: 4 cards one one rank, and 1 unrelated card. Ties broken by comparing values of the 4 of a kind. Straight Flush: A straight, and a flush: 5 cards in an unbroken sequence, all the same suit. A Straight Flush headed by the Ace as high card (A-K-Q-J-10) is a royal flush. The likelihood of a royal flush in 5 cards is about 1 in 650,000 (the exact odds are 649,739:1). In a best-5 game like Texas Hold 'Em, the odds are 'only' about 39,000 to 1 against.
APPENDIX 1: The standard deck.
The standard (US) deck of playing cards contains 4 suits-Spades, Hearts, Diamonds, Clubs-
each of which contains 13 cards, ranging in value from the Ace (1) to 10, plus the court cards (or 'face
cards') of the Jack, Queen, and King. In Poker, the Ace may count as the lowest value (1) or the
highest, above the King. Cards are usually indicated by a short string consisting of the rank then the
suit, with the rank being indicated by one of: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and the suit by S, H, D,
C. Thus 6D denotes the 6 of Diamonds, KS the King of Spades, etc.
APPENDIX 2: The value of the hands.
Poker hands consist of 5 cards, dealt from a well-shuffled deck. These are combinations, not
permutations; the order the cards are dealt is not significant. The usual rules allow ties to be broken
readily; if two hands are truly tied, count it as half a win. They're rare, but they do happen.
The rank of the hands, from lowest to highest:
• No pair (high card): The hand does not qualify for any of the other ranks listed. Ties are broken
by looking at the highest-ranking card in the hand. If the highest cards are tied, look at the
second-highest, and so on.
• One pair: Two cards are of the same rank; the other 3 cards have different ranks. Ties broken by
comparing values of the pair. If two hands have one pair at the same rank, compare the highest
of the remaining 3 cards, as for no pair.
Two pair: Two cards with the same rank; two cards with the same rank different from the first
pair; and an unrelated card. Break ties by comparing the higher pair; then the lower pair; then
the unrelated card.
Three of a kind: Three cards of the same rank, and two unrelated cards. Ties broken by
comparing the values of the 3 of a kind. (It's not possible for 2 hands to each have 3 cards of the
same rank.)
Straight: 5 cards forming an unbroken sequence; not all cards are the same suit. The Ace can
count as high (A-K-Q-J-10) or low (5-4-3-2-A). It is not possible to go 'around the corner,' i.e.
3-2-A-K-Q is NOT a straight. Ties broken by comparing highest card in sequence.
Transcribed Image Text:APPENDIX 1: The standard deck. The standard (US) deck of playing cards contains 4 suits-Spades, Hearts, Diamonds, Clubs- each of which contains 13 cards, ranging in value from the Ace (1) to 10, plus the court cards (or 'face cards') of the Jack, Queen, and King. In Poker, the Ace may count as the lowest value (1) or the highest, above the King. Cards are usually indicated by a short string consisting of the rank then the suit, with the rank being indicated by one of: A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, and the suit by S, H, D, C. Thus 6D denotes the 6 of Diamonds, KS the King of Spades, etc. APPENDIX 2: The value of the hands. Poker hands consist of 5 cards, dealt from a well-shuffled deck. These are combinations, not permutations; the order the cards are dealt is not significant. The usual rules allow ties to be broken readily; if two hands are truly tied, count it as half a win. They're rare, but they do happen. The rank of the hands, from lowest to highest: • No pair (high card): The hand does not qualify for any of the other ranks listed. Ties are broken by looking at the highest-ranking card in the hand. If the highest cards are tied, look at the second-highest, and so on. • One pair: Two cards are of the same rank; the other 3 cards have different ranks. Ties broken by comparing values of the pair. If two hands have one pair at the same rank, compare the highest of the remaining 3 cards, as for no pair. Two pair: Two cards with the same rank; two cards with the same rank different from the first pair; and an unrelated card. Break ties by comparing the higher pair; then the lower pair; then the unrelated card. Three of a kind: Three cards of the same rank, and two unrelated cards. Ties broken by comparing the values of the 3 of a kind. (It's not possible for 2 hands to each have 3 cards of the same rank.) Straight: 5 cards forming an unbroken sequence; not all cards are the same suit. The Ace can count as high (A-K-Q-J-10) or low (5-4-3-2-A). It is not possible to go 'around the corner,' i.e. 3-2-A-K-Q is NOT a straight. Ties broken by comparing highest card in sequence.
Expert Solution
Step 1

import java.util.Arrays

import java.util.Scanner;

/**
Program: Poker
*/
public final class Poker {

String[] suits = {"Hearts", "Diamonds", "Spades", "Clubs"};
char[] value = {'2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'};
// check cards in deck
char[][] cards = new char[13][4];
// array for [player][card][suit/val]
int[][][] pc = new int[2][5][2];
// comp and players hand, determines winner
int[] hand = new int[2];
int[] handVal = new int[2];
// for ai to complete without hand exposed
boolean aiDone = false;
// user input
Scanner input = new Scanner(System.in);
// for betting
int money = 100;
int bet = 0;

public void clear() {
System.out.println("\n\n");
}// end of clear

/**
* Make a bet
*/
public void makeBet() {
// check if money is available
if (money <= 0) {
System.out.println("You have no money!!!");
} else {
do {

System.out.print("You currently have $" + money
+ ".\nPlease place a bet:");
bet = input.nextInt();// user placed bet
} while (!valid(bet, money, 0));
}// end of if else
}// end of makeBet

/**
* Make sure bounds are kept
* @param tester number to be tested
* @param high highest limit
* @param low lowest limit
* @return go or no go
*/
public boolean valid(int tester, int high, int low) {
if (low <= tester && tester <= high) {
return true; // valid
}// end of if
// not valid
System.out.println("Please enter a valid response");
return false;
}// end of valid

/**
* Generate a hand
* @param player comp or human
*/
public void generateHand(int player) {
// is card taken
boolean cardTaken;
// card
int crd;
// current value
int curVal;
// current suit
int curSuit;

cardTaken = true;
// issue card
for (crd = 0; crd < 4; crd++) {
do {
// create a random value
pc[player][crd][0] = ((int) (Math.random() * 52) % 13);
// create a random suit
pc[player][crd][1] = ((int) (Math.random() * 52) % 4);
curVal = pc[player][crd][0];
curSuit = pc[player][crd][1];

if (cards[curVal][curSuit] == 'T') {
cardTaken = true;
} else {
cardTaken = false;
}
} while (cardTaken || pc[player][crd][0] > 12);
cards[curVal][curSuit] = ' ';
}// end of for loop
}// end of generate hand

/**
* Determine type of hand
* @param player comp or human
*/
public void determineHand(int player) {
boolean group1 = false;
// sort cards in numeric order
sortCards(player);
// easy to determine hand
int cardVal1 = pc[player][0][0];
int cardVal2 = pc[player][1][0];
int cardVal3 = pc[player][2][0];
int cardVal4 = pc[player][3][0];
int cardVal5 = pc[player][4][0];
int suitVal1 = pc[player][0][1];
int suitVal2 = pc[player][1][1];
int suitVal3 = pc[player][2][1];
int suitVal4 = pc[player][3][1];
int suitVal5 = pc[player][4][1];

hand[player] = 0;

// determine a pair
if (cardVal1 == cardVal2 || cardVal2 == cardVal3 || cardVal3 == cardVal4
|| cardVal4 == cardVal5) {
group1 = true;
hand[player] = 1;
// determine two pair
}
if (group1) {
if (cardVal1 == cardVal2 && cardVal3 == cardVal4
|| cardVal1 == cardVal2 && cardVal4 == cardVal5
|| cardVal2 == cardVal3 && cardVal4 == cardVal5) {
hand[player] = 2;
}
//Three of a kind
if (cardVal1 == cardVal2 && cardVal1 == cardVal3
|| cardVal2 == cardVal3 && cardVal2 == cardVal4
|| cardVal3 == cardVal4 && cardVal3 == cardVal5) {
hand[player] = 3;
}
// determine a fullhouse
if (cardVal1 == cardVal2 && cardVal3 == cardVal4
&& cardVal3 == cardVal5 || cardVal1 == cardVal2
&& cardVal1 == cardVal3 && cardVal4 == cardVal5) {
hand[player] = 6;

/**
* Determine type of hand
* @param player comp or human
*/
public void determineHand(int player) {
boolean group1 = false;
// sort cards in numeric order
sortCards(player);
// easy to determine hand
int cardVal1 = pc[player][0][0];
int cardVal2 = pc[player][1][0];
int cardVal3 = pc[player][2][0];
int cardVal4 = pc[player][3][0];
int cardVal5 = pc[player][4][0];
int suitVal1 = pc[player][0][1];
int suitVal2 = pc[player][1][1];
int suitVal3 = pc[player][2][1];
int suitVal4 = pc[player][3][1];
int suitVal5 = pc[player][4][1];

hand[player] = 0;

// determine a pair
if (cardVal1 == cardVal2 || cardVal2 == cardVal3 || cardVal3 == cardVal4
|| cardVal4 == cardVal5) {
group1 = true;
hand[player] = 1;
// determine two pair
}
if (group1) {
if (cardVal1 == cardVal2 && cardVal3 == cardVal4
|| cardVal1 == cardVal2 && cardVal4 == cardVal5
|| cardVal2 == cardVal3 && cardVal4 == cardVal5) {
hand[player] = 2;
}
//Three of a kind
if (cardVal1 == cardVal2 && cardVal1 == cardVal3
|| cardVal2 == cardVal3 && cardVal2 == cardVal4
|| cardVal3 == cardVal4 && cardVal3 == cardVal5) {
hand[player] = 3;
}
// determine a fullhouse
if (cardVal1 == cardVal2 && cardVal3 == cardVal4
&& cardVal3 == cardVal5 || cardVal1 == cardVal2
&& cardVal1 == cardVal3 && cardVal4 == cardVal5) {
hand[player] = 6;
}
// determine four of a kind
if (cardVal1 == cardVal2 && cardVal1 == cardVal3
&& cardVal1 == cardVal4 || cardVal2 == cardVal3
&& cardVal2 == cardVal4 && cardVal2 == cardVal5) {
hand[player] = 7;
}
}
group1 = false;
}
// check suits
if (!group1) {
// determine a straight
if (cardVal1 == (cardVal2 - 1)
&& cardVal2 == (cardVal3 - 1) && cardVal3
== (cardVal4 - 1) && cardVal4
== (cardVal5 - 1)) {
hand[player] = 4;
}
// determine straight flush
if (suitVal1 == suitVal2 && suitVal1 == suitVal3
&& suitVal1 == suitVal4
&& suitVal1 == suitVal5) {
hand[player] = 8;
}
// determine a royal flush
if (cardVal5 == 14)//Royal Flush
{
hand[player] = 9;
}// end of royalflush if
// determine flush
else if (suitVal1 == suitVal2
&& suitVal1 == suitVal3 && suitVal1
== suitVal4 && suitVal1 == suitVal5) {
hand[player] = 5;
} else {// determine just high Card
hand[player] = 0;
}// end of else
}
// display what player has
switch (player) {
case 0:
System.out.println("PLAYER has ");
break;

case 1:
System.out.println("CPU has ");
break;
}
switch (hand[player]) {
case 0:
System.out.println("A HIGH CARD");
break;
case 1:
System.out.println("A PAIR");
break;
case 2:
System.out.println("TWO PAIR");
break;
case 3:
System.out.println("THREE OF A KIND");
break;
case 4:
System.out.println("A STRAIGHT");
break;
case 5:
System.out.println("A FLUSH");
break;
case 6:
System.out.println("A FULL HOUSE");
break;
case 7:
System.out.println("FOUR OF A KIND");
break;
case 8:
System.out.println("STRAIGHT FLUSH");
break;
case 9:
System.out.println("ROYAL FLUSH");
break;
}// end of switch hand
}// end of determine hand

/**
* Determine the string of card value
* @param val index of array
* @return The string value (ie. Ace, 2, 3, ...)
*/
public String cardValString(int val) {
String tmpValStr = "";

if (val == 0) {
tmpValStr = "2";
}
if (val == 1) {
tmpValStr = "3";
}
if (val == 2) {
tmpValStr = "4";
}
if (val == 3) {
tmpValStr = "5";
}
if (val == 4) {
tmpValStr = "6";
}
if (val == 5) {
tmpValStr = "7";
}
if (val == 6) {
tmpValStr = "8";
}
if (val == 7) {
tmpValStr = "9";
}
if (val == 8) {
tmpValStr = "10";
}
if (val == 9) {
tmpValStr = "JACK";
}
if (val == 10) {
tmpValStr = "QUEEN";
}
if (val == 11) {
tmpValStr = "KING";
}
if (val == 12) {
tmpValStr = "ACE";
}

return tmpValStr + " of ";
}// end of cardValStr
/**

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY