Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jul 2005
    Location
    Chicago
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2D Array version of the dice game (help with array counter)

    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.
    Code:
                
    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.
    Last edited by cocoa; 07-14-2005 at 04:31 PM.

  • #2
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    946
    Thanks
    0
    Thanked 129 Times in 128 Posts
    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]++;
    }


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •