2016-09-22 49 views
0

对不起,可能会令人困惑的标题,这是我可以想出来描述我遇到的问题最好的。无论如何,下至黄铜钉:我正在开发一个Java任务(是的,这是课堂作业,听我说)来创建一个名为FiveDice2.java的骰子游戏。我需要做的是让它为每个CPU和人类玩家分别掷出五个骰子,然后通过确定他们是否有一对,两个一类,三个等等来确定哪个“玩家”赢得了游戏。我的问题在于最后一部分。我似乎无法了解如何真正实现这一目标。如何查找随机数组中有多少个值匹配?

我最好的想法是创建一个所有可能的滚动值的数组,1-6,并将骰子滚动与该数组进行比较,并跟踪每个数字在单独变量中获得的匹配数量(int ones, int twos,int threes等),但是我越想到它,就越意识到需要进行多少验证,而且我不禁感到自己并没有走上正轨。

我最好的尝试去有点像这样:

public class FiveDice2 
 
{ 
 
\t public static void main(String[] args) 
 
\t { 
 
     //Declare arrays for cpu and player dice 
 
     Die[] cpuDice = new Die[5]; 
 
     Die[] playerDice = new Die[5]; 
 
     int possibleValues = new int[]{1,2,3,4,5,6}; 
 
     int ones, twos, threes, fours, fives, sixes; 
 
     int cpuHand, playerHand; 
 
     
 
\t \t //Instantiate five Dice objects for the "CPU" 
 
     for(cpu = 0; cpu < cpuDice.length; cpu++) 
 
     { 
 
      cpuDice[cpu] = new Die(); 
 
     } 
 

 
\t \t //Instantiate five more Dice objects for the player 
 
     for(p = 0; p < cpuDice.length; p++) 
 
     { 
 
      playerDice[p] = new Die(); 
 
     } 
 

 
\t \t //Print rules of game to the console 
 
\t \t System.out.println("Dice Game"); 
 
\t \t System.out.println("Two sets of dice will be thrown."); 
 
\t \t System.out.println("Winner is determined by who has the better \"hand\" of dice."); 
 
\t \t System.out.println("Each combination in the table beats all the ones below it: "); 
 
\t \t System.out.println("-Five of a kind"); 
 
\t \t System.out.println("-Four of a kind"); 
 
\t \t System.out.println("-Three of a kind"); 
 
\t \t System.out.println("-Pair"); 
 
\t \t System.out.println("If none of the above, whoever has the highest total wins."); 
 

 
\t \t //Output values of the "CPU's" rolls 
 
     System.out.println("CPU dice:"); 
 
     for(cpu = 0; cpu < cpuDice.length; cpu++) 
 
     { 
 
      System.out.println(cpuDice[cpu].getDieValue()); 
 
     } 
 
     
 
     System.out.println(); 
 

 
\t \t //Output values of the player's rolls 
 
     System.out.println("Player dice:"); 
 
     for(p = 0; p < playerDice.length; p++) 
 
     { 
 
      System.out.println(playerDice[p].getDieValue()); 
 
     } 
 
     
 
     System.out.println(); 
 
     
 
     for(int i = 0; i < possibleValues.length; ++i) 
 
     { 
 
       if(cpuDice[i] == possibleValues[i]) 
 
       { 
 
        if(cpuDice[i] == 1) 
 
        { 
 
         ones++; 
 
        } 
 
        if(cpuDice[i] == 2) 
 
        { 
 
         twos++; 
 
        } 
 
        if(cpuDice[i] == 3) 
 
        { 
 
         threes++; 
 
        } 
 
        if(cpuDice[i] == 4) 
 
        { 
 
         fours++; 
 
        } 
 
        if(cpuDice[i] == 5) 
 
        { 
 
         fives++; 
 
        } 
 
        if(cpuDice[i] == 6) 
 
        { 
 
         sixes++; 
 
        } 
 
       } 
 
      
 
       if(playerDice[i] == possibleValues[i]) 
 
       { 
 
        if(playerDice[i] == 1) 
 
        { 
 
         ones++; 
 
        } 
 
        if(playerDice[i] == 2) 
 
        { 
 
         twos++; 
 
        } 
 
        if(playerDice[i] == 3) 
 
        { 
 
         threes++; 
 
        } 
 
        if(playerDice[i] == 4) 
 
        { 
 
         fours++; 
 
        } 
 
        if(playerDice[i] == 5) 
 
        { 
 
         fives++; 
 
        } 
 
        if(playerDice[i] == 6) 
 
        { 
 
         sixes++; 
 
        } 
 
       } 
 
     } 
 
\t } 
 
}

这将是巨大的,如果有人可以帮助我了解我要去哪里错了或者我应该如何改变我考虑如何做到这一点;我不是要求得到一个答案,我只是想帮助我理解如何到达那里。提前致谢。

+1

滚动五个骰子后,尝试排序值。这可以很容易地识别出一种类型和直线。 –

回答

1

嗯,首先:

int possibleValues = new int[]{1,2,3,4,5,6}; 

你不需要该数组。任何指数i的数值仅为i+1--所以只需在需要的地方使用i+1即可。在另一方面,

int ones, twos, threes, fours, fives, sixes; 

这是一个数组是什么。而不是有六个单独的变量,你可以有一个单一的数组;是这样的:

int [] valueCounts = new int[6]; 

的想法是,那些的数量将是valueCounts[0],三三两两的将是valueCounts[1],等等。不过,我假设你真的想要分别统计玩家骰子和CPU骰子;因此,你应该或者使用两个单独的阵列或二维阵列:

int [] playerValueCounts = new int[6]; 
    int [] cpuValueCounts = new int[6]; 
    // or just: 
    int [][] valueCounts = new int[2][6]; 

(在后一种情况下,valueCounts[0]表示6个值计数的播放器,并且valueCounts[1]表示CPU的6个值countes - 但是如果你还没有遇到多维数组,那么就去第一个选项)。

代替

然后:

   if(cpuDice[i] == 1) 
       { 
        ones++; 
       } 
       if(cpuDice[i] == 2) 
       { 
        twos++; 
       } 

...等,只是做:

cpuValueCounts[cpuDice[i].getDieValue() - 1]++; 

其实,你并不需要这个循环:

for(int i = 0; i < possibleValues.length; ++i) 

相反,你应该通过模具本身的循环:

for(int i = 0; i < playerDice.length; ++i) 

的身体该环可以从字面上用两行代替:

playerValueCounts[playerDice[i].getDieValue() - 1]++; 
cpuValueCounts[cpuDice[i].getDieValue() - 1]++; 

这会依次递增每个玩家和CPU死亡的适当值的计数。现在,确定“N种”的N的问题只是循环通过playerValueCounts(然后是cpuValueCounts)并寻找最高值 - 也就是最经常出现的值计数。

+0

非常感谢,这让我有了我需要的地方。我想我缺少类似于valueCounts数组的东西。 –