-1

I try to grograme a game called video poker. It's all most done, but Royal Flush seems to never appears.

The card deck contains 52 cards, 13 of each suit. At the beginning of the game, the deck is shuffled. 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:

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 5’s. 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 5’s. 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

 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Scanner;


public class VideoPoker {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //SPADES = 0;   // Codes for the 4 suits, plus Joker.
    //HEARTS = 1;
    //DIAMONDS = 2;
    //CLUBS = 3;

     //ACE = 1;      // Codes for the non-numeric cards.
     //JACK = 11;    //   Cards 2 through 10 have their 
     //QUEEN = 12;   //   numerical values for their codes.
     //KING = 0;

    final int MAX_CARD = 52; //The card deck contains 52 cards, 13 of each suit.

    Scanner scanner = new Scanner(System.in);

    int[] hand = new int[5]; //Five cards to play
    int matchFace = 0;
    boolean straight = false;
    boolean threeOfKind = false;
    boolean fourOfKind = false;
    boolean flush = false;
    boolean straightFlush = false;
    boolean fullHouse = false;
    boolean royalFlush = false;
    String str;
    int pair = 0;

    ArrayList<Integer> arrayList = new ArrayList<>();
    for (int i=0;i<MAX_CARD;i++) arrayList.add(i); //Fill the list
    Collections.shuffle(arrayList);

    //Deal 5 cards
    for (int i=0;i<hand.length;i++) {
        hand[i] = arrayList.get(0);
        arrayList.remove(0);
    }

    for (int i=0;i<hand.length;i++) {
        System.out.println(valueToString(hand[i]));
    }

    //The player can reject none, some,or all of the cards. 
    //The rejected cards are replaced from the top of the deck.

    for (int i=0;i<hand.length;i++){
        System.out.printf("Do you want to reject card number %d ? ",i+1);
        if (scanner.next().toUpperCase().charAt(0)=='Y'){
            arrayList.add(hand[i]); //Return to the end of deck
            hand[i] = arrayList.get(0);
            arrayList.remove(0);
        }
    }

    System.out.println();
    Arrays.sort(hand);


    for (int i=0;i<hand.length;i++) {
        System.out.println(valueToString(hand[i]));
    }

    ArrayList<String> strList = new ArrayList<>();
    for (int i=0;i<hand.length;i++) strList.add(valueToString(hand[i]));

    do{
        str = strList.get(0);
        strList.remove(0);
        for (int j=0;j<strList.size();j++){
            if (str.regionMatches(0, strList.get(j), 0, 2)){  //Compare the first two char
                matchFace++; 
                strList.remove(j);
                j--;
            }
        }
        if (matchFace ==3) {// Four of a kind—Four cards of the same value, such as four queens.
            fourOfKind = true;
            threeOfKind = false; //Reset
            pair = 0; //Reset
        }
        else if (matchFace ==2) { // Three of a kind—Three cards of the same value, for example three queens.
            threeOfKind = true;
            pair = 0; //Reset
        }else if (matchFace ==1 ) {
            pair++;
        }

        matchFace = 0; //Reset for each loop

    }while (!strList.isEmpty());

    ArrayList<Integer> intList = new ArrayList<>();

    //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.

    //Case 1 where King to value 13 and Ace set to value 14 
    for (int i=0;i<hand.length;i++) {
        if (hand[i]%13==0){
            intList.add(13); // set King to value 13
        }else if (hand[i]%13==1){
            intList.add(14); // set Ace to value 14
        }
        else intList.add((hand[i]%13));

    }
    Collections.sort(intList);
    if (intList.get(0)+1==intList.get(1)&&intList.get(1)+1==intList.get(2)
            &&intList.get(3)==intList.get(2)+1&&intList.get(4)==intList.get(3)+1){
        straight = true;
    }
    //Case 2 where Ace set to value 1
    for (int i=0;i<intList.size();i++) if (intList.get(i)==14)intList.set(i, 1); // Set Ace to value 1
    Collections.sort(intList);
    if (intList.get(0)+1==intList.get(1)&&intList.get(1)+1==intList.get(2)
            &&intList.get(3)==intList.get(2)+1&&intList.get(4)==intList.get(3)+1){
        straight = true;
    }

    //Flush — Five cards, not necessarily in order, of the same suit.   
    if ((hand[0]/13==hand[1]/13)&& (hand[1]/13==hand[2]/13)&& (hand[2]/13==hand[3]/13)&& (hand[3]/13==hand[4]/13) && (hand[4]/13==hand[0]/13)){
        flush = true; 
    }

    ////Full House — Three of a kind and a pair, for example three queens and two 5’s.
    if (threeOfKind && pair==1) fullHouse = true;

    // Straight Flush—A straight and a flush: Five cards with consecutive values of
    //the same suit. Payout: 50
    if(straight && flush ) straightFlush = true; 


    //Royal Flush—The best possible hand in poker. A 10, jack, queen, king, and ace,
    //all of the same suit. 
    if ((hand[0]%13==10&&hand[1]%13==11&&hand[2]%13==12&&hand[3]%13==0&&hand[4]%13==1) && 
            (hand[0]/13==hand[1]/13)&& (hand[1]/13==hand[2]/13)&& (hand[2]/13==hand[3]/13)&& (hand[3]/13==hand[4]/13) && (hand[4]/13==hand[0]/13) ) {
        royalFlush = true; 
    }

    if (royalFlush) {
        System.out.println("Royal Flush — The best possible hand in poker. A 10, jack, queen, king, and ace,all of the same suit. Payout: 250");
    } else 
    //Straight Flush—A straight and a flush: Five cards with consecutive values of
    //the same suit. Payout: 50
    if(straightFlush){
        System.out.println("Straight Flush — A straight and a flush: Five cards with consecutive values of the same suit. Payout: 50");
    }else 
    //Four of a Kind—Four cards of the same value, such as four queens. Payout: 25
    if (fourOfKind){ 
        System.out.println("Four of a Kind — Four cards of the same value, such as four queens. Payout: 25");
    }
    else 
    //Full House — Three of a kind and a pair, for example three queens and two 5’s.
    //Payout: 6
    if (fullHouse){ 
        System.out.println("Full House — Three of a kind and a pair, for example three queens and two 5’s.Payout: 6");
    }
    else
    //Flush — Five cards, not necessarily in order, of the same suit. Payout: 5 
    if (flush){
        System.out.println("Flush — Five cards, not necessarily in order, of the same suit. Payout: 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 a 2 or follow a king.
        //Payout: 4
    else 
    if (straight) {
        System.out.println("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");
    }
    //Three of a kind—Three cards of the same value, for example three queens.
    //Payout: 3
    else if (threeOfKind) {
        System.out.println("Three of a kind — Three cards of the same value. Payout: 3");
    }
    // Two pairs—Two pairs, for example two queens and two 5’s. Payout: 2
    else if (pair==2) {
        System.out.println("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 queens. Payout: 1
    else if (pair==1) {
        System.out.println("One pair — Two cards of the same value, for example two queens. Payout: 1");
    }
    else {
        System.out.println("No pair — The lowest hand, containing five separate cards that do not match up");       
    }

    scanner.close();
}

public static String valueToString(int value){
    String faces;
    String suits;

    if (value%13 ==0) faces = "King ";
    else if (value%13 ==1) faces = "Ace ";
    else if (value%13 ==2) faces = "Deuce ";
    else if (value%13 ==3) faces = "Three ";
    else if (value%13 ==4) faces = "Four ";
    else if (value%13 ==5) faces = "Five ";
    else if (value%13 ==6) faces = "Six ";
    else if (value%13 ==7) faces = "Seven ";
    else if (value%13 ==8) faces = "Eight ";
    else if (value%13 ==9) faces = "Nine ";
    else if (value%13 ==10) faces = "Ten ";
    else if (value%13 ==11) faces = "Jack ";
    else if (value%13 ==12) faces = "Queen ";
    else faces ="ERROR";

    if (value/13 ==0) suits = "Spades";
    else if (value/13 ==1) suits = "Hearts";
    else if (value/13 ==2) suits = "Diamonds ";
    else if (value/13 ==3) suits = "Clubs";
    else suits = "ERROR";

    return faces +"of " + suits;

}


}
MaSa
  • 3
  • 2
  • 3
  • `if ((hand[0]%13==10&&hand[1]%13==11&&hand[2]%13==12&&hand[3]%13==0&&hand[4]%13==1) && (hand[0]/13==hand[1]/13)&& (hand[1]/13==hand[2]/13)&& (hand[2]/13==hand[3]/13)&& (hand[3]/13==hand[4]/13) && (hand[4]/13==hand[0]/13) )` outch - my eyes! – assylias Jan 02 '14 at 19:18
  • Please don't regurgitate a homework question. Your question would be improved by removing all the homework instructions. You should post what you did to prove that it never displays – Ruan Mendes Jan 02 '14 at 19:18
  • Of course a royal flush should almost never appear. That's why it's the highest hand in poker – Sam I am says Reinstate Monica Jan 02 '14 at 19:19
  • Test your program by forcing a strait, or royal flush. Do you have some knowledge or probability? Strait flushes rarely occur. – Pete B. Jan 02 '14 at 19:19
  • Also, you should use enums for the cards rather than the `if/else`s with magic numbers http://stackoverflow.com/questions/3320678/class-for-representing-a-card-in-java – Ruan Mendes Jan 02 '14 at 19:21
  • it's generally preferable to try to post **only** the parts of the code that you think are relevant to the question, and to only post other parts if people ask for it. – Sam I am says Reinstate Monica Jan 02 '14 at 19:23

3 Answers3

4

hand[] is sorted (Arrays.sort(hand);), so a royal flush would look like hand[] = {10, 11, 12, 0, 1}, which, after sorting would become: {0, 1, 10, 11, 12}.

So the first part of your condition for your royal flush would not work as is, it should probably be something like:

hand[0]%13==0&&hand[1]%13==1&&hand[2]%13==10&&hand[3]%13==11&&hand[4]%13==12

instead of:

hand[0]%13==10&&hand[1]%13==11&&hand[2]%13==12&&hand[3]%13==0&&hand[4]%13==1

By the way your algo to detect a straight seems to account for that issue but your royal flush condition doesn't. That's at first glance, I haven't looked at the details.

In any case, your code is quite unreadable as it is and you would benefit from rewriting it in a more human-friendly way...

assylias
  • 321,522
  • 82
  • 660
  • 783
2

A royal flush only appears one in 649,739 times. Have you tested your program more than a million times to see if it appears?

Add some code to select which cards you get and see if the test for a royal flush works.

Damien Black
  • 5,579
  • 18
  • 24
0

This code is quite terrible. I added a few bits to make it more readable.

    for (Integer integer : arrayList) {
        System.out.println(integer + " : " + valueToString(integer));
    }

This prints out the cards so I could force a flush. The output looks like:

0 : King of Spades
1 : Ace of Spades
2 : Deuce of Spades
3 : Three of Spades
4 : Four of Spades
5 : Five of Spades
6 : Six of Spades
7 : Seven of Spades
8 : Eight of Spades
9 : Nine of Spades
10 : Ten of Spades
11 : Jack of Spades
12 : Queen of Spades
13 : King of Hearts
14 : Ace of Hearts
...

Using this, I added some code to force the hand to be what I wanted:

    // Deal 5 cards
    for (int i = 0; i < hand.length; i++) {
        hand[i] = arrayList.get(0);
        arrayList.remove(0);
    }

    // force a royal flush with Spades
    int[] flushHand = {1, 0, 12, 11, 10 };
    hand = flushHand;

As you can see, this hand fails your royal flush test. That's why it isn't working.

David S.
  • 6,567
  • 1
  • 25
  • 45