A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: how is this even possible??

  1. #1
    shavingcream incarnate gukinator's Avatar
    Join Date
    Jul 2008
    Location
    Santa Cruz
    Posts
    147

    how is this even possible??

    i have this code
    PHP Code:
    var bombs 10;
    mainArray = new Array();

    for (var 
    i=1i<=10i++) {
        
    mainArray[i] = new Array();
        for (
    j=1j<=10j++) {
            
    mainArray[i][j] = " ";
        }
    }
    for(var 
    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 
    1i<=10;i++){
        
    trace("#"+mainArray[i][1]+mainArray[i][2]+mainArray[i][3]+
                  
    mainArray[i][3]+mainArray[i][4]+mainArray[i][6]+
                  
    mainArray[i][5]+mainArray[i][6]+mainArray[i][9]+
                  
    mainArray[i][10]+"#");
        }
        
    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

  2. #2
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    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

  3. #3
    shavingcream incarnate gukinator's Avatar
    Join Date
    Jul 2008
    Location
    Santa Cruz
    Posts
    147
    heres the code again, it had a few errors sorry
    PHP Code:
    for(var 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[1], <- 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

  4. #4
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    array[1] Number will never be equal to "X" String. Is what whispers means.
    It can be equal to X, but not to "X".

    gparis

  5. #5
    shavingcream incarnate gukinator's Avatar
    Join Date
    Jul 2008
    Location
    Santa Cruz
    Posts
    147
    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

  6. #6
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    mainArray[i][j] = " ";

    makes it a String, like "1"

    Math.round a Number, like 1

    gparis

  7. #7
    shavingcream incarnate gukinator's Avatar
    Join Date
    Jul 2008
    Location
    Santa Cruz
    Posts
    147
    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[1][2] = value1;
    does that make it a little clearer? im not very good at explaining my thoughts clearly :/

  8. #8
    Senior Member
    Join Date
    Aug 2000
    Location
    Montréal
    Posts
    14,141
    random(10) will return a number between 0 and 9, plus there is no need to round them.

    gparis

  9. #9
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    could be your problem stems from i=1
    when arrays always start from zero.

    does this method address your problem ?

    PHP Code:
    var bombs 10
    mainArray = new Array(); 

    for (var 
    i=0i!=10i++) { 
        
    mainArray[i] = new Array(); 
        for (
    j=0j!=10j++) { 
            
    mainArray[i][j] = " "
        } 

    for(var 
    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 
    0i!=10;i++){ 
        
    trace("#"+mainArray[i][0]+mainArray[i][1]+mainArray[i][2]+mainArray[i][3]+ 
                  
    mainArray[i][3]+mainArray[i][4]+mainArray[i][6]+ 
                  
    mainArray[i][5]+mainArray[i][6]+mainArray[i][9]+"#"); 
                   
        } 
        
    trace("############"); 

    printScreen(); 

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center