boardgame.io icon indicating copy to clipboard operation
boardgame.io copied to clipboard

TypeScript: AiEnumerate return type

Open McLaynV opened this issue 2 years ago • 0 comments

I tried doing TicTacToe as much typed in TypeScript as possible. This AiEnumerate is the only type that I needed and that could be available from boardgame.io


Here is the code where I used the AiEnumerate type:

type TicTacToeGameOver = {
  draw?: boolean;
  winner?: PlayerID;
}

type TicTacToeCell = PlayerID;

export type TicTacToeG = {
  cells: TicTacToeCell[];
};

export type TicTacToeMoves = MoveMap<TicTacToeG> & {
  clickCell: (G: TicTacToeG, ctx: Ctx, cellId: number) => void;
};

type TicTacToeGame = Game<TicTacToeG> & {
  moves: TicTacToeMoves;
  endIf: (G: TicTacToeG, ctx: Ctx) => TicTacToeGameOver | undefined;
};

export const TicTacToe: TicTacToeGame = {
...
  ai: {
    enumerate: (G, _ctx, _playerId) => {
      const movesEnumeration: AiEnumerate = [];
      const moves = TicTacToe.moves;
      for (let i = 0; i < G.cells.length; i++) {
        if (G.cells[i] === null) {
          movesEnumeration.push({move: moves.clickCell.name, args: [i]});
        }
      }
      return movesEnumeration;
    },
  },
};

McLaynV avatar Jun 28 '22 12:06 McLaynV