add checkCaptures to Point, called during makeMove

This commit is contained in:
Sorrel Bri 2020-04-29 21:02:01 -07:00
parent 96af52823d
commit 13a882d212
2 changed files with 71 additions and 26 deletions

View file

@ -56,6 +56,13 @@ const HANDI_PLACE = {
]
};
const getSingleItemFromSet = set => {
let entry;
for (entry of set.entries()) {
}
return entry[0];
}
const pipeMap = (...funcs) => obj => {
const arr = Object.entries(obj).reduce((acc, [key, value], i, arr) => {
@ -92,11 +99,22 @@ const checkLegal = ({ point, Game }) => {
return { ...point, isInLiveGroup };
}
// if move would capture opposing group
// set capturing object and return true
// const isNotTurnStone = neighbor => neighbor.stone === Game.turn * -1;
// const isInGroupWithLastLiberty = neighbor => getGroupLiberties(neighbor).filter(isNotSamePoint)
// // .length === 0;
// const isCapturing = Object.values(point.neighbors).filter(isNotTurnStone).filter(isInGroupWithLastLiberty)
// // .length;
// if (isCapturing) {
// point.legal = true;
// return { ...point, isCapturing };
// }
point.legal = false;
return { ...point, adj: isEmptyAdjacent, isInLiveGroup };
}
// if move would capture opposing group
// set capturing object and return true
point.legal = true;
point.adj = isEmptyAdjacent;
return point;
@ -219,7 +237,8 @@ const Point = ({x, y, boardSize = 19}) => ({
makeMove: function(game) {
this.stone = game.turn;
this.legal = false;
return this.joinGroup({ point: this, game });
this.joinGroup({ point: this, game });
return this.checkCaptures(game);
},
joinGroup: function({ point, game }) {
@ -256,6 +275,25 @@ const Point = ({x, y, boardSize = 19}) => ({
liberties.add(pt)
}
});
},
checkCaptures: function(game) {
// if this stone has one liberty
const liberties = game.groups[this.group].liberties;
if (liberties.size === 1) {
const lastLiberty = getSingleItemFromSet(liberties);
lastLiberty.capturing = this;
}
// if neighbors have one liberty
const neighbors = Object.values(this.neighbors);
neighbors.filter(neighbor => neighbor.stone).forEach( neighbor => {
const liberties = game.groups[neighbor.group].liberties;
if (liberties.size === 1) {
const lastLiberty = getSingleItemFromSet(liberties);
lastLiberty.capturing = neighbor;
}
})
}
});

View file

@ -193,7 +193,6 @@ describe('Game.makeMove({ player: str, pos: { x: int, y: int } })', () => {
.makeMove({ player: 'white', pos: { x: 16, y: 16 }}).makeMove({ player: 'black', pos: { x: 5, y: 15 } }) // 5 1 x 1
.makeMove({ player: 'white', pos: { x: 16, y: 10 }}).makeMove({ player: 'black', pos: { x: 5, y: 17 } }) // 6 1
.makeMove({ player: 'white', pos: { x: 5, y: 16 }})
console.log(point.boardState['5-16'])
point.success.should.eql(false);
done();
});
@ -230,28 +229,36 @@ describe('makeMove group join and capture logic', () => {
done();
})
// const captureGame = Game({ gameData: { handicap: 2 } }).initGame()
// .makeMove({ player: 'white', pos: { x: 4, y: 15 } }) // 3 4 5
// .makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 15 -1
// .makeMove({ player: 'white', pos: { x: 3, y: 16 } }) // 16 -1 0 -1
// .makeMove({ player: 'black', pos: { x: 4, y: 10 } }) // 17 -1
// .makeMove({ player: 'white', pos: { x: 5, y: 16 } }) // 4,16 captured
// .makeMove({ player: 'black', pos: { x: 10, y: 4 } })
// .makeMove({ player: 'white', pos: { x: 4, y: 17 } })
// it('makeMove capture smoke test', done => {
// captureGame.success.should.eql(true);
// done();
// });
// it('makeMove capture removes captured stone', done => {
// captureGame.boardState['4-16'].stone.should.eql(0);
// done();
// });
// it('makeMove capture increases capturing players captures', done => {
// captureGame.playerState.wCaptures.should.eql(1);
// })
const captureGame = Game({ gameData: { handicap: 2 } }).initGame()
.makeMove({ player: 'white', pos: { x: 4, y: 15 } }) // 3 4 5
.makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 15 -1
.makeMove({ player: 'white', pos: { x: 3, y: 16 } }) // 16 -1 0 -1
.makeMove({ player: 'black', pos: { x: 4, y: 10 } }) // 17 -1
.makeMove({ player: 'white', pos: { x: 5, y: 16 } }) // 4,16 captured
.makeMove({ player: 'black', pos: { x: 10, y: 4 } })
it('makeMove capture smoke test', done => {
captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })
.success.should.eql(true);
done();
});
it('makeMove assesses captures', done => {
(!!captureGame.boardState['4-17'].capturing).should.eql(true);
done();
})
// it('makeMove capture removes captured stone', done => {
// captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })
// .boardState['4-16'].stone.should.eql(0);
// done();
// });
// it('makeMove capture increases capturing players captures', done => {
// captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })
// .playerState.wCaptures.should.eql(1);
// done();
// })
})