Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Program Specication: You are to develop a program to play a variation on a game

ID: 3573276 • Letter: P

Question

Program Specication: 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 a number in the range of [1, 9] printed on it - 9 possible values / cards. { There are 4 identical copies of each possible value / card - 36 cards in all. { Before each hand is dealt a new deck is opened and shued. A Dealer asks the Player to place a bet, the Player can bet anywhere from one to as many chips as they have. The Dealer gets a new deck and shues it. This will be done by choosing two cards from the new deck (at random positions) and swapping their locatiions. The Dealer then deals a hand to the player, by giving the player 4 cards selected sequentially from the shued 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. 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. 3. 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 38 chips for each chip that was placed in that bet. 4. 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. 5. Pair - 2 of a kind If the number on exactly two of the players' four cards is the same and the number on the remaining two cards are dierent then the player gets their bet back and in addition wins 1 chip for each chip that was placed in that bet. 6. Bubkiss If the players' cards do not constitute one of the above ve 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 nal report stateing: 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 o 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 denition is being provided for you to be called in your dealHand() method below: 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; } } 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 values 1 to 9 four times, consecutively - lling the array. { 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 void dealHand(int [] deck, int[] hand) Initialize deck - initDeck() Shue deck - shuffleDeck() Uses the rst four values in deck to assign the four values of hand Sort hand - sortHand() { 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 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 isStraight(int[] hand) Returns true if and only if the cards in hand qualify as a straight, 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. 3. Consider sorting the hand Array, to make your "is" methods easier to write. Sample run(s): Welcome to 4 Card Poker Your initial bank roll is $100.00 ++++++++++++++++++++++++++++++++++++++++++++++ Do you want to play a game? y Place your bet [1, 100] : 101 Place your bet [1, 100] : 0 Place your bet [1, 100] : 2 Let the cards fall where they may ... 1 1 2 2 Let's get things in order ... 2 2 1 1 Congrats: You got 2 Pair and have won $44. Do you want to play a game? y Place your bet [1, 144] : 20 Let the cards fall where they may ... 9 1 9 7 Let's get things in order ... 9 9 7 1 Congrats: You got a Pair and have won $20. Do you want to play a game? y Place your bet [1, 164] : 100 Let the cards fall where they may ... 5 7 1 9 Let's get things in order ... 9 7 5 1 Sorry: you got Bubkiss and have lost $100. Do you want to play a game? y Place your bet [1, 64] : 64 Let the cards fall where they may ... 8 2 6 9 Let's get things in order ... 9 8 6 2 Sorry: you got Bubkiss and have lost $64. +++++++++++++++++++++++++++++++++++++++++++ Party's Over:you are out of chips. Thanks for playing ... You played a total of 4 hands. Of which, you won 2. And you lost 2. But in the end you lost $100.

Explanation / Answer

package poker;
import java.io.*;
import java.lang.*;
import java.util.Random;
import java.util.Scanner;
import java.util.*;


public class Poker {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("Welcome to 4 card Poker Your initial bank roll is $100.00");
       System.out.println("Do you want to play? ");
       System.out.print("y/n: ");
       String choice = scanner.next();
       int[] deck = new int[36];
       int[] hand = new int[4];
       int won = 0, lost = 0;
       initDeck(deck);
       int totalEarning=100;
       int played=0;
       while(choice.equalsIgnoreCase("y") && totalEarning != 0)
       {
           System.out.println("Place your bet[1,"+totalEarning+"]");
           int bet=scanner.nextInt();
           if(bet == 0 || bet > totalEarning)
           {
               System.out.println("Do you want to play? ");
               System.out.print("y/n: ");
               choice = scanner.next();
               continue;
           }
           played=played + 1;
           shuffleDeck(deck, 4);
           dealHand(deck, hand);
           int i;
           for(i=0;i<4;i++)
               System.out.print(hand[i]);
           if(isQuad(hand))
           {
               System.out.println("Congrats you got four of a kind");
               int earning= bet * 6545;
               totalEarning=totalEarning + earning;
               won=won + 1;
           }
           else if(isThreeOfAKind(hand))
               {

               System.out.println("Congrats you got three of a kind");
               int earning= bet * 51;
               totalEarning=totalEarning + earning;
               won = won + 1;
               }
           else if(isTwoOfAKind(hand))
           {

               System.out.println("Congrats you got two of a kind");
               int earning= bet * 1;
               totalEarning=totalEarning + earning;
               won = won + 1;
           }
           else if(isTwoPair(hand))

               {System.out.println("Congrats you got two pair");
               int earning= bet * 22;
               totalEarning=totalEarning + earning;
               }
           else if(isStraight(hand))
           {
               System.out.println("Congrats you got Straight");
              
               int earning= bet* 38;
               totalEarning=totalEarning + earning;
               won = won + 1;
           }
           else
               {
               totalEarning=totalEarning - bet;
               System.out.println("Sorry: you got Bubkiss and have lost $" + bet);
               lost=lost + 1;
               }
           System.out.println("Do you want to play? ");
           System.out.print("y/n: ");
           choice = scanner.next();
          
       }
       if(choice.equalsIgnoreCase("n"))
       {
          
           System.out.println("Thanks for playing and your total earning is " + totalEarning);
          
          
       }
       else if(totalEarning==0)
       {
           System.out.println("Party's Over: you are out of chips");
           System.out.println("You played a total of" + played +"hands. Of which, you won " +won+
                   "And you lost "+lost+ ".But in the end you lost $100");
       }
      
       System.out.println("You played a total of" + played +"hands. Of which, you won " +won+
               "And you lost "+lost);

   }
   public static boolean isThreeOfAKind(int[] hand)
   {
       if(hand[0]==hand[2] || hand[1]==hand[3])
           return true;
       else
           return false;
      
   }
  
   public static boolean isTwoOfAKind(int[] hand)
   {
           if(hand[0]==hand[1] || hand[2]==hand[3] && !isThreeOfAKind(hand))
           {
               return true;
           }
           else
               return false;
   }
   public static boolean isTwoPair(int[] hand)
   {
       int i;
       if(hand[0]==hand[1] && hand[2]==hand[3])
           return true;
       else
           return false;
   }
   public static boolean isQuad(int[] hand)
   {
       if(hand[0]==hand[3])
           return true;
       else
           return false;
   }
   public static boolean isStraight(int[] hand)
   {
       int i;
       for(i=1; i<4;i++)
       {
           if(hand[i-1]==hand[i]-1)
               continue;
           else
               return false;
       }
       return true;
   }
   public static void dealHand(int[] deck, int[] hand)
   {
       int i;
       for(i=0;i<4;i++)
           hand[i]=deck[i];
       sortHand(hand);
   }
   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;
       }
   }
   public static void printDeck(int [] deck)
   {
       for(int i=0;i<36;i++)
           System.out.print(deck[i] + " ");
   }
   public static void initDeck(int[] deck)
   {
       int i;
       for(i=0;i<36;i++)
       {   if (((i+1) % 9)==0)
           deck[i]=9;
       else
           deck[i]=(i+1) % 9;
       }
      
   }
  
   public static void shuffleDeck(int[] deck, int n)
   {   int index1, index2;
   for(int i=0;i<20;i++){
       index1= randInt(0,35);
       index2=randInt(0,35);
       while(index1 == index2)
       {
           index2=randInt(0,35);
       }
       int temp = deck[index1];
       deck[index1]=deck[index2];
       deck[index2]=temp;
       }
   }
   public static int randInt(int min, int max)
   {
       Random r = new Random();
       int randomNum=r.nextInt((max - min) + 1 ) + min;
       return randomNum;
   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote