Simulating Thought
In a real game we’d look ahead several plys until we either reached the end of the game (a win, loss, or draw) or a timer ran out. For this example, we’ll use a timer to simulate one second of think time.
Using a Handler and postDelayed
To start, we need to declare and initialize an Android Handler instance in GameFragment.java:
ticTacToev3/src/main/java/org/example/tictactoe/GameFragment.java | |
| import android.os.Handler; |
| public class GameFragment extends Fragment { |
| // Data structures go here... |
| private Handler mHandler = new Handler(); |
| // ... |
| } |
A handler is a class that lets us delay doing something until later. We call its postDelayed() method, passing it the code that we want to run when the timer ...
Get Hello, Android, 4th Edition 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.