boardgame.io
boardgame.io copied to clipboard
TypeScript: AiEnumerate return type
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;
},
},
};