patch ko bug preventing snapback

This commit is contained in:
Sorrel Bri 2020-05-02 20:18:21 -07:00
parent 5ad997d276
commit 2237e344c1
2 changed files with 44 additions and 20 deletions

View file

@ -114,12 +114,20 @@ const checkLegal = ({ point, Game }) => {
const getBoardState = (Game) => { const getBoardState = (Game) => {
const getLegal = point => checkLegal({ point, Game }) const getLegal = point => checkLegal({ point, Game })
return pipeMap(getLegal)(Game.boardState); const boardState = pipeMap(getLegal)(Game.boardState);
Game.kos.forEach(ko => {
boardState[ko].legal = false;
});
return boardState;
} }
const getLegalMoves = (Game) => { const getLegalMoves = (Game) => {
const mapLegal = point => point.legal ? 'l' : point.stone; const mapLegal = point => point.legal ? 'l' : point.stone;
return pipeMap(mapLegal)(Game.boardState); const legalMoves = pipeMap(mapLegal)(Game.boardState);
Game.kos.forEach(ko => {
legalMoves[ko] = 'k';
});
return legalMoves;
} }
const getNeighbors = ({ Game, point }) => { const getNeighbors = ({ Game, point }) => {
@ -164,7 +172,7 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
boardSize: gameData.boardSize || 19, boardSize: gameData.boardSize || 19,
groups: {}, groups: {},
boardState: {}, boardState: {},
ko: [], kos: [],
gameRecord: gameRecord, gameRecord: gameRecord,
playerState: gameData.playerState || { playerState: gameData.playerState || {
bCaptures: 0, bCaptures: 0,
@ -314,9 +322,12 @@ const Point = ({x, y, boardSize = 19}) => {
for (let [capture, _] of capturesSet.entries()) { for (let [capture, _] of capturesSet.entries()) {
game = capture.removeStone(game); game = capture.removeStone(game);
if (capturesSet.size === 1) { if (capturesSet.size === 1) {
const liberties = getNeighbors({ point: this, Game: game }).filter(neighbor => neighbor.stone === 0); const neighbors = getNeighbors({ point: this, Game: game })
if (liberties.length === 1) { const liberties = neighbors.filter(neighbor => neighbor.stone === 0);
const groupStones = neighbors.filter(neighbor => neighbor.stone === this.stone);
if (liberties.length === 1 && groupStones.length === 0) {
capture.ko = true; capture.ko = true;
game.kos.push(capture.key)
} }
} }
} }
@ -354,11 +365,15 @@ module.exports = {
} }
Game().initGame() Game().initGame()
.makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 3 4 5 6 .makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 3 4 5 6 7
.makeMove({ player: 'white', pos: { x: 4, y: 5 } }) // 4 1 -1 .makeMove({ player: 'white', pos: { x: 5, y: 4 } }) // 4 1 1 -1
.makeMove({ player: 'black', pos: { x: 5, y: 3 } }) // 5 1 -1 1 -1 .makeMove({ player: 'black', pos: { x: 5, y: 6 } }) // 5 1 -1 -1 1 -1
.makeMove({ player: 'white', pos: { x: 5, y: 6 } }) // 6 1 -1 .makeMove({ player: 'white', pos: { x: 5, y: 7 } }) // 6 1 1 -1
.makeMove({ player: 'black', pos: { x: 6, y: 4 } }) .makeMove({ player: 'black', pos: { x: 4, y: 5 } }) // (13) at {5,6}
.makeMove({ player: 'white', pos: { x: 6, y: 5 } }) .makeMove({ player: 'white', pos: { x: 4, y: 6 } })
.makeMove({ player: 'black', pos: { x: 5, y: 5 } }) .makeMove({ player: 'black', pos: { x: 5, y: 3 } })
.makeMove({ player: 'white', pos: { x: 5, y: 4 } }) .makeMove({ player: 'white', pos: { x: 6, y: 6 } })
.makeMove({ player: 'black', pos: { x: 6, y: 5 } })
.makeMove({ player: 'white', pos: { x: 16, y: 16 } })
.makeMove({ player: 'black', pos: { x: 6, y: 4 } })
.makeMove({ player: 'white', pos: { x: 5, y: 5 } });

View file

@ -348,11 +348,12 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
.makeMove({ player: 'white', pos: { x: 16, y: 16 } }) .makeMove({ player: 'white', pos: { x: 16, y: 16 } })
.makeMove({ player: 'black', pos: { x: 6, y: 4 } }) .makeMove({ player: 'black', pos: { x: 6, y: 4 } })
.makeMove({ player: 'white', pos: { x: 5, y: 5 } }) .makeMove({ player: 'white', pos: { x: 5, y: 5 } })
.makeMove({ player: 'black', pos: { x: 5, y: 6 } })
it('snapback functions properly', done => { it('snapback functions properly', done => {
console.log(snapbackGame().boardState['5-6'])
snapbackGame() snapbackGame()
.success.should.eql(true); .makeMove({ player: 'black', pos: { x: 5, y: 6 } })
.success.should.eql(true);
done(); done();
}); });
@ -366,15 +367,23 @@ describe('capture logic: snapback, ko and playing in eyes', () => {
.makeMove({ player: 'black', pos: { x: 5, y: 5 } }) .makeMove({ player: 'black', pos: { x: 5, y: 5 } })
.makeMove({ player: 'white', pos: { x: 5, y: 4 } }) .makeMove({ player: 'white', pos: { x: 5, y: 4 } })
it('ko recognized properly on point', done => { it('ko recognized properly on Point', done => {
koGame() koGame()
.boardState['5-5'].ko.should.eql(true); .boardState['5-5'].ko.should.eql(true);
done(); done();
}) })
// ko marked on game object it('ko marked on Game object', done => {
// ko marked in legalMoves object koGame().kos.should.eql(['5-5']);
// ko cleared after move done();
});
it('ko marked in legalMoves', done => {
koGame().legalMoves['5-5'].should.eql('k');
done();
})
// ko cleared on Point after move
// ko cleared on Game after move
}) })