Skip to content

TypeScript: AiEnumerate return type#1080

Merged
vdfdev merged 3 commits into
boardgameio:mainfrom
McLaynV:types
Oct 5, 2022
Merged

TypeScript: AiEnumerate return type#1080
vdfdev merged 3 commits into
boardgameio:mainfrom
McLaynV:types

Conversation

@McLaynV
Copy link
Copy Markdown
Contributor

@McLaynV McLaynV commented Jun 28, 2022

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;
    },
  },
};

@vdfdev vdfdev merged commit 4bf203c into boardgameio:main Oct 5, 2022
@McLaynV McLaynV deleted the types branch October 7, 2022 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants