java视频扑克 - 皇家同花顺永远不会出现

时间:2014-01-02 19:16:23

标签: java poker

我试着对一款名为视频扑克的游戏进行编程。这一切都做得最多,但皇家同花顺似乎永远不会出现。

卡牌包含52张卡,每套13张。一开始 游戏中,牌组被洗牌。玩家为每场比赛支付代币。然后是顶部 甲板的五张牌呈现给玩家。玩家可以拒绝任何一个,一些, 或所有卡片。被拒绝的卡片将从卡座的顶部更换。现在 手得分。您的程序应将其发音为以下之一:

没有对 - 最低牌,包含五张不匹配的单独牌 创造下面的任何一手。

一对 - 具有相同值的两张牌,例如两张皇后。支付:1

两对 - 两对,例如两个皇后和两个5。支付:2

三种 - 三张相同价值的牌,例如三张牌。 支付:3

直 - 具有连续值的五张牌,不一定相同 套装,例如4,5,6,7和8. ace可以先于2或跟随国王。 支付:4

同花顺的五张牌,不一定是有序的。支付:5

满屋 - 三种和一对,例如三个皇后和两个5。 支付:6

四种 - 四张相同价值的牌,例如四张皇后牌。支付:25

同花顺 - 直线和同花:五张连续值为的牌 同样的诉讼。支付:50

皇家同花顺 - 最好的扑克之手。一个10,杰克,女王,国王和王牌, 所有相同的诉讼。支付: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;

}


}

3 个答案:

答案 0 :(得分:4)

hand[]已排序(Arrays.sort(hand);),因此皇家同花顺看起来像hand[] = {10, 11, 12, 0, 1},在排序后会变为:{0, 1, 10, 11, 12}

因此,您皇家同花顺的条件的第一部分不会起作用,它可能应该是这样的:

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

而不是:

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

顺便说一句,你的算法检测到直线似乎可以解释这个问题,但你的皇家冲洗条件却没有。乍一看,我没看过细节。

在任何情况下,您的代码都是不可读的,您可以通过更人性化的方式重写代码...

答案 1 :(得分:2)

皇家同花顺只出现649,739次。您是否测试过您的程序超过一百万次以查看它是否出现?

添加一些代码以选择您获得的牌,并查看皇家同花顺的测试是否有效。

答案 2 :(得分:0)

这段代码非常可怕。我添加了几位以使其更具可读性。

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

这打印出卡片,所以我可以强行冲洗。输出如下:

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

使用这个,我添加了一些代码来强制我想要的手:

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

正如您所看到的,这只手未通过您的皇家同花顺测试。这就是它无法正常工作的原因。