add getNeighbors to initBoard() allowing Points to directly reference neighbors
This commit is contained in:
parent
a54d4bf7e4
commit
bdfb6ebe85
2 changed files with 63 additions and 21 deletions
|
@ -67,8 +67,8 @@ const HANDI_PLACE = {
|
||||||
|
|
||||||
|
|
||||||
const pipeMap = (...funcs) => obj => {
|
const pipeMap = (...funcs) => obj => {
|
||||||
const arr = Object.entries(obj).reduce((acc, [key, value]) => {
|
const arr = Object.entries(obj).reduce((acc, [key, value], i, arr) => {
|
||||||
funcs.forEach(func => value = func(value));
|
funcs.forEach(func => value = func(value, i, arr));
|
||||||
return [...acc, [key, value]];
|
return [...acc, [key, value]];
|
||||||
},[]);
|
},[]);
|
||||||
return arr.reduce((acc, [key, value]) => {
|
return arr.reduce((acc, [key, value]) => {
|
||||||
|
@ -85,6 +85,15 @@ const getBoardState = (Game) => {
|
||||||
return pipeMap(getLegal)(Game.boardState);
|
return pipeMap(getLegal)(Game.boardState);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
const initBoard = ({ boardSize, handicap }) => {
|
const initBoard = ({ boardSize, handicap }) => {
|
||||||
const boardState = {};
|
const boardState = {};
|
||||||
for (let i = 0; i < Math.pow(boardSize, 2); i++) {
|
for (let i = 0; i < Math.pow(boardSize, 2); i++) {
|
||||||
|
@ -100,7 +109,8 @@ const initBoard = ({ boardSize, handicap }) => {
|
||||||
boardState[pt].stone = 1;
|
boardState[pt].stone = 1;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return boardState;
|
const boardStateWithNeighbors = pipeMap(getNeighbors)(boardState)
|
||||||
|
return boardStateWithNeighbors;
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns Game object
|
// returns Game object
|
||||||
|
|
|
@ -3,19 +3,19 @@ const should = chai.should();
|
||||||
const { Game, Point } = require('../services/Game.v2');
|
const { Game, Point } = require('../services/Game.v2');
|
||||||
|
|
||||||
describe('Game', () => {
|
describe('Game', () => {
|
||||||
it('smoke test Game', done => {
|
it('smoke test Game()', done => {
|
||||||
(typeof Game())
|
(typeof Game())
|
||||||
.should.eql('object');
|
.should.eql('object');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('smoke test Point', done => {
|
it('smoke test Point()', done => {
|
||||||
(typeof Point({x: 1, y: 1}))
|
(typeof Point({x: 1, y: 1}))
|
||||||
.should.eql('object');
|
.should.eql('object');
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('smoke test init Game', done => {
|
it('smoke test initGame()', done => {
|
||||||
(typeof Game().initGame())
|
(typeof Game().initGame())
|
||||||
.should.eql('object');
|
.should.eql('object');
|
||||||
done();
|
done();
|
||||||
|
@ -27,23 +27,44 @@ describe('Game', () => {
|
||||||
Game().initGame().getMeta()
|
Game().initGame().getMeta()
|
||||||
.should.eql({ ...initialMeta, turn: 1 });
|
.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('initGame() returns default 19x19', done => {
|
||||||
Game().initGame().legalMoves
|
Game().initGame().legalMoves
|
||||||
.should.eql(emptyBoard);
|
.should.eql(emptyBoard);
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('init Game with 2 handicap returns boardState with stones', done => {
|
it('initGame() with 2 handicap returns legalMoves with stones', done => {
|
||||||
Game({gameData: { handicap: 2 }}).initGame().legalMoves
|
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('initGame() returns Game with all Points having neighbors', done => {
|
||||||
|
const boardState = Game().initGame().boardState;
|
||||||
|
const oneOneNeighbors = boardState['1-1'].neighbors;
|
||||||
|
const fiveSevenNeighbors = boardState['5-7'].neighbors;
|
||||||
|
const nineteenTenNeighbors = boardState['19-10'].neighbors;
|
||||||
|
|
||||||
|
oneOneNeighbors.rgt.pos.should.eql({x: 1, y: 2});
|
||||||
|
oneOneNeighbors.btm.pos.should.eql({x: 2, y: 1});
|
||||||
|
|
||||||
|
fiveSevenNeighbors.top.pos.should.eql({x: 4, y: 7});
|
||||||
|
fiveSevenNeighbors.btm.pos.should.eql({x: 6, y: 7});
|
||||||
|
fiveSevenNeighbors.lft.pos.should.eql({x: 5, y: 6});
|
||||||
|
fiveSevenNeighbors.rgt.pos.should.eql({x: 5, y: 8});
|
||||||
|
|
||||||
|
nineteenTenNeighbors.top.pos.should.eql({x: 18, y: 10});
|
||||||
|
nineteenTenNeighbors.lft.pos.should.eql({x: 19, y: 9});
|
||||||
|
nineteenTenNeighbors.rgt.pos.should.eql({x: 19, y: 11});
|
||||||
|
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('initGame( 19x19 ) with all levels of handicap returns legalMoves with stones', done => {
|
||||||
Game({gameData: { boardSize: 19, handicap: 2 }}).initGame().legalMoves
|
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().legalMoves
|
Game({gameData: { boardSize: 19, handicap: 3 }}).initGame().legalMoves
|
||||||
|
@ -63,13 +84,13 @@ describe('Game.initGame() returns boardState', () => {
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
|
|
||||||
it('init 13x13 Game returns boardState', done => {
|
it('initGame( 13x13) returns legalMoves', done => {
|
||||||
Game({gameData: { boardSize: 13 }}).initGame().legalMoves
|
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('initGame( 13x13 ) with all levels of handicap returns legalMoves with stones', done => {
|
||||||
Game({gameData: { boardSize: 13, handicap: 2 }}).initGame().legalMoves
|
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().legalMoves
|
Game({gameData: { boardSize: 13, handicap: 3 }}).initGame().legalMoves
|
||||||
|
@ -89,13 +110,13 @@ describe('Game.initGame() returns boardState', () => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('init 9x9 Game returns boardState', done => {
|
it('initGame( 9x9 ) returns legalMoves', done => {
|
||||||
Game({gameData: { boardSize: 9 }}).initGame().legalMoves
|
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('initGame( 9x9 ) with all levels of handicap returns legalMoves with stones', done => {
|
||||||
Game({gameData: { boardSize: 9, handicap: 2 }}).initGame().legalMoves
|
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().legalMoves
|
Game({gameData: { boardSize: 9, handicap: 3 }}).initGame().legalMoves
|
||||||
|
@ -108,12 +129,23 @@ 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 } }).success
|
Game().initGame().makeMove({ player: 'black', pos: { x: 4, y: 4 } }).legalMoves
|
||||||
.should.eql(true)
|
.should.eql({ ...emptyBoard, '4-4': 1 });
|
||||||
// .should.eql({ ...emptyBoard, '4-4': 1 });
|
Game({ gameData: { handicap: 2 } }).initGame().makeMove({ player: 'white', pos: { x: 4, y: 4 } }).legalMoves
|
||||||
|
.should.eql({ ...emptyBoard, '4-16': 1, '16-4': 1, '4-4': -1 });
|
||||||
done();
|
done();
|
||||||
})
|
});
|
||||||
})
|
|
||||||
|
it('make move returns success: false with move out of turn', done => {
|
||||||
|
Game().initGame().makeMove({ player: 'white', pos: { x: 4, y: 4 } }).success
|
||||||
|
.should.eql(false);
|
||||||
|
Game({ gameData: { handicap: 2 } }).initGame().makeMove({ player: 'black', pos: { x: 4, y: 4 } }).success
|
||||||
|
.should.eql(false);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// describe('Point.joinGroup() ')
|
||||||
|
|
||||||
const initialMeta = {
|
const initialMeta = {
|
||||||
winner: null,
|
winner: null,
|
||||||
|
|
Loading…
Reference in a new issue