Search code examples
javaarraysdice

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


Sorry for the possibly confusing title, that was the best I could come up with to describe the problem I'm having.

Anyway, down to brass tacks: I am working on a Java assignment(yes, this is classwork, hear me out) to create a dice game, called FiveDice2.java. What I need to do is have it roll five dice each for a CPU and human player, and then determine which "player" won the game, by determining if they have a pair, two of a kind, three of a kind, etc. My problem lies in that last part. I can't seem to get an idea of how to actually accomplish that goal.

My best idea was to create an array of all the possible roll values, 1-6, and compare the dice rolls to that array, and keep track of how many matches each number got in a separate variable(int ones, int twos, int threes, etc.), but the more I thought about it, the more I realized how much validation that would take, and I can't help but feel I'm not on the right track.

My best attempt goes a little something like this:

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

It would be great if someone could help me understand where I'm going wrong or how I should be changing my thinking about how to do this; I'm not asking to be hand-fed an answer, I'd just like some assistance in understanding how to get there myself. Thanks in advance.


Solution

  • Well, for starters:

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

    You don't need that array. The value at any index i is just i+1 - so just use i+1 where you need it. On the other hand,

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

    This is what an array is for. Rather than having six separate variables, you could have a single array; something like:

        int [] valueCounts = new int[6];
    

    The idea is that the count of ones would be in valueCounts[0], of twos would be in valueCounts[1], and so on. However, I assume you really want to count the player dice and CPU dice separately; therefore, you should either use two separate arrays, or a two dimensional array:

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

    (In the latter case, valueCounts[0] represents the 6 value counts for the player, and valueCounts[1] represents the 6 value countes for the CPU - but if you haven't come across multidimensional arrays yet, just go with the first option).

    Then, instead of:

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

    ... etc, just do:

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

    In fact, you don't need this loop:

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

    Instead, you should loop through the dies themselves:

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

    The body of the loop can literally be replaced with two lines:

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

    This will increment the count of the appropriate value for each player and CPU die in turn. Now, the problem of determining the N for "N-of-a-kind" is just a matter of looping through the playerValueCounts (and then the cpuValueCounts) and looking for the highest value - that is, the value count which occurs most often.