#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Problem about calculate adjacent mines


    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:
    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 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>';
    			
    			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 () {
        document.getElementById('td1').innerHTML = "Some text to enter";
    }
  2. #2
  3. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    Code:
    if (addmines.toString().indexOf(id) == -1) {
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep 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
  6. #4
  7. JavaScript is not spelt java
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2011
    Location
    Landan, England
    Posts
    743
    Rep Power
    165
    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 6th, 2013 at 11:24 PM.
    "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

IMN logo majestic logo threadwatch logo seochat tools logo