add makeCaptures to Point

This commit is contained in:
Sorrel Bri 2020-04-29 22:24:27 -07:00
parent 13a882d212
commit a5fbeea929
2 changed files with 45 additions and 26 deletions

View file

@ -99,18 +99,11 @@ const checkLegal = ({ point, Game }) => {
return { ...point, isInLiveGroup }; return { ...point, isInLiveGroup };
} }
// if move would capture opposing group // if move would capture opposing group return true
// set capturing object and return true if (point.capturing[Game.turn].length) {
// const isNotTurnStone = neighbor => neighbor.stone === Game.turn * -1; point.legal = true;
// const isInGroupWithLastLiberty = neighbor => getGroupLiberties(neighbor).filter(isNotSamePoint) return point;
// // .length === 0; }
// const isCapturing = Object.values(point.neighbors).filter(isNotTurnStone).filter(isInGroupWithLastLiberty)
// // .length;
// if (isCapturing) {
// point.legal = true;
// return { ...point, isCapturing };
// }
point.legal = false; point.legal = false;
return { ...point, adj: isEmptyAdjacent, isInLiveGroup }; return { ...point, adj: isEmptyAdjacent, isInLiveGroup };
@ -225,7 +218,10 @@ const Point = ({x, y, boardSize = 19}) => ({
stone: 0, // can be 1, -1, 0, or 'k' for ko stone: 0, // can be 1, -1, 0, or 'k' for ko
legal: true, legal: true,
territory: 0, territory: 0,
capturing: [], capturing: {
'1': [],
'-1': []
},
group: null, group: null,
neighbors: { neighbors: {
top: x > 1 ? `${ x - 1 }-${ y }` : null, top: x > 1 ? `${ x - 1 }-${ y }` : null,
@ -237,6 +233,9 @@ const Point = ({x, y, boardSize = 19}) => ({
makeMove: function(game) { makeMove: function(game) {
this.stone = game.turn; this.stone = game.turn;
this.legal = false; this.legal = false;
if (this.capturing[this.stone].length) {
this.makeCaptures(game);
}
this.joinGroup({ point: this, game }); this.joinGroup({ point: this, game });
return this.checkCaptures(game); return this.checkCaptures(game);
}, },
@ -282,18 +281,36 @@ const Point = ({x, y, boardSize = 19}) => ({
const liberties = game.groups[this.group].liberties; const liberties = game.groups[this.group].liberties;
if (liberties.size === 1) { if (liberties.size === 1) {
const lastLiberty = getSingleItemFromSet(liberties); const lastLiberty = getSingleItemFromSet(liberties);
lastLiberty.capturing = this; lastLiberty.capturing[this.stone * -1].push(this.group);
} }
// if neighbors have one liberty // if neighbors have one liberty
const neighbors = Object.values(this.neighbors); const neighbors = Object.values(this.neighbors).filter(neighbor => neighbor.stone === -1 * this.stone)
neighbors.filter(neighbor => neighbor.stone).forEach( neighbor => { neighbors.forEach( neighbor => {
const liberties = game.groups[neighbor.group].liberties; const liberties = game.groups[neighbor.group] && game.groups[neighbor.group].liberties;
if (liberties.size === 1) { if (liberties && liberties.size === 1) {
const lastLiberty = getSingleItemFromSet(liberties); const lastLiberty = getSingleItemFromSet(liberties);
lastLiberty.capturing = neighbor; lastLiberty.capturing[neighbor.stone * -1].push(neighbor.group);
} }
}) })
},
makeCaptures: function(game) {
// for each group
this.capturing[this.stone].forEach(captureGroup => {
const capturesSet = game.groups[captureGroup].stones;
for (let [capture] of capturesSet.entries()) {
capture.removeStone(game);
}
})
},
removeStone: function(game) {
// reset point
this.stone = 0;
this.group = null;
this.capturing[game.turn] = [];
// add captures
} }
}); });

View file

@ -212,7 +212,7 @@ describe('makeMove group join and capture logic', () => {
}); });
it('group with only remaining liberty at point to be played returns success: false', done => { it('group with only remaining liberty at point to be played returns success: false', done => {
Game({ gameData: { handicap: 2 } }).initGame() const point = Game({ gameData: { handicap: 2 } }).initGame()
.makeMove({ player: 'white', pos: { x: 4, y: 15 } }) // 3 4 5 6 .makeMove({ player: 'white', pos: { x: 4, y: 15 } }) // 3 4 5 6
.makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 15 -1 -1 .makeMove({ player: 'black', pos: { x: 4, y: 4 } }) // 15 -1 -1
.makeMove({ player: 'white', pos: { x: 5, y: 15 } }) // 16 -1 1h 0 -1 .makeMove({ player: 'white', pos: { x: 5, y: 15 } }) // 16 -1 1h 0 -1
@ -224,6 +224,8 @@ describe('makeMove group join and capture logic', () => {
.makeMove({ player: 'white', pos: { x: 4, y: 17 } }) .makeMove({ player: 'white', pos: { x: 4, y: 17 } })
.makeMove({ player: 'black', pos: { x: 10, y: 16 } }) .makeMove({ player: 'black', pos: { x: 10, y: 16 } })
.makeMove({ player: 'white', pos: { x: 5, y: 17 } }) .makeMove({ player: 'white', pos: { x: 5, y: 17 } })
console.log(point.boardState['5-16']);
point
.makeMove({ player: 'black', pos: { x: 5, y: 16 } }) .makeMove({ player: 'black', pos: { x: 5, y: 16 } })
.success.should.eql(false); .success.should.eql(false);
done(); done();
@ -244,15 +246,15 @@ describe('makeMove group join and capture logic', () => {
}); });
it('makeMove assesses captures', done => { it('makeMove assesses captures', done => {
(!!captureGame.boardState['4-17'].capturing).should.eql(true); captureGame.boardState['4-17'].capturing[-1].length.should.eql(1);
done(); done();
}) })
// it('makeMove capture removes captured stone', done => { it('makeMove capture removes captured stone', done => {
// captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } }) captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })
// .boardState['4-16'].stone.should.eql(0); .boardState['4-16'].stone.should.eql(0);
// done(); done();
// }); });
// it('makeMove capture increases capturing players captures', done => { // it('makeMove capture increases capturing players captures', done => {
// captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } }) // captureGame.makeMove({ player: 'white', pos: { x: 4, y: 17 } })