对不起,可能会令人困惑的标题,这是我可以想出来描述我遇到的问题最好的。无论如何,下至黄铜钉:我正在开发一个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 }
}
这将是巨大的,如果有人可以帮助我了解我要去哪里错了或者我应该如何改变我考虑如何做到这一点;我不是要求得到一个答案,我只是想帮助我理解如何到达那里。提前致谢。
滚动五个骰子后,尝试排序值。这可以很容易地识别出一种类型和直线。 –