2022-05-04 15:04:59 +03:00
|
|
|
import { GRClearBoardAtEnd } from '../src/common/game_rules/GRClearBoardAtEnd';
|
2022-05-02 00:42:59 +03:00
|
|
|
import { GRLastStoneInBank } from '../src/common/game_rules/GRLastStoneInBank';
|
|
|
|
|
import { GRLastStoneInEmptyPit } from '../src/common/game_rules/GRLastStoneInEmptyPit';
|
|
|
|
|
import { Board } from '../src/core/Board';
|
|
|
|
|
import { MancalaGame } from '../src/core/MancalaGame';
|
|
|
|
|
|
|
|
|
|
function createGame(): MancalaGame {
|
|
|
|
|
const board = new Board(6, 4);
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
2022-05-05 23:04:10 +03:00
|
|
|
const game = new MancalaGame('0', board, player1Id, player2Id, player1Id, [
|
2022-05-02 00:42:59 +03:00
|
|
|
new GRLastStoneInEmptyPit(),
|
2022-05-04 15:04:59 +03:00
|
|
|
new GRLastStoneInBank(),
|
|
|
|
|
new GRClearBoardAtEnd()
|
2022-05-02 00:42:59 +03:00
|
|
|
]);
|
|
|
|
|
return game;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
describe('Game Test', () => {
|
2022-05-04 15:04:59 +03:00
|
|
|
test('test getPlayerIdByIndex', () => {
|
2022-05-02 00:42:59 +03:00
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
|
|
|
|
expect(game.getPlayerIdByIndex(0)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(1)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(2)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(3)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(4)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(5)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(6)).toStrictEqual(player1Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(7)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(8)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(9)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(10)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(11)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(12)).toStrictEqual(player2Id);
|
|
|
|
|
expect(game.getPlayerIdByIndex(13)).toStrictEqual(player2Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
});
|
|
|
|
|
test('test canPlayerMove', () => {
|
|
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 0)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 1)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 2)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 3)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 4)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 5)).toBe(true);
|
|
|
|
|
expect(game.canPlayerMove(player1Id, 6)).toBe(false);
|
|
|
|
|
expect(game.canPlayerMove(player2Id, 0)).toBe(false);
|
2022-05-02 00:42:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('test moveByPlayerPit', () => {
|
|
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('initial');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player1Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
|
2022-05-04 15:04:59 +03:00
|
|
|
game.moveByPlayerPit(player1Id, 0);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('playing');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player2Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
|
2022-05-04 15:04:59 +03:00
|
|
|
game.moveByPlayerPit(player2Id, 0);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('playing');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player1Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('test game end test', () => {
|
|
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('initial');
|
|
|
|
|
game.board.pits[0].stoneCount = 0;
|
|
|
|
|
game.board.pits[1].stoneCount = 0;
|
|
|
|
|
game.board.pits[2].stoneCount = 0;
|
|
|
|
|
game.board.pits[3].stoneCount = 0;
|
|
|
|
|
game.board.pits[4].stoneCount = 0;
|
|
|
|
|
game.board.pits[5].stoneCount = 1;
|
2022-05-04 15:04:59 +03:00
|
|
|
game.moveByPlayerPit(player1Id, 5);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('ended');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('test last stone in bank', () => {
|
|
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('initial');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player1Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
|
2022-05-04 15:04:59 +03:00
|
|
|
game.moveByPlayerPit(player1Id, 3);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('playing');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player1Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('test empty pit 1', () => {
|
|
|
|
|
const game = createGame();
|
2022-05-04 15:04:59 +03:00
|
|
|
const player1Id = '0';
|
|
|
|
|
const player2Id = '1';
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('initial');
|
|
|
|
|
game.board.pits[0].stoneCount = 1;
|
|
|
|
|
game.board.pits[1].stoneCount = 0;
|
2022-05-04 15:04:59 +03:00
|
|
|
game.moveByPlayerPit(player1Id, 0);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.state).toBe('playing');
|
2022-05-04 15:04:59 +03:00
|
|
|
expect(game.turnPlayerId).toBe(player2Id);
|
2022-05-02 00:42:59 +03:00
|
|
|
expect(game.board.getStoneArray()).toStrictEqual([
|
2022-05-04 15:04:59 +03:00
|
|
|
0, 0, 4, 4, 4, 4, 5, 4, 4, 4, 4, 0, 4, 0
|
2022-05-02 00:42:59 +03:00
|
|
|
]);
|
|
|
|
|
});
|
|
|
|
|
});
|