Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
249 lines (204 loc) · 7.34 KB
/
Copy pathmain.py
File metadata and controls
249 lines (204 loc) · 7.34 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import random
class Game:
def __init__(self):
self.board = [[" "] * 3 for i in range(3)]
while True:
choice = input("Player 1 Choose X or O: ").upper()
if choice in ["X", "O"]:
break
print("Invalid input! Please enter X or O.")
if choice == "X":
self.playersym = "X"
self.botsym = "O"
else:
self.playersym = "O"
self.botsym = "X"
self.current = self.playersym
def reset_board(self):
self.board = [[" "] * 3 for i in range(3)]
self.current = self.playersym
def show_board(self):
for row in self.board:
print(" | ".join(row))
print('-' * 9)
def win_check(self):
if self.board[0][0] == self.board [1][1] == self.board[2][2] != " " :
return self.board[0][0]
if self.board[0][2] == self.board [1][1] == self.board[2][0] != " " :
return self.board[0][2]
for row in self.board:
if row[0] == row[1] == row[2] != " " :
return row[0]
for col in range(3) :
if self.board[0][col] == self.board[1][col] == self.board[2][col] != " " :
return self.board[0][col]
return None
def move_left(self):
for row in self.board:
if row[0] == " " or row[1] == " " or row[2] == " " :
return True
return False
def player(self) :
while True:
self.show_board()
try:
move = int(input(f"Player {self.current}, Enter Your Move (1-9): "))
if not 1 <= move <= 9:
print("Invalid move! Please enter a number between 1 and 9.")
continue
except ValueError:
print("Invalid input! Please enter a number.")
continue
row = (move-1)//3
col = (move-1)%3
if self.board[row][col] != " " :
print("Invalid Move!")
else:
self.board[row][col] = self.current
break
def Eval(self,playersym,compsym) :
winner = self.win_check()
if winner == playersym:
return -10
elif winner == compsym:
return 10
else :
return 0
def easy(self):
while True:
move = random.randint(1,9)
row = (move-1)//3
col = (move-1)%3
if self.board[row][col] == " " :
self.board[row][col] = self.current
break
def medium(self):
for i in range(3):
for j in range(3):
if self.board[i][j] == " ":
self.board[i][j] = self.current
if self.win_check() == self.current:
return
self.board[i][j] = " "
opponent = "X" if self.current == "O" else "O"
for i in range(3):
for j in range(3):
if self.board[i][j] == " ":
self.board[i][j] = opponent
if self.win_check() == opponent:
self.board[i][j] = self.current
return
self.board[i][j] = " "
self.easy()
def minimax(self,depth,isMax):
score = self.Eval(self.playersym,self.botsym)
if score == 10:
return score - depth
elif score == -10:
return score + depth
elif not self.move_left():
return 0
if isMax :
best = -1000
for i in range(3):
for j in range(3):
if self.board[i][j] == " ":
self.board[i][j] = self.botsym
value = self.minimax(depth+1,False)
if value > best :
best = value
self.board[i][j] = " "
return best
if not isMax :
best = 1000
for i in range(3):
for j in range(3):
if self.board[i][j] == " ":
self.board[i][j] = self.playersym
value = self.minimax(depth+1,True)
if value < best :
best = value
self.board[i][j] = " "
return best
def hard(self):
bestVal = -1000
bestMove = (-1,-1)
for i in range(3):
for j in range(3):
if self.board[i][j] == " ":
self.board[i][j] = self.botsym
moveVal = self.minimax(0,False)
self.board[i][j] = " "
if moveVal > bestVal:
bestMove = (i,j)
bestVal = moveVal
self.board[bestMove[0]][bestMove[1]] = self.botsym
def game_loop(self, bot_function = None):
while True:
if bot_function == None:
self.player()
else :
if self.current == self.playersym:
self.player()
else:
bot_function()
win = self.win_check()
if win :
self.show_board()
print(f"Winner is player {win}")
break
elif not self.move_left() :
self.show_board()
print("Draw!")
break
self.current = "O" if self.current == "X" else "X"
def Instruct():
print("Welcome to Tic-Tac-Toe!")
print("Players take turns entering a number (1-9) to place their symbol (X or O).")
print("The board positions are as follows:")
print(" 1 | 2 | 3 ")
print("-----------")
print(" 4 | 5 | 6 ")
print("-----------")
print(" 7 | 8 | 9 ")
print("Let the game begin!\n")
def main():
Instruct()
game = Game()
while True:
print("Who do you wanna play with?")
print("1.Player\n2.Computer")
while True:
try:
ch = int(input("Enter Your Choice (1-2): "))
if ch in [1, 2]:
break
print("Invalid choice! Please enter 1 or 2.")
except ValueError:
print("Invalid input! Please enter a number.")
if ch == 1 :
game.game_loop()
if ch == 2 :
print("Select Difficulty")
print("1.Easy\n2.Medium\n3.Hard")
while True:
try:
choice = int(input("Enter Your Choice (1-3): "))
if choice in [1, 2, 3]:
break
print("Invalid choice! Please enter 1, 2, or 3.")
except ValueError:
print("Invalid input! Please enter a number.")
if choice == 1 :
game.game_loop(game.easy)
elif choice == 2:
game.game_loop(game.medium)
elif choice == 3:
game.game_loop(game.hard)
play_again = input("\nPlay Again? (y/n): ").lower()
if play_again != "y":
print("Thanks for playing!")
break
game.reset_board()
if __name__ == "__main__" :
main()