-
Notifications
You must be signed in to change notification settings - Fork 0
/
tic_tac_toe_python.py
100 lines (82 loc) · 3.7 KB
/
tic_tac_toe_python.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -*- coding: utf-8 -*-
"""Tic-Tac-Toe-python.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Fpw-WJ8rNRJMBQEELLn3a8uW6NpxS5ii
"""
import random
# Function for printing the Tic-Tac-Toe board
def print_board(board):
print(" " + "-" * 13) # Printing the top border of the board
for i in range(3):
# Printing each row of the board with cell values
print(f"| {board[i][0]} | {board[i][1]} | {board[i][2]} |")
print("|---|---|---|") # Printing the separator between rows
print(" " + "-" * 13) # Printing the bottom border of the board
# Function to check if a player has won
def checking_winner(board, symbol):
for row in board:
# Checking if all cells in a row have the same symbol
if all(cell == symbol for cell in row):
return True
for col in range(3):
# Checking if all cells in a column have the same symbol
if all(board[row][col] == symbol for row in range(3)):
return True
# Checking if all cells in a diagonal have the same symbol
if all(board[i][i] == symbol for i in range(3)) or all(board[i][2 - i] == symbol for i in range(3)):
return True
return False
# Function to check if the board is full
def is_board_full(board):
# Checking if all cells in the board are filled
return all(cell != ' ' for row in board for cell in row)
# Function for the player to make a move
def player_move(board, symbol):
while True:
try:
# Get the player's move as input
move = int(input(f"Enter your move (1-9) for {symbol}: "))
if 1 <= move <= 9:
# Convert the move to row and column indices
row, col = divmod(move - 1, 3)
if board[row][col] == ' ':
# If the cell is empty, place the player's symbol
board[row][col] = symbol
break
else:
print("Cell already taken. Try again.")
else:
print("Invalid move. Enter a number between 1 and 9.")
except ValueError:
print("Invalid input. Please enter a number.")
# Function for the computer to make a move
def computer_move(board, symbol):
# Find all empty cells on the board
empty_cells = [(i, j) for i in range(3) for j in range(3) if board[i][j] == ' ']
# Choose a random empty cell for the computer's move
return random.choice(empty_cells)
# Function to play the Tic-Tac-Toe game
def play_game():
board = [[' ' for _ in range(3)] for _ in range(3)] # Initializing an empty 3x3 board
symbols = ['X', 'O'] # Defining player symbols
current_symbol = symbols[0] # Start with the first player's symbol
print("Welcome to Tic-Tac-Toe!\n")
print_board(board) # Printing the initial empty board
for _ in range(9): # Maximum of 9 moves in a game
if current_symbol == 'X':
player_move(board, current_symbol) # Player's turn
else:
row, col = computer_move(board, current_symbol) # Computer's turn
board[row][col] = current_symbol
print(f"\nComputer chooses position {row * 3 + col + 1} ({current_symbol})\n")
print_board(board) # Printing the updated board
if checking_winner(board, current_symbol):
print(f"\nHooray! {current_symbol} wins!\n")
break
elif is_board_full(board):
print("\nAhh,It's a tie!\n")
break
current_symbol = symbols[1] if current_symbol == symbols[0] else symbols[0] # Switch player symbols
if __name__ == "__main__":
play_game() # Start the Tic-Tac-Toe game when the script is executed