如何找到随机数数组中有多少个值匹配?

How to find how many values in an array of random numbers match?

抱歉标题可能令人困惑,这是我能想到的最好的描述我遇到的问题的标题。

总之,回到正题:我正在做一项 Java 作业(是的,这是课堂作业,听我说完)来创建一个名为 FiveDice2.java 的骰子游戏。我需要做的是让它为 CPU 和人类玩家各掷五个骰子,然后确定哪个 "player" 赢得了比赛,通过确定他们是否有一对,两个同类,三个等等。我的问题在于最后一部分。我似乎不知道如何真正实现该目标。

我最好的想法是创建一个包含所有可能掷骰值 1-6 的数组,并将骰子掷骰与该数组进行比较,并在单独的变量(整数)中跟踪每个数字获得的匹配次数, int twos, int threes, etc.), 但我越想越意识到这需要多少验证,我不禁觉得我不在正确的轨道上。

我最好的尝试是这样的:

public class FiveDice2
{
 public static void main(String[] args)
 {
        //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;
        
  //Instantiate five Dice objects for the "CPU"
        for(cpu = 0; cpu < cpuDice.length; cpu++)
        {
            cpuDice[cpu] = new Die();
        }

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

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

  //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();

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

如果有人能帮助我理解我哪里出错了或者我应该如何改变我对如何做这件事的想法,那就太好了;我并不是要成为 hand-fed 的答案,我只是想获得一些帮助,帮助我自己了解如何到达那里。提前致谢。

嗯,对于初学者来说:

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

你不需要那个数组。任何索引 i 处的值只是 i+1 - 因此只需在需要的地方使用 i+1 即可。另一方面,

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

这就是数组 for。您可以拥有一个数组,而不是六个独立的变量;类似于:

    int [] valueCounts = new int[6];

我们的想法是,1 的计数在 valueCounts[0] 中,2 的计数在 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 个值计数 - 但如果您没有还没有遇到多维数组,就用第一个选项吧。

然后,而不是:

               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-of-a-kind" 的 N 的问题只是循环遍历 playerValueCounts(然后是 cpuValueCounts)并寻找最大值 - 即值计数这是最常发生的。