Game Development Reference
In-Depth Information
9. (Because there is no 0 on the keypad, we will just ignore the string at index 0 in our
list.)
Game AI
When we talk about how our AI
behaves, we will be talking about which
types of spaces on the board it will move
on. Just to be clear, we will label three
types of spaces on the Tic Tac Toe board:
corners, sides, and the center. Figure 10-3
is a chart of what each space is:
The AI for this game will follow a
simple algorithm. An algorithm is a
series of instructions to compute
something. This is a very loose In the case
of our Tic Tac Toe AI's algorithm, the
series of steps will determine which is the
best place to move. There is nothing in the
code that says, "These lines are an algorithm." like there is with a function's def-block. We
just consider the AI algorithm as all the code that is used in our program that determines the
AI's next move.
Figure 10-3: Locations of the side, corner, and center places.
Our algorithm will have the following steps:
1. First, see if there is a move the computer can make that will win the game. If there is,
take that move. Otherwise, go to step 2.
2. See if there is a move the player can make that will cause the computer to lose the
game. If there is, we should move there to block the player. Otherwise, go to step 3.
3. Check if any of the corner spaces (spaces 1, 3, 7, or 9) are free. (We always want to
take a corner piece instead of the center or a side piece.) If no corner piece is free,
then go to step 4.
4.
Check if the center is free. If so, move there. If it isn't, then go to step 5.
5.
Move on any of the side pieces (spaces 2, 4, 6, or 8). There are no more steps,
because if we have reached step 5 the side spaces are the only spaces left.
This all takes place in the "Get computer's move." box on our flow chart. We could add
this information to our flow chart like this:
Search WWH ::




Custom Search