2013-03-25 23 views
0

試圖獲得這些算法來評估一個數組,填充範圍從1到6的五個隨機選擇的整數。不幸的是,它只會返回高位和一對選項。當我擲出兩雙,三種等等時,我如何獲得更高的分數?骰子撲克分數算法?

private static int[] getCounts(int[] dice) { 

    int[] counts = new int[6]; 
    String resValue = ""; 
    for (int i = 0; i < dice.length; i++) { 
     if (dice[i] == 1) { 
      counts[0]++; 
     } else if (dice[i] == 2) { 
      counts[1]++; 
     } else if (dice[i] == 3) { 
      counts[2]++; 
     } else if (dice[i] == 4) { 
      counts[3]++; 
     } else if (dice[i] == 5) { 
      counts[4]++; 
     } else if (dice[i] == 6) { 
      counts[5]++; 
     } 
    } 
    return counts; 
} 

private static String getResult(int[] dice) { 
    int[] counts = getCounts(dice); 
    String resValue = " "; 

    for (int i = 0; i < counts.length; i++) { 
     if (counts[i] == 5) { 
      resValue = "Five of a kind "; 
     } else if (counts[i] == 4) { 
      resValue = "Four of a kind "; 
     } else if (counts[i] == 3) { 
      for (int j = 0; j < counts.length; j++) { 
       if (counts[j] == 2) { 
        resValue = "Full House "; 
       } 
      } 
      resValue = "Three of a Kind "; 
     } else if (counts[i] == 2) { 
      for (int j = 0; j < counts.length; j++) { 
       if (counts[j] == 2) { 
        resValue = "Two Pairs "; 
       } 
      } 
      resValue = "One Pair "; 
     } else { 
      resValue = "Highest Card "; 
     } 
    } 
    return resValue; 
} 
+0

不知道你problem..but解決這個環節將是採用全爲你.. http://probabilityandstats.wordpress.com/2010/04/28/the-game-of-poker-dice-and-the-multinomial-theorem/ – 2013-03-25 04:51:54

+1

你應該使用'counts [dice [i] - 1] ++'而不是6 if-else聲明。 – Shivam 2013-03-25 06:11:07

回答

0

你如果有其他範圍廣。即使我們之前發現更高的resValue,最後一個else也會執行。要麼添加檢查,查看新的resValue是否更高,要麼在每個子句的末尾添加中斷。

編輯:在這裏,它的工作對我來說:

private static int[] getCounts(int[] dice) { 
     int[] counts = new int[6]; 
     String resValue = ""; 
     for (int i = 0; i < dice.length; i++) { 
      if (dice[i] == 1) { 
       counts[0]++; 
      } else if (dice[i] == 2) { 
       counts[1]++; 
      } else if (dice[i] == 3) { 
       counts[2]++; 
      } else if (dice[i] == 4) { 
       counts[3]++; 
      } else if (dice[i] == 5) { 
       counts[4]++; 
      } else if (dice[i] == 6) { 
       counts[5]++; 
      } 
     } 
     return counts; 
    } 

    private static String getResult(int[] dice) { 
     int[] counts = getCounts(dice); 
     String resValue = " "; 

     for (int i = 0; i < counts.length; i++) { 
      if (counts[i] == 5) { 
       resValue = "Five of a kind "; 
       break; 
      } else if (counts[i] == 4) { 
       resValue = "Four of a kind "; break; 
      } else if (counts[i] == 3) { 
       resValue = "Three of a Kind "; 
       for (int j = 0; j < counts.length; j++) { 
        if (counts[j]==2) { 
         resValue = "Full House "; break; 
        } 
       } 
       break; 
      } else if (counts[i] == 2) { 
       resValue = "One Pair "; 
       for (int j = 0; j < counts.length; j++) { 
        if (counts[j] == 2) { 
         resValue = "Two Pairs "; break; 
        } 
       } 
       break; 
      } else { 
       resValue = "Highest Card "; 
      } 
     } 
     return resValue; 
    }