mancala/packages/mancala.js/tests/MancalaGame.test.ts

177 lines
6.1 KiB
TypeScript
Raw Normal View History

2022-05-08 16:57:59 +03:00
import { GameStep, MoveHistoryItem } from '../src/core/HistoryItem';
import { GAME_STEP_GAME_MOVE, MancalaGame } from '../src/core/MancalaGame';
import { createGame } from './TestUtil';
2022-05-02 00:42:59 +03:00
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
]);
});
2022-05-05 23:50:26 +03:00
test('test game history', () => {
const game = createGame();
const player1Id = '0';
const player2Id = '1';
game.moveByPlayerPit(player1Id, 0);
game.moveByPlayerPit(player2Id, 0);
game.moveByPlayerPit(player1Id, 1);
game.moveByPlayerPit(player2Id, 1);
2022-05-08 16:59:35 +03:00
const historyItem1 = new MoveHistoryItem(
player1Id,
0,
[1, 5, 5, 5, 4, 4, 0, 4, 4, 4, 4, 4, 4, 0],
[
new GameStep(0, GAME_STEP_GAME_MOVE),
new GameStep(1, GAME_STEP_GAME_MOVE),
new GameStep(2, GAME_STEP_GAME_MOVE),
new GameStep(3, GAME_STEP_GAME_MOVE)
]
);
const historyItem2 = new MoveHistoryItem(
player2Id,
7,
[1, 5, 5, 5, 4, 4, 0, 1, 5, 5, 5, 4, 4, 0],
[
new GameStep(7, GAME_STEP_GAME_MOVE),
new GameStep(8, GAME_STEP_GAME_MOVE),
new GameStep(9, GAME_STEP_GAME_MOVE),
new GameStep(10, GAME_STEP_GAME_MOVE)
]
);
const historyItem3 = new MoveHistoryItem(
player1Id,
1,
[1, 1, 6, 6, 5, 5, 0, 1, 5, 5, 5, 4, 4, 0],
[
new GameStep(1, GAME_STEP_GAME_MOVE),
new GameStep(2, GAME_STEP_GAME_MOVE),
new GameStep(3, GAME_STEP_GAME_MOVE),
new GameStep(4, GAME_STEP_GAME_MOVE),
new GameStep(5, GAME_STEP_GAME_MOVE)
]
);
const historyItem4 = new MoveHistoryItem(
player2Id,
8,
[1, 1, 6, 6, 5, 5, 0, 1, 1, 6, 6, 5, 5, 0],
[
new GameStep(8, GAME_STEP_GAME_MOVE),
new GameStep(9, GAME_STEP_GAME_MOVE),
new GameStep(10, GAME_STEP_GAME_MOVE),
new GameStep(11, GAME_STEP_GAME_MOVE),
new GameStep(12, GAME_STEP_GAME_MOVE)
]
);
expect(game.history[0]).toStrictEqual(historyItem1);
expect(game.history[1]).toStrictEqual(historyItem2);
expect(game.history[2]).toStrictEqual(historyItem3);
expect(game.history[3]).toStrictEqual(historyItem4);
2022-05-05 23:50:26 +03:00
});
2022-07-09 12:46:20 +03:00
test('test getWonPlayerId', () => {
const game = createGame();
const player1Id = '0';
const player2Id = '1';
game.board.clear();
game.board.pits[game.board.player1BankIndex()].stoneCount = 1;
game.board.pits[game.board.player2BankIndex()].stoneCount = 0;
expect(game.getWonPlayerId()).toBe(player1Id);
game.board.pits[game.board.player1BankIndex()].stoneCount = 0;
game.board.pits[game.board.player2BankIndex()].stoneCount = 1;
expect(game.getWonPlayerId()).toBe(player2Id);
game.board.pits[game.board.player1BankIndex()].stoneCount = 0;
game.board.pits[game.board.player2BankIndex()].stoneCount = 0;
expect(game.getWonPlayerId()).toBe(undefined);
});
2022-05-02 00:42:59 +03:00
});