#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Research about Randomising , need assistance here.


    I'm currently doing some research about random number ,
    my plan is to create a table which lay ten mines everytime i refresh it,
    i try a lot of different way to fix bugs ,
    but it still not able to lay ten mines every times.
    I realize i miss "end if" , "end while",
    but when we put it in the code the function will stop working.

    My current code looks like this:

    Code:
    <script type="text/javascript">
    function addmines (x) {
    var n = 10; //number of mines
    var mines = []; //an empty array
    while (n>0) {
    var row = Math.floor(1+Math.random()*10);
    var col = Math.floor(1+Math.random()*10);
    
    if (row >= 0 && row < 10){
    if (col >= 0 && col < 10) {
    var id = "r"+ row + "c" + col;
    
    n++;
    
    var a = document.createElement("img");
    a.src = "mine32.gif";
    a.height = 30;
    a.width = 30;
    document.getElementById(id).appendChild(a);
    
    }
    }
    
    //n++;
    
    }
    return x;
    }
    
    </script>
    
    </head>
    
    <body>
    <h1>Minesweeper</h1>
    
    
    
    <script language="JavaScript" type="text/javascript">
    
    //Creates Table
    document.write('<table>');
    //for loop that creates the row, increments from 0 to 9 each time it reiterates
    for (var row=0; row<9; row++) {
    //creates the table row
    document.write('<tr>');
    //a nested for loop that reiterates 9 times creating the <td>'s and then exits to the parent for loop.
    for (var col=0; col<9; col++) {
    //references all the <td>'s with a specific id attribute.
    document.write('<td id="r', row, 'c', col, '">');
    //document.write('&nbsp;');
    //closes the <td>
    document.write('</td>');
    }
    //closes the <tr> or row in plain English
    document.write('</tr>');
    }
    //closes the <table> tag
    document.write('</table>');
    
    addmines();
    
    </script>
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Your for loop does not have enough iterations. With the current logic, it applies the counter after a row has been drawn, so when it comes around on the 10th loop, "row" equals 9 and the loop stops never drawing the 10th row. You need to use "row < 10" or change your logic. The same applies for your columns.

    Is it your intention to allow for more than one mine in each cell?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    you talkin about "row < 10" ,
    did u mean my if statement?

    i think i'd like to avoid the overlap as well
    do my code have any problem about it?
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,009
    Rep Power
    2791
    Sorry about the late reply, I've been away for the last week. Have you made any progress?
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    74
    Rep Power
    18
    Originally Posted by Winters
    Sorry about the late reply, I've been away for the last week. Have you made any progress?
    I would advise to use a for-loop, rather than a while-loop. A for loop usually iterates over a fixed length (in this case, n=10), a while-loop usually iterates until a certain condition is not met anymore.
    Besides that: your code will generate overlap, i.e. multiple bombs can end on a similar cell. This is due to this line of code:
    Code:
    var row = Math.floor(1+Math.random()*10);
    var col = Math.floor(1+Math.random()*10);
    in each iteration, these values can become the same (since it is random). Another way is to create a list, with the indices of all cells, counting horizontally, so for example if you have a 10x10 grid, then you make a list [1....100]. Then you shuffle that list, pop the first 10 (or n) elements, and then convert these indices back to cell coordinates.

    Also, the return 'x' is not needed at the end, in fact the whole x argument is never used.

IMN logo majestic logo threadwatch logo seochat tools logo