I am currently working on a minesweeper game function about calculate adjacent mines,i have done the fully code now, but i might some error inside itso when i open up the page, there's no number came out.My target is when i open up a browser and there will be ten mines withnumber around it can someone tell me what i should correct? this is the code to lay 10 random mines,it working good: Code:
function addmines () { var mine = 9; var mineNumber = 0; var n = 10; //number of mines while (n>0) { var row = Math.floor(Math.random()*9); var col = Math.floor(Math.random()*9); var id = "r" + row + "c" + col; var td = document.getElementById(id); if (td.children.length > 0) continue; //there's already a mine here n--; var a = document.createElement("img"); a.src = "mine32.gif"; a.height = 30; a.width = 30; a.id = 'mine' + mineNumber; td.appendChild(a); mineNumber++; }}
The code below is the main problemi had made a little function to test if the loop works or not,and i can see it is working but there is still no number appear. Code:
function neighbours () { var dr = [-1, -1, 0, 1, 1, 1, 0, -1]; var dc = [ 0, 1, 1, 1, 0, -1, -1, -1]; var newRow = 9; var newCol = 9; for (var row=0; row<newRow; row++) { //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<newCol; col++) { //document.getElementById('r1c1').innerHTML = '<span class="label4">4</span>'; var id = 'r' + row + 'c' + col; //document.getElementById(id).innerHTML = '<span class="label4">4</span>'; if (addmines.toString().indexOf(id) == -1) { var counter = 0; //looping over for (var i=0; i<dr.length; i++) { /* //CODE GIVES MEGA ISSUES if(row+dr[i]>0 && col+dc[i]>0 && row+dr[i]<=newRow && col+dc[i]<=newCol) { //document.getElementById(id).innerHTML = '<span class="label4">4</span>'; DOES NOT WORK IN HERE var id = 'r' + (row+dr[i]) + 'c' + (col+dc[i]); if (mines.toString().indexOf(id) != -1) { counter++; } //END IF } //END IF //END OF PROBLEM CODE */ document.getElementById(id).innerHTML = '<span class="label4">4</span>'; //WORKS IF PROBLEM AREA IS DISABLED var id = 'r' + r + 'c' + c; var obj = document.getElementById(id); obj.firstChild.nodeValue = count; //value obj.className += ' m' + count; //style } //END FOR } //END IF } //END FOR }} function insertText () {