theEclipse
Programmer
I am creating a chess game with javascript.<br>here are the specs:<br>I create a 2-dimmensional array (8 by 8) of objects, called chess.<br>each array slot's objects consist of .owner .piece and .occpuied .<br><br>Almost everything works fine, the one thing that dosn't work is the function that moves the piece already selected.<br><br>here it is:<br><FONT FACE=monospace><br>function mover(){<br>chess[endRow][endCol]=chess[startRow][startCol];<br>chess[startRow][startCol].occupied=false;<br>alert('chess[startRow][startCol].occupied='+chess[startRow][startCol].occupied+'\nchess[endRow][endCol].occupied='+chess[endRow][endCol].occupied);<br>//alert statement added for debugging purposes<br>print(chess);<br>}<br></font><br>the values endRow,endCol,startRow, and startCol are all determined in another function.<br><br>here is what happens:<br><br>The script recives the values for endRow,endCol,startRow, and startCol, and then calls mover. mover is supposed to take one piece and copy it to another location:<br><FONT FACE=monospace><br>chess[endRow][endCol]=chess[startRow][startCol];<br></font><br>then set the old location to not occupied:<br><FONT FACE=monospace><br>chess[startRow][startCol].occupied=false;<br></font><br>and lastly, run the printer:<br><FONT FACE=monospace><br>print(chess);<br></font><br>but for some reason, when I set the old location to not occupied, it also sets the new location to the same.<br><br><br>here is the full .html script, but be warned I have only tested it in IE 5.0, and I have not equiped it for use with netscape, or lower versions of IE.<br><br>I have replaced all array loop counters with [ i] , for this forum.<br><br>------------------------------------------------------------<br><FONT FACE=monospace><br><HTML><br><HEAD><br><title></title><br><script language="javascript"><br>var chess=new Array(8),moverWin,startCol,startRow,endCol,endRow,cPlayer='white';<br>startCol=startRow=endCol=endRow=null;<br><br>function checkOut(row,col){<br> if ((startRow==null)¦¦(startCol==null)){<br> if ((row >= 0)&&(row <= 7)&&(col >= 0)&&(col <= 7)){<br> startRow=row;<br> startCol=col;<br> document.all.statusMsg.innerHTML='Select the Destination square';<br> }<br> } else if ((endRow==null)¦¦(endCol==null)){<br> if ((row >= 0)&&(row <= 7)&&(col >= 0)&&(col <= 7)){<br> endRow=row;<br> endCol=col;<br> document.all.statusMsg.innerHTML='';<br> mover();<br> }<br> }<br>}<br><br><br>function print(theBoard){<br> var stOut='<table border=2 cellpadding=0 cellspacing=0 width=90% height=90%>';<br> for (i=0;i<theBoard.length;i++){<br> stOut+='<tr>';<br> for (k=0;k<theBoard[ i].length;k++){<br> stOut+='<td width=12% height=12% align=center onclick=checkOut('+i+','+k+'); >';<br> if (theBoard[ i][k].occupied==true){<br> if (theBoard[ i][k].owner=='black'){<br> stOut+=theBoard[ i][k].piece.toUpperCase();}<br> else {<br> stOut+=theBoard[ i][k].piece<br> }<br> } else {<br> stOut+=' '<br> }<br> stOut+='</td>\n';<br> }//for (k)<br> stOut+='</tr>';<br> }//for (i)<br> stOut+='</table>';<br> document.all.chessboard.innerHTML=stOut;<br>}<br><br>function initBoard(theBoard){<br> //set up the theBoardboard<br> for (i=0;i<theBoard.length;i++){<br> theBoard[ i]=new Array(8);<br> for (j=0;j<theBoard[ i].length;j++) theBoard[ i][j]=new chesspiece();<br> }<br><br>//put the pieces in their places<br> for (i=0; i<theBoard.length; i++){<br> for (j=0; j<theBoard[ i].length; j++){<br> switch (i){<br> case 0:<br> theBoard[ i][j].occupied=true;<br> switch (j){<br> case 0: theBoard[ i][j].piece='rook'; break;<br> case 7: theBoard[ i][j].piece='rook'; break;<br> case 1: theBoard[ i][j].piece='knight'; break;<br> case 6: theBoard[ i][j].piece='knight'; break;<br> case 2: theBoard[ i][j].piece='bishop'; break;<br> case 5: theBoard[ i][j].piece='bishop'; break;<br> case 3 : theBoard[ i][j].piece='queen'; break;<br> case 4 : theBoard[ i][j].piece='king'; break;<br> } //switch (j)<br> theBoard[ i][j].owner='black';<br> break; {0}<br> case 7:<br> theBoard[ i][j].occupied=true;<br> switch (j){<br> case 0: theBoard[ i][j].piece='rook'; break;<br> case 7: theBoard[ i][j].piece='rook'; break;<br> case 1: theBoard[ i][j].piece='knight'; break;<br> case 6: theBoard[ i][j].piece='knight'; break;<br> case 2: theBoard[ i][j].piece='bishop'; break;<br> case 5: theBoard[ i][j].piece='bishop'; break;<br> case 3 : theBoard[ i][j].piece='queen'; break;<br> case 4 : theBoard[ i][j].piece='king'; break; } //switch (j)<br> theBoard[ i][j].owner='white';<br> break; {0}<br><br> case 6:<br> theBoard[ i][j].occupied=true;<br> theBoard[ i][j].piece='pawn';<br> theBoard[ i][j].owner='white';<br> break;<br> case 1:<br> theBoard[ i][j].occupied=true;<br> theBoard[ i][j].piece='pawn';<br> theBoard[ i][j].owner='black';<br> break;<br> case 3 :<br> theBoard[ i][j].occupied=false;<br> break;<br> case 4 :<br> theBoard[ i][j].occupied=false;<br> break;<br> case 5 :<br> theBoard[ i][j].occupied=false;<br> break;<br> case 6 :<br> theBoard[ i][j].occupied=false;<br> break;<br> } //switch (i)<br> } //for (j)<br> } //for (i)<br> return theBoard;<br>} <br><br><br>function mover(){<br> chess[endRow][endCol]=chess[startRow][startCol];<br> chess[startRow][startCol].occupied=false;<br> alert('chess[startRow][startCol].occupied='+chess[startRow][startCol].occupied+'\nchess[endRow][endCol].occupied='+chess[endRow][endCol].occupied);<br> print(chess);<br>}<br><br>function changeplayer(player){<br> if (player=='white'){ player='black' } else { player='white'; }<br> return player;<br>}<br><br>function begin(){<br> chess=initBoard(chess);<br> print(chess);<br> document.all.statusMsg.innerHTML='Select the Origin square';<br>}<br><br>function chesspiece(){<br> this.occupied=false;<br> this.piece=' ';<br> this.owner='';<br> return this<br>}<br><br></script><br><style><br> #chessboard {font-family:courier,comic sans ms; color:#c0c0c0;}<br></style><br></head><br><body onload="begin();" bgcolor=#000000 text=#c0c0c0 topmargin=0 leftmargin=0><br><div id=chessboard></div><br><div id=statusMsg ></div><br></body><br></html><br></font><br>thanks for any help <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.