1

I need to make a method in my poker code in which the hierarchy is represented as integer values:

1 - High Single

2 - High Pair

3 - Two Pair

4 - Three of a Kind

5 - Straight

6 - Flush

7 - Full House

8 - Four of a Kind

9 - Straight Flush

10 - Royal Flush

The method checks your hands to determine the best set of cards. I have a piece of code here already that does some other things such as returning random integers and generating them into strings. Now I need this bestHand method to complete the program. How would you do this?

Code:

import java.util.Scanner;

public class FiveCardPoker {

    static Scanner input = new Scanner(System.in);

    static int coinBalance = 100;

    static double coinBet = 0;

    static int[][] deckCards;

    private short value, suit;

    static int [][]userDeck = new int [5][2];
    static int [][]compDeck = new int [5][2];

    static String userDeckString = "";
    static String userSuitString = "";

    static String compDeckString = "";
    static String compSuitString = "";

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

    }

    public static void cardValue() {

        for (int i = 0; i < 5; i++) { 
            int cardValue = (int)(Math.random() * (14 - 2) + 2);
            userDeck[i][0] = cardValue;
            compDeck[i][0] = cardValue;

        }       

    }

    public static void cardSuit() {

        for (int i = 0; i < 5; i++) {
            int cardSuit = (int)(Math.random() * (4 - 1) + 2);
            userDeck[i][0] = cardSuit;
            compDeck[i][0] = cardSuit;
        }

    }

    public static void cardString() {

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

            switch(userDeck[i][0]) {

            case 1: userDeckString = ("Two"); break;
            case 2: userDeckString = ("Three"); break;
            case 3: userDeckString = ("Four"); break;
            case 4: userDeckString = ("Five"); break;
            case 5: userDeckString = ("Six"); break;
            case 6: userDeckString = ("Seven"); break;
            case 7: userDeckString = ("Eight"); break;
            case 8: userDeckString = ("Nine"); break;
            case 9: userDeckString = ("Ten"); break;
            case 10: userDeckString = ("Jack"); break;
            case 11: userDeckString = ("Queen"); break;
            case 12: userDeckString = ("King"); break;
            case 13: userDeckString = ("Ace"); break;

            }

            switch(userDeck[i][1]) {

            case 1: userSuitString = ("Spades"); break;
            case 2: userSuitString = ("Hearts"); break;
            case 3: userSuitString = ("Diamonds"); break;
            case 4: userSuitString = ("Clubs"); break;

            }

        }

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

            switch(compDeck[i][0]) {

            case 1: compDeckString = ("Two"); break;
            case 2: compDeckString = ("Three"); break;
            case 3: compDeckString = ("Four"); break;
            case 4: compDeckString = ("Five"); break;
            case 5: compDeckString = ("Six"); break;
            case 6: compDeckString = ("Seven"); break;
            case 7: compDeckString = ("Eight"); break;
            case 8: compDeckString = ("Nine"); break;
            case 9: compDeckString = ("Ten"); break;
            case 10: compDeckString = ("Jack"); break;
            case 11: compDeckString = ("Queen"); break;
            case 12: compDeckString = ("King"); break;
            case 13: compDeckString = ("Ace"); break;

            }

            switch(compDeck[i][1]) {

            case 1: compSuitString = ("Spades"); break;
            case 2: compSuitString = ("Hearts"); break;
            case 3: compSuitString = ("Diamonds"); break;
            case 4: compSuitString = ("Clubs"); break;

            }

        }

    }

    public static void bestUserHand() {

    }

    public static void BestCompHand() {

    }

    public static void updateBalance() {

        do {    

            System.out.println("How many coins would you like to bet?");
            coinBet = input.nextDouble();

            if (coinBet > coinBalance || coinBalance > 25) {
                System.out.println("You can only bet up to 25 coins!");
            }
            else if (coinBet >= 1 && coinBet <= 25) {

            }

        } while (coinBet < 1 || coinBet > 25);

    }

}
PiNaKa30
  • 608
  • 7
  • 20
tahat04
  • 29
  • 5
  • 1
    the hierarchy is actually a bit more subtle. You need logic to say 4 kings beats 4 queens or that a straight flush whose highest value is 6 loses to a straight flush that goes up to a queen. Anyway, though, I think you would start from the best hand and see if you have it, and if not work your way down. You can probably reuse some code, for instance straightFlush() = straight() && flush() – Jeremy Kahan Dec 12 '19 at 04:49
  • How tricky would you like to get here? Would you be happy to know just that, say 7,7,7, K, K is a "full house"? Do you want to be able to go further and be able to say that it's "full house, sevens full of kings"? Or do you need to be able to **compare** hands (example, K, K, A, A,A beats 7,7,7,K,K,; and 2,2,2,2,Q beats them both)? – Kevin Anderson Dec 12 '19 at 05:00
  • 1
    _Now I need this bestHand method to complete the program._ But what about method `main()` which is empty in the code you posted. – Abra Dec 12 '19 at 05:02
  • Oops: wasn't paying close enough attention (;-(): you want to compare two hands and declare a winner (or a tie), yes? – Kevin Anderson Dec 12 '19 at 05:20
  • This should work for you, https://stackoverflow.com/a/20715903/1349365 – Moshi Dec 12 '19 at 11:08
  • General strategy for poker hand evaluation: first, sort the cards in the hand by rank; that will make future tests easier. Then, check if all five are the same suit and set a flag. Then, check for each hand type in order from strongest to weakest: that is, first check for straight flush, then quads, then full house, etc., stopping when you find a match. If all those tests fail, you're left with no pair. You will still need, as pointed out, more methods to compare two hands of the same type. – Lee Daniel Crocker Dec 12 '19 at 17:01
  • @KevinAnderson Exactly. I stated that I wanted the method to check the hand it created and see what the best set of cards are. – tahat04 Dec 12 '19 at 21:57
  • @JeremyKahan So I basically declare the hand rankings, set a score to them and in the main method I would basically see if user or comp has the best hand correct? – tahat04 Dec 12 '19 at 22:11
  • @Abra Yeah I was thinking that I should work on the bestHand method then after that I would return all of the methods back to main method. But I'm leaving it empty for now. – tahat04 Dec 12 '19 at 22:13
  • @Moshii Correct me if I'm wrong but isn't that code in python? How am I supposed to implement it? Or are you saying to follow the logic of that code? I am sorry I am still fairly new to java. – tahat04 Dec 12 '19 at 22:14
  • @LeeDanielCrocker If I were to sort the cards in the hand by rank as you said, should I use a sorting algorithm to achieve this or is there some easier way in doing that? Also what do you mean by setting a flag? And how would I check from strongest to weakest? Isn't that the same as ranking? And are you also suggesting that I need more methods to compare the two hands? I am sorry for asking so many questions as I stated in a previous comment I am still pretty new. – tahat04 Dec 12 '19 at 22:20
  • I had not thought of setting a score to them, but that's a nice idea for comparing hands (though as others point out, you'll need to write the tiebreakers and allow for there being a tie -- for example we both hold A, 2, 3, 4, 5 mine are diamonds yours are clubs). Java has a built in method to sort arrays so long as you give it a method that tells it how to compare two entries. I suspect that will be easier than learning (or imitating)a sort algorithm. – Jeremy Kahan Dec 12 '19 at 22:32

0 Answers0