Jump to content

Problem about calculate adjacent mines

Recommended Posts

I am currently working on a minesweeper gameicon1.png 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 () {

Edited by anson920520
Link to post
Share on other sites

I don't know if I spotted everything, but here are two things that I noticed.


This worries me. If addmines is the function at the top of the post, then first it has no return value to convert to a string, and second you're not executing the function because you have no (parens).

var id = 'r' + r + 'c' + c;

As far as I can tell (but I'm half-blind) there are no vars called r or c. Did you mean row and col?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...