Game Development Reference
In-Depth Information
10 - Tic Tac Toe
Tic Tac Toe is a very easy and short game to play on paper. In our Tic Tac Toe computer
game, we'll let the player choose if they want to be X or O, randomly choose who goes
first, and then let the player and computer take turns making moves on the board. Here is
what a flow chart of this game could look like:
You can see a lot of the boxes on the left side of the chart are what happens during the
player's turn. The right side of the chart shows what happens on the computer's turn. The
player has an extra box for drawing the board because the computer doesn't need the board
printed on the screen. After the player or computer makes a move, we check if they won or
caused a tie, and then the game switches turns. If either the computer or player ties or wins
the game, we ask the player if they want to play again.
Representing the Board as Data
First, we need to figure out how we are going to represent the board as a variable. On
paper, the Tic Tac Toe board is drawn as a pair of horizontal lines and a pair of vertical
lines, with either an X, O, or empty space in each of the nine spaces.
In our program, we are going to represent the Tic Tac Toe board as a list of strings. Each
string will represent one of the nine positions on the board. We will give a number to each
of the spaces on the board. To make it easier to remember which index in the list is for
which piece, we will mirror the numbers on the keypad of our keyboard. See Figure 10-2.
Figure 10-2: The board will be numbered like the keyboard's number pad.
The strings will either be 'X' for the X player, 'O' for the O player, or a space string '
' to mark a spot on the board where no one has marked yet. The index of the string in the
list will also be the number of the space on the board.
So if we had a list with ten strings named board , then board[7] would be the top-left
square on the board (either an X, O, or blank space). board[5] would be the very center.
When the player types in which place they want to move, they will type a number from 1 to
Search WWH ::




Custom Search