node-go/packages/server/services/Game.v2.js
2020-04-18 20:04:00 -07:00

145 lines
No EOL
4.2 KiB
JavaScript

/*----- 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 = {
'9' : [
0, 0,
[[ 7, 3 ], [ 3, 7 ] ],
[ [ 7, 7 ], [ 7, 3 ], [ 3, 7 ] ],
[ [ 3, 3 ], [ 7, 7 ], [ 3, 7 ], [ 7, 3 ] ]
],
'13' : [
0, 0,
[ [ 4, 10 ], [ 10, 4 ] ],
[ [ 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 ] ],
[ [ 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] ],
]
};
const pipeMap = (...funcs) => obj => {
const arr = Object.entries(obj).reduce((acc, [key, value]) => {
funcs.forEach(func => value = func(value));
return [...acc, [key, value]];
},[]);
return arr.reduce((acc, [key, value]) => {
return { ...acc, [key]: value }
}, {});
}
const checkLegal = ({ point, Game }) => {
return 'l';
}
const getBoardState = (Game) => {
const getLegal = point => checkLegal({ point, Game });
return pipeMap(getLegal)(Game.boardState);
}
const initBoard = boardSize => {
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;
}
return boardState;
}
// returns Game object
const Game = ({gameData, gameRecord} = {}) => ({
winner: gameData ? gameData.winner : null,
turn: gameData ? gameData.turn : 1, // turn logic depends on handicap stones
pass: gameData ? gameData.pass : 0, // -1 represents state in which resignation has been submitted, not confirmed
komi: gameData ? gameData.komi : 6.5, // komi depends on handicap stones + player rank
handicap: gameData ? gameData.handicap : 0,
boardSize: gameData ? gameData.boardSize : 19,
groups: {},
boardState: {},
gameRecord: gameRecord || [],
playerState: gameData ? gameData.playerState : {
bCaptures: 0,
wCaptures: 0,
bScore: 0,
wScore: 0
},
initGame: function() {
this.winner = null;
this.pass = 0;
this.turn = this.handicap ? -1 : 1;
this.boardState = initBoard(this.boardSize)
return getBoardState(this);
}
});
const Point = ({x, y, boardSize = 19}) => ({
pos: {x, y},
stone: 0, // can be 1, -1, 0, or 'k' for ko
legal: 0,
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
}
});
module.exports = {
Game,
Point
}