简体   繁体   English

Java:扑克手

[英]Java: Poker Hand

So, I have to create a poker hand program using functions/methods and arrays.所以,我必须使用函数/方法和数组创建一个扑克手程序。

Here's a sample output I need to have:这是我需要的示例输出:

Enter five numeric cards, no face cards. Use 2 - 9.Card 1: 8 

Card 2: 7
Card 3: 8
Card 4: 2
Card 5: 7
Two Pair!

Enter five numeric cards, no face cards. Use 2 - 9.
Card 1: 4 
Card 2: 5
Card 3: 6
Card 4: 8
Card 5: 7
Straight!

Enter five numeric cards, no face cards. Use 2 - 9.
Card 1: 9
Card 2: 2
Card 3: 3
Card 4: 4
Card 5: 5
High Card!

And here's my code (I'm having issues with logic in determining if one gets pair, 3 of a kind, etc.).这是我的代码(我在确定一个人是否得到一对、3 个等方面的逻辑有问题)。 They have be methods/functions.它们是方法/函数。 So, if I can figure out how to do 1 or 2 of them, it should be hopefully be a breeze from there:所以,如果我能弄清楚如何做其中的 1 或 2 个,那么从那里开始应该是轻而易举的:

import java.util.Scanner;

public class Assignment4 
{
    public static void main(String args[])
    {
        final int LEN = 5;
        int[] hand = new int[LEN];

    Scanner input = new Scanner(System.in);

    //input the hand
    System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
    for (int index = 0; index < hand.length; index++) {
        System.out.print("Card " + (index + 1) + ": ");
        hand[index] = input.nextInt();
    }

    //sort the collection
    bubbleSortCards(hand);

    //determine players hand type
    //flow of evaluation -> checking complex hands first  
    if (containsFullHouse(hand)) {
        System.out.println("Full House!");
    } else if (containsStraight(hand)) {
        System.out.println("Straight!");
    } else if (containsFourOfaKind(hand)) {
        System.out.println("Four of a Kind!");
    } else if (containsThreeOfaKind(hand)) {
        System.out.println("Three of a Kind!");
    } else if (containsTwoPair(hand)) {
        System.out.println("Two Pair!");
    } else if (containsPair(hand)) {
        System.out.println("Pair!");
    } else 
        System.out.println("High Card!");   
}

And this is the recommended way from the assignment's instructions:这是作业说明中推荐的方法:

public class PokerHand
{
    public static void main(String args[])
    {
        int hand[] = {5, 2, 2, 3, 8};

        if (containsAPair(hand)) {
                System.out.println("Pair!");
        } else {
                System.out.println("Not a pair!");
        }
}

public static boolean containsAPair(int hand[]) {
        // Your code here... don’t return true every time...
        return true;
}

} }

If more information is needed, I'll be more than happy to supply that.如果需要更多信息,我将非常乐意提供。 THANKS!谢谢!

Instead of sorting the hand, I would recommend that you tally the contents of a hand and generate an array of counts, where the i th element of the array has the number of cards with value i .我建议您不要对手牌进行排序,而是对手牌的内容进行计数并生成一个计数数组,其中数组的第i元素具有值为i的卡片数量。 You should then be able to figure out how to use that array to decide whether it is a particular type of hand.然后,您应该能够弄清楚如何使用该数组来确定它是否是特定类型的手牌。

Since this is homework, I'll point you in a direction to get started to help you think about a solution.由于这是家庭作业,我会为您指明一个开始的方向,以帮助您思考解决方案。

In your post, you need to create the code for the containsFullHouse() , containsStraight() and so on.在您的帖子中,您需要为containsFullHouse()containsStraight()等创建代码。 So...所以...

  • Grab a deck of cards.拿一副牌。 Remove the face cards and aces.删除面卡和 A。
  • Imagine you're playing poker.想象一下你在玩扑克。 Give yourself a hand that's a four of a kind.给自己一只手,这是一种四。 Think about how you, as a person, would determine what you have.想想你,作为一个人,将如何决定你拥有什么。 I would personally sort my hand, then count the incidences of each card.我会亲自我的手进行排序,然后计算每张牌的出现次数。 If I have 4 of the same value, then I've got a four of a kind and can stop.如果我有 4 个相同的值,那么我有一个 4 并且可以停止。
  • Okay, now a straight.好的,现在是直的。 I'd again sort my hand.我会再次整理我的手。 If every card c has c + 1 as the next card's value, except the last, I have a straight.如果每张牌c都有c + 1作为下一张牌的价值,除了最后一张,我有顺子。
  • Repeat this process until you work down to the lowest valued hands.重复这个过程,直到你找到价值最低的牌。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM