2013-12-15 110 views
-1

我正在研究這個撲克遊戲項目,並且我一直在關於如何在功能結束時打印出卡類型和默認的「抱歉,你失去了」。我不確定我的代碼是否正確,我會很感激任何類型的幫助。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] 

我無法打印類型(Royal Flush部分)。

/** 
* 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; 
} 
+4

考慮改進你的問題,告訴我們你的程序應該如何工作,你的代碼是如何工作的 - 通過它來引導我們,它做什麼不正確,...等等... –

+0

I'對不起,沒有徹底解釋。基本上我必須打印出我的撲克牌遊戲的牌類型。例如,「[黑桃,10黑桃,Q黑桃,J黑桃,K黑桃] Royal Flush!」但我的程序只打印出[A黑桃,10黑桃,Q黑桃,J黑桃,K黑桃]部分。我不知道如何打印出Royal Flush部分......這是否合理?我不知道如何解釋它 – LC13

+1

你在哪裏試圖打印出Royal Flush部分? – Houseman

回答

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。有幾種方法可以實現邏輯的可讀性和/或減少鍵入和其他問題,但按照該順序的陳述是合理的。他們每個只執行一個語句,所以你可以不用括號。

+0

謝謝你的幫助! – LC13