簡體   English   中英

Java:撲克手

[英]Java: Poker Hand

所以,我必須使用函數/方法和數組創建一個撲克手程序。

這是我需要的示例輸出:

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!

這是我的代碼(我在確定一個人是否得到一對、3 個等方面的邏輯有問題)。 它們是方法/函數。 所以,如果我能弄清楚如何做其中的 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!");   
}

這是作業說明中推薦的方法:

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;
}

}

如果需要更多信息,我將非常樂意提供。 謝謝!

我建議您不要對手牌進行排序,而是對手牌的內容進行計數並生成一個計數數組,其中數組的第i元素具有值為i的卡片數量。 然后,您應該能夠弄清楚如何使用該數組來確定它是否是特定類型的手牌。

由於這是家庭作業,我會為您指明一個開始的方向,以幫助您思考解決方案。

在您的帖子中,您需要為containsFullHouse()containsStraight()等創建代碼。 所以...

  • 拿一副牌。 刪除面卡和 A。
  • 想象一下你在玩撲克。 給自己一只手,這是一種四。 想想你,作為一個人,將如何決定你擁有什么。 我會親自我的手進行排序,然后計算每張牌的出現次數。 如果我有 4 個相同的值,那么我有一個 4 並且可以停止。
  • 好的,現在是直的。 我會再次整理我的手。 如果每張牌c都有c + 1作為下一張牌的價值,除了最后一張,我有順子。
  • 重復這個過程,直到你找到價值最低的牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM