Originally Posted by

**Skynet928**
...more than two dice ...how many sides...]

Look at your program, and think back to the time when you were designing it.

What statements in the program depend on the number of dice being two?

What statements in the program depend on each die having six sides?

You already have variables "dice" and "sides" that you should be able to incorporate into the program in places where they make a difference. For this discussion, I'll call them

*N* and

*S*.

Well, one part is easy: Change the random number call to let the value of a single die be

*1, 2, ... , S* rather than

*1, 2, ..., 6* that you have now.

Next:

What is the range of values that you can get if you toss

*N* of those dice? That will determine the size that the

*result* array should be.

Lowest value of a single play is

*N*, highest is

*N*S*
The easiest way to accommodate this range of sums might be to declare an array with

*N*S+1* elements, knowing that

*0, 1, ..., N-1* won't be used, and the sums will be held in

*N, N+1, ... , N*S*
Then, for each play, you simply add up the

*N* dice values and store the sum in the array element with index equal to the sum. If the sum is, say 8, store it in

*result[8]*.

After a play, if you want to know and report, say, the number of sevens that were thrown, just look at

*result[7].*
If you don't want to have the useless lower values, then you should be able to use your present approach and figure out what the size of the array will be and figure out how the index depends on the sum so that it will be stored in the correct array member. (Same approach to associating an index value to retrieve a given sum.)

I mean, look at the "magic numbers" in statements like the following that depend on two six-sided dice). For example:

result = new int[11];
while (tmp < 11) {
++result[add - 2];
.
int counter = tmp + 2;

See what I mean? Where did the numbers

*11* and

*2* come from?

They depend on the number of dice and the number of sides of each die.

Start by changing the constants

*2* and

*11* to

*expressions* involving your

*dice* and

*sides* variables such that they work for 2 six-sided dice. Then try with three six-sided dice. (Set dice equal to 2 and sides equal to 6.)

Now, don't just start plugging stuff into the program and see if you get lucky. Work it out on pencil and paper to make sure the expressions make sense for different numbers of dice and different numbers of sides, then test. Maybe put some print statements in the loop that shows the individual die values and the sum. That might help you pull things together:

for (int i = 0; i < roll; i++) {
sum = 0;
for (int x = 0; x < dice; x++) {
//
// Change the next line to take into account the number of sides
int die1 = (int) (Math.random() * ???) + 1;
System.out.print(die1 + " "); // For debugging
sum += die1;
}
System.out.println("--> " + sum); // For debugging
// Put code here to increment the appropriate element of result
++result[what???]
}

Test with two six-sided dice. Test a lot. Make sure it does and reports exactly what you expect.

Change it to test with three six-sided dice. Test a lot.

Then, change to, say, two 12-sided dice and see how the values of the expressions change. Test.

Etc.

Cheers!

Z