Merge pull request #5 from jhalitaksoy/fix/getWonPlayerId
Fix/get won player
This commit is contained in:
commit
2c5b9981c4
@ -198,17 +198,18 @@ export class MancalaGame {
|
|||||||
return false;
|
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.checkGameIsEnded()) {
|
||||||
if (
|
if (player1StoneCount === player2StoneCount) {
|
||||||
this.board.player1Bank.stoneCount > this.board.player2Bank.stoneCount
|
return undefined;
|
||||||
) {
|
} else if (player1StoneCount > player2StoneCount) {
|
||||||
return this.player1Id;
|
return this.player1Id;
|
||||||
} else {
|
} else {
|
||||||
return this.player2Id;
|
return this.player2Id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static createFromMancalaGame(mancalaGame: MancalaGame): MancalaGame {
|
public static createFromMancalaGame(mancalaGame: MancalaGame): MancalaGame {
|
||||||
|
|||||||
@ -153,4 +153,24 @@ describe('Game Test', () => {
|
|||||||
expect(game.history[2]).toStrictEqual(historyItem3);
|
expect(game.history[2]).toStrictEqual(historyItem3);
|
||||||
expect(game.history[3]).toStrictEqual(historyItem4);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user