-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_max.py
119 lines (98 loc) · 2.8 KB
/
min_max.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Tic-Tac-Toe Board
board = [" " for _ in range(9)]
# Function to print the board
def print_board():
print(f"{board[0]} | {board[1]} | {board[2]}")
print("---------")
print(f"{board[3]} | {board[4]} | {board[5]}")
print("---------")
print(f"{board[6]} | {board[7]} | {board[8]}")
# Function to check if the board is full
def is_full(board):
return " " not in board
# Function to check if a player has won
def is_winner(board, player):
# Check rows
for i in range(0, 9, 3):
if board[i] == board[i + 1] == board[i + 2] == player:
return True
# Check columns
for i in range(3):
if board[i] == board[i + 3] == board[i + 6] == player:
return True
# Check diagonals
if board[0] == board[4] == board[8] == player:
return True
if board[2] == board[4] == board[6] == player:
return True
return False
# Min-Max algorithm
def minimax(board, depth, is_maximizing):
scores = {
"X": 1,
"O": -1,
"Tie": 0,
}
if is_winner(board, "X"):
return scores["X"] - depth
if is_winner(board, "O"):
return scores["O"] + depth
if is_full(board):
return scores["Tie"]
if is_maximizing:
best_score = float("-inf")
for i in range(9):
if board[i] == " ":
board[i] = "X"
score = minimax(board, depth + 1, False)
board[i] = " "
best_score = max(score, best_score)
return best_score
else:
best_score = float("inf")
for i in range(9):
if board[i] == " ":
board[i] = "O"
score = minimax(board, depth + 1, True)
board[i] = " "
best_score = min(score, best_score)
return best_score
# Function to find the best move
def find_best_move(board):
best_move = -1
best_score = float("-inf")
for i in range(9):
if board[i] == " ":
board[i] = "X"
score = minimax(board, 0, False)
board[i] = " "
if score > best_score:
best_score = score
best_move = i
return best_move
# Main game loop
while True:
print_board()
move = int(input("Enter your move (0-8): "))
if board[move] != " ":
print("Invalid move. Try again.")
continue
board[move] = "O"
if is_winner(board, "O"):
print_board()
print("You win!")
break
if is_full(board):
print_board()
print("It's a tie!")
break
best_move = find_best_move(board)
board[best_move] = "X"
if is_winner(board, "X"):
print_board()
print("Computer wins!")
break
if is_full(board):
print_board()
print("It's a tie!")
break