Java扑克游戏计划

时间:2013-12-15 22:21:23

标签: java

我正在研究这个扑克游戏项目,我一直坚持如何打印出卡片类型以及功能结束时的默认“对不起,你输了”。我不确定我的代码是否正确,我将不胜感激任何类型的帮助。

基本上,我的程序需要打印出来:

       [A Spades, 10 Spades, Q Spades, J Spades, K Spades] 
       Royal Flush!
         --------------------------------------------------

        [9 Spades, 10 Spades, Q Spades, J Spades, K Spades]
         Straight Flush!
         --------------------------------------------------
等等......

但我的程序只打印出来:

          [A Spades, 10 Spades, Q Spades, J Spades, K Spades]

我在打印类型(皇家同花顺部分)时遇到问题。

/**
 * Check current currentHand using multipliers and goodHandTypes arrays Must
 * print yourHandType (default is "Sorry, you lost") at the end of function.
 * This can be checked by testCheckHands() and main() method.
 */
private void checkHands() {
    // implement this method!
  List<Card> sortedHand = new ArrayList<Card>(currentHand);
  Collections.sort(sortedHand, new Comparator<Card>() {

@Override

public int compare(Card card1, Card card2) {
    int rank1 = card1.getRank();
    int rank2 = card2.getRank();

    if (rank1 > rank2) {
        return 1;
    }

    if (rank1 < rank2){
        return -1;
    }



    return 0;
}

  }


    int rank = 0;
    String ranks;

    if (isRoyalPair() == true) {
        rank = 1;
     if (isTwoPair() == true) {
        rank = 2;
    }
    if (isThreeOfAKind() == true) {
        rank = 3;
    }

    if (isStraight() == true) {
        rank = 4;
    }
    if (isFlush() == true) {
        rank = 5;
    }
    if (isFullHouse() == true) {
        rank = 6;
    }
    if (isFourOfAKind() == true) {
        rank = 7;
    }
    if (isStraightFlush() == true) {
        rank = 8;
    }
    if (isRoyalFlush() == true) {
        rank = 9;
    }
    }

    rank -= 1;
    if (rank < 0) {
        ranks = "Sorry, you lost!";

    } else {
        ranks = goodHandTypes[rank];
    }

    System.out.println("" + ranks);



    switch (ranks) {
       case "1":
            this.balance += (this.bet * multipliers[0]);
            break;
        case "2":
           this.balance += (this.bet * multipliers[1]);
            break;
        case "3":
           this.balance += (this.bet * multipliers[2]);
            break;
       case "4":
           this.balance += (this.bet * multipliers[3]);
            break;
        case "5":
           this.balance += (this.bet * multipliers[4]);
            break;
        case "6":
            this.balance += (this.bet * multipliers[5]);
           break;
        case "7":
            this.balance += (this.bet * multipliers[6]);
           break;
        case "8":
            this.balance += (this.bet * multipliers[7]);
            break;
        case "9":
            this.balance += (this.bet * multipliers[8]);
            break;
        default:
            break;


            }
          }

我已经拥有了他们的所有方法,我只需要帮助找到打印它们的方法:

    private boolean isStraight() {

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

        if (currentHand.get(i).getRank() != currentHand.get(i + 1).getRank()) {

            return false;

        }

    }

    return true;

}

private boolean isFlush() {
    for (int i = 0; i < numberOfCards; i++) {
        if (currentHand.get(i).getSuit() != currentHand.get(i + 1).getSuit()) {
            return false;
        }

    }

    return true;
}

private boolean isStraightFlush() {
    if (isStraight() == true && isFlush() == true) {
        return true;
    }
    return false;
}

private boolean isRoyalFlush() {
    if (isFlush() == false || isStraight() == false) {
        return false;
    } else {
        if (currentHand.get(0).getRank() == 10) {
            return true;
        }
        return false;
    }

}

private boolean isFourOfAKind() {

    //runs thru the hand for exactly 4 matches

    for (int i = 0; i < numberOfCards - 1; i++) {

        int counter = 0;

        for (int y = i + 1; y < numberOfCards; y++) {

            if (currentHand.get(i).getRank() == currentHand.get(0).getRank()) {

                counter++;
            }

        }

        if (counter == 4) {

            return true;

        } else {
            return false;
        }

    }
    return false;
}

private boolean isFullHouse() {
    if (isThreeOfAKind() == true && isOnePair() == true) {
        return true;
    } else {
        return false;
    }
}

private boolean isThreeOfAKind() {


    //matches three

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

        int counter = 0;

        for (int y = 0; y < numberOfCards; y++) {

            if (currentHand.get(i).getRank() == currentHand.get(y).getRank()) {

                counter++;

                for (int x = 0; x < numberOfCards; x++) {

                    if (currentHand.get(i).getRank() == currentHand.get(x).getRank()) {

                        counter++;
                    }
                }


                if (counter == 3) {
                    return true;


                } else {


                    return false;





                }
            }



private boolean isTwoPair() {
    //check if it is four of a kind or two pair))
    if (isFourOfAKind() == true) {
        return false;
    }
    int numberOfPair = 0;
    int counter = 1;

    return false;
}

private boolean isOnePair() {
    return false;
}

public boolean isRoyalPair() {

    if (isOnePair() == false && isTwoPair() == false && isThreeOfAKind() == false && isFourOfAKind() == false
            && isFullHouse() == false && isRoyalFlush() == false && isFlush() == false && isStraight() == false && isStraightFlush() == false) {

        return true;
    }



    return false;
}

1 个答案:

答案 0 :(得分:1)

看起来checkHands()没有被调用!

稍后...

 Collections.sort(sortedHand, new Comparator<Card>() {

@Override

public int compare(Card card1, Card card2) {
int rank1 = card1.getRank();
int rank2 = card2.getRank();

if (rank1 > rank2) {
    return 1;
}

if (rank1 < rank2){
    return -1;
}



return 0;
}

  }   /* Should be  );  here */ 


int rank = 0;
...

不太重要

if (isRoyalPair() == true) {
    rank = 1;
 if (isTwoPair() == true) {
    rank = 2;
}
...

手牌(一套5张牌)不应该有排名,(至少如果你要调用每张牌的排名,则不会有排名)。双手应该有一个名字,或者更确切地说是String,以获得这些牌的最佳牌局。例如,

String type = "none";
...
if (isRoyalFlush()) type = "Royal Flush!!!!!";
//or   if(isRoyalFlush()) type = goodHandTypes[9];

请注意,没有必要将其检查的内容truetrue进行比较。有几种方法可以实现逻辑的可读性和/或更少的输入和其他问题,但是这个顺序的9 if语句是合理的。而且他们每个只执行一个语句,所以你可以不用括号。

相关问题