diff --git a/src/core/MancalaGame.ts b/src/core/MancalaGame.ts index d613bf2..75a42d3 100644 --- a/src/core/MancalaGame.ts +++ b/src/core/MancalaGame.ts @@ -198,17 +198,18 @@ export class MancalaGame { return false; } - public getWonPlayerId(): string | null { + public getWonPlayerId(): string | undefined { + const player1StoneCount = this.board.player1Bank.stoneCount; + const player2StoneCount = this.board.player2Bank.stoneCount; if (this.checkGameIsEnded()) { - if ( - this.board.player1Bank.stoneCount > this.board.player2Bank.stoneCount - ) { + if (player1StoneCount === player2StoneCount) { + return undefined; + } else if (player1StoneCount > player2StoneCount) { return this.player1Id; } else { return this.player2Id; } } - return null; } public static createFromMancalaGame(mancalaGame: MancalaGame): MancalaGame { diff --git a/tests/MancalaGame.test.ts b/tests/MancalaGame.test.ts index e8c39d9..4073244 100644 --- a/tests/MancalaGame.test.ts +++ b/tests/MancalaGame.test.ts @@ -153,4 +153,24 @@ describe('Game Test', () => { expect(game.history[2]).toStrictEqual(historyItem3); expect(game.history[3]).toStrictEqual(historyItem4); }); + + 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); + }); });