-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tic-Tac-Toe Game.py
87 lines (76 loc) · 2.59 KB
/
Tic-Tac-Toe Game.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
import os
import random
def clear():
if os.name == 'nt':
_ = os.system('cls')
else:
_ = os.system('clear')
def initialize_board():
return [[" " for _ in range(3)] for _ in range(3)]
def print_board(board):
clear()
for row in board:
print("|" + "|".join(row) + "|")
print("-" * 5)
def check_win(board, player):
win_conditions = [
[board[0][0], board[0][1], board[0][2]],
[board[1][0], board[1][1], board[1][2]],
[board[2][0], board[2][1], board[2][2]],
[board[0][0], board[1][0], board[2][0]],
[board[0][1], board[1][1], board[2][1]],
[board[0][2], board[1][2], board[2][2]],
[board[0][0], board[1][1], board[2][2]],
[board[2][0], board[1][1], board[0][2]]
]
return any(all(cell == player for cell in condition) for condition in win_conditions)
def check_draw(board):
return all(board[i][j] != " " for i in range(3) for j in range(3))
def ai_move(board, ai_player):
player = "X" if ai_player == "O" else "O"
for check_player in [ai_player, player]:
for i in range(3):
for j in range(3):
if board[i][j] == " ":
board[i][j] = check_player
if check_win(board, check_player):
return
board[i][j] = " "
while True:
i, j = random.randint(0, 2), random.randint(0, 2)
if board[i][j] == " ":
board[i][j] = ai_player
break
def player_move(board, player):
while True:
try:
row = int(input("Enter row (0, 1, 2): "))
col = int(input("Enter column (0, 1, 2): "))
if board[row][col] == " ":
board[row][col] = player
break
else:
print("Invalid move. Try again.")
except (ValueError, IndexError):
print("Please enter valid row and column numbers.")
def play_game():
board = initialize_board()
player = "X"
ai_player = "O"
current_player = player
while True:
print_board(board)
if current_player == player:
player_move(board, player)
else:
ai_move(board, ai_player)
if check_win(board, current_player):
print_board(board) # Show final board state
print(f"Player {current_player} wins!")
break
if check_draw(board):
print_board(board) # Show final board state
print("It's a draw!")
break
current_player = ai_player if current_player == player else player
play_game()