node-go/packages/server/services/Game.js

482 lines
14 KiB
JavaScript
Raw Normal View History

// 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],
};
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-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-05-03 06:00:20 +00:00
return entry[0];
2020-05-30 23:01:13 +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-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-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-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-05-03 06:00:20 +00:00
point.legal = false;
return point;
}
2020-05-03 06:00:20 +00:00
point.legal = true;
return point;
2020-05-30 23:01:13 +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-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-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-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-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-05-03 06:00:20 +00:00
return boardState;
2020-05-30 23:01:13 +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 = [] } = {}) => {
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,
};
});
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-05-30 23:01:13 +00:00
return gameRecord.reduce(
(game, move) => game.makeMove(move),
Game({ gameData }).initGame()
);
}
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,
groups: {},
boardState: {},
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-05-03 06:00:20 +00:00
},
2020-05-30 23:01:13 +00:00
makeMove: function ({ player, pos: { x, y } }) {
if (this.pass > 1) {
return { ...this, success: false };
}
2020-05-03 06:00:20 +00:00
let game = this;
let success = false;
const point = game.boardState[`${x}-${y}`];
2020-05-30 23:01:13 +00:00
const isTurn =
(game.turn === 1 && player === "black") ||
(game.turn === -1 && player === "white");
2020-05-03 06:00:20 +00:00
if (isTurn) {
if (point.legal) {
game.pass = 0;
2020-05-03 06:00:20 +00:00
game.addToRecord({ player, pos: { x, y } });
if (this.kos.length) helper.clearKo.call(this);
2020-05-03 06:00:20 +00:00
point.makeMove(game);
game.turn *= -1;
success = true;
}
}
game.boardState = getBoardState(game);
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 };
}
if (this.pass > 0) {
return this.endGame();
}
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-30 23:01:13 +00:00
endGame: function () {
// TODO manage territory counting
2020-05-30 23:01:13 +00:00
// form groups for empty points
const joinEmptyPoints = (point) => {
if (point.stone) return point;
return point.joinGroup({ point, Game: this });
};
const boardState = pipeMap(joinEmptyPoints)(this.boardState);
2020-05-30 23:01:13 +00:00
// for each empty point group determine territory
// for each non-empty point group determine life
// submit board state to users
// if boardState is approved calculate winner
// submit end game board state and data for study
// (study module should run client side and only )
// if toggleGroups(key) toggle that group
// submit board state to winner
this.turn = 0;
return this;
2020-05-30 23:01:13 +00:00
},
};
2020-05-03 06:00:20 +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-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-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-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;
};
module.exports = {
2020-05-03 06:00:20 +00:00
Game,
2020-05-30 23:01:13 +00:00
Point,
};