...

View Full Version : 2D Array version of the dice game (help with array counter)



cocoa
07-14-2005, 04:19 PM
Hey guys, I'm new here, but I've been struggling with the code for this. I think it's a matter of where I'm putting my statements. The array is supposed to count how many times a combination of number has been rolled (ie. 1, 4 or 5, 3) For some reason nothing is being written into the array. My first for loop is supposed to run the dice toss however many times the user enters...Here's what I have.


for(var count = 0; count < roll; count++)
{
for(die1 in a)
{
for(die2 in a[die1])
{
die1 = Math.floor(1 + Math.random() * 6);
die2 = Math.floor(1 + Math.random() * 6);
++a[die1][die2];
}
}
}

Any help would be appreciated. :thumbsup:

Lerura
08-12-2005, 04:49 PM
try this:


// ----- initializing the twodimensional array and sets each value to 0 .
a=new Array;
for (x = 1; x < 7; x++){
a[x] = new Array;
for (y = 1; y < 7; y++){
a[x][y] = 0;
}}
roll=eval(input);

for(var count = 0; count < roll; count++){
die1 = Math.floor(1 + Math.random() * 6);
die2 = Math.floor(1 + Math.random() * 6);
a[die1][die2]++;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum