Click to See Complete Forum and Search --> : 15 slide game only sliding once


X_X
01-10-2004, 09:36 AM
The code below seems to run through and stop working. I was expecting the pointing from DrawBoard() to Move() and vice versa to allow the game to work. I have removed all of the randomisation so the code is hopefully not too large.
-------------------
<html>
<title>15</title>
<head>
</head>
<body>
<script language=javascript>

var n = 4;

board = new Array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15);

DrawBoard();

function DrawBoard(){
var y , x ;

for ( y = 0 ; y < n ; ++y) {
for ( x = 0 ; x < n ; ++x) {
document.write('<a href="javascript:Move('+x+','+y+');"><img src="img'+board[x+y*n]+'.gif" width=16 height=16></a>');
}
document.write('<br>');
}
}


function Move(x,y) {
var temp;

if(board[(x+y*n)-1] == 15){
temp = board[x+y*n];
board[x+y*n] = 15;
board[(x+y*n)-1] = temp;
}

if(board[(x+y*n)+1] == 15){
temp = board[x+y*n];
board[x+y*n] = 15;
board[(x+y*n)+1] = temp;
}

if(board[(x+y*n)-n] == 15){
temp = board[x+y*n];
board[x+y*n] = 15;
board[(x+y*n)-n] = temp;
}

if(board[(x+y*n)+n] == 15){
temp = board[x+y*n];
board[x+y*n] = 15;
board[(x+y*n)+n] = temp;
}

DrawBoard();
}

</script>
</body>
</html>

Khalid Ali
01-10-2004, 09:39 AM
looks like problemis in your DrawBoard() function.
See when you use
document.write
once the page is loaded after that it will write the document and there wont be any more javascript which you have on the actual document.
You will need to re think your logic. or use DOM and innerHTML to write to an element