2014-09-10 78 views
0

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

下面是一個示例輸出,我需要有:

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

}

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

+5

什麼是你的問題 – Jay 2014-09-10 20:55:46

+0

作業4 :-) – rlegendi 2014-09-10 20:57:05

+3

所以在這裏說'//你的代碼在這裏......不要每次都返回真實......',你有什麼嘗試? – jdphenix 2014-09-10 20:59:34

回答

1

相反排序的手,我會建議你統計手的內容,併產生計數,數組的i元素具有的卡值i數量的數組。然後您應該能夠弄清楚如何使用該數組來決定它是否是特定類型的手。

1

由於這是作業,我會指出你開始幫助你考慮解決方案的方向。

在您的文章中,您需要爲containsFullHouse()containsStraight()等創建代碼。所以...

  • 搶一副牌。去掉面牌和ace。
  • 想象一下你正在玩撲克。給自己一個這樣的四手牌。想想你作爲一個人將如何決定你的所作所爲。我親自將sort我的手,然後計算每張牌的發生率。如果我有四個相同的價值,那麼我有四種,可以停止。
  • 好吧,現在是直線。我會再次整理我的手。如果每張卡片c都有c + 1作爲下一張卡片的價值,除了最後一張,我有一個筆直的。
  • 重複這個過程,直到你工作到最低價值的手。