2020-04-19 03:04:00 +00:00
|
|
|
/*----- constants -----*/
|
|
|
|
const STONES_DATA = {
|
|
|
|
'-1': 'white',
|
|
|
|
'0': 'none',
|
|
|
|
'1': 'black',
|
|
|
|
'k': 'ko'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// index corresponds to difference in player rank
|
|
|
|
const KOMI_REC = {
|
|
|
|
'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 = {
|
|
|
|
'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
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
// index represents handicap placement for different board-sizes, eg handiPlace['9][1] = { (3, 3), (7, 7) }
|
|
|
|
// last array in each property also used for hoshi rendering
|
|
|
|
const HANDI_PLACE = {
|
2020-04-19 22:02:09 +00:00
|
|
|
9 : [
|
2020-04-19 03:04:00 +00:00
|
|
|
0, 0,
|
2020-04-19 22:02:09 +00:00
|
|
|
[ '7-3', '3-7' ], // 2
|
|
|
|
[ '7-7', '7-3', '3-7' ],
|
|
|
|
[ '3-3', '7-7', '3-7', '7-3' ]
|
2020-04-19 03:04:00 +00:00
|
|
|
],
|
2020-04-19 22:02:09 +00:00
|
|
|
13 : [
|
2020-04-19 03:04:00 +00:00
|
|
|
0, 0,
|
2020-04-19 22:02:09 +00:00
|
|
|
[ '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' ],
|
2020-04-19 03:04:00 +00:00
|
|
|
],
|
2020-04-19 22:02:09 +00:00
|
|
|
19 : [
|
2020-04-19 03:04:00 +00:00
|
|
|
0, 0,
|
2020-04-19 22:02:09 +00:00
|
|
|
[ '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-19 03:04:00 +00:00
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2020-04-19 22:02:09 +00:00
|
|
|
|
2020-04-19 03:04:00 +00:00
|
|
|
const pipeMap = (...funcs) => obj => {
|
2020-04-21 00:34:49 +00:00
|
|
|
const arr = Object.entries(obj).reduce((acc, [key, value], i, arr) => {
|
|
|
|
funcs.forEach(func => value = func(value, i, arr));
|
2020-04-19 03:04:00 +00:00
|
|
|
return [...acc, [key, value]];
|
|
|
|
},[]);
|
|
|
|
return arr.reduce((acc, [key, value]) => {
|
|
|
|
return { ...acc, [key]: value }
|
|
|
|
}, {});
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkLegal = ({ point, Game }) => {
|
2020-04-19 22:02:09 +00:00
|
|
|
return point.stone || 'l';
|
2020-04-19 03:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getBoardState = (Game) => {
|
|
|
|
const getLegal = point => checkLegal({ point, Game });
|
|
|
|
return pipeMap(getLegal)(Game.boardState);
|
|
|
|
}
|
|
|
|
|
2020-04-21 00:34:49 +00:00
|
|
|
const getNeighbors = (point, _, boardState) => {
|
|
|
|
const { top, btm, lft, rgt} = point.neighbors;
|
|
|
|
point.neighbors.top = top ? boardState.find(val => val[0] === top)[1] : top;
|
|
|
|
point.neighbors.btm = btm ? boardState.find(val => val[0] === btm)[1] : btm;
|
|
|
|
point.neighbors.lft = lft ? boardState.find(val => val[0] === lft)[1] : lft;
|
|
|
|
point.neighbors.rgt = rgt ? boardState.find(val => val[0] === rgt)[1] : rgt;
|
|
|
|
return point;
|
|
|
|
}
|
|
|
|
|
2020-04-19 22:02:09 +00:00
|
|
|
const initBoard = ({ boardSize, handicap }) => {
|
2020-04-19 03:04:00 +00:00
|
|
|
const boardState = {};
|
|
|
|
for (let i = 0; i < Math.pow(boardSize, 2); i++) {
|
|
|
|
const point = Point({
|
|
|
|
x: Math.floor(i / boardSize) + 1,
|
|
|
|
y: i % boardSize + 1,
|
|
|
|
boardSize
|
|
|
|
});
|
|
|
|
boardState[`${point.pos.x}-${point.pos.y}`] = point;
|
|
|
|
}
|
2020-04-19 22:02:09 +00:00
|
|
|
if (handicap) {
|
|
|
|
HANDI_PLACE[boardSize][handicap].forEach(pt => {
|
|
|
|
boardState[pt].stone = 1;
|
|
|
|
});
|
|
|
|
}
|
2020-04-21 00:34:49 +00:00
|
|
|
const boardStateWithNeighbors = pipeMap(getNeighbors)(boardState)
|
|
|
|
return boardStateWithNeighbors;
|
2020-04-19 03:04:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// returns Game object
|
2020-04-19 22:02:09 +00:00
|
|
|
const Game = ({gameData = {}, gameRecord = []} = {}) => ({
|
|
|
|
winner: gameData.winner ||null,
|
2020-04-20 05:58:26 +00:00
|
|
|
turn: gameData.turn || 0, // turn logic depends on handicap stones
|
2020-04-19 22:02:09 +00:00
|
|
|
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-04-19 03:04:00 +00:00
|
|
|
groups: {},
|
|
|
|
boardState: {},
|
2020-04-19 22:02:09 +00:00
|
|
|
gameRecord: gameRecord,
|
|
|
|
playerState: gameData.playerState || {
|
2020-04-19 03:04:00 +00:00
|
|
|
bCaptures: 0,
|
|
|
|
wCaptures: 0,
|
|
|
|
bScore: 0,
|
|
|
|
wScore: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
initGame: function() {
|
|
|
|
this.winner = null;
|
|
|
|
this.pass = 0;
|
|
|
|
this.turn = this.handicap ? -1 : 1;
|
2020-04-19 22:02:09 +00:00
|
|
|
this.boardState = initBoard({ boardSize: this.boardSize, handicap: this.handicap})
|
|
|
|
// return this.boardState
|
2020-04-20 05:58:26 +00:00
|
|
|
return { ...this, legalMoves: getBoardState(this)};
|
2020-04-20 05:26:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
getMeta: function() {
|
|
|
|
return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord }
|
2020-04-20 05:58:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
makeMove: function({ player, pos: {x, y}}) {
|
|
|
|
let 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) {
|
|
|
|
point.makeMove(this.turn);
|
|
|
|
this.turn *= -1;
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {...this, legalMoves: getBoardState(this), success };
|
2020-04-19 03:04:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const Point = ({x, y, boardSize = 19}) => ({
|
|
|
|
pos: {x, y},
|
|
|
|
stone: 0, // can be 1, -1, 0, or 'k' for ko
|
2020-04-20 05:58:26 +00:00
|
|
|
legal: true,
|
2020-04-19 03:04:00 +00:00
|
|
|
territory: 0,
|
|
|
|
capturing: [],
|
|
|
|
groupMembers: [ this ],
|
|
|
|
neighbors: {
|
|
|
|
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-04-20 05:58:26 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
makeMove: function(turn) {
|
|
|
|
this.stone = turn;
|
|
|
|
this.legal = false;
|
2020-04-19 03:04:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Game,
|
|
|
|
Point
|
|
|
|
}
|