# How to pass a turn to AI and draw its move in a game?

I am writing a simple game for 2 players, where they have to make turns after each other (select a few objects to remove from the screen) and press "End turn" at the end to pass the turn to another player.

The turns are switched in onClick method on a onClickListener, attached to the button "End turn". And it works fine for the case of 2 humans playing (on the same device). But if a person plays with the computer, the AI calculates and makes its move in the same onClick method. Thus, its move are not drawn on the screen and the player sees as if after his move the board was redrawn immediately with the changes made by the computer's move (objects removed). How can I make is so automatic moves are drawn as in case of human play - the objects are selected by the AI and then removed (at this time the button "End turn" would be disabled)?

The exact answer to this is going to be pretty specific to the way you've designed things.

Essentially the strategy is to make the AI use the same methods that the player does. It shouldn't just run everything in a single tight loop. Break the AI's decisions and actions into discrete parts. Then, using something like a decision tree or behaviour tree structure, have the AI perform these decisions and actions with some delay between each. During the delays the AI should be relinquishing control to the rendering and UI threads.

• Now all objects on the board have attached onClickListeners, upon clicking the IDs of the objects are placed in an array. And a variable contains information, which player is currently making a turn. So when "End turn" is pressed, the number of the player just changes and the array is cleaned, and everything is ready for the next "human" player. Where to put AI's calculations and moves in this scheme, if not in onClick method of the button "End turn"?
– Vera
Oct 8 '14 at 17:09
• OK? The onClickListeners should be calling a method that takes an ID, the AI can use the same method that's being called by onClickListener. Remember that this site isn't for discussions. Comments should be used to request clarification in an answer, not ask additional questions or discuss how you specifically might implement this with your design.
– House
Oct 8 '14 at 17:16

To make the moves of the AI more noticeable, you could animate them.

Instead of removing the objects immediately, you could, for example, fade them to alpha over the course of a second. You could also do something more flashy like drawing a destruction animation or having them explode with a particle effect, depending on what's appropriate for the theme of your game.