From 12f9847a0ead4489c99949a351a22a7ca3e7af79 Mon Sep 17 00:00:00 2001 From: sorrelbri Date: Thu, 4 Jun 2020 23:47:25 -0700 Subject: [PATCH] add toggleTerritory logic to Game --- packages/server/services/Game.js | 31 ++++++++++++++++++++++++++----- packages/server/test/Game.spec.js | 13 ++++++++++++- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/packages/server/services/Game.js b/packages/server/services/Game.js index eac2160..cffd3b4 100644 --- a/packages/server/services/Game.js +++ b/packages/server/services/Game.js @@ -125,6 +125,16 @@ const getLegalMoves = (Game) => { 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 }) => { let { top = null, btm = null, lft = null, rgt = null } = point.neighbors; const { boardState, boardSize } = Game; @@ -335,10 +345,21 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => { return this; }, - toggleTerritory: function () { - // if toggleGroups(key) toggle that group - // submit board state - return this; + toggleTerritory: function (key) { + if (this.turn) return { ...this, success: false }; + const groupKey = this.boardState[key].group; + 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 () { @@ -366,7 +387,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => { this.winner = this.score > 0 ? 1 : -1; // submit end game board state and data for study // (study module should run client side and only ) - return this; + return { ...this, territory: getTerritory(this) }; }, }; }; diff --git a/packages/server/test/Game.spec.js b/packages/server/test/Game.spec.js index afd8e88..b0712bb 100644 --- a/packages/server/test/Game.spec.js +++ b/packages/server/test/Game.spec.js @@ -1032,6 +1032,7 @@ describe("Game end logic", () => { honinboGame.boardState["17-4"].group.should.eql(group); done(); }); + const isWhite = (x) => x < 0; const isBlack = (x) => x > 0; 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(); + // console.log(game.territory); game.winner.should.eql(1); game.score.should.eql(1.5); done();