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

package PJ4; import java.util.*; /* * Ref: http://en.wikipedia.org/wiki/Video_po

ID: 3771688 • Letter: P

Question

package PJ4;
import java.util.*;

/*
* Ref: http://en.wikipedia.org/wiki/Video_poker
* http://www.google.com/ig/directory?type=gadgets&url=www.labpixies.com/campaigns/videopoker/videopoker.xml
*
*
* Short Description and Poker rules:
*
* Video poker is also known as draw poker.
* The dealer uses a 52-card deck, which is played fresh after each playerHand.
* The player is dealt one five-card poker playerHand.
* After the first draw, which is automatic, you may hold any of the cards and draw
* again to replace the cards that you haven't chosen to hold.
* Your cards are compared to a table of winning combinations.
* The object is to get the best possible combination so that you earn the highest
* payout on the bet you placed.
*
* Winning Combinations
*
* 1. Jacks or Better: a pair pays out only if the cards in the pair are Jacks,
*    Queens, Kings, or Aces. Lower pairs do not pay out.
* 2. Two Pair: two sets of pairs of the same card denomination.
* 3. Three of a Kind: three cards of the same denomination.
* 4. Straight: five consecutive denomination cards of different suit.
* 5. Flush: five non-consecutive denomination cards of the same suit.
* 6. Full House: a set of three cards of the same denomination plus
*    a set of two cards of the same denomination.
* 7. Four of a kind: four cards of the same denomination.
* 8. Straight Flush: five consecutive denomination cards of the same suit.
* 9. Royal Flush: five consecutive denomination cards of the same suit,
*    starting from 10 and ending with an ace
*
*/


/* This is the main poker game class.
* It uses Deck and Card objects to implement poker game.
* Please do not modify any data fields or defined methods
* You may add new data fields and methods
* Note: You must implement defined methods
*/

MY RESULT JUST MAKE ME WIN EVERY TIME NO MATTER IT'S FLUSH STRAIGHT ....PAIRS OR NOT.

HOW CAN I FIX IT?

public class PokerGame {

// default constant values
private static final int startingBalance=100;
private static final int numberOfCards=5;
  
// default constant payout value and playerHand types
private static final int[] multipliers={1,2,3,5,6,9,25,50,250};
private static final String[] goodHandTypes={
   "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush   ",
   "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };

// must use only one deck
private static final Deck Deck(1);

// holding current poker 5-card hand, balance, bet
private List<Card> playerHand;
private int playerBalance;
private int playerBet;

/** default constructor, set balance = startingBalance */
public PokerGame()
{
   this(startingBalance);
}

/** constructor, set given balance */
public PokerGame(int balance)
{
   this.playerBalance= balance;
}

/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
   System.out.println(" ");
   System.out.println("Payout Table    Multiplier ");
   System.out.println("=======================================");
   int size = multipliers.length;
   for (int i=size-1; i >= 0; i--) {
       System.out.println(goodHandTypes[i]+" | "+multipliers[i]);
   }
   System.out.println(" ");
}

/** Check current playerHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is "Sorry, you lost") at the end of function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
// implement this method!
   Collections.sort(this.playerHand, new Comparator<Card>() {
       public int compare(Card c1, Card c2){
       int rank1 = c1.getRank();
       int rank2 = c2.getRank();
       if (rank1 > rank2) {
       return -1;
       } else{
       return 1;
   }
   }
   });
   displayHand();
   boolean isStraightResult = isStraight();
   boolean isFlushResult = isFlush();
   int handtype = 0;

   if (isFlushResult && isStraightResult) {
       if (playerHand.get(0).getRank() == ACE && playerHand.get(1).getRank() == 10)
   handtype = 8; //Royal Flush
   else{
   handtype = 7; //Straight Flush
   }
   }
   else if(isFourofAKind()){
   handtype = 6;
   }
   else if(isFullHouse()){
   handtype = 5;
   }
   else if(isFlush()){
   handtype = 4;
   }
   else if(isStraight()){
   handtype = 3;
   }
   else if(isThreeofAKind()){
   handtype = 2;
   }
   else if(isTwoPair()){
   handtype = 1;
   }
   else if(isRoyalPair()){
   handtype = 0;
   }
   else{
   handtype = -1;
   }

   switch (handtype){
   case -1:
   System.out.println("Sorry, you lost!");
   break;
   default:
   int bonus = multipliers[handtype];
   System.out.printf("%s ", goodHandTypes[handtype]);
   this.playerBalance += (this.playerBet * bonus);
   break;
   }
}

/*************************************************
* add new private methods here ....
*
*************************************************/
private boolean isFlush() {
  
return true;
}
private boolean isStraight() {
int consectCards = 0;
int firstRank = playerHand.get(0).getRank();
int rank = 0;
for (int i = 0; i < numberOfCards -1; i++) {
if (playerHand.get(i).getRank() + 1 != playerHand.get(i + 1).getRank()) {
consectCards++;
rank = playerHand.get(i + 1).getRank();
}
if (consectCards == 3 && rank == 13 && firstRank == 1){
return true;
}
}
return consectCards == 4;
}

private boolean isFourofAKind() {
if (playerHand.get(0).getRank() == playerHand.get(1).getRank() || playerHand.get(numberOfCards - 2).getRank() == playerHand.get(numberOfCards - 1).getRank()) {
int start = playerHand.get(0).getRank() == playerHand.get(1).getRank() ? 0 : 1;
int end = playerHand.get(0).getRank() == playerHand.get(1).getRank() ? (numberOfCards - 1) : numberOfCards;
for (int i = start; i < end; i++) {
if (playerHand.get(i).getRank() != playerHand.get(i + 1).getRank()) {
// cards are not the same, so it is not four of a kind
return false;
}
}
return true;
}
return false;
}

private boolean isFullHouse(){
/* if (isThreeofAKind() == true && isRoyalPair() == true){
return true;
}
else {
return false;
} */
return isThreeofAKind() && isTwoofAKind() && isTwoofAKindFinder() != isThreeofAKindFinder();
}

private boolean isThreeofAKind() {   
return isThreeofAKindFinder() != -1;
}

private int isThreeofAKindFinder(){
int cardsWithSameRank = 1;
int rank = 0;
for (int i = 0; i < numberOfCards - 1; i++){
if(playerHand.get(i).getRank() != playerHand.get(i + 1).getRank()){
cardsWithSameRank = 1;
} else {
cardsWithSameRank++;
rank = playerHand.get(i).getRank();
}
}
if (cardsWithSameRank == 3){
return rank;
}
else {
return -1;
}
}

private boolean isTwoofAKind() {
return isTwoofAKindFinder() != -1;
}

private int isTwoofAKindFinder(){

int rank;
  
for(int i = 0; i < numberOfCards - 1; i++) {
rank = playerHand.get(i).getRank();
if(playerHand.get(i).getRank() == playerHand.get(i +1).getRank()){
return rank;
}
}
return -1;
}

private boolean isTwoPair(){
int pairCnt = 0;
for(int i = 0; i < numberOfCards - 1;) {
if(playerHand.get(i).getRank() == playerHand.get(i +1).getRank()){
pairCnt++;
i = i + 2;
} else {
i++;
}
}
return pairCnt == 2;
}

private boolean isRoyalPair() {
   for (int i = 0; i < numberOfCards - 1; i++){
   if ( playerHand.get(i).getRank() == playerHand.get(i + 1).getRank() && (playerHand.get(i).getRank() == ACE || playerHand.get(i).getRank() > 10)){
   return true;
   }
   }
   return false;
}

private void showBalance(){
System.out.printf("Your balance is %d ", playerBalance);
}
private void getBetAndVerify(){
System.out.printf("Please enter your bet: ", playerBalance);
Scanner get_bet = new Scanner(System.in);

//get bet and verify
do{
this.playerBet = get_bet.nextInt();
} while(!(this.playerBet > 0) || !(this.playerBet <= this.playerBalance));
}

private void updateBalance(){
this.playerBalance -= this.playerBet;
}
private void displayHand(){
int currentSize = playerHand.size();
for (int k = 0; k < currentSize ; k++){
System.out.print(playerHand.get(k).toString());
}
}

private void updateCards(){
String userInput; //for the scanner
List<String> keep_list;
List<Card> replacementHand = new ArrayList<Card>(); //get new cards
int keep_size;
int test_value;
boolean enter_new_number = false; //checking if valid input
//prompt the user for cards to keep
do{
System.out.print("Enter positions of cards to keep (e.g. 1, 4, 5): ");
Scanner keepScan = new Scanner( System.in);
userInput = keepScan.nextLine();
//storing locations of the cards to keep
keep_list = Arrays.asList(userInput.split(", *"));
keep_size = keep_list.size();
//need to test that we got numbers and that they are valid
for (int i = 0; i <= keep_size -1 ; i++){
try{
test_value = Integer.parseInt(keep_list.get(i));
if ( test_value > 0 && test_value <= numberOfCards)
{
enter_new_number = false;
}
else{
enter_new_number = true;
System.out.println("invalid input");
break; // if invalid input break from the for loop and ask again
}

} catch(NumberFormatException e){
enter_new_number = true;
System.out.println("not a number");
}
}
} while(enter_new_number); //at this point we know we can parse to integers from our keep_list
int number_to_replace = numberOfCards - keep_list.size(); //get number of cards to replace
try{
replacementHand = oneDeck.deal(number_to_replace); //get new cards
} catch (PlayingCardException e){
System.out.println("Exception dealing a new hand" + e.getMessage());
}

//replace cards not on the keep list
//replace card if not on keep list
//following requires that the keep_list is sorted
for(int i = 0; i < keep_list.size(); i++){
replacementHand.add( playerHand.get(Integer.parseInt(keep_list.get(i))));
}
//replace current hand with replacement hand
playerHand = replacementHand;

System.out.println("" + playerHand.toString());
}

private boolean retry(){
boolean newGame = false;
Scanner choiceScan = new Scanner(System.in);
String choice = "n";

if (playerBalance <= 0) {
System.out.printf("Your balance is %d Bye! ", playerBalance);
newGame = false;
return newGame;
}

System.out.printf("Your balance:$%d, one more game (y or n)? ", playerBalance);
Scanner playAgain = new Scanner(System.in);
if (playAgain.hasNext() && (playAgain.nextLine().equalsIgnoreCase("y"))) {
System.out.printf("Want to see payout table? (y or n) " );
Scanner showTable = new Scanner(System.in);
if (showTable.hasNext() && (showTable.nextLine().equalsIgnoreCase("y"))) {showPayoutTable();}
oneDeck.reset();
newGame = true;
}
else {
System.out.println("Bye");
return false;
}
return newGame;
}
public void play()
{
/** The main algorithm for single player poker game
*
* Steps:
*        showPayoutTable()
*
*        ++  
*        show balance, get bet
*       verify bet value, update balance
*       reset deck, shuffle deck,
*       deal cards and display cards
*       ask for positions of cards to keep
* get positions in one input line
*       update cards
*       check hands, display proper messages
*       update balance if there is a payout
*       if balance = O:
*           end of program
*       else
*           ask if the player wants to play a new game
*           if the answer is "no" : end of program
*           else : showPayoutTable() if user wants to see it
*           goto ++
*/

// implement this method!
   showPayoutTable();

boolean keep_playing = true;

while (keep_playing) {
showBalance();
getBetAndVerify();
updateBalance();
oneDeck.reset();
oneDeck.shuffle();
try{
playerHand = oneDeck.deal(numberOfCards);
}catch (PlayingCardException e) {
System.out.println("Exception dealing a new hand" + e.getMessage());
}
displayHand();
updateCards();
checkHands();
keep_playing = retry(); //false if user quits or loses
}
}


/*************************************************
* Do not modify methods below
/*************************************************


/** testCheckHands() is used to test checkHands() method
* checkHands() should print your current hand type
*/
public void testCheckHands()
{
   try {
       playerHand = new ArrayList<Card>();

       // set Royal Flush
       playerHand.add(new Card(1,4));
       playerHand.add(new Card(10,4));
       playerHand.add(new Card(12,4));
       playerHand.add(new Card(11,4));
       playerHand.add(new Card(13,4));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Straight Flush
       playerHand.set(0,new Card(9,4));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Straight
       playerHand.set(4, new Card(8,2));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Flush
       playerHand.set(4, new Card(5,4));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush   ",
      // "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };

       // set Four of a Kind
       playerHand.clear();
       playerHand.add(new Card(8,4));
       playerHand.add(new Card(8,1));
       playerHand.add(new Card(12,4));
       playerHand.add(new Card(8,2));
       playerHand.add(new Card(8,3));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Three of a Kind
       playerHand.set(4, new Card(11,4));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Full House
       playerHand.set(2, new Card(11,2));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Two Pairs
       playerHand.set(1, new Card(9,2));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // set Royal Pair
       playerHand.set(0, new Card(3,2));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");

       // non Royal Pair
       playerHand.set(2, new Card(3,4));
       System.out.println(playerHand);
       checkHands();
       System.out.println("-----------------------------------");
   }
   catch (Exception e)
   {
       System.out.println(e.getMessage());
   }
}

/* Quick testCheckHands() */
public static void main(String args[])
{
   PokerGame pokergame = new PokerGame();
   pokergame.testCheckHands();
}
}

Explanation / Answer

package PJ4; import java.util.*; import java.util.Collections; import java.util.logging.Level; import java.util.logging.Logger; /** * class PlayingCardException: It is used for errors related to Card and Deck * objects Do not modify this class! */ class PlayingCardException extends Exception { /* Constructor to create a PlayingCardException object */ PlayingCardException() { super(); } PlayingCardException(String reason) { super(reason); } } /** * class Card : for creating playing card objects it is an immutable class. Rank * - valid values are 1 to 13 Suit - valid values are 0 to 3 Do not modify this * class! */ class Card { /* constant suits and ranks */ static final String[] Suit = {"Clubs", "Diamonds", "Hearts", "Spades"}; static final String[] Rank = {"", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"}; /* Data field of a card: rank and suit */ private int cardRank; /* values: 1-13 (see Rank[] above) */ private int cardSuit; /* values: 0-3 (see Suit[] above) */ /* Constructor to create a card */ /* throw PlayingCardException if rank or suit is invalid */ public Card(int rank, int suit) throws PlayingCardException { if ((rank < 1) || (rank > 13)) { throw new PlayingCardException("Invalid rank:" + rank); } else { cardRank = rank; } if ((suit < 0) || (suit > 3)) { throw new PlayingCardException("Invalid suit:" + suit); } else { cardSuit = suit; } } /* Accessor and toString */ /* You may impelemnt equals(), but it will not be used */ public int getRank() { return cardRank; } public int getSuit() { return cardSuit; } public String toString() { return Rank[cardRank] + " " + Suit[cardSuit]; } /* Few quick tests here */ public static void main(String args[]) { try { Card c1 = new Card(1, 3); // A Spades System.out.println(c1); c1 = new Card(10, 0); // 10 Clubs System.out.println(c1); c1 = new Card(10, 5); // generate exception here } catch (PlayingCardException e) { System.out.println("PlayingCardException e.getMessage()); } } } /** * class Decks represents : n decks of playing cards Use class Card to construct * n * 52 playing cards! * * Do not add new data fields! Do not modify any methods You may add private * methods */ class Decks { /* this is used to keep track of original n*52 cards */ private List originalDecks; /* this starts with n*52 cards deck from original deck */ /* it is used to keep track of remaining cards to deal */ /* see reset(): it resets dealDecks to a full deck */ private List dealDecks; /* number of decks in this object */ private int numberDecks; /** * Constructor: Creates default one deck of 52 playing cards in * originalDecks and copy them to dealDecks. initialize numberDecks=n Note: * You need to catch PlayingCardException from Card constructor Use * ArrayList for both originalDecks & dealDecks */ public Decks() { // implement this method! originalDecks = new ArrayList(); numberDecks=1; for (int i = 0; i number of remaining cards * * Note: You need to create ArrayList to stored dealt cards and should * removed dealt cards from dealDecks * */ public List deal(int numberCards) throws PlayingCardException { // implement this method! List dealtCards = new ArrayList(); if (numberCards > dealDecks.size()) { throw new PlayingCardException("Not enough cards to deal"); } for (int i = 0; i