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

Could question Big Java (Project P7.7 in 5th Edition) be answered? In this assig

ID: 3603808 • Letter: C

Question

Could question Big Java (Project P7.7 in 5th Edition) be answered?

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 doesn't need to be efficient). The player pays a token for each game. Then the top of the five cards of th edeck are presented to the player. The player can reject none, some, or all of the cards. The rejected cards are replcaed from the top of the desk. Now, the hand is scored. Your program should pronounce it to be one of the following:

- No pair - the lowest hand, containing five separate cards that do not match up to create any of the hands below.

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

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

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

- 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 a 2 or follow a king. Payout : 4

- Flush - five cards, not necessarily in order of the same suit. payout: 5

- Full house - three of a kind and a pair, for example, three queens and two 5s. Payout: 6

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

- Straight flush - a straight and a flush. Five cards with consecutive values of the same suit. Payout: 50

- Royal flush - the best possible hand in poker. A 10, jack, queen, king, and ace, all of the same suit. Payout : 250

Explanation / Answer

public class Card
{
private short rank, suit;

private static String[] suits = { "hearts", "spades", "diamonds", "clubs" };
private static String[] ranks = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };

public static String rankAsString( int __rank ) {
return ranks[__rank];
}

Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}

public @Override String toString()
{
return ranks[rank] + " of " + suits[suit];
}

public short getRank() {
return rank;
}

public short getSuit() {
return suit;
}

public boolean sameSuit(Card card) {
// TODO Auto-generated method stub
return false;
}
}


Here is my Deck class: package javapoker;
import java.util.ArrayList;
import java.util.Random;
public class Deck {
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>();
int index_1, index_2;
Random generator = new Random();
Card temp;

for (short a=0; a<=3; a++)
{
for (short b=0; b<=12; b++)
{
cards.add( new Card(a,b) );
}
}

int size = cards.size() -1;

for (short i=0; i<100; i++)
{
index_1 = generator.nextInt( size );
index_2 = generator.nextInt( size );

temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}

public Card drawFromDeck()
{
return cards.remove( cards.size()-1 );
}

public int getTotalCards()
{
return cards.size();
//we could use this method when making
//a complete poker game to see if we needed a new deck
}
}

Here is my Player class (it is very flawed, feel free to trash it and start over): package javapoker;
import java.util.Random;
public class Player
{
private static final int random = 0;
public static void main(String[] args)
{
Card [] hand = new Card [5];
hand[0] = new //I know variables go here but I'm trying and
//failing to match them to the random card suits and ranks
hand[1] = new
hand[2] = new
hand[3] = new
hand[4] = new

System.out.println("The hand dealt is: ");
for (int i = 0; i < hand.length; i++)
System.out.println(hand[i]);


if (flush(hand))
System.out.print("Your hand is a flush");
else if (threeKind(hand) && twoKind(hand))
System.out.print("Your hand is a full house");
else if (fourKind(hand))
System.out.print("Your hand is a four of a kind");
else if (threeKind(hand))
System.out.print("Your hand is a three of a kind");
else if (twoKind(hand))
System.out.print("Your hand is a two of a kind");
else if (straight(hand))
System.out.println("You've got a straight hand");
else if (straightFlush(hand))
System.out.println("You've got a straight flush");
else
System.out.print("Trade them all in");

System.out.println();
System.out.print("Thanks for playing");
}
public static boolean flush(Card [] hand)
{
boolean isFlush = true;
Card firstOne = new Card(hand[0].getSuit(), hand[0].face());
//I think I need to define method-Face for the Card class to get rid of these errors
for (int i = 1; isFlush && i < hand.length; i++)
if (!firstOne.sameSuit(hand[i]))
isFlush = false;

return isFlush;
}
public static boolean fullHouse(Card [] hand)
{
System.out.println("in full house");
return (threeKind(hand) && twoKind(hand));
}
public static boolean fourKind(Card [] hand)
{
int count = 0;

for (int i = 0; count < 3 && i < hand.length; i++)
{
count = 0;

for (int j = i + 1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count++;
}
return (count == 3);
}
public static boolean threeKind(Card [] hand)
{
int count = 0;

for (int i = 0; count < 2 && i < hand.length; i++)
{
count = 0;

for (int j = i + 1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count++;
}
return (count == 2);
}
public static boolean twoKind(Card [] hand)
{
int count = 0;

for (int i = 0; count < 1 && i < hand.length; i++)
{
count = 0;

for (int j = i +1; j < hand.length; j++)
if (hand[i].sameFace(hand[j]))
count ++;
}
return (count == 1);
}
public static boolean straight(Card [] hand)
{
boolean isStraight = true;
Card firstOne = new Card(hand[0].getSuit(), hand[0].face());

for (int i = 1; isStraight && i < hand.length; i++)
if (!firstOne.sameFace(hand[i]))
isStraight = false;

return isStraight;
}
public static boolean straightFlush(Card [] hand)
{
System.out.println("in straight flush");
return (straight(hand) && flush(hand));
}
}