simplify some conditions
This commit is contained in:
parent
48d17a68de
commit
22449d8405
63
js/chess.js
63
js/chess.js
|
|
@ -32,12 +32,20 @@ function cloneJSON(obj){
|
|||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
function isLight(side){
|
||||
return side[0] === 'l';
|
||||
}
|
||||
|
||||
function isDark(side){
|
||||
return side[0] === 'd';
|
||||
}
|
||||
|
||||
function normalizeSide(side){
|
||||
return (side[0] === 'd') ? 'dark' : 'light';
|
||||
return isDark(side) ? 'dark' : 'light';
|
||||
}
|
||||
|
||||
function otherSide(side){
|
||||
return (side[0] === 'd') ? 'light' : 'dark';
|
||||
return isDark(side) ? 'light' : 'dark';
|
||||
}
|
||||
|
||||
function boardGet(board, where, side){
|
||||
|
|
@ -56,7 +64,7 @@ function boardGet(board, where, side){
|
|||
}
|
||||
|
||||
function boardPut(board, where, side, piece){
|
||||
side = (side[0] === 'd') ? 'dark' : 'light';
|
||||
side = isDark(side) ? 'dark' : 'light';
|
||||
var column = 'abcdefgh'.indexOf(where[0]);
|
||||
var row = Number(where[1]) - 1;
|
||||
var data = board[side][row];
|
||||
|
|
@ -111,24 +119,26 @@ function scanPath(accum, board, side, from, canCapture, columnsLeft, rowsUp, rem
|
|||
}
|
||||
}
|
||||
|
||||
function hasMoved(board, side, from){
|
||||
function hasMoved(board, side, where){
|
||||
side = normalizeSide(side);
|
||||
|
||||
while (true) {
|
||||
if (!board || !board['move']) {
|
||||
if (!board) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!board['move']['pass'] && board['move']['side'][0] === side[0]) {
|
||||
if (board['move']['to'] === from) {
|
||||
const move = board['move'];
|
||||
|
||||
if (move && move['side'] === side && move['to'] === where) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
board = board['prior'];
|
||||
}
|
||||
}
|
||||
|
||||
function legalMoves(board, side, type, from, canCapture){
|
||||
if (board['move'] && board['move']['took'] && board['move']['took'][0] === 'k') {
|
||||
if (board['move'] && board['move']['took'] === 'k') {
|
||||
return [];
|
||||
}
|
||||
|
||||
|
|
@ -144,7 +154,7 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
for (const dir of ortho.concat(diag)) {
|
||||
scanPath(legals, board, side, from, false, dir[0], dir[1], 0);
|
||||
}
|
||||
if (from[0] === 'd' && from[1] === (side[0] === 'd' ? '8' : '1')) {
|
||||
if (from[0] === 'd' && from[1] === (isDark(side) ? '8' : '1')) {
|
||||
/* check for castling conditions */
|
||||
if (!hasMoved(board, side, from)) {
|
||||
if (boardGet(board, 'c' + from[1], side) === ' ' &&
|
||||
|
|
@ -186,14 +196,15 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
scanPath(legals, board, side, from, canCapture, dir[0], dir[1], 8);
|
||||
}
|
||||
} else if (type === 'p') {
|
||||
var forward = offsetSquare(from, 0, (side[0] === 'd') ? -1 : 1);
|
||||
var forward2 = offsetSquare(from, 0, (side[0] === 'd') ? -2 : 2);
|
||||
var diagL = offsetSquare(from, -1, (side[0] === 'd') ? -1 : 1);
|
||||
var diagR = offsetSquare(from, 1, (side[0] === 'd') ? -1 : 1);
|
||||
const dark = isDark(side);
|
||||
const forward = offsetSquare(from, 0, dark ? -1 : 1);
|
||||
const forward2 = offsetSquare(from, 0, dark ? -2 : 2);
|
||||
const diagL = offsetSquare(from, -1, dark ? -1 : 1);
|
||||
const diagR = offsetSquare(from, 1, dark ? -1 : 1);
|
||||
|
||||
if (forward && validDestination(board, side, forward, false)) {
|
||||
legals.push(forward);
|
||||
if ((side[0] === 'd') ? (from[1] >= '7') : (from[1] <= '2')) {
|
||||
if (dark ? (from[1] >= '7') : (from[1] <= '2')) {
|
||||
if (forward2 && validDestination(board, side, forward2, false)) {
|
||||
legals.push(forward2);
|
||||
}
|
||||
|
|
@ -213,8 +224,10 @@ function legalMoves(board, side, type, from, canCapture){
|
|||
if (otherBoard && otherBoard['move']) {
|
||||
const move = otherBoard['move'];
|
||||
if (move['side'][0] !== side[0] && move['type'] === 'p') {
|
||||
const from = (move['from'] === 'phantom')
|
||||
? otherBoard['prior']['phantom']['from'] : move['from'];
|
||||
const from =
|
||||
(move['from'] === 'phantom') ?
|
||||
otherBoard['prior']['phantom']['from'] :
|
||||
move['from'];
|
||||
if (from[0] === there[0] && from[1] === forward2[1]) {
|
||||
/* en passant */
|
||||
legals.push(there);
|
||||
|
|
@ -277,12 +290,12 @@ function movePiece(priorBoard, side, from, to){
|
|||
boardPut(board, 'h' + actuallyFrom[1], side, ' ');
|
||||
boardPut(board, 'e' + actuallyFrom[1], side, 'r');
|
||||
}
|
||||
if (type === 'p' && ((side[0] === 'd') ? (to[1] === '1') : (to[1] === '8'))) {
|
||||
board['move']['promotion'] = 'queen'; /* TODO: allow other choices */
|
||||
if (type === 'p' && (isDark(side) ? (to[1] === '1') : (to[1] === '8'))) {
|
||||
board['move']['promotion'] = 'q'; /* TODO: allow other choices */
|
||||
type = 'q';
|
||||
}
|
||||
if (alongside === 'p' && ((other[0] === 'd') ? (to[1] === '1') : (to[1] === '8'))) {
|
||||
board['move']['promotion'] = 'queen'; /* TODO: allow other choices */
|
||||
if (alongside === 'p' && (isDark(other) ? (to[1] === '1') : (to[1] === '8'))) {
|
||||
board['move']['promotion'] = 'q'; /* TODO: allow other choices */
|
||||
alongside = 'q';
|
||||
}
|
||||
if (type === 'p' && alongside === ' ' && to[0] !== actuallyFrom[0]) {
|
||||
|
|
@ -377,7 +390,7 @@ function renderHistory(board) {
|
|||
result += '(' + move['promotion'][0].toUpperCase() + ')';
|
||||
}
|
||||
|
||||
if (move['took'] && move['took'][0] === 'k') {
|
||||
if (move['took'] === 'k') {
|
||||
result += '#';
|
||||
}
|
||||
}
|
||||
|
|
@ -454,7 +467,7 @@ function renderBoard(board){
|
|||
$('#cb_phantom .ui-draggable-disabled').filter(clss).draggable('enable');
|
||||
} else {
|
||||
$('#cb_board .ui-draggable').draggable('disable');
|
||||
if (!board['move'] || !board['move']['took'] || board['move']['took'][0] !== 'k') {
|
||||
if (!board['move'] || board['move']['took'] !== 'k') {
|
||||
$('#cb_board .ui-draggable-disabled').filter(clss).draggable('enable');
|
||||
}
|
||||
}
|
||||
|
|
@ -469,7 +482,7 @@ function renderBoard(board){
|
|||
}
|
||||
|
||||
var msg = '';
|
||||
if (board['move'] && board['move']['took'] && board['move']['took'][0] === 'k') {
|
||||
if (board['move'] && board['move']['took'] === 'k') {
|
||||
msg = (board['move']['side'][0] === 'd' ? 'Dark' : 'Light') + ' player won!';
|
||||
} else {
|
||||
msg = (board['player'][0] === 'd' ? 'Dark' : 'Light') + " player's move";
|
||||
|
|
@ -550,7 +563,7 @@ function switchGameId(newId){
|
|||
const board = JSON.parse(d['board']);
|
||||
const cb_board = $('#cb_board').first();
|
||||
if ($('#cb_notify')[0].checked && cb_board.data('skip_notify') !== d['board']) {
|
||||
if (board['move'] && board['move']['took'] && board['move']['took'][0] === 'k') {
|
||||
if (board['move'] && board['move']['took'] === 'k') {
|
||||
notify((board['move']['side'][0] === 'd' ? 'Dark' : 'Light') + ' player won!');
|
||||
} else {
|
||||
notify((board['player'][0] === 'd' ? 'Dark' : 'Light') + ' player\'s turn');
|
||||
|
|
|
|||
Loading…
Reference in New Issue