Use Python to compile a simple tic-tac-toe game tutorial, pythontic-tac-toe
In this tutorial, we will show you how to use python to create a well-known game. Here, we will use functions, arrays, if condition statements, while loop statements, and error capture.
First, we need to create two functions. The first function is used 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 the map, which is a two-dimensional array containing location information.
The game board looks like this:
| | | | | | X | X | O | X | O | O | X X | X | XX | X | XX | X | X
Next we need a function check_done () to check whether the game is over. If the message ends, True is returned and the message is printed.
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
Check whether one or more rows or columns are not empty and contain three identical symbols in the horizontal and vertical directions. Then, check the oblique direction. If one of the above directions is met, the game ends and "Won!" is printed !!!". Check the variable changes to indicate the current player.
At the same time, we need to check whether the current game board is filled and no one wins. The game draws.
With the above two functions, we will create three variables:
turn = "X"map = [[" "," "," "], [" "," "," "], [" "," "," "]]done = False
Turn: Who is the turn
Map: Game Board
Done: whether the game is over
Start the game now:
while done != True: print_board() print turn, "'s turn" print moved = False while moved != True:
Here, the while loop is used until the game ends and true is returned. in this loop, another while loop is used to check whether the player is moving. If the player is not moving, the program jumps to the next loop.
The next step is to tell gamers 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 to check whether the number is between 1 and 9. In addition, we need an error handling logic here. We also need to check whether players can move to a position:
Y = pos/3 X = pos%3 if X != 0: X -=1 else: X = 2 Y -=1
The following is all the 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"