A Flash Developer Resource Site

# Thread: how is this even possible??

1. ## how is this even possible??

i have this code
PHP Code:
``` var bombs = 10; mainArray = new Array(); for (var i=1; i<=10; i++) {     mainArray[i] = new Array();     for (j=1; j<=10; j++) {         mainArray[i][j] = " ";     } } for(var i = 1;i<=bombs;i++){     var randNum1 = Math.round(random(10));     var randNum2 = Math.round(random(10));     if(mainArray[i][randNum2] != "X"){         mainArray[i][randNum2] = "X";     } }     function printScreen(){     trace("############");     for(var i = 1; i<=10;i++){     trace("#"+mainArray[i]+mainArray[i]+mainArray[i]+               mainArray[i]+mainArray[i]+mainArray[i]+               mainArray[i]+mainArray[i]+mainArray[i]+               mainArray[i]+"#");     }     trace("############"); } printScreen();  ```
basically what it does is create a two dimensional array,mainArray, and give all of the values of the array " ", then it makes two random integers between 1 and 10 (the length of the array). it then checks if the array with those two numbers is equal to "X", if it is not, make it X
then prints out the array in a grid.

how am i getting more than 10 X's? i would be able to get less than 10 if it gets to an array value that already has an X, but im getting 11-15 X's
is that even possible??? please help i am so confused thanks  Reply With Quote

2. Im not full following yoru code I guess..

(sorry)

1) I dont see where you add randNum1 + randNum2 together to check it against anything?

(also checking a NUMBER to see if it equals a string (ie: "X") will always be false)

2.) I dont see: var randNum1 = Math.round(random(10)); being used ANYWHERE?

where and for what is randNum1 being used for anything?

are you trying:

1.) add two random numbers togther.. (and stick in an array)
2.) check to see if this new number matches or equals = "X" (a string value of "X"?)

and it if it DOESNT.. make that value in that index "X"..

is this correct?

are you randomly trying to check the array? liek check index 1 then index 5 (randomly?)

or are you just trying to go though eacj index in the array checking the value?

Thanks  Reply With Quote

3. heres the code again, it had a few errors sorry
PHP Code:
``` for(var i = 1;i<=bombs;i++){     var randNum1 = random(10)+1;     var randNum2 = random(10)+1;     if(mainArray[randNum1][randNum2] != "X"){         mainArray[ranNum1][randNum2] = "X";     } }  ```
ill try to explain it more clearly. this generates two random numbers between 1 and 10, then checks if my two dimensional array with those two values as (what do you call the number after an array thats in brackets? ex: array, <- that number) is equal to X, if it is not, it sets it equal to X, so its not checking to see if the random number equals X, its checking to see if the value of the array with the indicies (is that it) of those two random is X

what this is trying to accomplish is set 10 different array values equal to X
was that clear? it helps if you put the code into flash, it will stand alone  Reply With Quote

4. array Number will never be equal to "X" String. Is what whispers means.
It can be equal to X, but not to "X".

gparis  Reply With Quote

5. why? later in my code i have an if statement that returns true all of the time
PHP Code:
``` if (mainArray[i-1][j] == 'X' && mainArray[i][j] != 'X') {    mainArray[i][j] += 1;}  ```
ive found that " and ' are interchangeable, im assuiming flash can compare strings and characters  Reply With Quote

6. mainArray[i][j] = " ";

makes it a String, like "1"

Math.round a Number, like 1

gparis  Reply With Quote

7. i think im not explaining myself clearly
im getting random array indicies, i was using round to get an integer instead of a decimal value but i dont need to any more since im not using Math.random(), just random().
so it would be
value1 = random integer between 1 and 10
value2 = random integer between 1 and 10
array[value1][value2] = "X";
not
array = value1;
does that make it a little clearer? im not very good at explaining my thoughts clearly :/  Reply With Quote

8. random(10) will return a number between 0 and 9, plus there is no need to round them.

gparis  Reply With Quote

9. could be your problem stems from i=1
when arrays always start from zero.

PHP Code:
``` var bombs = 10;  mainArray = new Array();  for (var i=0; i!=10; i++) {      mainArray[i] = new Array();      for (j=0; j!=10; j++) {          mainArray[i][j] = " ";      }  }  for(var i = 0;i!=bombs;i++){      var randNum1 = Math.round(Math.random()*10); trace(randNum1);     var randNum2 = Math.round(Math.random()*10);  trace(randNum2);     if(mainArray[i][randNum2] != "X"){          mainArray[i][randNum2] = "X";      }  }      function printScreen(){      trace("############");      for(var i = 0; i!=10;i++){      trace("#"+mainArray[i]+mainArray[i]+mainArray[i]+mainArray[i]+                mainArray[i]+mainArray[i]+mainArray[i]+                mainArray[i]+mainArray[i]+mainArray[i]+"#");                      }      trace("############");  }  printScreen();  ```  Reply With Quote

#### Posting Permissions

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

 » Home » Movies » Tutorials » Submissions » Board » Links » Reviews » Feedback » Gallery » Fonts » The Lounge » Sound Loops » Sound FX » About FK » Sitemap 