stub makeMove in Game v2 API

This commit is contained in:
Sorrel Bri 2020-04-19 22:58:26 -07:00
parent 37db281d08
commit a54d4bf7e4
2 changed files with 51 additions and 30 deletions

View file

@ -106,7 +106,7 @@ const initBoard = ({ boardSize, handicap }) => {
// returns Game object // returns Game object
const Game = ({gameData = {}, gameRecord = []} = {}) => ({ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
winner: gameData.winner ||null, winner: gameData.winner ||null,
turn: gameData.turn || 1, // turn logic depends on handicap stones 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 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 komi: gameData.komi || 6.5, // komi depends on handicap stones + player rank
handicap: gameData.handicap || 0, handicap: gameData.handicap || 0,
@ -127,18 +127,33 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({
this.turn = this.handicap ? -1 : 1; this.turn = this.handicap ? -1 : 1;
this.boardState = initBoard({ boardSize: this.boardSize, handicap: this.handicap}) this.boardState = initBoard({ boardSize: this.boardSize, handicap: this.handicap})
// return this.boardState // return this.boardState
return { ...this, boardState: getBoardState(this)}; return { ...this, legalMoves: getBoardState(this)};
}, },
getMeta: function() { getMeta: function() {
return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord } return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord }
},
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 };
} }
}); });
const Point = ({x, y, boardSize = 19}) => ({ const Point = ({x, y, boardSize = 19}) => ({
pos: {x, y}, pos: {x, y},
stone: 0, // can be 1, -1, 0, or 'k' for ko stone: 0, // can be 1, -1, 0, or 'k' for ko
legal: 0, legal: true,
territory: 0, territory: 0,
capturing: [], capturing: [],
groupMembers: [ this ], groupMembers: [ this ],
@ -147,6 +162,11 @@ const Point = ({x, y, boardSize = 19}) => ({
btm: x < boardSize ? `${ x + 1 }-${ y }` : null, btm: x < boardSize ? `${ x + 1 }-${ y }` : null,
rgt: y < boardSize ? `${ x }-${ y + 1 }` : null, rgt: y < boardSize ? `${ x }-${ y + 1 }` : null,
lft: y > 1 ? `${ x }-${ y - 1 }` : null lft: y > 1 ? `${ x }-${ y - 1 }` : null
},
makeMove: function(turn) {
this.stone = turn;
this.legal = false;
} }
}); });

View file

@ -25,82 +25,82 @@ describe('Game', () => {
Game().getMeta() Game().getMeta()
.should.eql(initialMeta); .should.eql(initialMeta);
Game().initGame().getMeta() Game().initGame().getMeta()
.should.eql(initialMeta); .should.eql({ ...initialMeta, turn: 1 });
done(); done();
}) })
}); });
describe('Game.initGame() returns boardState', () => { describe('Game.initGame() returns boardState', () => {
it('init Game returns default 19x19', done => { it('init Game returns default 19x19', done => {
Game().initGame().boardState Game().initGame().legalMoves
.should.eql(emptyBoard); .should.eql(emptyBoard);
done(); done();
}); });
it('init Game with 2 handicap returns boardState with stones', done => { it('init Game with 2 handicap returns boardState with stones', done => {
Game({gameData: { handicap: 2 }}).initGame().boardState Game({gameData: { handicap: 2 }}).initGame().legalMoves
.should.eql({...emptyBoard, '4-16': 1, '16-4': 1}); .should.eql({...emptyBoard, '4-16': 1, '16-4': 1});
done(); done();
}); });
it('init 19x19 Game with all levels of handicap returns boardState with stones', done => { it('init 19x19 Game with all levels of handicap returns boardState with stones', done => {
Game({gameData: { boardSize: 19, handicap: 2 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 2 }}).initGame().legalMoves
.should.eql({...emptyBoard, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 3 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 3 }}).initGame().legalMoves
.should.eql({...emptyBoard, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 4 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 4 }}).initGame().legalMoves
.should.eql({...emptyBoard, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 5 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 5 }}).initGame().legalMoves
.should.eql({...emptyBoard, '10-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '10-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 6 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 6 }}).initGame().legalMoves
.should.eql({...emptyBoard, '10-4': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '10-4': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 7 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 7 }}).initGame().legalMoves
.should.eql({...emptyBoard, '10-10': 1, '10-4': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '10-10': 1, '10-4': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 8 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 8 }}).initGame().legalMoves
.should.eql({...emptyBoard, '16-10': 1, '10-4': 1, '10-16': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '16-10': 1, '10-4': 1, '10-16': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
Game({gameData: { boardSize: 19, handicap: 9 }}).initGame().boardState Game({gameData: { boardSize: 19, handicap: 9 }}).initGame().legalMoves
.should.eql({...emptyBoard, '10-10': 1, '16-10': 1, '10-4': 1, '10-16': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 }); .should.eql({...emptyBoard, '10-10': 1, '16-10': 1, '10-4': 1, '10-16': 1, '4-10': 1, '4-4': 1, '16-16': 1, '4-16': 1, '16-4': 1 });
done(); done();
}) })
it('init 13x13 Game returns boardState', done => { it('init 13x13 Game returns boardState', done => {
Game({gameData: { boardSize: 13 }}).initGame().boardState Game({gameData: { boardSize: 13 }}).initGame().legalMoves
.should.eql(emptyBoard13); .should.eql(emptyBoard13);
done(); done();
}); });
it('init 13x13 Game with all levels of handicap returns boardState with stones', done => { it('init 13x13 Game with all levels of handicap returns boardState with stones', done => {
Game({gameData: { boardSize: 13, handicap: 2 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 2 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 3 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 3 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 4 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 4 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 5 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 5 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '7-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '7-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 6 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 6 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '7-4': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '7-4': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 7 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 7 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '7-7': 1, '7-4': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '7-7': 1, '7-4': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 8 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 8 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '10-7': 1, '7-4': 1, '7-10': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '10-7': 1, '7-4': 1, '7-10': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
Game({gameData: { boardSize: 13, handicap: 9 }}).initGame().boardState Game({gameData: { boardSize: 13, handicap: 9 }}).initGame().legalMoves
.should.eql({...emptyBoard13, '7-7': 1, '10-7': 1, '7-4': 1, '7-10': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 }); .should.eql({...emptyBoard13, '7-7': 1, '10-7': 1, '7-4': 1, '7-10': 1, '4-7': 1, '4-4': 1, '10-10': 1, '4-10': 1, '10-4': 1 });
done(); done();
}); });
it('init 9x9 Game returns boardState', done => { it('init 9x9 Game returns boardState', done => {
Game({gameData: { boardSize: 9 }}).initGame().boardState Game({gameData: { boardSize: 9 }}).initGame().legalMoves
.should.eql(emptyBoard9); .should.eql(emptyBoard9);
done(); done();
}); });
it('init 9x9 Game with all levels of handicap returns boardState with stones', done => { it('init 9x9 Game with all levels of handicap returns boardState with stones', done => {
Game({gameData: { boardSize: 9, handicap: 2 }}).initGame().boardState Game({gameData: { boardSize: 9, handicap: 2 }}).initGame().legalMoves
.should.eql({...emptyBoard9, '3-7': 1, '7-3': 1 }); .should.eql({...emptyBoard9, '3-7': 1, '7-3': 1 });
Game({gameData: { boardSize: 9, handicap: 3 }}).initGame().boardState Game({gameData: { boardSize: 9, handicap: 3 }}).initGame().legalMoves
.should.eql({...emptyBoard9, '7-7': 1, '3-7': 1, '7-3': 1 }); .should.eql({...emptyBoard9, '7-7': 1, '3-7': 1, '7-3': 1 });
Game({gameData: { boardSize: 9, handicap: 4 }}).initGame().boardState Game({gameData: { boardSize: 9, handicap: 4 }}).initGame().legalMoves
.should.eql({...emptyBoard9, '3-3': 1, '7-7': 1, '3-7': 1, '7-3': 1 }); .should.eql({...emptyBoard9, '3-3': 1, '7-7': 1, '3-7': 1, '7-3': 1 });
done(); done();
}); });
@ -108,15 +108,16 @@ describe('Game.initGame() returns boardState', () => {
describe('Game.makeMove({ player: str, pos: { x: int, y: int } })', () => { describe('Game.makeMove({ player: str, pos: { x: int, y: int } })', () => {
it('place move returns game object with proper board', done => { it('place move returns game object with proper board', done => {
Game().initGame().makeMove({ player: 'black', pos: { x: 4, y: 4 } }).boardState Game().initGame().makeMove({ player: 'black', pos: { x: 4, y: 4 } }).success
.should.eql({ ...emptyBoard, '4-4': 1 }); .should.eql(true)
// .should.eql({ ...emptyBoard, '4-4': 1 });
done(); done();
}) })
}) })
const initialMeta = { const initialMeta = {
winner: null, winner: null,
turn: 1, turn: 0,
pass: 0, pass: 0,
playerState: { playerState: {
bCaptures: 0, bCaptures: 0,