"Michel" <Mi**************@wanadoo.fr> writes:
I have an html page which, thanks to some javascript, allows to move pieces
on a chessboard.
No verification that the move is legal, or even that it's the right
color, though.
I can't find how to reflect the moves in a list displayed by the same page.
What have you tried?
To understand what I'm trying to do you can have a look at:
http://mdevat.phpnet.org/echecs/firstmoves.php
(don't be misleaded by the .php nor by the code, I'm just revamping
everything to drop the php content and have an html + javascipt page)
Yes, quite simple design.
Ok, you move the piece in the second part of the "mdown" function (after
the "else"). That would be the obvious place to update the list too.
When you move the piece, you have the row and column of the target as
numbers, and as strings ("rowdest", "coldest", "roworig", and "colorig").
The column names are not assigned correctly. After each case in the switch,
you should add "break;", otherwise it continues with the next case.
You have tried combining them too, but failed (there shouldn't be quotes
around the variable names).
The text fields in the list are named "movewhite[0]", "moveblack[0]",
"movewhite[1]", etc.
They will probably be missing in some browsers, since the are placed
*after* "</body></html>". Don't do that. Actually, it seems you have
put an entire page, from <head> to </html>, inside another page. Fix
that, and make the HTML validate!
Anyway, to write to "movewhite[0]", you would write
document.forms[0].elements["movewhite[0]"].value = ...;
To write the moves one after another, you would need to track which
color it is and which move is next. You can use one or two variables
for that. Two is easier to read.
Add this to the beginning of the script tag;
var colors = ["white","black"];
var color = 0;
var movenumber = 0;
Then at the end of the else branch in mdown, replace the last two
lines with:
if (movenumber < 13) { // you only have 13 input fields, so stop writing
document.forms[0].elements["move"+colors[color]+"["+movenumber+"]"].
value = colorig + roworig + coldest + rowdest;
color++; // increment color, 0=white,1=black,2=goto 0, but increment move
if (color == 2) {
color = 0;
movenumber++;
}
}
The page needs serious work (the HTML is disasterous), but it seems to work
with the above changes (remember the "break" statements).
Good luck.
/L
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'