Thursday, 22 August 2013

javascript minesweeper expand messing up counter

javascript minesweeper expand messing up counter

i made a minesweeper game in javascript, which was finally running pretty
smoothly, until i added the "expand()" function, (see below) i have 3
issues:
when it expands it adds too many to "flippedCount" ( see code below ) - in
the image below the div to the right displays "flippedCount", and its 39
instead of 35.
as a result if a player exceeds 90 squares ( amount to win ) during a
"expand()" the winning screen doesnt show.
it also doesnt expand properly, ( see images below ).
the relevant code and a link is below these 2 images


var flippedCount = 0;
var alreadySetAsZero = [];
var columnAmount = 10;
function processClick(clicked) { //i use a "(this)" to pass as "(clicked)"
nextToBombCheck( parseInt(clicked.id) );
checkWin();
}
nextToBombCheck( boxNum ) {
flippedCount++;
document.getElementById("flipped").innerHTML = flippedCount;
//long function setting "bombCount" to # bombs around clicked square goes
here
if ( bombCount !== 0 ) { //blah blah blah
} else {
alreadySetAsZero[ boxNum ] = "yes";
expand(boxNum);
}
}
function expand( emptyBoxId ) {
checkRightOfEmpty( emptyBoxId + 1 );
checkLeftOfEmpty( emptyBoxId - 1 );
checkAboveEmpty( emptyBoxId - columnAmount );
checkBelowEmpty( emptyBoxId + columnAmount );
}
function checkRightOfEmpty( boxToTheRightId ) {
//check if already marked as zero
if ( alreadySetAsZero[ boxToTheRightId ] === "yes" )
return;
//if box is at the edge
if ( boxToTheRightId % columnAmount === ( 0 ) ) {
//do nothing
} else {
nextToBombCheck( boxToTheRightId );
}
}
//and the rest are 3 similar functions
i was not able to find a pattern with the lack of expansion, or numbers
added to flipped count.
link here
p.s. sorry about the title i dont know what else to call it

No comments:

Post a Comment