Tic Tac Toe, the classic game we've all played at some point, As a student still learning about python I have used all my beginner knowledge of the language to create this game. In this tutorial, we'll create a simple Tic Tac Toe game that runs in the terminal using Python. This project is beginner-friendly and will help you understand basic game logic and user input handling. Let's dive in!
Setting Up the Game Board
We'll start by initializing the game board, which in our case will be a list representing the Tic Tac Toe grid. Each cell of the grid will be represented by a number: 0 for empty, 1 for 'X', and 2 for 'O'. Here's how we initialize the board:
board = [
0, 0, 0,
0, 0, 0,
0, 0, 0
]
Displaying the Board
To make the game more user-friendly, we'll define a function to print the current state of the board. This function iterates through the board list and replaces the numeric values with 'X', 'O', or '-' for empty cells. Here's the print_board()
function:
def print_board():
display_position = ['-', '-', '-', '-', '-', '-', '-', '-', '-']
for i in range(9):
if board[i] == 1:
display_position[i] = 'X'
elif board[i] == 2:
display_position[i] = 'O'
print('''
______________
| {} | {} | {} |
|____|____|____|
| {} | {} | {} |
|____|____|____|
| {} | {} | {} |
|____|____|____|
'''.format(*display_position))
This function will print out the current state of the board in a visually appealing manner.
Checking for a Winner
Next, we need to implement a function to check if a player has won the game. We'll define a check_win()
function that examines the board state for any winning combinations. If a winning combination is found, the function returns True
; otherwise, it returns False
.
def check_win(num):
if board[0] == num and board[1] == num and board[2] == num:
return True
elif board[3] == num and board[4] == num and board[5] == num:
return True
# Other winning combinations...
else:
return False
Main Game Loop
Finally, we'll implement the main game loop. This loop controls the flow of the game, alternating between player turns until a winner is determined or the game ends in a draw. Players input their moves via the terminal.
turn = 0
while turn < 9:
# Player 1's turn.
while True:
try:
p1_position = int(input("Player 1 enter position number (1 through 9): ")) - 1
if 0 <= p1_position <= 8 and board[p1_position] == 0:
board[p1_position] = 1
break
elif p1_position < 0 or p1_position > 8:
print('Invalid position number. Please enter a number between 1 and 9.')
else:
print('Position already taken. Please choose another position.')
except ValueError:
print('Invalid input. Please enter a number.')
# Increment turn counter and print the board.
turn += 1
print_board()
# Check if player 1 wins or if it's a draw.
if turn == 9:
print('Draw')
break
player_1_stat = check_win(1)
if player_1_stat:
print('Player 1 WINS')
break
# Player 2's turn.
while True:
try:
p2_position = int(input("Player 2 enter position number (1 through 9): ")) - 1
if 0 <= p2_position <= 8 and board[p2_position] == 0:
board[p2_position] = 2
break
elif p2_position < 0 or p2_position > 8:
print('Invalid position number. Please enter a number between 1 and 9.')
else:
print('Position already taken. Please choose another position.')
except ValueError:
print('Invalid input. Please enter a number.')
# Increment turn counter and print the board.
turn += 1
print_board()
# Check if player 2 wins.
player_2_stat = check_win(2)
if player_2_stat:
print('Player 2 WINS')
break
Conclusion
Congratulations! You've built a simple yet functional Tic Tac Toe game in Python. Feel free to expand upon this project by adding features like a replay option or implementing a graphical interface using libraries like Pygame or Tkinter. Happy coding!
github link to my code : https://github.com/ThevinuDan/Python-TIC-TAC-TOE.git
Top comments (0)