Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript: AiEnumerate return type #1080

Merged
merged 3 commits into from
Oct 5, 2022
Merged

TypeScript: AiEnumerate return type #1080

merged 3 commits into from
Oct 5, 2022

Conversation

McLaynV
Copy link
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.

None yet

2 participants