Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object mirror problem

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
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>&lt;HTML&gt;<br>&lt;HEAD&gt;<br>&lt;title&gt;&lt;/title&gt;<br>&lt;script language=&quot;javascript&quot;&gt;<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>&nbsp;if ((startRow==null)¦¦(startCol==null)){<br>&nbsp;&nbsp;if ((row &gt;= 0)&&(row &lt;= 7)&&(col &gt;= 0)&&(col &lt;= 7)){<br>&nbsp;&nbsp;&nbsp;startRow=row;<br>&nbsp;&nbsp;&nbsp;startCol=col;<br>&nbsp;&nbsp;&nbsp;document.all.statusMsg.innerHTML='Select the Destination square';<br>&nbsp;&nbsp;}<br>&nbsp;} else if ((endRow==null)¦¦(endCol==null)){<br>&nbsp;&nbsp;if ((row &gt;= 0)&&(row &lt;= 7)&&(col &gt;= 0)&&(col &lt;= 7)){<br>&nbsp;&nbsp;&nbsp;endRow=row;<br>&nbsp;&nbsp;&nbsp;endCol=col;<br>&nbsp;&nbsp;&nbsp;document.all.statusMsg.innerHTML='';<br>&nbsp;&nbsp;&nbsp;mover();<br>&nbsp;&nbsp;}<br>&nbsp;}<br>}<br><br><br>function print(theBoard){<br>&nbsp;var stOut='&lt;table border=2 cellpadding=0 cellspacing=0 width=90% height=90%&gt;';<br>&nbsp;for (i=0;i&lt;theBoard.length;i++){<br>&nbsp;&nbsp;stOut+='&lt;tr&gt;';<br>&nbsp;&nbsp;for (k=0;k&lt;theBoard[ i].length;k++){<br>&nbsp;&nbsp;&nbsp;stOut+='&lt;td width=12% height=12% align=center onclick=checkOut('+i+','+k+'); &gt;';<br>&nbsp;&nbsp;&nbsp;if (theBoard[ i][k].occupied==true){<br>&nbsp;&nbsp;&nbsp;&nbsp;if (theBoard[ i][k].owner=='black'){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stOut+=theBoard[ i][k].piece.toUpperCase();}<br>&nbsp;&nbsp;&nbsp;&nbsp;else {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stOut+=theBoard[ i][k].piece<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;} else {<br>&nbsp;&nbsp;&nbsp;&nbsp;stOut+=' '<br>&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;stOut+='&lt;/td&gt;\n';<br>&nbsp;&nbsp;}//for (k)<br>&nbsp;&nbsp;stOut+='&lt;/tr&gt;';<br>&nbsp;}//for (i)<br>&nbsp;stOut+='&lt;/table&gt;';<br>&nbsp;document.all.chessboard.innerHTML=stOut;<br>}<br><br>function initBoard(theBoard){<br>&nbsp;//set up the theBoardboard<br>&nbsp;&nbsp;for (i=0;i&lt;theBoard.length;i++){<br>&nbsp;&nbsp;&nbsp;theBoard[ i]=new Array(8);<br>&nbsp;&nbsp;&nbsp;for (j=0;j&lt;theBoard[ i].length;j++) theBoard[ i][j]=new chesspiece();<br>&nbsp;}<br><br>//put the pieces in their places<br>&nbsp;for (i=0; i&lt;theBoard.length; i++){<br>&nbsp;&nbsp;for (j=0; j&lt;theBoard[ i].length; j++){<br>&nbsp;&nbsp;&nbsp;switch (i){<br>&nbsp;&nbsp;&nbsp;&nbsp;case 0:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (j){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 0: theBoard[ i][j].piece='rook'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 7: theBoard[ i][j].piece='rook'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1: theBoard[ i][j].piece='knight'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 6: theBoard[ i][j].piece='knight'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 2: theBoard[ i][j].piece='bishop'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 5: theBoard[ i][j].piece='bishop'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 3&nbsp;&nbsp;&nbsp;: theBoard[ i][j].piece='queen'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 4&nbsp;&nbsp;&nbsp;: theBoard[ i][j].piece='king'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} //switch (j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].owner='black';<br>&nbsp;&nbsp;&nbsp;&nbsp;break; {0}<br>&nbsp;&nbsp;&nbsp;&nbsp;case 7:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (j){<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 0: theBoard[ i][j].piece='rook'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 7: theBoard[ i][j].piece='rook'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1: theBoard[ i][j].piece='knight'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 6: theBoard[ i][j].piece='knight'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 2: theBoard[ i][j].piece='bishop'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 5: theBoard[ i][j].piece='bishop'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 3&nbsp;&nbsp;&nbsp;: theBoard[ i][j].piece='queen'; break;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 4&nbsp;&nbsp;&nbsp;: theBoard[ i][j].piece='king'; break;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} //switch (j)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].owner='white';<br>&nbsp;&nbsp;&nbsp;&nbsp;break; {0}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;case 6:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].piece='pawn';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].owner='white';<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 1:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=true;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].piece='pawn';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].owner='black';<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 3 :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=false;<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 4 :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=false;<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 5 :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=false;<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;&nbsp;case 6 :<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theBoard[ i][j].occupied=false;<br>&nbsp;&nbsp;&nbsp;&nbsp;break;<br>&nbsp;&nbsp;&nbsp;} //switch (i)<br>&nbsp;&nbsp;} //for (j)<br>&nbsp;} //for (i)<br>&nbsp;return theBoard;<br>} <br><br><br>function mover(){<br>&nbsp;chess[endRow][endCol]=chess[startRow][startCol];<br>&nbsp;chess[startRow][startCol].occupied=false;<br>&nbsp;alert('chess[startRow][startCol].occupied='+chess[startRow][startCol].occupied+'\nchess[endRow][endCol].occupied='+chess[endRow][endCol].occupied);<br>&nbsp;print(chess);<br>}<br><br>function changeplayer(player){<br>&nbsp;if (player=='white'){ player='black' } else { player='white'; }<br>&nbsp;return player;<br>}<br><br>function begin(){<br>&nbsp;chess=initBoard(chess);<br>&nbsp;print(chess);<br>&nbsp;document.all.statusMsg.innerHTML='Select the Origin square';<br>}<br><br>function chesspiece(){<br>&nbsp;this.occupied=false;<br>&nbsp;this.piece=' ';<br>&nbsp;this.owner='';<br>&nbsp;return this<br>}<br><br>&lt;/script&gt;<br>&lt;style&gt;<br>&nbsp;#chessboard {font-family:courier,comic sans ms; color:#c0c0c0;}<br>&lt;/style&gt;<br>&lt;/head&gt;<br>&lt;body onload=&quot;begin();&quot; bgcolor=#000000 text=#c0c0c0 topmargin=0 leftmargin=0&gt;<br>&lt;div id=chessboard&gt;&lt;/div&gt;<br>&lt;div id=statusMsg &gt;&lt;/div&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<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.
 
nevermind, i decided to just use:<br>chess[endRow][endCol].piece=chess[startRow][startCol].piece;<br>chess[endRow][endCol].owner=chess[startRow][startCol].owner;<br><br>in other words, I am copying over each property individually, instead of all at once.<br><br>thanks anyway <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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top