Handle AI Turns with Coroutines and First-Class Functions
We’re going to add the ability for the app to run one (or more) of the players in the game and give users the option to have Penny Drop be a single-(human-)player game. When the AI is playing, we’re going to disable both the Roll and Pass buttons and have the app start rolling/passing on its own. A handy way to trigger this is from the updateFromGameHandler function once everything else is updated. Add a call to playAITurn near the end of that function:
| if (!result.isGameOver && result.currentPlayer?.isHuman == false) { |
| canRoll.value = false |
| canPass.value = false |
» | playAITurn() |
| } |
We’re only going to have a player be run by the app if we’re sure it’s not human. We’re using ...
Get Kotlin and Android Development featuring Jetpack now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.