In this tutorial, we will learn how to create a simple Tic Tac Toe game using Python. This classic game consists of a 3×3 grid where players take turns to place their symbols (typically ‘X’ and ‘O’) until one player has their symbols in an uninterrupted line (horizontally, vertically, or diagonally) or the board is completely filled, resulting in a draw.
We will create a program that consists of functions for:
– Displaying the Tic Tac Toe board
– Taking turns for player moves
– Checking if a player has won or the game has ended in a draw
Note: This tutorial assumes you have a basic understanding of Python programming.
Step 1: Creating the Tic Tac Toe Board
We’ll create a function that initializes a 3×3 grid to represent the Tic Tac Toe board. The grid is represented as a list of lists, with each list representing a row on the board.
1 |
Step 2: Displaying the Tic Tac Toe Board
Next, we’ll create a function to display the Tic Tac Toe board in a user-friendly format. The board is printed with row and column separators and proper formatting.
1 2 3 4 5 6 7 8 9 |
def display_board(board): for i in range(len(board)): for j in range(len(board[i])): print(board[i][j], end = '') if j<2: print('|', end='') print('') if i<2: print('-'*5) |
Step 3: Taking Player Turns
We need a function to handle player moves. This function takes the board, row, column and player symbol as input and updates the board. We also need to ensure the move is valid (within board bounds and not overwriting an existing symbol).
1 2 3 4 5 |
def make_move(board, row, col, symbol): if 0<=row<3 and 0<=col<3 and board[row][col]==' ': board[row][col] = symbol return True return False |
Step 4: Checking the Game Status
After each move, we need to check if the game has been won, lost or drawn. The following function takes the board and checks all horizontal, vertical, and diagonal rows for a win, or all positions filled for a draw.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
def check_game_status(board): # check rows and columns for i in range(3): if board[i][0]==board[i][1]==board[i][2] !=' ': return board[i][0] + " wins" if board[0][i]==board[1][i]==board[2][i] !=' ': return board[0][i] + " wins" # check diagonals if board[0][0]==board[1][1]==board[2][2] !=' ': return board[0][0] + " wins" if board[0][2]==board[1][1]==board[2][0] !=' ': return board[0][2] + " wins" # check draw draw = True for row in board: if ' ' in row: draw = False if draw: return "Draw" return None |
Step 5: Running the Game
Finally, we’ll create a function to run the game loop. It initializes the board, displays it, and takes input from the players, making a move, checking for a win or draw, and switching turns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
def play_game(): board = create_board() current_player = 'X' display_board(board) while True: row = int(input("Enter row (0-2): ")) col = int(input("Enter col (0-2): ")) if make_move(board, row, col, current_player): display_board(board) status = check_game_status(board) if status: print(status) break # Switch to the other player current_player = 'O' if current_player == 'X' else 'X' play_game() |
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
def create_board(): return [[' ' for _ in range(3)] for _ in range(3)] def display_board(board): for i in range(len(board)): for j in range(len(board[i])): print(board[i][j], end = '') if j<2: print('|', end='') print('') if i<2: print('-'*5) def make_move(board, row, col, symbol): if 0<=row<3 and 0<=col<3 and board[row][col]==' ': board[row][col] = symbol return True return False def check_game_status(board): # check rows and columns for i in range(3): if board[i][0]==board[i][1]==board[i][2] !=' ': return board[i][0] + " wins" if board[0][i]==board[1][i]==board[2][i] !=' ': return board[0][i] + " wins" # check diagonals if board[0][0]==board[1][1]==board[2][2] !=' ': return board[0][0] + " wins" if board[0][2]==board[1][1]==board[2][0] !=' ': return board[0][2] + " wins" # check draw draw = True for row in board: if ' ' in row: draw = False if draw: return "Draw" return None def play_game(): board = create_board() current_player = 'X' display_board(board) while True: row = int(input("Enter row (0-2): ")) col = int(input("Enter col (0-2): ")) if make_move(board, row, col, current_player): display_board(board) status = check_game_status(board) if status: print(status) break # Switch to the other player current_player = 'O' if current_player == 'X' else 'X' play_game() |
Conclusion
In this tutorial, we learned how to create a simple Tic Tac Toe game in Python. By following these steps and understanding the functions, you can further optimize the code and implement additional features, such as a more intelligent computer player or improved user input validation. Enjoy playing and building upon your new Tic Tac Toe game!