Java programming, Please follow the guidelines stated below. Thanks!! Main topic
ID: 3691942 • Letter: J
Question
Java programming, Please follow the guidelines stated below. Thanks!!
Main topics: Random number generators Arrays, Programmer defined methods
Program Specification:
You are to develop a program to play a variation on a game of chance called single player Poker.
Game Description:
There is a Player who starts with 100 chips, each worth $1.00.
There is a deck of 36 cards:
– Each card has value - a number in the range of [1, 9] printed on it - 9 possible values.
– Each card has a suit - [”Clubs”, ”Spades”, ”Hearts”, ”Diamonds”] printed on it - 4 possible
suits.
– Before each hand is dealt, it is verified that there are enough cards in the existing deck, otherwise a new deck is opened and shuffled.
A Dealer asks the Player to place a bet, the Player can bet anywhere from one to as many chips as they have.
If needed, the Dealer gets a new deck and shuffles it. This is done by (repeatedly) choosing two cards from the new deck (at random locations) and swapping their locations.
The Dealer then deals a hand to the player, by giving the player 4 cards selected sequentially from the shuffled deck described above.
A determination is then made as to whether the Player has won or lost as such:
1. 4 of a kind
If the number on each of the players’ four cards is the same then the player gets their bet back and in addition wins 6545 chips for each chip that was placed in that bet.
2. Straight Flush
If the number on the players’ four cards can be arranged so that they form a consecutive 4 number sequence and all four cards are of the same suit then the player gets their bet back and in addition wins 2454 chips for each chip that was placed in that bet.
3. Flush
If all four cards are of the same suit then the player gets their bet back and in addition wins 123 chips for each chip that was placed in that bet.
4. Straight
If the number on the players’ four cards can be arranged so that they form a consecutive 4 number sequence then the player gets their bet back and in addition wins 79 chips for each chip that was placed in that bet.
5. 3 of a kind
If the number on exactly three of the players’ four cards is the same then the player gets their bet back and in addition wins 51 chips for each chip that was placed in that bet.
6. Two Pair
If the number on two of the players’ four cards is the same and the number on the remaining two cards is also the same yet the number on all four cards in not the same then the player gets their bet back and in addition wins 22 chips for each chip that was placed in that bet.
7. Pair-2ofakind
If the number on exactly two of the players’ four cards is the same and the number on the remaining two cards are different then the player gets their bet back and in addition wins 1 chip for each chip that was placed in that bet.
8. Bubkiss
If the players’ cards do not constitute one of the above five hands then the player losses - does not get their bet back.
• A report of the players’ new chip total is made.
• This process repeats as long as the Player still has any chips and wishes to play. • A final report stating:
1. How many hands the Player played.
2. How bets the Player won.
3. How bets the Player lost.
4. The net winnings (or net losings) of the Player - Based off of starting with 100 chips.
Rules and Requirements:
All user input must be validated.
You must represent the deck of cards using an Array of ints (of size 36).
You must represent the players hand using an Array of ints (of size 4).
The following method definition is being provided for you to be called in your dealHand() method below:
} }
• For each of the following headings / descriptions, write and use a method that adheres to it: – public static void initDeck(int[] deck)
Assign the elements of deck the 36 unique card values
– public static void shuffleDeck(int[] deck, int n)
The following is performed exactly n times:
Generate two random numbers (index1, index2) in the range 0 to 35 inclusive and then swap the value of array element at index1 with the value of the array element at index2
– public static int dealHand(int [] deck, int nextCard, int[] hand) If there are at least four cards left in deck - base on the value of nextCard:
Uses the four values in deck (starting at location nextCard) to assign the four values of hand
Sort hand - sortHand()
return the value of nextCard + 4 Else
Initialize deck - initDeck()
Shuffle deck - shuffleDeck()
Uses the first four values in deck to assign the four values of hand
Sort hand - sortHand()
return the value 4
– public static void displayHand(int[] hand)
Prints the cards of hand in some reasonable report format.
– public static boolean isQuad(int[] hand)
Returns true if and only if the cards in hand qualify as 4 of a kind, returns false otherwise.
– public static boolean isStraightFlush(int[] hand)
Returns true if and only if the cards in hand qualify as a straight flush, returns false otherwise.
– public static boolean isFlush(int[] hand)
Returns true if and only if the cards in hand qualify as a flush, returns false otherwise.
– public static boolean isStraight(int[] hand)
Returns true if and only if the cards in hand qualify as a straight, returns false otherwise.
– public static boolean isTrip(int[] hand)
Returns true if and only if the cards in hand qualify as 3 of a kind, returns false otherwise.
– public static boolean is2Pair(int[] hand)
Returns true if and only if the cards in hand qualify as two pair, returns false otherwise.
– public static boolean isPair(int[] hand)
Returns true if and only if the cards in hand qualify as a pair, returns false otherwise.
Notes and Hint:
1. Read the user’s option(s) as a String.
2. Use the String classes’ equalsIgnoreCase method for comparisons.
Sample run(s):
Explanation / Answer
Poker.java
import java.util.Random;
import java.util.Scanner;
public class Poker{
public static void main(String[] args)
{
System.out.println(" Welcome to 4 Card Poker");
System.out.println(" Your initial bank roll is $100.00");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++");
//variable to store the total chips the player has
int balance = 100;
//variable to store whether player wants to end the game
boolean end = false;
//variable to store the deck
int[] deck = new int[36];
//variable to store the hand
int[] hand = new int[4];
//variable to store the total hands played
int totalHands = 0;
//variable to store the total hands won
int wins = 0;
//scanner object to take input from the user
Scanner input = new Scanner(System.in);
do
{
char ch =' ';
//while user does not enter a valid input
while(true)
{
//prompting user whether to play next round or not
System.out.print("Do you want to play a game? ");
String nextRound = input.nextLine();
//checking the input is y or n
if(nextRound.length() == 1)
{
if(nextRound.charAt(0) == 'y' || nextRound.charAt(0) == 'n'){ch=nextRound.charAt(0);break;}
}
}
//if user has entered n then break out of the loop
if(ch=='n'){end = true;break;}
//else play the game
else
{
int bet = 0;
//while user does not enter a valid bet
while(true)
{
//prompt the user for bet
System.out.print("Place your bet[1, "+balance+"] : ");
//taking bet from user
bet = input.nextInt();
//skipping the rest of the line
input.nextLine();
//checking whether bet is in the range
if(bet>=1 && bet<=balance)break;
}
//calling the deal hand function
dealHand(deck, hand);
//incrementing the number of hands played
totalHands++;
//calculating the winnings of the current hand
int winnings=displayWinnings(hand,bet);
//if the winnings are > 0
//then the user has won that hand
if(winnings>0)wins++;
//add the winnings to the balance
balance +=winnings;
}
}
while(balance>0 && !end);
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++ ");
//if player runs out of chips
if(balance==0)System.out.println("Party's Over: you are out of chips.");
//printing out the statistics
System.out.println("Thanks for playing ... ");
System.out.println("You played a total of "+totalHands+" hands.");
System.out.println("Of which, you won "+wins+".");
System.out.println("And you lost "+(totalHands-wins)+". ");
System.out.print("But in the end you ");
if(balance-100>0)System.out.println("won $"+(balance-100));
else System.out.println("lost $"+(100-balance));
}
//function to sort the hand
//provided by the instructor
public static void sortHand(int[] hand) {
for (int i = 0; i < hand.length; ++i) {
int maxLoc = i;
for (int j = i + 1; j < hand.length; ++j)
if (hand[j] > hand[maxLoc])
maxLoc = j;
int tmp = hand[i];
hand[i] = hand[maxLoc];
hand[maxLoc] = tmp;
}
}
//function to fill the deck
//with 1 to 9 values 4times
public static void initDeck(int[] deck)
{
//looping through the cards 4 times
for(int i=0;i<4;i++)
{
//for every 9 cards
//assign the card numbers
for(int j=0;j<9;j++)
{
deck[i*9+j]=(j+1);
}
}
}
public static void shuffleDeck(int[] deck, int n)
{
//creating random object
Random ran = new Random();
//looping to shuffle the cards n times
for(int i=0;i<n;i++)
{
//generating two random numbers
//between 0 and 35
int index1 = ran.nextInt(36);
int index2 = ran.nextInt(36);
//swapping the card at index1 with card at index2
int tempCard = deck[index1];
deck[index1] = deck[index2];
deck[index2] = tempCard;
}
}
public static void displayHand(int[] hand)
{
//looping through the hand array
for(int i=0;i<hand.length;i++)
{
//printing each card with a space after it
System.out.print(hand[i]+" ");
}
System.out.println();
}
public static void dealHand(int[] deck, int[] hand)
{
//initializing the hand
initDeck(deck);
//shuffling the deck 30 times
shuffleDeck(deck, 30);
//assigning the first 4 cards of deck to hand
for(int i=0;i<4;i++)
{
hand[i] = deck[i];
}
System.out.println("Let the cards fall where they may ...");
//displaying the hand
displayHand(hand);
//sorting the hand
sortHand(hand);
System.out.println("Let's get thigs in order ...");
//displaying the hand
displayHand(hand);
}
//checking whether the hand has a four of a kind or not
public static boolean isQuad(int[] hand)
{
//looping through the hand
//if a card is not equal to card at zero
//means no four of a kind
for(int i=0;i<hand.length;i++)
{
if(hand[i]!=hand[0])return false;
}
return true;
}
//checking whether the hand has three of a kind
public static boolean isTrip(int[] hand)
{
//looping through the hand
for(int i=0;i<hand.length-2;i++)
{
//checking if i i+1 and i+2 are equal or not
if(hand[i]==hand[i+1] && hand[i]==hand[i+2])return true;
}
return false;
}
//checking whether the hand has a straight
public static boolean isStraight(int[] hand)
{
//looping through the hand
for(int i=0;i<hand.length;i++)
{
//as the hand has been sorted previously
//difference between successive hands must be one
//if its not then not straight
if(hand[0]-i!=hand[i])return false;
}
return true;
}
//checking whether the hand has two pairs
public static boolean is2Pair(int[] hand)
{
//variable to count number of pairs
int count = 0;
//looping through the hand
for(int i=0;i<hand.length-1;i++)
{
//if i and i+1 are equal
//then its a Pair
if(hand[i]==hand[i+1])count++;
}
//if count == 2
//means two pairs
//return true
if(count == 2)return true;
return false;
}
//checking whether the hand has a Pair
public static boolean isPair(int[] hand)
{
//looping through the hand
for(int i=0;i<hand.length-1;i++)
{
//if i and i+1 are equal
//then its a Pair
if(hand[i]==hand[i+1])return true;
}
return false;
}
//method to calculate the winnings based on the hand and display it to the user
public static int displayWinnings(int[] hand,int bet)
{
//variable to store the total winnings
int win = 0;
//if its a quad
//player wins bet times 6545 chips
if(isQuad(hand))
{
win = bet*6545;
System.out.println("Congrats: You got Quad and have won $"+win);
}
//if its a triple
//player wins bet times 51 chips
else if(isTrip(hand))
{
win = bet*51;
System.out.println("Congrats: You got Trip and have won $"+win);
}
//if its a straight
//player wins bet times 38 chips
else if(isStraight(hand))
{
win = bet*38;
System.out.println("Congrats: You got Straight and have won $"+win);
}
//if its a two pairs
//player wins bet times 22 chips
else if(is2Pair(hand))
{
win = bet*22;
System.out.println("Congrats: You got 2 Pair and have won $"+win);
}
//if its a Pairs
//player wins bet chips
else if(isPair(hand))
{
win = bet*1;
System.out.println("Congrats: You got a Pair and have won $"+win);
}
//else player looses his bet
else
{
win = -1*bet;
System.out.println("Sorry: You got Bubkiss and have lost $"+bet);
}
return win;
}
}
sample output
Welcome to 4 Card Poker
Your initial bank roll is $100.00
+++++++++++++++++++++++++++++++++++++++++++++++++
Do you want to play a game? yes
Do you want to play a game? y
Place your bet[1, 100] : 101
Place your bet[1, 100] : -1
Place your bet[1, 100] : 10
Let the cards fall where they may ...
2 9 5 3
Let's get thigs in order ...
9 5 3 2
Sorry: You got Bubkiss and have lost $10
Do you want to play a game? n
+++++++++++++++++++++++++++++++++++++++++++++++++
Thanks for playing ...
You played a total of 1 hands.
Of which, you won 0.
And you lost 1.
But in the end you lost $10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.