android - How to pass a turn to AI in a game? -


I am writing a simple game for 2 players, where they have to change after each other - something to remove Select objects from the board (extracting is actually setting a ImageView invisible), then a player presses "end turning" to turn the other player in the end.

The Board is a GridLayout with ImageView s, all on ImageView s onClickListener () , And they are all visible in the beginning of the game. A player clicks on many ImageView s (their id is placed in an array in these moments) and then clicks on "end turning", analyzing this array of moves The corresponding ImageView is set to invisible, and after this the turn should be passed to the other player (the array is cleared and the number of changes in the existing player).

When clicking on the onClickListeners () attached to all the objects on the board, their ID is placed in an array, and the number of the current player in a variable is. Turn it into a onClickListener () onClick () method, is connected to the "end turning" button, and it is simply clearing the array of IDs and Updating it is the current player's number.

It works fine for 2 people's game affairs (on the same device). But if someone plays with the computer, AI calculates and tries to do the same in the onClick () method as the user interface updates only onClick () After processing on, (the ImageView s is set to invisible, and so on), the AI ​​moves do not appear on the screen, and the player sees that after his move (object Click), excessive by the move of the board (objects removed) Immediately after the person changes the board was ready again.

Therefore, I am not in any type of AI, to attract the selection of AI and to hide selected ideas, not in the onClick () of the "last turn" button Players can do their next move. But how to do it and where?

for onClick () method for the "last turn" button:

  Click Public Zero (see V) { Hide selected image view (); If (currentGame.isWinningState ()) {// you won} else {changePlayer (); If (playWithAI) {calculateAImove (currentGame); showSelectedImageViewsByAI (); // does not work with .invalidate () Hide selected image view (); If (currentGame.isWinningState ()) {// AI wins}} player changing (); }}}   

The selected image view () in the show, is set for a specific image resource ImageView s, so that they are different ("Selected").

You can create a new thread where it will run the AI ​​moves, When AI makes your selections, you can delay the thread in a few seconds (hence their selection can be shown) and then remove them. Your code has a problem with that problem, once AI selects it, it is immediately removed (even if you have invalid () twice)

If you Example:

  // I think that your code was an activity within the main activity of the public class, the action taken out of the proceedings runnables {thread aiThread; /*...*/ Public click on Zero (see V) {Hide selected image view (); If (currentGame.isWinningState ()) / / you win * /} else {changePlayer (); If (playWithAI) {aiThread = new thread (this); AiThread.start (); }}} Public Zero Run () {calculateAImove (currentGame); ShowSelectedImageViewsByAI (); Thread.Sleep (1000); // It will wait thread for 1 second before removing it from selected (note that the number is in milliseconds) Hide selected image view (); If (currentGame.isWinningState ()) {/ * AI wins} /} changePlayer (); }}   

Please note that when using a thread to affect the UI, you should not directly affect the view, instead you should see view.post () if you are unfamiliar with either the threads or view.post () method, then I recommend searching online for them.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -