Write a simple tutorial for the tic tac toe game using Python
- 2020-05-09 18:50:40
- OfStack
In this tutorial, we will show you how to create a tic-tac-toe game using python. We will use functions, arrays, if conditionals, while loops, error catching, and so on.
First we need to create two functions, the first function to display the game board:
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 ""
We use two for loops to traverse map, which is a 2-dimensional array containing location information.
The board looks like this:
| |
| |
| |
X | X |
O | X | O
| O | X
X | X | X
X | X | X
X | X | X
Next we need a function check_done() to check if the game is over. If it ends, it returns True and prints the 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 directions, whether 1 row or 1 column is not empty and contains 3 identical symbols, then we check the oblique direction. If one of the directions above is satisfied, the game ends and the "Won!!" is printed. . Note the variable change, which is used to mark which player is currently playing.
At the same time we need to check if the current board is full and no one wins, the game is tied.
With the above two functions, let's create three variables:
turn = "X"
map = [[" "," "," "],
[" "," "," "],
[" "," "," "]]
done = False
turn: whose turn is it
map: game board
done: is the game over
Now launch the game:
while done != True:
print_board()
print turn, "'s turn"
print
moved = False
while moved != True:
The while loop is used until the end of the game and true is returned. Within this loop, another while loop is used to check whether the player has moved or not. If the player has not moved, the program will skip to the next loop.
The next step tells the player how to play:
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 check to see if it is between 1 and 9. In addition, we need a piece of error handling logic here, and we also need to check whether the player can move to 1 position:
Y = pos/3
X = pos%3
if X != 0:
X -=1
else:
X = 2
Y -=1
Here's the full 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"