Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
January 6th, 2013, 12:31 PM
 anson920520
Registered User

Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 27 m 32 sec
Reputation Power: 0

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 it
so 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 with
number around it

can someone tell me what i should correct?

this is the code to lay 10 random mines,
it working good:
Code:
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 problem
i 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>';

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 () {
document.getElementById('td1').innerHTML = "Some text to enter";
}

#2
January 6th, 2013, 07:49 PM
 AndrewSW
JavaScript is not spelt java

Join Date: Feb 2011
Location: Landan, England
Posts: 743
Time spent in forums: 2 Weeks 1 Day 23 h 14 m 42 sec
Reputation Power: 164
Code:

addmines is a function; is it correct that you want to use toString() on a function?

You use similar code on something called mines; is this mines correctly defined somewhere?
__________________
"The mysql extension is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used." the docs

#3
January 6th, 2013, 09:04 PM
 Darianthomson
Registered User

Join Date: Jan 2013
Posts: 1
Time spent in forums: 9 m 37 sec
Reputation Power: 0
Hi,

I'm the other team member on this, thanks that fixes an issue we would of later had but one we can't seem to make this line work

if(row+dr[i]>0 && col+dc[i]>0 && row+dr[i]<=newRow && col+dc[i]<=newCol) {

do you have any ideas?

thanks

Darian

#4
January 7th, 2013, 12:19 AM
 AndrewSW
JavaScript is not spelt java

Join Date: Feb 2011
Location: Landan, England
Posts: 743
Time spent in forums: 2 Weeks 1 Day 23 h 14 m 42 sec
Reputation Power: 164
You'll have to use the browsers' console to debug it. Set a breakpoint, step through it, output values:

console.log(row, col, dr[i]); // etc.

It may be that you need to use >= 0.

Last edited by AndrewSW : January 7th, 2013 at 12:24 AM.

 Viewing: Dev Shed Forums > Web Design > JavaScript Development > Homework - Problem about calculate adjacent mines