// getting the game board, assigning it to variable 'board'.
let board = document.getElementsByClassName("chessboard")[0];
// creating variables for player 1 and 2.
let player1 = '\u2660';
let player2 = '\u2658';
let from;
let selected;
let index1 = -1;
let index2 = -1;
let player1total = 12;
let player2total = 12;
console.log (board.children[3].innerHTML);
// function for player 1
Array.from(board.children).forEach(function(cell, index) {
// Add a click listener to each square
cell.onclick = function(elem) {
// Check if a piece was selected
if (elem.target.innerHTML == player1) {
selected = player1;
}
else if (elem.target.innerHTML == player2) {
selected = player2;
}
if (elem.target.innerHTML === player1 || elem.target.innerHTML === player2) {
//get exact selected code
from = elem.target;
index1 = index;
}
// Check if a move can be made
else if (from && isLegalMove(from, elem.target)) {
index2 = index;
if(playerRules(selected,index1,index2)) {
// Put a piece within the selected square
var total = index1 - index2;
console.log("player" + selected + " index"+ index1 + " " + index2 + " DIFF: " + total);
elem.target.innerHTML = selected;
// Remove it from the old square
from.innerHTML = '';
// Clear the `from` variable
from = null;
index1 = -1;
index2 = -1;
}
}
}
});
function printElem (id, content) {
document.getElementById(id).innerHTML = content;
}
// function to determine if a move is legal
function isLegalMove(from, to) {
let result = to.innerHTML !== player1 && to.innerHTML !== player2;
result = result && to.className.indexOf('yellow') > -1;
return result;
}
let flag = false;
function playerRules(sel, from, to) {
//rule - one cannot move backward
var moveDiff = from - to;
console.log(moveDiff);
if (sel == player1) {
flag = (to < from);
if (flag) {
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.