simplify some conditions

This commit is contained in:
Jesse D. McDonald 2020-03-12 00:25:39 -05:00
parent 48d17a68de
commit 22449d8405
1 changed files with 39 additions and 26 deletions

View File

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