add toggleTerritory logic to Game

This commit is contained in:
sorrelbri 2020-06-04 23:47:25 -07:00
parent 09b346064f
commit 12f9847a0e
2 changed files with 38 additions and 6 deletions

View file

@ -125,6 +125,16 @@ const getLegalMoves = (Game) => {
return legalMoves; return legalMoves;
}; };
const getTerritory = (Game) => {
const mapTerritory = (point) => {
if (point.territory === "d") return "d";
if (point.territory > 0) return 1;
if (point.territory < 0) return -1;
};
const territory = pipeMap(mapTerritory)(Game.boardState);
return territory;
};
const getNeighbors = ({ Game, point }) => { const getNeighbors = ({ Game, point }) => {
let { top = null, btm = null, lft = null, rgt = null } = point.neighbors; let { top = null, btm = null, lft = null, rgt = null } = point.neighbors;
const { boardState, boardSize } = Game; const { boardState, boardSize } = Game;
@ -335,10 +345,21 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
return this; return this;
}, },
toggleTerritory: function () { toggleTerritory: function (key) {
// if toggleGroups(key) toggle that group if (this.turn) return { ...this, success: false };
// submit board state const groupKey = this.boardState[key].group;
return this; const group = this.groups[groupKey];
const newStones = Array.from(group.stones).forEach((point) => {
// console.log(point);
if (point.stone) {
return (point.territory = -1 * point.territory);
}
if (point.territory === "d") return (point.territory = 1);
if (point.territory > 0) return (point.territory = -1);
if (point.territory < 0) return (point.territory = "d");
});
this.groups[groupKey];
return { ...this, territory: getTerritory(this) };
}, },
endGame: function () { endGame: function () {
@ -366,7 +387,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
this.winner = this.score > 0 ? 1 : -1; this.winner = this.score > 0 ? 1 : -1;
// submit end game board state and data for study // submit end game board state and data for study
// (study module should run client side and only ) // (study module should run client side and only )
return this; return { ...this, territory: getTerritory(this) };
}, },
}; };
}; };

View file

@ -1032,6 +1032,7 @@ describe("Game end logic", () => {
honinboGame.boardState["17-4"].group.should.eql(group); honinboGame.boardState["17-4"].group.should.eql(group);
done(); done();
}); });
const isWhite = (x) => x < 0; const isWhite = (x) => x < 0;
const isBlack = (x) => x > 0; const isBlack = (x) => x > 0;
const territories = [ const territories = [
@ -1053,8 +1054,18 @@ describe("Game end logic", () => {
}); });
}); });
it.skip("end game counts territory properly", (done) => { it("end game toggleTerritory switches through territory values", (done) => {
const game = honinboGame;
const territory = game.toggleTerritory("5-14").toggleTerritory("5-14")
.territory;
territory["5-14"].should.eql("d");
territory["5-15"].should.eql("d");
done();
});
it("end game counts territory properly", (done) => {
const game = honinboGame.endGame(); const game = honinboGame.endGame();
// console.log(game.territory);
game.winner.should.eql(1); game.winner.should.eql(1);
game.score.should.eql(1.5); game.score.should.eql(1.5);
done(); done();