1

I am writing a simple turn-based Android game which allows a player to play against the computer (i.e. phone in this case).

What would be a good/recommended way of letting the game update the computer's move against the player during the game? One way I could think of is to make the class maintaining the board visible to the computer player (pass the instance to the AI player instance) and update it directly, but this does not seem like a "good" approach. As sometimes it may take time to compute the next move. Also in future if we want to incorporate network play it should not involve a major rewrite to handle an update from the network player.

Any help with this is much appreciated.

source.rar
  • 8,002
  • 10
  • 50
  • 82
  • Thanks to IZI_Shadow_IZI for the suggestion on AsyncTask. However I had hoped for more alternatives (which could be evaluated to determine the best choice). Would really appreciate any additional suggestions in this regard. – source.rar Jul 05 '11 at 16:57

1 Answers1

1

If computations may take a long time then I reccomend looking into AsyncTask you could notify the player that the computer is making its move then pop up a progress dialog which notifies the user that the other player is doing something? Is this sorta what you are looking for? Here are some resources:

Android Docs: http://developer.android.com/reference/android/os/AsyncTask.html

Async Template: http://jyro.blogspot.com/2009/11/android-asynctask-template.html

IZI_Shadow_IZI
  • 1,921
  • 4
  • 30
  • 59
  • Thanks. I am looking into the AsyncTask (having some trouble wrapping my head around it as of now :)). Quick question, when we use AsyncTask I think we need to design the background computation as if it would return a result to the UI on completion right? And not as if were a separate task that will send a reply later after computation? – source.rar Jun 14 '11 at 07:19
  • I just found this maybe it will help? http://stackoverflow.com/questions/6053602/what-arguments-are-passed-into-asynctaskarg1-arg2-arg3/6053673#6053673 – IZI_Shadow_IZI Jun 14 '11 at 12:44
  • I've got a working AsyncTask implementation which I now use to invoke the AI player but would like some advice on good design. I basically need the AI to be informed of 4 actions (RESET, GET_NEXT_MOVE, UPDATE_USER_MOVE and GAME_OVER). Should all these 4 be handled in a single AsyncTask class or do we need to have 4 separate classes for each action? In either case is there some way to ensure they do not run simultaneously? – source.rar Jun 22 '11 at 13:28
  • hmmm just reading over this...an idea popped in my head...maybe you could pass the action into the ASyncTask method...according to what it is then perform whatever you need to do (in the inBackRound method) using if else or switch or what not. This would return the actions move or if its game_over/reset...then onPostExecute(I think thats what the method is called called) update the GUI according to what was returned from the inBackgroundMethod – IZI_Shadow_IZI Jun 22 '11 at 14:05
  • If we need to reset the game while computing the next move? Wouldn't the current AsyncTask be interrupted (or will the reset only be processed after the computation processing is completed?) – source.rar Jun 22 '11 at 14:09
  • You can call the cancel method for AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html#cancel(boolean) and then call isCancelled() and do whatever is needed there – IZI_Shadow_IZI Jun 22 '11 at 14:16
  • or onCancelled() take a look at the docs...let me know if it helps? but you should be able to get the above to work. – IZI_Shadow_IZI Jun 22 '11 at 14:17
  • Well I got the AsyncTask just doing one task (getting the next move from the AI) now. I tried with switching between the 4 actions in the doInBackground() but that was acting weirdly (sometimes the user move update was reaching before the result of the AI player was received etc.). – source.rar Jun 23 '11 at 15:37
  • Well it works ok with only a single task being handled in the AsyncTask. Trying to handle more than one seems to not be worth the effort (I think in that case using Handler/Threads may perhaps be better?) – source.rar Jul 05 '11 at 16:55