boardgame.io
boardgame.io copied to clipboard
AI framework automatically picks first active player
When stages were introduced (https://github.com/boardgameio/boardgame.io/commit/da2f0eaadfe53701fa62370578bcb1c9ee5a1a7d) the Step
function (back then in client.js) received the following update:
let playerID = ctx.currentPlayer;
if (ctx.stage) {
playerID = Object.keys(ctx.stage)[0];
}
This results in the Step
-Function always picking the first player that is currently active, instead of picking the currentPlayer
, or, a player of the developer's choice.
What's the reasoning behind this? Wouldn't it make more sense to pick the currentPlayer, or allow the caller to declare for which player (probably the bot player) the step should be executed?
In my use case this currently makes it impossible for me to make the bot player player 2, as it will not execute any moves, as player 1 is also able to make moves in the enemy turn (but mostly decides not to do it).
I think this was a quick and pragmatic decision made to get the AI feature shipped, which we should definitely improve upon.
The step method in the client was retired and now similar logic is part of the GetBotPlayer
method in the Local transport and in Step
and Simulate
methods of the AI framework.
https://github.com/boardgameio/boardgame.io/blob/059a1cc3848f7944626b287c7d2d0f29385688e5/src/client/transport/local.ts#L33-L36
https://github.com/boardgameio/boardgame.io/blob/059a1cc3848f7944626b287c7d2d0f29385688e5/src/ai/ai.ts#L22-L25
https://github.com/boardgameio/boardgame.io/blob/059a1cc3848f7944626b287c7d2d0f29385688e5/src/ai/ai.ts#L66-L69
It would be good to consolidate these. Allowing the developer to control who plays would be a good start. Another good default option might be to run each node for all active players and then randomly choose which player gets to make an action. Given that, it might make sense to make the developer facing solution also allow to specify more than one player.
Thanks a lot for the quick reply!
Yes, I know the Step
-function is in the AI framework now, I just dig up the commit that introduced the "problem".
I will take a closer look at it, think about how it could be improved and maybe send in a PR in the near future.
Great! Let me know if you have any questions.