-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_vs_me.py
More file actions
130 lines (119 loc) · 3.66 KB
/
ai_vs_me.py
File metadata and controls
130 lines (119 loc) · 3.66 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
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
120
121
122
123
124
125
126
127
128
129
130
import random
board = ["-", "-", "-",
"-", "-", "-",
"-", "-", "-"]
currentPlayer = "X"
winner = None
gameRunning = True
# PRINTING THE BOARD
def printboard(board):
print(board[0] + " | " + board[1] + " | " + board[2])
print("---------")
print(board[3] + " | " + board[4] + " | " + board[5])
print("---------")
print(board[6] + " | " + board[7] + " | " + board[8])
# TAKING PLAYER INPUT
def playerInput(board):
value = int(input("Enter the number from 1 - 9 : "))
if value >= 1 and value <= 9 and board[value - 1] == "-":
board[value - 1] = currentPlayer
else:
print("This spot is already used or invalid input!")
# CHECK FOR WIN OR TIE
def check_win(board):
global winner
if board[0] == board[1] == board[2] and board[1] != "-":
winner = board[0]
return True
elif board[3] == board[4] == board[5] and board[3] != "-":
winner = board[3]
return True
elif board[6] == board[7] == board[8] and board[6] != "-":
winner = board[6]
return True
elif board[0] == board[3] == board[6] and board[0] != "-":
winner = board[0]
return True
elif board[1] == board[4] == board[7] and board[1] != "-":
winner = board[1]
return True
elif board[2] == board[5] == board[8] and board[2] != "-":
winner = board[2]
return True
elif board[0] == board[4] == board[8] and board[0] != "-":
winner = board[0]
return True
elif board[2] == board[4] == board[6] and board[2] != "-":
winner = board[2]
return True
return False
def check_tie(board):
return "-" not in board
# SWITCH PLAYER
def switchPlayer():
global currentPlayer
if currentPlayer == "X":
currentPlayer = "O"
else:
currentPlayer = "X"
# MINIMAX ALGORITHM
def minimax(board, depth, is_maximizing):
if check_win(board):
if winner == "X":
return -10 + depth, None
elif winner == "O":
return 10 - depth, None
else:
return 0, None
elif check_tie(board):
return 0, None
if is_maximizing:
best_score = float('-inf')
best_move = None
for i in range(9):
if board[i] == "-":
board[i] = "O"
score, _ = minimax(board, depth + 1, False)
board[i] = "-"
if score > best_score:
best_score = score
best_move = i
return best_score, best_move
else:
best_score = float('inf')
best_move = None
for i in range(9):
if board[i] == "-":
board[i] = "X"
score, _ = minimax(board, depth + 1, True)
board[i] = "-"
if score < best_score:
best_score = score
best_move = i
return best_score, best_move
def strongest_opponent(board):
_, move = minimax(board, 0, True)
if move is not None:
board[move] = "O"
while gameRunning:
printboard(board)
playerInput(board)
if check_win(board):
printboard(board)
print(f"The winner is {winner}!")
break
if check_tie(board):
printboard(board)
print("It's a tie!")
break
switchPlayer()
strongest_opponent(board)
if check_win(board):
printboard(board)
print(f"The winner is {winner}!")
break
if check_tie(board):
printboard(board)
print("It's a tie!")
break
switchPlayer()