javascript - Make sure that my for loop generates exactly 8 unique number pairs -


it's minesweeper game. objective here generate exact number of mines. in case have 8x8 grid of boxes 10 mines (see @ bottom). so, nested loop generates 64 objects x , y coordinates (for later drawing grid) , state property indicate whether field mined. then, in generatebombs generate 10 objects state:mined random x , y overwrite 8 of 64 objects in boxes array randomly , plant mines. problem approach here there possibility of 2 non-unique pairs of x , y objects generated, , way i'll end less original number of mines, because same object overwritten twice. approach here?

also, 1 of requirements generator use helper function mines, take same arguments, need might defeated.

var minesweeper = {   boxes: [],   //rows   boxesnum: 0,   bombsnum: 0,    //creates 8x8 grid    generatemap: function (width, height, bombsnum) {     (i = 1; < height; i++) {         this.boxes.push({             x: i,             y: 1,             state: "safe"         });         (j = 1; j < width; j++) {             this.boxes.push({                 x: 1,                 y: j,                 state: "safe"             });         }     }     this.generatebombs(width, height, bombsnum)   },   //mines random fields grid   generatebombs: function (width, height, bombsnum) {       (k = 0; k < bombsnum; k++) {           this.boxes.push({              x: math.floor(math.random() * width + 1),              y: math.floor(math.random() * height + 1),              state: "mined"           });       }    } }  minesweeper.generatemap(8, 8, 10); 

you'd better off working on array of boxes itself, rather generating bombs first.

generatebombs: function (width, height, bombsnum) {     var bombcount = 0;                          // count how many bombs planted,     while(bombcount < 10){                      // loop until have 10 bombs,         var index = parseint(math.random() * this.boxes.length + 1); // random box id,         if(this.boxes[index].state === "safe"){ // if box safe, plant bomb.             this.boxes[index].state = "mined";             bombcount++;                        // increase bomb count 1.         }     } } 

this method guarantee have 10 bombs planted @ 10 different locations. could select same box twice, if so, tries again.

in best case scenario, have 10 iterations of loop, compared other methods require arrays of coordinates checked every bomb want generate.

however, there's 1 problem method of randomly picking bomb position:
if increase bomb count, method hit more , more boxes bombs have been planted, resulting in exponential increase of iterations required plant many bombs want. basically, denser field is, more function randomly select cell has bomb, it'd have try again.

i don't expect noticeable @ bomb counts of, say, 50% or lower, though.


your generatemap function broken. try instead:

generatemap: function (width, height, bombsnum) {     (var = 0; < width; i++) {         (var j = 0; j < height; j++) {             this.boxes.push({                 x: (i + 1),                 y: (j + 1),                 state: "safe"             });         }     }     this.generatebombs(width, height, bombsnum) }, 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -