This commit is contained in:
Jose Conde 2024-07-08 10:21:56 +02:00
parent f67c262b0e
commit ca0f1466c2
4 changed files with 18 additions and 5 deletions

View File

@ -120,7 +120,11 @@ export class LoggingService {
} }
_getStringObject(data: any) { _getStringObject(data: any) {
return JSON.stringify(data, null, 2); try {
return JSON.stringify(data, null, 2);
} catch (error) {
return data;
}
} }
} }

View File

@ -27,6 +27,7 @@ export class DominoesGame {
handSize: number = 7; handSize: number = 7;
notificationManager: PlayerNotificationService = new PlayerNotificationService(); notificationManager: PlayerNotificationService = new PlayerNotificationService();
lastMove: PlayerMove | null = null; lastMove: PlayerMove | null = null;
forcedInitialPlayerIndex: number | null = null;
constructor(public players: PlayerInterface[], seed: PRNG) { constructor(public players: PlayerInterface[], seed: PRNG) {
this.id = uuid(); this.id = uuid();
@ -43,6 +44,10 @@ export class DominoesGame {
this.board.boneyard = this.generateTiles(); this.board.boneyard = this.generateTiles();
} }
setForcedInitialPlayerIndex(index: number) {
this.forcedInitialPlayerIndex = index;
}
reset() { reset() {
this.board.reset(); this.board.reset();
this.initializeGame(); this.initializeGame();
@ -174,7 +179,7 @@ export class DominoesGame {
this.tileSelectionPhase = false; this.tileSelectionPhase = false;
this.gameInProgress = true; this.gameInProgress = true;
this.currentPlayerIndex = this.getStartingPlayerIndex(); this.currentPlayerIndex = (this.forcedInitialPlayerIndex !== null) ? this.forcedInitialPlayerIndex : this.getStartingPlayerIndex();
printLine(`${this.players[this.currentPlayerIndex].name} is the starting player:`); printLine(`${this.players[this.currentPlayerIndex].name} is the starting player:`);
while (!this.gameOver) { while (!this.gameOver) {
await this.playTurn(); await this.playTurn();

View File

@ -69,15 +69,18 @@ export class MatchSession {
let gameNumber: number = 0; let gameNumber: number = 0;
this.matchInProgress = true this.matchInProgress = true
this.playerNotificationManager.notifyMatchState(this); this.playerNotificationManager.notifyMatchState(this);
let winnerIndex: number | null = null;
while (this.matchInProgress) { while (this.matchInProgress) {
this.currentGame = new DominoesGame(this.players, this.rng); this.currentGame = new DominoesGame(this.players, this.rng);
if (winnerIndex !== null) {
this.currentGame.setForcedInitialPlayerIndex(winnerIndex);
}
gameNumber += 1; gameNumber += 1;
this.state = 'started' this.state = 'started'
this.logger.info(`Game #${gameNumber} started`); this.logger.info(`Game #${gameNumber} started`);
// this.game.reset() // this.game.reset()
const gameSummary = await this.currentGame.start(); const gameSummary = await this.currentGame.start();
this.logger.debug('gameSummary :>> ', gameSummary); winnerIndex = this.players.findIndex(player => player.id === gameSummary.winner?.id);
this.setScores(); this.setScores();
this.checkMatchWinner(); this.checkMatchWinner();
this.resetPlayers(); this.resetPlayers();

View File

@ -13,7 +13,8 @@ export class PlayerAI extends AbstractPlayer {
} }
async makeMove(board: Board): Promise<PlayerMove | null> { async makeMove(board: Board): Promise<PlayerMove | null> {
await wait(500); // Simulate thinking time const rndWait = Math.floor(Math.random() * 1000) + 1000;
await wait(rndWait); // Simulate thinking time
if (board.tiles.length === 0) { if (board.tiles.length === 0) {
printLine('playing the first tile'); printLine('playing the first tile');
const highestPair = this.getHighestPair(); const highestPair = this.getHighestPair();