Today's Question:  What are you most afraid of as a programmer?

# The tic-tac-toe game with Python

sonic0002      2013-07-30 02:49:09      11,255    0    1

In this tutorial, we will show how to build a tic-tac-toe game with Python. We will use functions,arrays,if statements,while statements, for loops and error handling etc.

First, we need to create two functions, the first function will display the borad of the game:

```def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print "|",
print ""```

Here we used two for loops to loop through the map, this map is a two dimensional array which contains the location information.

The board looks like:

```  |   |
|   |
|   |
```
```X | X |
O | X | O
| O | X
```
```X | X | X
X | X | X
X | X | X
```

Next we need to create a check_done() function to check whether the game is over after each movement. If the game is over, then return True and print a message.

```def check_done():
for i in range(0,3):
if map[i][0] == map[i][1] == map[i][2] != " " \
or map[0][i] == map[1][i] == map[2][i] != " ":
print turn, "won!!!"
return True

if map[0][0] == map[1][1] == map[2][2] != " " \
or map[0][2] == map[1][1] == map[2][0] != " ":
print turn, "won!!!"
return True

if " " not in map[0] and " " not in map[1] and " " not in map[2]:
print "Draw"
return True

return False```

There are a few places to check, first check the horizontal and vertical direction, if there is one row or one column which is not empty and contains the same symbol in all 3 cells, then we need to check diagonally. If either one of these is fulfilled, the game is over and "Won!!!" will be printed. Also please note variable turn, its use is to check which player is the current player.

Also we need to check whether the board is filled up and no one wins, then the game is tie.

With above two functions, now we create 3 variables :

```turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False```
• turn : Who's turn
• map : Game board
• done : Whether the game is over or not

Now start the game:

```while done != True:
print_board()

print turn, "'s turn"
print

moved = False
while moved != True:
```

Here the while loop will continue until done returns True. Note in this while loop, there is one more while loop which will check whether the player moves or not, if the player doesn't move, then it goes to next loop.

Next we tell the player how to play the game:

```print "Please select position by typing in a number between 1 and 9, see below for which number that is which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print```
```try:
pos = input("Select: ")
if pos <=9 and pos >=1:
```

We expect the player to enter a number and then we check whether it's between 1 ad 9. In addition, we need to handle error handling logic here, we need to check whether the player can move to one place or not:

```Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1
```

Here is the complete code:

```def print_board():
for i in range(0,3):
for j in range(0,3):
print map[2-i][j],
if j != 2:
print "|",
print ""

def check_done():
for i in range(0,3):
if map[i][0] == map[i][1] == map[i][2] != " " \
or map[0][i] == map[1][i] == map[2][i] != " ":
print turn, "won!!!"
return True

if map[0][0] == map[1][1] == map[2][2] != " " \
or map[0][2] == map[1][1] == map[2][0] != " ":
print turn, "won!!!"
return True

if " " not in map[0] and " " not in map[1] and " " not in map[2]:
print "Draw"
return True

return False

turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False

while done != True:
print_board()

print turn, "'s turn"
print

moved = False
while moved != True:
print "Please select position by typing in a number between 1 and 9,\
see below for which number that is which position..."
print "7|8|9"
print "4|5|6"
print "1|2|3"
print

try:
pos = input("Select: ")
if pos <=9 and pos >=1:
Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1

if map[Y][X] == " ":
map[Y][X] = turn
moved = True
done = check_done()

if done == False:
if turn == "X":
turn = "O"
else:
turn = "X"

except:
print "You need to add a numeric value"```