-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTic-Tac-Toe.py
More file actions
50 lines (42 loc) · 1.58 KB
/
Tic-Tac-Toe.py
File metadata and controls
50 lines (42 loc) · 1.58 KB
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
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 9)
def check_win(board, player):
# Check rows, columns and diagonals for a win
for i in range(3):
if all([cell == player for cell in board[i]]) or all([board[j][i] == player for j in range(3)]):
return True
if board[0][0] == board[1][1] == board[2][2] == player or board[0][2] == board[1][1] == board[2][0] == player:
return True
return False
def check_tie(board):
return all([cell != ' ' for row in board for cell in row])
def get_move(player):
while True:
try:
move = int(input(f"Player {player}, enter your move (1-9): ")) - 1
x, y = divmod(move, 3)
if board[x][y] == ' ':
return x, y
else:
print("This cell is already taken. Please choose another cell.")
except (IndexError, ValueError):
print("Invalid move. Please enter a number between 1 and 9.")
def play_game():
board = [[' ' for _ in range(3)] for _ in range(3)]
current_player = 'X'
while True:
print_board(board)
x, y = get_move(current_player)
board[x][y] = current_player
if check_win(board, current_player):
print_board(board)
print(f"Player {current_player} wins!")
break
if check_tie(board):
print_board(board)
print("It's a tie!")
break
current_player = 'O' if current_player == 'X' else 'X'
play_game()