check legality of move prior to posting

This commit is contained in:
Sorrel Bri 2020-01-30 23:24:40 -08:00 committed by sorrelbri
parent be294f669c
commit 76f29cf9d3
2 changed files with 41 additions and 28 deletions

View file

@ -87,6 +87,7 @@ class Game {
}
getBoardState = () => {
this.boardState.forEach(point => point.legal = checkLegal(point, this))
return this.boardState.reduce((boardState, point) => {
boardState[`${point.pos[0]}-${point.pos[1]}`] = point.legal || point.stone;
return boardState;
@ -100,7 +101,7 @@ class Game {
makeMove = (move) => {
const player = move.player === 'white' ? -1 : 1;
const point = this.findPointFromIdx([move.pos.X, move.pos.Y])
if ( !checkLegal(point, this) ) throw 'illegal move';
if ( !checkLegal(point, this) ) throw Error('illegal move');
clearKo(this);
clearPass(this);
resolveCaptures(point, this);
@ -289,16 +290,16 @@ function resolveCaptures(point, Game) {
function checkLegal(point, Game) {
// clearOverlay();
// first step in logic: is point occupied, or in ko
if (point.stone) return false;
if (point.stone) return 0;
// if point is not empty check if liberties
if (point.getLiberties(Game).length < 1) {
//if no liberties check if enemy group has liberties
if ( point.checkCapture(Game).length ) return true;
if ( point.checkCapture(Game).length ) return 'l';
//if neighboring point is not empty check if friendly group is alive
if (point.checkGroup(Game)) return true;
return false;
if (point.checkGroup(Game)) return 'l';
return 0;
}
return true;
return 'l';
}
function clearOverlay() {

View file

@ -1,10 +1,11 @@
const assert = require('assert');
const chai = require('chai');
const should = chai.should();
const gameServices = require('../services/gameServices');
describe('game services', () => {
it('init game returns game board', done => {
gameServices.initGame({id: 1, handicap: 4})
assert.deepEqual(gameServices.getBoard(1), fourHandicapBoard)
gameServices.getBoard(1).should.eql(fourHandicapBoard)
done();
});
@ -12,31 +13,42 @@ describe('game services', () => {
gameServices.initGame({id: 1, handicap: 4})
const afterMoveOne = gameServices.makeMove({id: 1}, {player: 'white', pos: { X:6, Y:3 }});
const afterMoveOneShould = { board:{ ...fourHandicapBoard, '6-3': -1}, meta: {} };
assert.deepEqual(afterMoveOne, afterMoveOneShould);
afterMoveOne.should.eql(afterMoveOneShould);
done();
});
it('illegal move throws error', done => {
try {
gameServices.initGame({id: 1, handicap: 4})
const afterIllegalMove = gameServices.makeMove({id: 1}, {player: 'white', pos: { X:4, Y:4 }});
}
catch (err) {
err.message.should.equal('illegal move')
done();
}
})
})
const fourHandicapBoard = {
'1-1': 0,'1-2': 0,'1-3': 0,'1-4': 0,'1-5': 0,'1-6': 0,'1-7': 0,'1-8': 0,'1-9': 0,'1-10': 0,'1-11': 0,'1-12': 0,'1-13': 0,'1-14': 0,'1-15': 0,'1-16': 0,'1-17': 0,'1-18': 0,'1-19': 0,
'2-1': 0,'2-2': 0,'2-3': 0,'2-4': 0,'2-5': 0,'2-6': 0,'2-7': 0,'2-8': 0,'2-9': 0,'2-10': 0,'2-11': 0,'2-12': 0,'2-13': 0,'2-14': 0,'2-15': 0,'2-16': 0,'2-17': 0,'2-18': 0,'2-19': 0,
'3-1': 0,'3-2': 0,'3-3': 0,'3-4': 0,'3-5': 0,'3-6': 0,'3-7': 0,'3-8': 0,'3-9': 0,'3-10': 0,'3-11': 0,'3-12': 0,'3-13': 0,'3-14': 0,'3-15': 0,'3-16': 0,'3-17': 0,'3-18': 0,'3-19': 0,
'4-1': 0,'4-2': 0,'4-3': 0,'4-4': 1,'4-5': 0,'4-6': 0,'4-7': 0,'4-8': 0,'4-9': 0,'4-10': 0,'4-11': 0,'4-12': 0,'4-13': 0,'4-14': 0,'4-15': 0,'4-16': 1,'4-17': 0,'4-18': 0,'4-19': 0,
'5-1': 0,'5-2': 0,'5-3': 0,'5-4': 0,'5-5': 0,'5-6': 0,'5-7': 0,'5-8': 0,'5-9': 0,'5-10': 0,'5-11': 0,'5-12': 0,'5-13': 0,'5-14': 0,'5-15': 0,'5-16': 0,'5-17': 0,'5-18': 0,'5-19': 0,
'6-1': 0,'6-2': 0,'6-3': 0,'6-4': 0,'6-5': 0,'6-6': 0,'6-7': 0,'6-8': 0,'6-9': 0,'6-10': 0,'6-11': 0,'6-12': 0,'6-13': 0,'6-14': 0,'6-15': 0,'6-16': 0,'6-17': 0,'6-18': 0,'6-19': 0,
'7-1': 0,'7-2': 0,'7-3': 0,'7-4': 0,'7-5': 0,'7-6': 0,'7-7': 0,'7-8': 0,'7-9': 0,'7-10': 0,'7-11': 0,'7-12': 0,'7-13': 0,'7-14': 0,'7-15': 0,'7-16': 0,'7-17': 0,'7-18': 0,'7-19': 0,
'8-1': 0,'8-2': 0,'8-3': 0,'8-4': 0,'8-5': 0,'8-6': 0,'8-7': 0,'8-8': 0,'8-9': 0,'8-10': 0,'8-11': 0,'8-12': 0,'8-13': 0,'8-14': 0,'8-15': 0,'8-16': 0,'8-17': 0,'8-18': 0,'8-19': 0,
'9-1': 0,'9-2': 0,'9-3': 0,'9-4': 0,'9-5': 0,'9-6': 0,'9-7': 0,'9-8': 0,'9-9': 0,'9-10': 0,'9-11': 0,'9-12': 0,'9-13': 0,'9-14': 0,'9-15': 0,'9-16': 0,'9-17': 0,'9-18': 0,'9-19': 0,
'10-1': 0,'10-2': 0,'10-3': 0,'10-4': 0,'10-5': 0,'10-6': 0,'10-7': 0,'10-8': 0,'10-9': 0,'10-10': 0,'10-11': 0,'10-12': 0,'10-13': 0,'10-14': 0,'10-15': 0,'10-16': 0,'10-17': 0,'10-18': 0,'10-19': 0,
'11-1': 0,'11-2': 0,'11-3': 0,'11-4': 0,'11-5': 0,'11-6': 0,'11-7': 0,'11-8': 0,'11-9': 0,'11-10': 0,'11-11': 0,'11-12': 0,'11-13': 0,'11-14': 0,'11-15': 0,'11-16': 0,'11-17': 0,'11-18': 0,'11-19': 0,
'12-1': 0,'12-2': 0,'12-3': 0,'12-4': 0,'12-5': 0,'12-6': 0,'12-7': 0,'12-8': 0,'12-9': 0,'12-10': 0,'12-11': 0,'12-12': 0,'12-13': 0,'12-14': 0,'12-15': 0,'12-16': 0,'12-17': 0,'12-18': 0,'12-19': 0,
'13-1': 0,'13-2': 0,'13-3': 0,'13-4': 0,'13-5': 0,'13-6': 0,'13-7': 0,'13-8': 0,'13-9': 0,'13-10': 0,'13-11': 0,'13-12': 0,'13-13': 0,'13-14': 0,'13-15': 0,'13-16': 0,'13-17': 0,'13-18': 0,'13-19': 0,
'14-1': 0,'14-2': 0,'14-3': 0,'14-4': 0,'14-5': 0,'14-6': 0,'14-7': 0,'14-8': 0,'14-9': 0,'14-10': 0,'14-11': 0,'14-12': 0,'14-13': 0,'14-14': 0,'14-15': 0,'14-16': 0,'14-17': 0,'14-18': 0,'14-19': 0,
'15-1': 0,'15-2': 0,'15-3': 0,'15-4': 0,'15-5': 0,'15-6': 0,'15-7': 0,'15-8': 0,'15-9': 0,'15-10': 0,'15-11': 0,'15-12': 0,'15-13': 0,'15-14': 0,'15-15': 0,'15-16': 0,'15-17': 0,'15-18': 0,'15-19': 0,
'16-1': 0,'16-2': 0,'16-3': 0,'16-4': 1,'16-5': 0,'16-6': 0,'16-7': 0,'16-8': 0,'16-9': 0,'16-10': 0,'16-11': 0,'16-12': 0,'16-13': 0,'16-14': 0,'16-15': 0,'16-16': 1,'16-17': 0,'16-18': 0,'16-19': 0,
'17-1': 0,'17-2': 0,'17-3': 0,'17-4': 0,'17-5': 0,'17-6': 0,'17-7': 0,'17-8': 0,'17-9': 0,'17-10': 0,'17-11': 0,'17-12': 0,'17-13': 0,'17-14': 0,'17-15': 0,'17-16': 0,'17-17': 0,'17-18': 0,'17-19': 0,
'18-1': 0,'18-2': 0,'18-3': 0,'18-4': 0,'18-5': 0,'18-6': 0,'18-7': 0,'18-8': 0,'18-9': 0,'18-10': 0,'18-11': 0,'18-12': 0,'18-13': 0,'18-14': 0,'18-15': 0,'18-16': 0,'18-17': 0,'18-18': 0,'18-19': 0,
'19-1': 0,'19-2': 0,'19-3': 0,'19-4': 0,'19-5': 0,'19-6': 0,'19-7': 0,'19-8': 0,'19-9': 0,'19-10': 0,'19-11': 0,'19-12': 0,'19-13': 0,'19-14': 0,'19-15': 0,'19-16': 0,'19-17': 0,'19-18': 0,'19-19': 0
'1-1': 'l','1-2': 'l','1-3': 'l','1-4': 'l','1-5': 'l','1-6': 'l','1-7': 'l','1-8': 'l','1-9': 'l','1-10': 'l','1-11': 'l','1-12': 'l','1-13': 'l','1-14': 'l','1-15': 'l','1-16': 'l','1-17': 'l','1-18': 'l','1-19': 'l',
'2-1': 'l','2-2': 'l','2-3': 'l','2-4': 'l','2-5': 'l','2-6': 'l','2-7': 'l','2-8': 'l','2-9': 'l','2-10': 'l','2-11': 'l','2-12': 'l','2-13': 'l','2-14': 'l','2-15': 'l','2-16': 'l','2-17': 'l','2-18': 'l','2-19': 'l',
'3-1': 'l','3-2': 'l','3-3': 'l','3-4': 'l','3-5': 'l','3-6': 'l','3-7': 'l','3-8': 'l','3-9': 'l','3-10': 'l','3-11': 'l','3-12': 'l','3-13': 'l','3-14': 'l','3-15': 'l','3-16': 'l','3-17': 'l','3-18': 'l','3-19': 'l',
'4-1': 'l','4-2': 'l','4-3': 'l','4-4': 1,'4-5': 'l','4-6': 'l','4-7': 'l','4-8': 'l','4-9': 'l','4-10': 'l','4-11': 'l','4-12': 'l','4-13': 'l','4-14': 'l','4-15': 'l','4-16': 1,'4-17': 'l','4-18': 'l','4-19': 'l',
'5-1': 'l','5-2': 'l','5-3': 'l','5-4': 'l','5-5': 'l','5-6': 'l','5-7': 'l','5-8': 'l','5-9': 'l','5-10': 'l','5-11': 'l','5-12': 'l','5-13': 'l','5-14': 'l','5-15': 'l','5-16': 'l','5-17': 'l','5-18': 'l','5-19': 'l',
'6-1': 'l','6-2': 'l','6-3': 'l','6-4': 'l','6-5': 'l','6-6': 'l','6-7': 'l','6-8': 'l','6-9': 'l','6-10': 'l','6-11': 'l','6-12': 'l','6-13': 'l','6-14': 'l','6-15': 'l','6-16': 'l','6-17': 'l','6-18': 'l','6-19': 'l',
'7-1': 'l','7-2': 'l','7-3': 'l','7-4': 'l','7-5': 'l','7-6': 'l','7-7': 'l','7-8': 'l','7-9': 'l','7-10': 'l','7-11': 'l','7-12': 'l','7-13': 'l','7-14': 'l','7-15': 'l','7-16': 'l','7-17': 'l','7-18': 'l','7-19': 'l',
'8-1': 'l','8-2': 'l','8-3': 'l','8-4': 'l','8-5': 'l','8-6': 'l','8-7': 'l','8-8': 'l','8-9': 'l','8-10': 'l','8-11': 'l','8-12': 'l','8-13': 'l','8-14': 'l','8-15': 'l','8-16': 'l','8-17': 'l','8-18': 'l','8-19': 'l',
'9-1': 'l','9-2': 'l','9-3': 'l','9-4': 'l','9-5': 'l','9-6': 'l','9-7': 'l','9-8': 'l','9-9': 'l','9-10': 'l','9-11': 'l','9-12': 'l','9-13': 'l','9-14': 'l','9-15': 'l','9-16': 'l','9-17': 'l','9-18': 'l','9-19': 'l',
'10-1': 'l','10-2': 'l','10-3': 'l','10-4': 'l','10-5': 'l','10-6': 'l','10-7': 'l','10-8': 'l','10-9': 'l','10-10': 'l','10-11': 'l','10-12': 'l','10-13': 'l','10-14': 'l','10-15': 'l','10-16': 'l','10-17': 'l','10-18': 'l','10-19': 'l',
'11-1': 'l','11-2': 'l','11-3': 'l','11-4': 'l','11-5': 'l','11-6': 'l','11-7': 'l','11-8': 'l','11-9': 'l','11-10': 'l','11-11': 'l','11-12': 'l','11-13': 'l','11-14': 'l','11-15': 'l','11-16': 'l','11-17': 'l','11-18': 'l','11-19': 'l',
'12-1': 'l','12-2': 'l','12-3': 'l','12-4': 'l','12-5': 'l','12-6': 'l','12-7': 'l','12-8': 'l','12-9': 'l','12-10': 'l','12-11': 'l','12-12': 'l','12-13': 'l','12-14': 'l','12-15': 'l','12-16': 'l','12-17': 'l','12-18': 'l','12-19': 'l',
'13-1': 'l','13-2': 'l','13-3': 'l','13-4': 'l','13-5': 'l','13-6': 'l','13-7': 'l','13-8': 'l','13-9': 'l','13-10': 'l','13-11': 'l','13-12': 'l','13-13': 'l','13-14': 'l','13-15': 'l','13-16': 'l','13-17': 'l','13-18': 'l','13-19': 'l',
'14-1': 'l','14-2': 'l','14-3': 'l','14-4': 'l','14-5': 'l','14-6': 'l','14-7': 'l','14-8': 'l','14-9': 'l','14-10': 'l','14-11': 'l','14-12': 'l','14-13': 'l','14-14': 'l','14-15': 'l','14-16': 'l','14-17': 'l','14-18': 'l','14-19': 'l',
'15-1': 'l','15-2': 'l','15-3': 'l','15-4': 'l','15-5': 'l','15-6': 'l','15-7': 'l','15-8': 'l','15-9': 'l','15-10': 'l','15-11': 'l','15-12': 'l','15-13': 'l','15-14': 'l','15-15': 'l','15-16': 'l','15-17': 'l','15-18': 'l','15-19': 'l',
'16-1': 'l','16-2': 'l','16-3': 'l','16-4': 1,'16-5': 'l','16-6': 'l','16-7': 'l','16-8': 'l','16-9': 'l','16-10': 'l','16-11': 'l','16-12': 'l','16-13': 'l','16-14': 'l','16-15': 'l','16-16': 1,'16-17': 'l','16-18': 'l','16-19': 'l',
'17-1': 'l','17-2': 'l','17-3': 'l','17-4': 'l','17-5': 'l','17-6': 'l','17-7': 'l','17-8': 'l','17-9': 'l','17-10': 'l','17-11': 'l','17-12': 'l','17-13': 'l','17-14': 'l','17-15': 'l','17-16': 'l','17-17': 'l','17-18': 'l','17-19': 'l',
'18-1': 'l','18-2': 'l','18-3': 'l','18-4': 'l','18-5': 'l','18-6': 'l','18-7': 'l','18-8': 'l','18-9': 'l','18-10': 'l','18-11': 'l','18-12': 'l','18-13': 'l','18-14': 'l','18-15': 'l','18-16': 'l','18-17': 'l','18-18': 'l','18-19': 'l',
'19-1': 'l','19-2': 'l','19-3': 'l','19-4': 'l','19-5': 'l','19-6': 'l','19-7': 'l','19-8': 'l','19-9': 'l','19-10': 'l','19-11': 'l','19-12': 'l','19-13': 'l','19-14': 'l','19-15': 'l','19-16': 'l','19-17': 'l','19-18': 'l','19-19': 'l'
};