I'm having some problems with my assignment, which was to make chess pieces move freely across the board without the use of ids in native javascript. I have managed to set them on board and have also tried with getting coordinates from clicking on a piece, then getting coordinates from clicking somewhere on board and assigning the chosen piece those newly acquired coordinates, but nothing seems to work correctly.
I'd really appreciate some help :)
<head>
<meta charset="utf-8">
<style>
body
{
padding: 20px;
}
h1
{
text-align: center
}
table
{
margin-left:auto;
margin-right:auto;
border: 4px solid black;
border-collapse: collapse;
}
td
{
width: 40px;
height: 40px;
text-align: center;
}
.up
{
border-top-style: hidden;
border-left-style: hidden;
border-right-style: hidden;
border-bottom-style: solid;
background-color: white;
font-weight: bold
}
.down
{
border-top-style: solid;
border-left-style: hidden;
border-right-style: hidden;
border-bottom-style: hidden;
background-color: white;
font-weight: bold
}
.left
{
border-top-style: hidden;
border-left-style: hidden;
border-right-style: solid;
border-bottom-style: hidden;
background-color: white;
font-weight: bold
}
.right
{
border-top-style: hidden;
border-left-style: solid;
border-right-style: hidden;
border-bottom-style: hidden;
background-color: white;
font-weight: bold
}
.gray
{
background-color: gray;
}
.white
{
background-color: white;
}
</style>
</head>
<body>
<script> // chessboard
document.write("<h1>Game</h1>");
document.write("<table><tr class=up><td></td>");
for (var k=0; k<=7; k++){ //letters on top, 97 is a, 104 is h
document.write("<td>" + String.fromCharCode(k+97) + "</td>");
}
document.write("</tr>");
for (var j=8; j>0; j--){ //numbers dropping on sides
document.write("<tr onclick='indexRow(this)'><td class=left>" + j + "</td>");
if((j%2)==0){
for (var i=0; i<8; i++){
if((i%2)==0){
document.write('<td onclick="indexCell(this)" class=white></td>'); //grab index
} else{
document.write('<td onclick="indexCell(this)" class=gray></td>'); //grab index
};
}
} else{
for (var i=0; i<8; i++){
if((i%2)==0){
document.write('<td onclick="indexCell(this)" class=gray></td>'); //grab index
} else{
document.write('<td onclick="indexCell(this)" class=white></td>'); //grab index
};
}
};
document.write("<td class=right>" + j + "</td>");
}
document.write("<tr class=down><td></td>");
for (var k=0; k<=7; k++){ //letters on top,97 is a, 104 is h
document.write("<td>" + String.fromCharCode(k+97) + "</td>");
}
document.write("</tr></table>");
//pieces
var pieces=new Array( //pieces
new Array(" "," "," "," "," "," "," "," "," "),
new Array(" ","♖"," "," ","♔","♕","♗","♘","♖"),
new Array(" ","♙","♙","♙"," "," ","♙","♙","♙"),
new Array(" "," "," "," "," ","♙"," "," "," "),
new Array(" "," "," "," ","♙"," "," "," "," "),
new Array(" "," "," "," ","♘"," "," ","♗"," "),
new Array(" "," "," ","♞"," "," "," "," "," "),
new Array(" ","♟","♟","♟","♞","♟","♟","♟","♟"),
new Array(" ","♜"," ","♝","♚","♛","♝"," ","♜")
);
//sets up the board
function setUp(){
for (i = 1; i <=8; i++){
for (j = 1; j <=8; j++){
document.getElementsByTagName('tr')[j].getElementsByTagName('td')[i].innerHTML = pieces[j][i];
}
}
}
setUp();
//attempts with making things move
var clickedon = false;
var save_x = 0;
var save_y = 0;
var piece = 0;
var _x = 0;
var _y = 0;
//gets cell index
function indexCell(x)
{
//alert("Cell index is: " + x.cellIndex);
_x = x.cellIndex;
if(clickedon == false)
{
if(pieces[_x][_y] != " ")
{
figura = pieces[_x][_y];
save_x = _x;
save_y = _y;
clickedon = true;
alert(piece)
}
}
else
{
if(pieces[_x][_y] == " ")
{
pieces[save_x][save_y] = " ";
pieces[_x][_y] = piece;
setUp();
clickedon = false
}
}
}
//grabs row index
function indexRow(y){
//alert("Row index is: " + x.rowIndex);
_y = y.rowIndex;
}
</script>
</body>
Edit: I've now posted the version with my attempts at making pieces move and with numbers and letters around.