Commit b9a9afab authored by Vadym Gidulian's avatar Vadym Gidulian

Added basic rules

parent 6239858c
......@@ -28,6 +28,7 @@ const COLOR_PIECE_WHITE = '#fff';
const game = {
state: '',
stateData: {},
turn: 'black',
pieces: [
{position: 'A1', color: 'black', isKing: false},
{position: 'A3', color: 'black', isKing: false},
......@@ -88,6 +89,7 @@ function gameUpdate() {
}
function moveStart(position) {
if (!getPieceByPosition(position)) return;
if (game.state == 'moving') return;
game.state = 'moving';
......@@ -105,7 +107,13 @@ function moveGoing(x, y) {
function moveEnd(position) {
if (game.state != 'moving') return;
game.stateData.piece.position = position;
if (move(game.stateData.piece, game.stateData.originalPosition, position)) {
game.stateData.piece.position = position;
becomeKing(game.stateData.piece);
nextTurn();
} else {
game.stateData.piece.position = game.stateData.originalPosition;
}
game.stateData = {};
game.state = 'waiting';
gameUpdate();
......@@ -121,6 +129,46 @@ function getPieceIndexByPosition(position) {
}).indexOf(position);
}
// Rules
function move(piece, oldPosition, newPosition) {
if (piece.color != game.turn) return false;
if (getPieceByPosition(newPosition)) return false;
var ox = oldPosition.charCodeAt(0);
var oy = oldPosition.charCodeAt(1);
var nx = newPosition.charCodeAt(0);
var ny = newPosition.charCodeAt(1);
return piece.color == 'black' && (ny - oy) == 1 && Math.abs(nx - ox) == 1
|| piece.color == 'black' && (ny - oy) == 2 && Math.abs(nx - ox) == 2 && beat(piece, fromCharCodes((ox+nx)/2, (oy+ny)/2))
|| piece.color == 'white' && (oy - ny) == 1 && Math.abs(nx - ox) == 1
|| piece.color == 'white' && (oy - ny) == 2 && Math.abs(nx - ox) == 2 && beat(piece, fromCharCodes((ox+nx)/2, (oy+ny)/2))
|| piece.isKing && piece.color == 'black' && Math.abs(ny - oy) == 1 && Math.abs(nx - ox) == 1
|| piece.isKing && piece.color == 'black' && Math.abs(ny - oy) == 2 && Math.abs(nx - ox) == 2 && beat(piece, fromCharCodes((ox+nx)/2, (oy+ny)/2))
|| piece.isKing && piece.color == 'white' && Math.abs(oy - ny) == 1 && Math.abs(nx - ox) == 1
|| piece.isKing && piece.color == 'white' && Math.abs(oy - ny) == 2 && Math.abs(nx - ox) == 2 && beat(piece, fromCharCodes((ox+nx)/2, (oy+ny)/2));
}
function beat(piece, position) {
var index = getPieceIndexByPosition(position);
if (!~index) return false;
if (piece.color == game.pieces[index].color) return false;
game.pieces.splice(index, 1);
return true;
}
function becomeKing(piece) {
piece.isKing = piece.isKing
|| piece.color == 'black' && piece.position[1] == '8'
|| piece.color == 'white' && piece.position[1] == '1';
}
function nextTurn() {
game.turn = game.turn == 'black' ? 'white' : 'black';
console.log(game.turn);
}
// Graphics
function init() {
......@@ -230,3 +278,13 @@ function getPositionByCoordinates(x, y) {
return String.fromCharCode(col + 65) + String.fromCharCode((7-row) + 49);
}
// Utils
function fromCharCodes() {
var s = '';
for (var i = 0; i < arguments.length; i++) {
s += String.fromCharCode(arguments[i]);
}
return s;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment