2020-01-31 05:03:27 +00:00
|
|
|
// index corresponds to difference in player rank
|
|
|
|
const KOMI_REC = {
|
2020-05-30 23:01:13 +00:00
|
|
|
"9": [5.5, 2.5, -0.5, -3.5, -6.5, -9.5, 12.5, 15.5, 18.5, 21.5],
|
|
|
|
"13": [5.5, 0.5, -5.5, 0.5, -5.5, 0.5, -5.5, 0.5, -5.5, 0.5],
|
|
|
|
"19": [7.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
|
|
|
const HANDI_REC = {
|
2020-05-30 23:01:13 +00:00
|
|
|
"9": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
"13": [0, 0, 0, 2, 2, 3, 3, 4, 4, 5],
|
|
|
|
"19": [0, 0, 2, 3, 4, 5, 6, 7, 8, 9],
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-04-17 06:19:39 +00:00
|
|
|
// index represents handicap placement for different board-sizes, eg handiPlace['9][1] = { (3, 3), (7, 7) }
|
2020-05-30 23:01:13 +00:00
|
|
|
// last array in each property also used for hoshi rendering
|
2020-04-17 06:19:39 +00:00
|
|
|
const HANDI_PLACE = {
|
2020-05-30 23:01:13 +00:00
|
|
|
9: [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
["7-3", "3-7"], // 2
|
|
|
|
["7-7", "7-3", "3-7"],
|
|
|
|
["3-3", "7-7", "3-7", "7-3"],
|
2020-04-17 06:19:39 +00:00
|
|
|
],
|
2020-05-30 23:01:13 +00:00
|
|
|
13: [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
["4-10", "10-4"], // 2
|
|
|
|
["10-10", "4-10", "10-4"],
|
|
|
|
["4-4", "10-10", "4-10", "10-4"],
|
|
|
|
["7-7", "4-4", "10-10", "4-10", "10-4"],
|
|
|
|
["7-4", "4-7", "4-4", "10-10", "4-10", "10-4"],
|
|
|
|
["7-7", "7-4", "4-7", "4-4", "10-10", "4-10", "10-4"],
|
|
|
|
["10-7", "7-4", "7-10", "4-7", "4-4", "10-10", "4-10", "10-4"],
|
|
|
|
["7-7", "10-7", "7-4", "7-10", "4-7", "4-4", "10-10", "4-10", "10-4"],
|
|
|
|
],
|
|
|
|
19: [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
["4-16", "16-4"], // 2
|
|
|
|
["16-16", "4-16", "16-4"],
|
|
|
|
["4-4", "16-16", "4-16", "16-4"],
|
|
|
|
["10-10", "4-4", "16-16", "4-16", "16-4"],
|
|
|
|
["10-4", "4-10", "4-4", "16-16", "4-16", "16-4"],
|
|
|
|
["10-10", "10-4", "4-10", "4-4", "16-16", "4-16", "16-4"],
|
|
|
|
["16-10", "10-4", "10-16", "4-10", "4-4", "16-16", "4-16", "16-4"],
|
|
|
|
["10-10", "16-10", "10-4", "10-16", "4-10", "4-4", "16-16", "4-16", "16-4"],
|
2020-04-17 06:19:39 +00:00
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
const getSingleItemFromSet = (set) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
let entry;
|
|
|
|
for (entry of set.entries()) {
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-03 06:00:20 +00:00
|
|
|
return entry[0];
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
const pipeMap = (...funcs) => (obj) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
const arr = Object.entries(obj).reduce((acc, [key, value], i, arr) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
funcs.forEach((func) => (value = func(value, i, arr)));
|
2020-05-03 06:00:20 +00:00
|
|
|
return [...acc, [key, value]];
|
2020-05-30 23:01:13 +00:00
|
|
|
}, []);
|
2020-05-03 06:00:20 +00:00
|
|
|
return arr.reduce((acc, [key, value]) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
return { ...acc, [key]: value };
|
2020-05-03 06:00:20 +00:00
|
|
|
}, {});
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const checkLegal = ({ point, Game }) => {
|
|
|
|
// if stone (includes ko) return false
|
|
|
|
if (point.stone) {
|
|
|
|
point.legal = false;
|
|
|
|
return point;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-30 23:01:13 +00:00
|
|
|
const neighbors = getNeighbors({ Game, point });
|
2020-05-03 06:00:20 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
const isEmpty = (point) => point.stone === 0 && point.legal === true;
|
2020-05-03 06:00:20 +00:00
|
|
|
const isEmptyAdjacent = neighbors.filter(isEmpty);
|
|
|
|
|
|
|
|
// if empty point adjacent return true
|
|
|
|
if (!isEmptyAdjacent.length) {
|
|
|
|
// if group has liberties return true
|
2020-05-30 23:01:13 +00:00
|
|
|
const isTurnStone = (neighbor) => neighbor.stone === Game.turn;
|
|
|
|
const getGroupLiberties = (point) =>
|
|
|
|
Array.from(Game.groups[point.group].liberties);
|
|
|
|
const isNotSamePoint = (liberty) =>
|
2020-05-31 04:44:52 +00:00
|
|
|
!(liberty.pos.x === point.pos.x && liberty.pos.y === point.pos.y);
|
2020-05-30 23:01:13 +00:00
|
|
|
const isInGroupWithLiberties = (neighbor) =>
|
|
|
|
getGroupLiberties(neighbor).filter(isNotSamePoint).length;
|
|
|
|
const isInLiveGroup = neighbors
|
|
|
|
.filter(isTurnStone)
|
|
|
|
.filter(isInGroupWithLiberties).length;
|
2020-05-03 06:00:20 +00:00
|
|
|
|
|
|
|
if (isInLiveGroup) {
|
|
|
|
point.legal = true;
|
|
|
|
return point;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
// if move would capture opposing group return true
|
|
|
|
if (point.capturing[Game.turn].size) {
|
|
|
|
point.legal = true;
|
|
|
|
return point;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
point.legal = false;
|
|
|
|
return point;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-03 06:00:20 +00:00
|
|
|
point.legal = true;
|
|
|
|
return point;
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const getBoardState = (Game) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
const getLegal = (point) => checkLegal({ point, Game });
|
2020-05-03 06:00:20 +00:00
|
|
|
const boardState = pipeMap(getLegal)(Game.boardState);
|
2020-05-30 23:01:13 +00:00
|
|
|
Game.kos.forEach((ko) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
boardState[ko].legal = false;
|
|
|
|
});
|
|
|
|
return boardState;
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const getLegalMoves = (Game) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
const mapLegal = (point) => (point.legal ? "l" : point.stone);
|
2020-05-03 06:00:20 +00:00
|
|
|
const legalMoves = pipeMap(mapLegal)(Game.boardState);
|
2020-05-30 23:01:13 +00:00
|
|
|
Game.kos.forEach((ko) => {
|
|
|
|
legalMoves[ko] = "k";
|
2020-05-03 06:00:20 +00:00
|
|
|
});
|
|
|
|
return legalMoves;
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-06-05 06:47:25 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const getNeighbors = ({ Game, point }) => {
|
2020-05-30 23:01:13 +00:00
|
|
|
let { top = null, btm = null, lft = null, rgt = null } = point.neighbors;
|
2020-05-03 06:00:20 +00:00
|
|
|
const { boardState, boardSize } = Game;
|
|
|
|
// boardState[0] = [ '1-1', Point({x:1, y:1, boardSize}) ]
|
|
|
|
if (top) top = boardState[top];
|
|
|
|
if (btm) btm = boardState[btm];
|
|
|
|
if (lft) lft = boardState[lft];
|
|
|
|
if (rgt) rgt = boardState[rgt];
|
2020-05-30 23:01:13 +00:00
|
|
|
return [top, btm, lft, rgt].filter((value) => value);
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
const initBoard = (game) => {
|
|
|
|
const boardState = {};
|
|
|
|
const { boardSize, handicap } = game;
|
|
|
|
for (let i = 0; i < Math.pow(boardSize, 2); i++) {
|
|
|
|
const point = Point({
|
2020-05-30 23:01:13 +00:00
|
|
|
x: Math.floor(i / boardSize) + 1,
|
|
|
|
y: (i % boardSize) + 1,
|
|
|
|
boardSize,
|
2020-05-03 06:00:20 +00:00
|
|
|
});
|
|
|
|
boardState[`${point.pos.x}-${point.pos.y}`] = point;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
if (handicap) {
|
2020-05-30 23:01:13 +00:00
|
|
|
HANDI_PLACE[boardSize][handicap].forEach((pt) => {
|
|
|
|
boardState[pt].makeMove({ ...game, boardState });
|
2020-05-03 06:00:20 +00:00
|
|
|
});
|
|
|
|
game.turn *= -1;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-03 06:00:20 +00:00
|
|
|
return boardState;
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
// returns Game object
|
2020-05-30 23:01:13 +00:00
|
|
|
const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
2020-05-05 06:22:50 +00:00
|
|
|
const helper = {
|
2020-05-30 23:01:13 +00:00
|
|
|
clearKo: function () {
|
|
|
|
this.kos.forEach((ko) => {
|
|
|
|
this.boardState[ko] = {
|
|
|
|
...this.boardState[ko],
|
|
|
|
legal: true,
|
|
|
|
ko: false,
|
|
|
|
};
|
|
|
|
});
|
2020-05-05 06:22:50 +00:00
|
|
|
this.kos = [];
|
|
|
|
},
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-05-03 06:00:20 +00:00
|
|
|
if (gameRecord.length) {
|
|
|
|
// play through all the moves
|
2020-07-01 21:32:03 +00:00
|
|
|
const game = gameRecord.reduce(
|
2020-05-30 23:01:13 +00:00
|
|
|
(game, move) => game.makeMove(move),
|
|
|
|
Game({ gameData }).initGame()
|
|
|
|
);
|
2020-07-01 21:32:03 +00:00
|
|
|
// ? why is this being wrapped?
|
2020-07-01 22:01:12 +00:00
|
|
|
if (gameData && gameData.gameData && gameData.gameData.winner) {
|
2020-07-01 21:32:03 +00:00
|
|
|
const { winner, score } = gameData.gameData;
|
|
|
|
return game.manualEnd({ winner, score });
|
|
|
|
}
|
|
|
|
return game;
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-03 06:00:20 +00:00
|
|
|
return {
|
2020-05-30 23:01:13 +00:00
|
|
|
winner: gameData.winner || null,
|
|
|
|
turn: gameData.turn || 0, // turn logic depends on handicap stones
|
|
|
|
pass: gameData.pass || 0, // -1 represents state in which resignation has been submitted, not confirmed
|
|
|
|
komi: gameData.komi || 6.5, // komi depends on handicap stones + player rank
|
|
|
|
handicap: gameData.handicap || 0,
|
|
|
|
boardSize: gameData.boardSize || 19,
|
2020-06-02 07:23:21 +00:00
|
|
|
score: 0,
|
2020-06-02 00:12:28 +00:00
|
|
|
groups: {}, // key is Symbol(position): {points: Set(), liberties: Set()}
|
2020-05-30 23:01:13 +00:00
|
|
|
boardState: {},
|
2020-06-08 00:06:26 +00:00
|
|
|
territory: {},
|
2020-05-30 23:01:13 +00:00
|
|
|
kos: [],
|
|
|
|
gameRecord: gameRecord,
|
|
|
|
playerState: gameData.playerState || {
|
2020-05-03 06:00:20 +00:00
|
|
|
bCaptures: 0,
|
|
|
|
wCaptures: 0,
|
|
|
|
bScore: 0,
|
2020-05-30 23:01:13 +00:00
|
|
|
wScore: 0,
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
initGame: function () {
|
|
|
|
this.winner = null;
|
|
|
|
this.pass = 0;
|
|
|
|
this.turn = 1;
|
2020-05-03 06:00:20 +00:00
|
|
|
this.boardState = initBoard(this);
|
|
|
|
this.boardState = getBoardState(this);
|
2020-05-30 23:01:13 +00:00
|
|
|
this.legalMoves = getLegalMoves(this);
|
2020-05-03 06:00:20 +00:00
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
addToRecord: function (moveObject) {
|
2020-05-03 06:00:20 +00:00
|
|
|
this.gameRecord.push(moveObject);
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
getMeta: function () {
|
|
|
|
// cannot be chained
|
2020-05-03 06:00:20 +00:00
|
|
|
// does not affect game object
|
|
|
|
return {
|
2020-05-30 23:01:13 +00:00
|
|
|
winner: this.winner,
|
|
|
|
turn: this.turn,
|
|
|
|
pass: this.pass,
|
|
|
|
playerState: this.playerState,
|
|
|
|
gameRecord: this.gameRecord,
|
|
|
|
boardSize: this.boardSize,
|
|
|
|
handicap: this.handicap,
|
|
|
|
komi: this.komi,
|
2020-06-20 23:56:03 +00:00
|
|
|
score: this.score,
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
|
|
|
|
2020-07-07 23:52:45 +00:00
|
|
|
checkMove: function ({ player, pos: { x, y } }) {
|
|
|
|
// if game is over
|
|
|
|
// TODO either change logic here or add additional method for handling moves off of record
|
|
|
|
if (this.pass > 1) {
|
|
|
|
return { ...this, success: false };
|
|
|
|
}
|
|
|
|
const point = this.boardState[`${x}-${y}`];
|
|
|
|
const isTurn =
|
|
|
|
(this.turn === 1 && player === "black") ||
|
|
|
|
(this.turn === -1 && player === "white");
|
|
|
|
if (isTurn) {
|
|
|
|
if (point.legal) {
|
|
|
|
return { ...this, success: true, move: { player, pos: { x, y } } };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return { ...this, success: false };
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
makeMove: function ({ player, pos: { x, y } }) {
|
|
|
|
if (this.pass > 1) {
|
|
|
|
return { ...this, success: false };
|
|
|
|
}
|
2020-07-20 16:52:11 +00:00
|
|
|
if (x === 0) return game.submitPass(player);
|
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
let success = false;
|
2020-07-08 00:08:02 +00:00
|
|
|
let game = this;
|
|
|
|
|
|
|
|
// if checkMove has not been run, determine legality
|
|
|
|
if (!game.move) {
|
|
|
|
game = game.checkMove({ player, pos: { x, y } });
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
// if move is legal
|
|
|
|
// ? unclear if checking move values is beneficial to prevent race conditions
|
|
|
|
game.move &&
|
|
|
|
game.move.player === player &&
|
|
|
|
game.move.pos.x === x &&
|
|
|
|
game.move.pos.y
|
|
|
|
) {
|
|
|
|
const point = game.boardState[`${x}-${y}`];
|
|
|
|
game.pass = 0;
|
|
|
|
// allows for recording of prior move on game record
|
|
|
|
game.addToRecord(game.move);
|
|
|
|
if (game.kos.length) helper.clearKo.call(game);
|
|
|
|
point.makeMove(game);
|
|
|
|
game.turn *= -1;
|
|
|
|
success = true;
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|
|
|
|
game.boardState = getBoardState(game);
|
2020-07-08 00:08:02 +00:00
|
|
|
// remove move attribute to prevent duplicate moves
|
|
|
|
delete game.move;
|
2020-05-30 23:01:13 +00:00
|
|
|
return { ...game, legalMoves: getLegalMoves(game), success };
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
initGroup: function (point) {
|
2020-05-03 06:00:20 +00:00
|
|
|
const group = Symbol(`${point.pos.x}-${point.pos.y}`);
|
2020-05-30 23:01:13 +00:00
|
|
|
this.groups[group] = { stones: new Set(), liberties: new Set() };
|
2020-05-03 06:00:20 +00:00
|
|
|
return { game: this, group };
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
returnToMove: function (lastMove) {
|
2020-05-03 06:00:20 +00:00
|
|
|
const { komi, handicap, boardSize } = this;
|
|
|
|
if (lastMove === 0) {
|
|
|
|
return Game({
|
2020-05-30 23:01:13 +00:00
|
|
|
gameData: { komi, handicap, boardSize },
|
2020-05-03 06:00:20 +00:00
|
|
|
}).initGame();
|
|
|
|
}
|
|
|
|
const length = this.gameRecord.length;
|
|
|
|
const index = lastMove < 0 ? length + lastMove : lastMove;
|
|
|
|
if (lastMove >= length && lastMove > 0) return this;
|
2020-05-30 23:01:13 +00:00
|
|
|
return Game({
|
|
|
|
gameData: { komi, handicap, boardSize },
|
|
|
|
gameRecord: [...this.gameRecord.slice(0, index)],
|
2020-05-03 06:00:20 +00:00
|
|
|
});
|
2020-05-15 03:59:49 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
submitPass: function (player) {
|
|
|
|
if (player !== "black" && player !== "white") {
|
2020-05-15 06:21:19 +00:00
|
|
|
return { ...this, success: false };
|
|
|
|
}
|
2020-05-16 05:00:08 +00:00
|
|
|
if (this.pass > 0) {
|
2020-06-02 05:53:02 +00:00
|
|
|
return this.calculateTerritory();
|
2020-05-16 05:00:08 +00:00
|
|
|
}
|
|
|
|
this.pass = 1;
|
2020-05-15 06:21:19 +00:00
|
|
|
this.addToRecord({ player, pos: { x: null, y: null } });
|
|
|
|
if (this.kos.length) helper.clearKo.call(this);
|
2020-05-30 23:01:13 +00:00
|
|
|
this.turn = player === "black" ? -1 : 1;
|
2020-05-15 06:21:19 +00:00
|
|
|
this.boardState = getBoardState(this);
|
2020-05-30 23:01:13 +00:00
|
|
|
return { ...this, legalMoves: getLegalMoves(this), success: true };
|
2020-05-15 06:21:19 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
submitResign: function (player) {
|
|
|
|
if (player === "black") this.winner = -1;
|
|
|
|
if (player === "white") this.winner = 1;
|
2020-05-15 03:59:49 +00:00
|
|
|
this.turn = 0;
|
|
|
|
return this;
|
2020-05-16 05:00:08 +00:00
|
|
|
},
|
|
|
|
|
2020-06-02 05:53:02 +00:00
|
|
|
calculateTerritory: function () {
|
2020-05-16 05:00:08 +00:00
|
|
|
// TODO manage territory counting
|
2020-05-30 23:01:13 +00:00
|
|
|
// form groups for empty points
|
2020-06-01 06:27:38 +00:00
|
|
|
const joinEmptyPoints = (point) => {
|
|
|
|
if (point.stone) return point;
|
2020-06-02 00:12:28 +00:00
|
|
|
point.joinEmptyPoints({ point, Game: this });
|
2020-06-01 22:58:43 +00:00
|
|
|
return point;
|
2020-06-01 06:27:38 +00:00
|
|
|
};
|
2020-06-01 22:58:43 +00:00
|
|
|
// for each point determine territory
|
|
|
|
const determineTerritory = (point) => {
|
|
|
|
if (point.stone || point.territory) return point;
|
|
|
|
const points = Array.from(this.groups[point.group].stones);
|
|
|
|
const neighbors = points.flatMap((pt) =>
|
|
|
|
getNeighbors({ Game: this, point: pt })
|
|
|
|
);
|
|
|
|
const stones = neighbors.filter((point) => point.stone !== 0);
|
|
|
|
point.territory =
|
|
|
|
stones.reduce((acc, { stone }) => acc + stone, 0) || "d";
|
|
|
|
return point;
|
|
|
|
};
|
2020-06-02 00:12:28 +00:00
|
|
|
// for each non-empty point group determine life
|
|
|
|
const determineLife = (point) => {
|
|
|
|
if (!point.stone || point.territory) return point;
|
|
|
|
const liberties = Array.from(this.groups[point.group].liberties);
|
|
|
|
point.territory =
|
2020-06-02 07:23:21 +00:00
|
|
|
liberties.reduce(
|
|
|
|
(acc, { territory }) => (territory === "d" ? acc : acc + territory),
|
|
|
|
0
|
|
|
|
) || point.stone;
|
2020-06-02 00:12:28 +00:00
|
|
|
return point;
|
|
|
|
};
|
2020-06-01 22:58:43 +00:00
|
|
|
let boardState = pipeMap(joinEmptyPoints)(this.boardState);
|
|
|
|
boardState = pipeMap(determineTerritory)(boardState);
|
2020-06-02 00:12:28 +00:00
|
|
|
boardState = pipeMap(determineLife)(boardState);
|
2020-06-01 22:58:43 +00:00
|
|
|
this.boardState = boardState;
|
2020-05-30 23:01:13 +00:00
|
|
|
// submit board state to users
|
2020-06-02 05:53:02 +00:00
|
|
|
this.turn = 0;
|
2020-06-08 00:06:26 +00:00
|
|
|
return { ...this, territory: getTerritory(this) };
|
2020-06-02 05:53:02 +00:00
|
|
|
},
|
|
|
|
|
2020-06-05 06:47:25 +00:00
|
|
|
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) };
|
2020-06-02 05:53:02 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
endGame: function () {
|
2020-05-30 23:01:13 +00:00
|
|
|
// if boardState is approved calculate winner
|
2020-06-02 07:23:21 +00:00
|
|
|
const [blackTerritory, whiteTerritory] = Object.values(
|
|
|
|
this.boardState
|
|
|
|
).reduce(
|
|
|
|
([blackTerritory, whiteTerritory], { territory, stone }) => {
|
|
|
|
if (territory === "d") return [blackTerritory, whiteTerritory];
|
|
|
|
if (territory > 0) {
|
|
|
|
if (stone === -1) return [blackTerritory + 2, whiteTerritory];
|
|
|
|
return [blackTerritory + 1, whiteTerritory];
|
|
|
|
}
|
|
|
|
if (territory < 0) {
|
|
|
|
if (stone === 1) return [blackTerritory, whiteTerritory + 2];
|
|
|
|
return [blackTerritory, whiteTerritory + 1];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[0, 0]
|
|
|
|
);
|
|
|
|
this.playerState.bScore = blackTerritory + this.playerState.bCaptures;
|
|
|
|
this.playerState.wScore = whiteTerritory + this.playerState.wCaptures;
|
2020-06-06 05:54:19 +00:00
|
|
|
const score =
|
2020-06-02 07:23:21 +00:00
|
|
|
this.playerState.bScore - (this.playerState.wScore + this.komi);
|
2020-06-06 05:54:19 +00:00
|
|
|
return { ...this, score, winner: score > 0 ? 1 : -1 };
|
2020-05-30 23:01:13 +00:00
|
|
|
},
|
2020-07-01 21:32:03 +00:00
|
|
|
|
|
|
|
// for playing historic games
|
|
|
|
manualEnd: function ({ winner, score }) {
|
|
|
|
this.turn = 0;
|
|
|
|
this.winner = winner;
|
|
|
|
this.score = score;
|
|
|
|
return this;
|
|
|
|
},
|
2020-05-30 23:01:13 +00:00
|
|
|
};
|
2020-05-03 06:00:20 +00:00
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
const Point = ({ x, y, boardSize = 19 }) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
let point = {
|
2020-05-30 23:01:13 +00:00
|
|
|
pos: { x, y },
|
|
|
|
key: `${x}-${y}`,
|
|
|
|
stone: 0, // can be 1, -1, 0,
|
|
|
|
ko: false,
|
|
|
|
legal: true,
|
|
|
|
territory: 0,
|
|
|
|
capturing: {
|
|
|
|
"1": new Set(),
|
|
|
|
"-1": new Set(),
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
2020-05-30 23:01:13 +00:00
|
|
|
group: null,
|
2020-05-03 06:00:20 +00:00
|
|
|
neighbors: {
|
2020-05-30 23:01:13 +00:00
|
|
|
top: x > 1 ? `${x - 1}-${y}` : null,
|
|
|
|
btm: x < boardSize ? `${x + 1}-${y}` : null,
|
|
|
|
rgt: y < boardSize ? `${x}-${y + 1}` : null,
|
|
|
|
lft: y > 1 ? `${x}-${y - 1}` : null,
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
makeMove: function (Game) {
|
2020-05-03 06:00:20 +00:00
|
|
|
this.stone = Game.turn;
|
|
|
|
this.legal = false;
|
|
|
|
if (this.capturing[this.stone].size) {
|
|
|
|
Game = this.makeCaptures(Game);
|
|
|
|
}
|
|
|
|
Game = this.joinGroup({ point: this, Game });
|
|
|
|
return this.checkCaptures(Game);
|
|
|
|
},
|
2020-05-30 23:01:13 +00:00
|
|
|
|
|
|
|
joinGroup: function ({ point, Game }) {
|
2020-05-03 06:00:20 +00:00
|
|
|
if (point.group !== this.group || !point.group) {
|
|
|
|
// if point has no group set current group to new Symbol in game object
|
|
|
|
if (!point.group) {
|
|
|
|
const { game, group } = Game.initGroup(point);
|
|
|
|
this.group = group;
|
|
|
|
Game = game;
|
|
|
|
}
|
2020-05-30 23:01:13 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
// add current point to global group and override current group
|
|
|
|
Game.groups[point.group].stones.add(this);
|
|
|
|
if (this.group !== point.group) {
|
|
|
|
this.group = point.group;
|
|
|
|
}
|
|
|
|
Game = this.setLiberties(Game);
|
2020-05-30 23:01:13 +00:00
|
|
|
getNeighbors({ point: this, Game }).forEach((neighbor) => {
|
|
|
|
if (
|
|
|
|
neighbor.stone === this.stone &&
|
2020-05-03 06:00:20 +00:00
|
|
|
// this check prevents infinite call chains
|
2020-05-30 23:01:13 +00:00
|
|
|
neighbor.group !== this.group
|
2020-05-03 06:00:20 +00:00
|
|
|
) {
|
|
|
|
Game = neighbor.joinGroup({ point: this, Game });
|
|
|
|
}
|
2020-05-30 23:01:13 +00:00
|
|
|
});
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|
|
|
|
return Game;
|
|
|
|
},
|
|
|
|
|
2020-06-02 00:12:28 +00:00
|
|
|
joinEmptyPoints: function ({ point, Game }) {
|
|
|
|
if (point.group !== this.group || !point.group) {
|
|
|
|
if (!point.group) {
|
|
|
|
const { game, group } = Game.initGroup(point);
|
|
|
|
this.group = group;
|
|
|
|
Game = game;
|
|
|
|
}
|
|
|
|
Game.groups[point.group].stones.add(this);
|
|
|
|
if (this.group !== point.group) {
|
|
|
|
this.group = point.group;
|
|
|
|
}
|
|
|
|
getNeighbors({ point: this, Game }).forEach((neighbor) => {
|
|
|
|
if (
|
|
|
|
neighbor.stone === this.stone &&
|
|
|
|
// this check prevents infinite call chains
|
|
|
|
neighbor.group !== this.group
|
|
|
|
) {
|
|
|
|
Game = neighbor.joinEmptyPoints({ point: this, Game });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return Game;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
setLiberties: function (Game) {
|
2020-05-03 06:00:20 +00:00
|
|
|
const neighbors = getNeighbors({ point: this, Game });
|
|
|
|
const liberties = Game.groups[this.group].liberties;
|
|
|
|
// if point is occupied remove it from liberties set of point group, else add it
|
2020-05-30 23:01:13 +00:00
|
|
|
neighbors.forEach((neighbor) => {
|
2020-05-03 06:00:20 +00:00
|
|
|
if (neighbor.stone !== 0) {
|
|
|
|
liberties.delete(neighbor);
|
|
|
|
Game.groups[neighbor.group].liberties.delete(this);
|
2020-05-30 23:01:13 +00:00
|
|
|
}
|
2020-05-03 06:00:20 +00:00
|
|
|
if (neighbor.stone === 0) {
|
2020-05-30 23:01:13 +00:00
|
|
|
liberties.add(neighbor);
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return Game;
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
checkCaptures: function (game) {
|
2020-05-03 06:00:20 +00:00
|
|
|
// if this stone has one liberty
|
|
|
|
const liberties = game.groups[this.group].liberties;
|
|
|
|
if (liberties.size === 1) {
|
|
|
|
const lastLiberty = getSingleItemFromSet(liberties);
|
|
|
|
lastLiberty.capturing[this.stone * -1].add(this.group);
|
2020-01-31 05:03:27 +00:00
|
|
|
}
|
2020-05-12 23:47:17 +00:00
|
|
|
if (liberties.size > 1) {
|
2020-05-30 23:01:13 +00:00
|
|
|
liberties.forEach((liberty) =>
|
|
|
|
liberty.capturing[this.stone * -1].delete(this.group)
|
|
|
|
);
|
2020-05-12 23:47:17 +00:00
|
|
|
}
|
2020-01-31 05:03:27 +00:00
|
|
|
|
2020-05-03 06:00:20 +00:00
|
|
|
// if neighbors have one liberty
|
2020-05-30 23:01:13 +00:00
|
|
|
const neighbors = getNeighbors({ point: this, Game: game }).filter(
|
|
|
|
(neighbor) => neighbor.stone === -1 * this.stone
|
|
|
|
);
|
|
|
|
neighbors.forEach((neighbor) => {
|
|
|
|
const liberties =
|
|
|
|
game.groups[neighbor.group] && game.groups[neighbor.group].liberties;
|
2020-05-03 06:00:20 +00:00
|
|
|
if (liberties && liberties.size === 1) {
|
|
|
|
const lastLiberty = getSingleItemFromSet(liberties);
|
|
|
|
lastLiberty.capturing[neighbor.stone * -1].add(neighbor.group);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return game;
|
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
makeCaptures: function (game) {
|
2020-05-03 06:00:20 +00:00
|
|
|
// for each group
|
|
|
|
for (let [captureGroup, _] of this.capturing[this.stone].entries()) {
|
|
|
|
const capturesSet = game.groups[captureGroup].stones;
|
|
|
|
for (let [capture, _] of capturesSet.entries()) {
|
|
|
|
game = capture.removeStone(game);
|
|
|
|
if (capturesSet.size === 1) {
|
2020-05-30 23:01:13 +00:00
|
|
|
const neighbors = getNeighbors({ point: this, Game: game });
|
|
|
|
const liberties = neighbors.filter(
|
|
|
|
(neighbor) => neighbor.stone === 0
|
|
|
|
);
|
|
|
|
const groupStones = neighbors.filter(
|
|
|
|
(neighbor) => neighbor.stone === this.stone
|
|
|
|
);
|
2020-05-03 06:00:20 +00:00
|
|
|
if (liberties.length === 1 && groupStones.length === 0) {
|
|
|
|
capture.ko = true;
|
2020-05-30 23:01:13 +00:00
|
|
|
game.kos.push(capture.key);
|
2020-05-03 06:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// points with stones cannot be played to capture
|
2020-05-30 23:01:13 +00:00
|
|
|
this.capturing = { "1": new Set(), "-1": new Set() };
|
|
|
|
return { ...game, boardState: { ...game.boardState, [this.key]: this } };
|
2020-05-03 06:00:20 +00:00
|
|
|
},
|
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
removeStone: function (game) {
|
|
|
|
if ((this.stone = 0)) {
|
2020-05-03 06:00:20 +00:00
|
|
|
return game;
|
|
|
|
}
|
|
|
|
// reset point
|
|
|
|
this.stone = 0;
|
|
|
|
this.group = null;
|
|
|
|
this.capturing[game.turn] = new Set();
|
|
|
|
// add captures
|
2020-05-30 23:01:13 +00:00
|
|
|
const player = game.turn > 0 ? "b" : "w";
|
2020-05-03 06:00:20 +00:00
|
|
|
game.playerState[`${player}Captures`] += 1;
|
2020-05-12 23:47:17 +00:00
|
|
|
|
|
|
|
// add as liberty to neighbors
|
2020-05-30 23:01:13 +00:00
|
|
|
const neighbors = getNeighbors({ point: this, Game: game }).filter(
|
|
|
|
(neighbor) => neighbor.stone !== 0 && neighbor.group
|
|
|
|
);
|
|
|
|
neighbors.forEach((neighbor) => {
|
2020-05-12 23:47:17 +00:00
|
|
|
game.groups[neighbor.group].liberties.add(this);
|
2020-05-30 23:01:13 +00:00
|
|
|
neighbor.checkCaptures(game);
|
|
|
|
});
|
2020-05-12 23:47:17 +00:00
|
|
|
|
2020-05-30 23:01:13 +00:00
|
|
|
return { ...game, boardState: { ...game.boardState, [this.key]: this } };
|
|
|
|
},
|
|
|
|
};
|
2020-05-03 06:00:20 +00:00
|
|
|
for (let [key, value] of Object.entries(point.neighbors)) {
|
|
|
|
if (value) continue;
|
|
|
|
delete point.neighbors[key];
|
|
|
|
}
|
|
|
|
return point;
|
|
|
|
};
|
2020-01-31 05:03:27 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2020-05-03 06:00:20 +00:00
|
|
|
Game,
|
2020-05-30 23:01:13 +00:00
|
|
|
Point,
|
|
|
|
};
|