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

In this assignment, you will implement a simulation of a popular casino game usu

ID: 3547325 • Letter: I

Question

In this assignment, you will implement a simulation of a popular casino game usually called video poker. The card deck contains 52 cards, 13 of each suit. At the beginning of the game, the deck is shuffled. You need to devise a fair method for shuffling. (It does not have to be efficient.) The player pays a token for each game. Then the top five cards of the deck are presented to the player. The player can reject none, some, or all of the cards. The rejected cards are replaced from the top of the deck. Now the hand is scored. Your program should pronounce it to be one of the following:

You need to implement two classes PokerGame and Card

Card class needs to have the following:

For the PokerGame class, you would need to have the following:

Main method will contain at least the following:

while ( tokens > 0 ) // initial token count is 10

{

System.out.println("You have " + tokens + " tokens.");  // display current money!

displayGameMenu();  // prompt user to ask whether they want to play or not

int play = in.nextInt();

if ( play == 1 ) // if player choose to play

{

generateDeck();  // create 52 cards

shuffleDeck();     // shuffle the deck randomly

dealCards();        // deals 5 cards to player's hand

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

{

displayCardMenu( i ); // prompt user to ask them whether they want to keep this card or not

int keepCard = in.nextInt();

if ( keep == 0 ) // 0 means they want to swap the card from their hand to a new card

{

swapCard( i );

}

}

int reward = checkReward(); // check player's hand has awarded anything

tokens += reward;

}

else {

break;  // end of the game loop

}

}

Explanation / Answer

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package VideoPoker;


import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import java.util.Scanner;


public class PokerGame {


List<Card> deck;

Card[] hand;

  

public PokerGame(){

hand = null;

deck = new ArrayList<Card>();

System.out.println("fsdfsd");

}

  

public void generateDeck() {

deck.clear();

for (int i = Card.SPADE; i <=Card.CLUB; i++) {

for (int j = 1; j <= 13; j++) {

deck.add(new Card(i, j));

}

}

}


public void shuffleDeck() {

int randnum;

Random randGen = new Random();

for (int i = 0; i < 200; i++) {

randnum = randGen.nextInt(52);

Card re = deck.remove(randnum);

deck.add(re);

}

}


public void displayGameMenu() {

System.out.println("Do you want to continue game?[1(Y)/0(N)]: ");

}


public void displayCardMenu(int index) {

System.out.println("Do you want to keep " + hand[index] + " ?[1(Y)/0(N)]: ");

}


public void dealCards() {

hand = new Card[5];


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

hand[i] = deck.remove(0);

}

}


public void displayHand() {

System.out.println("You have: ");

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

System.out.println(hand[i]);

}

System.out.println("");

}


public void swapCard(int index) {

hand[index] = deck.remove(0);

}


public int checkReward() {

boolean samesuit=false;

  

//Sord the hand cards for easy comparision

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

for(int j=i; j<5; j++)

if( hand[i].getNumber() > hand[j].getNumber() ){

Card temp = hand[i];

hand[i] = hand[j];

hand[j] = temp;

}

//just to avoid writing hand[x].getNumber everytime

int h[] = new int[5];

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

h[i] = hand[i].getNumber();

  

//Check for same suit

samesuit = hand[0].getSuit() == hand[4].getSuit() &&

hand[1].getSuit() == hand[4].getSuit() &&

hand[2].getSuit() == hand[4].getSuit() &&

hand[3].getSuit() == hand[4].getSuit() ;

  

if(samesuit){

//Royal Flush

if( h[0]==1 && h[1]==10 && h[2]==11 && h[3]==12 && h[4]==13 )

return 250;

//Straight flush

if( h[1]==h[0]+1 && h[2]==h[0]+2 && h[3]==h[0]+3 && h[4]==h[0]+4)

return 50;

}

//Four of a Kind - Four cards of the same value, such as four queens. Payout: 25.

if( h[1] == h[2] && h[2] == h[3] ){

if( h[0]== h[1] || h[3]== h[4] )

return 25;

}

  

//Full House - Three of a kind and a pair, for example three queens and two 5's. Payout: 6.

if( (h[0]==h[1] && h[1]==h[2] && h[3]==h[4] ) ||

(h[0]==h[1] && h[2]==h[3] && h[3]==h[4] ) )

return 6;

  

//Flush - Five cards, not necessarily in order, of the same suit. Payout: 5.

if( samesuit ) return 5;

  

//Straight - Five cards with consecutive values, not necessarily of the same suit, such as 4, 5, 6, 7, and 8. The ace can either precede as 2 or follow a king. Payout: 4.

if( h[4]==h[3]+1 && h[3]==h[2]+1 && h[2]==h[1]+1 )

if( (h[0]==0 && h[4]==13) || (h[1] == h[0]+1) )

return 4;

//Three of a kind - Three cards of the same value, for example three queens. Payout: 3.

if( h[0]== h[1] && h[1] == h[2] ||

h[1]== h[2] && h[2] == h[3] ||

h[1]== h[3] && h[3] == h[4] )

  

return 3;

//Two pairs - Two pairs, for example two queens and two 5's. Payout: 2

//One pair - Two cards of the same value, for example two queen. Payout: 1

int pair=0;

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

if( h[i]==h[i+1])

pair++;

if( pair==2)

return 2;

else if ( pair==1)

return 1;

  

return 0;

}


public static void main(String [] args) {

int tokens=10;

PokerGame pg = new PokerGame();

Scanner in = new Scanner(System.in);

  

while (tokens > 0) // initial token count is 10

{

tokens--;

System.out.println("You have " + tokens + " tokens."); // display current money!

pg.displayGameMenu(); // prompt user to ask whether they want to play or not

int play = in.nextInt();

if (play == 1) // if player choose to play

{

pg.generateDeck(); // create 52 cards

pg.shuffleDeck(); // shuffle the deck randomly

pg.dealCards(); // deals 5 cards to player's hand

pg.displayHand();

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

pg.displayCardMenu(i); // prompt user to ask them whether they want to keep this card or not

int keepCard = in.nextInt();

if (keepCard == 0) // 0 means they want to sw1ap the card from their hand to a new card

{

pg.swapCard(i);

}

}

int reward = pg.checkReward(); // check player's hand has awarded anything

pg.displayHand();

switch(reward){

case 250 : System.out.println("--Royal Flush--"); break;

case 50 : System.out.println("--Straight Flush--"); break;

case 25 : System.out.println("--Four of a Kind--"); break;

case 6 : System.out.println("--Full House--"); break;

case 5 : System.out.println("--Flush--"); break;

case 4 : System.out.println("--Straight--"); break;

case 3 : System.out.println("--Three of a Kind--"); break;

case 2 : System.out.println("--Two Pairs--"); break;

case 1 : System.out.println("--One Pair--"); break;

case 0 : System.out.println("--No Pair--"); break;

}

tokens += reward;

} else {

break; // end of the game loop

}

}

}

}