add support for resignation

This commit is contained in:
Jesse D. McDonald 2020-03-15 15:57:17 -05:00
parent 85375530a9
commit 2e6764ffb5
2 changed files with 61 additions and 10 deletions

View File

@ -149,6 +149,7 @@
<input id="cb_notify" type="checkbox"><label for="cb_notify">Notify</label> <input id="cb_notify" type="checkbox"><label for="cb_notify">Notify</label>
<button id="cb_undo" type="button" disabled="true">Undo</button> <button id="cb_undo" type="button" disabled="true">Undo</button>
<button id="cb_redo" type="button" disabled="true">Redo</button> <button id="cb_redo" type="button" disabled="true">Redo</button>
<button id="cb_resign" type="button" disabled="true">Resign</button>
<button id="cb_reset" type="button">Reset</button> <button id="cb_reset" type="button">Reset</button>
<button id="cb_pass" type="button">Pass</button> <button id="cb_pass" type="button">Pass</button>
<span id="cb_message"></span><br> <span id="cb_message"></span><br>

View File

@ -39,11 +39,11 @@ function cloneJSON(obj){
} }
function isLight(side){ function isLight(side){
return side[0] === 'l'; return side === 'l' || side === 'light';
} }
function isDark(side){ function isDark(side){
return side[0] === 'd'; return side === 'd' || side === 'dark';
} }
function normalizeSide(side){ function normalizeSide(side){
@ -161,9 +161,20 @@ function hasMoved(board, side, where){
} }
} }
function gameEnded(board){
if (!board.move) {
return false;
} else if (board.move.took === 'k') {
return board.move.side;
} else if (board.move.resign) {
return otherSide(board.move.side);
} else {
return false;
}
}
function legalMoves(board, side, type, from, canCapture){ function legalMoves(board, side, type, from, canCapture){
if (board.move && board.move.took === 'k') { if (gameEnded(board)) {
/* checkmate, the game is over */
return []; return [];
} }
@ -428,6 +439,10 @@ function renderHistory(currentBoard) {
const board = list.pop(); const board = list.pop();
const move = board.move; const move = board.move;
if (move.resign) {
break;
}
if (move.from === 'phantom') { if (move.from === 'phantom') {
result += SHY + '*'; result += SHY + '*';
} else { } else {
@ -511,6 +526,17 @@ function renderHistory(currentBoard) {
} }
} }
let winner = gameEnded(currentBoard);
if (winner) {
if (isLight(winner)) {
result += ' 1-0';
} else if (isDark(winner)) {
result += ' 0-1';
} else {
result += ' \u00bd-\u00bd'; /* 1/2-1/2 */
}
}
return result; return result;
} }
@ -600,10 +626,14 @@ function renderBoard(board){
} }
let msg = ''; let msg = '';
if (board.move && board.move.took === 'k') { let winner = gameEnded(board);
msg = (isDark(board.move.side) ? 'Dark' : 'Light') + ' player won!'; if (winner) {
if (board.move.resign) {
msg += (isLight(board.move.side) ? 'Light' : 'Dark') + ' player resigned. ';
}
msg += (isLight(winner) ? 'Light' : 'Dark') + ' player won!';
} else { } else {
msg = (isDark(board.player) ? 'Dark' : 'Light') + " player's turn"; msg += (isLight(board.player) ? 'Light' : 'Dark') + " player's turn.";
} }
$('#cb_message').text(msg); $('#cb_message').text(msg);
@ -632,7 +662,13 @@ function setVisibleBoard(board){
const liveBoard = $('#cb_board').data('board'); const liveBoard = $('#cb_board').data('board');
$('#cb_undo').attr('disabled', liveBoard.prior ? false : true); $('#cb_undo').attr('disabled', liveBoard.prior ? false : true);
$('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true); $('#cb_redo').attr('disabled', liveBoard.subsequent ? false : true);
if (gameEnded(liveBoard)) {
$('#cb_pass').attr('disabled', true);
$('#cb_resign').attr('disabled', true);
} else {
$('#cb_pass').attr('disabled', liveBoard.phantom ? true : false); $('#cb_pass').attr('disabled', liveBoard.phantom ? true : false);
$('#cb_resign').attr('disabled', false);
}
$('#cb_nav_last').attr('disabled', true); $('#cb_nav_last').attr('disabled', true);
$('#cb_board').addClass('cb-live'); $('#cb_board').addClass('cb-live');
$('#cb_board').removeClass('cb-archive'); $('#cb_board').removeClass('cb-archive');
@ -640,6 +676,7 @@ function setVisibleBoard(board){
$('#cb_undo').attr('disabled', true); $('#cb_undo').attr('disabled', true);
$('#cb_redo').attr('disabled', true); $('#cb_redo').attr('disabled', true);
$('#cb_pass').attr('disabled', true); $('#cb_pass').attr('disabled', true);
$('#cb_resign').attr('disabled', true);
$('#cb_board .ui-draggable').draggable('disable'); $('#cb_board .ui-draggable').draggable('disable');
$('#cb_nav_last').attr('disabled', false); $('#cb_nav_last').attr('disabled', false);
$('#cb_board').removeClass('cb-live'); $('#cb_board').removeClass('cb-live');
@ -693,7 +730,8 @@ function putMeta(){
let lightName = $('#cb_light_name').val(); let lightName = $('#cb_light_name').val();
let darkName = $('#cb_dark_name').val(); let darkName = $('#cb_dark_name').val();
let meta = gun.get(PacoSakoUUID).get('meta').get(gameId); let meta = gun.get(PacoSakoUUID).get('meta').get(gameId);
let stat = (board.move && board.move.took === 'k') ? 'mate' : null; let winner = gameEnded(board);
let stat = !winner ? null : (board.move.took === 'k') ? 'mate' : 'ended';
meta.put({ meta.put({
gameId: gameId, gameId: gameId,
lightName: lightName, lightName: lightName,
@ -955,7 +993,7 @@ $(function (){
$('#cb_pass').on('click', function(){ $('#cb_pass').on('click', function(){
let board = $('#cb_board').data('board'); let board = $('#cb_board').data('board');
if (!board.phantom) { if (!gameEnded(board) && !board.phantom) {
let newBoard = cloneJSON(board); let newBoard = cloneJSON(board);
newBoard.prior = board; newBoard.prior = board;
newBoard.move = { side: board.player, pass: true }; newBoard.move = { side: board.player, pass: true };
@ -965,6 +1003,18 @@ $(function (){
} }
}); });
$('#cb_resign').on('click', function(){
let board = $('#cb_board').data('board');
if (!gameEnded(board)) {
let newBoard = cloneJSON(board);
newBoard.prior = board;
newBoard.move = { side: board.player, resign: true };
newBoard.player = otherSide(board.player);
newBoard.timestamp = new Date().getTime();
putState(newBoard);
}
});
$('#cb_select_game').on('change', function(){ $('#cb_select_game').on('change', function(){
let optIndex = $('#cb_select_game')[0].selectedIndex; let optIndex = $('#cb_select_game')[0].selectedIndex;
if (optIndex === 0) { if (optIndex === 0) {