-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.py
290 lines (262 loc) · 9.14 KB
/
AI.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import random
board=[0,0,0,0,0,0,0,0,0]
difficulty=0
botGoesFirst=False
def makeGuess(mode):
if boardFull():
return -1
if detectWin():
return -1
if mode==1:
notPlaced=True
while notPlaced:
slot = random.randint(0,8)
if board[slot]!=0:
continue
board[slot]=-1
notPlaced=False
return slot
#end of mode = 1
if mode==2:
tmpFalse=False
if towInaRow()!=-1:#if the player has 2 in a row and can win then win
print("2 in a row ")
tile=towInaRow()
board[tile]=-1
return tile
elif block()!=-1:#Block: If the opponent has two in a row, the player must play the third themselves to block the opponent.
print("block")
tile=block()
board[tile]=-1
return tile
elif tmpFalse:#Fork: Cause a scenario where the player has two ways to win (two non-blocked lines of 2).
a=""#i cant be bothered to figure out how to implement these steps
elif tmpFalse:#Blocking an opponent's fork: If there is only one possible fork for the opponent, the player should block it. Otherwise, the player should block all forks in any way that simultaneously allows them to make two in a row. Otherwise, the player should make a two in a row to force the opponent into defending, as long as it does not result in them producing a fork. For example, if "X" has two opposite corners and "O" has the center, "O" must not play a corner move to win. (Playing a corner move in this scenario produces a fork for "X" to win.)
a=""
elif board[4]==0:#Center: A player marks the center.
print("center")
board[4]=-1
return 4
elif opposetCorner()!=-1:#Opposite corner: If the opponent is in the corner, the player plays the opposite corner.
print("opposed corner")
tile=opposetCorner()
board[tile]=-1
return tile
elif emptyCorner():#Empty corner: The player plays in a corner square.
print("empty corner")
tile=emptyCorner()
board[tile]=-1
return tile
elif emptySide()!=-1:#Empty side: The player plays in a middle square on any of the four sides.
print("empty side")
tile=emptySide()
board[tile]=-1
return tile
else:#if somehow it didn't decide to do one of the previous then randomly choose a place to place
return makeGuess(1)
#end of mode 2
def botGo():#call this function to have the bot make its move
randMode=random.randint(0,100)
if difficulty==0:
return makeGuess(1)
if difficulty==1:
if randMode>75:
return makeGuess(2)
else:
return makeGuess(1)
if difficulty==2:
if randMode>50:
return makeGuess(2)
else:
return makeGuess(1)
if difficulty==3:
if randMode>25:
return makeGuess(2)
else:
return makeGuess(1)
if difficulty==4:
return makeGuess(2)
def boardFull():
return board[0] != 0 and board[1] != 0 and board[2] != 0 and board[3] != 0 and board[4] != 0 and board[5] != 0 and board[6] != 0 and board[7] != 0 and board[8] != 0
def detectWin():
i =-1
if board[0] == i and board[1] == i and board[2] == i:# top row
return True
if board[3] == i and board[4] == i and board[5] == i:# middle row
return True
if board[6] == i and board[7] == i and board[8] == i:# bottom row
return True
if board[0] == i and board[3] == i and board[6] == i:# left colom
return True
if board[1] == i and board[4] == i and board[7] == i:# middle colom
return True
if board[2] == i and board[5] == i and board[8] == i:# right colom
return True
if board[0] == i and board[4] == i and board[8] == i:# diagonal 1
return True
if board[2] == i and board[4] == i and board[6] == i:# diagonal 2
return True
i = 1
if board[0] == i and board[1] == i and board[2] == i: # top row
return True
if board[3] == i and board[4] == i and board[5] == i: # middle row
return True
if board[6] == i and board[7] == i and board[8] == i: # bottom row
return True
if board[0] == i and board[3] == i and board[6] == i: # left colom
return True
if board[1] == i and board[4] == i and board[7] == i: # middle colom
return True
if board[2] == i and board[5] == i and board[8] == i: # right colom
return True
if board[0] == i and board[4] == i and board[8] == i: # diagonal 1
return True
if board[2] == i and board[4] == i and board[6] == i: # diagonal 2
return True
return False
def towInaRow():
#top row
if board[0] == -1 and board[1] == -1 and board[2] == 0:
return 2
if board[0] == -1 and board[1] == 0 and board[2] == -1:
return 1
if board[0] == 0 and board[1] == -1 and board[2] == -1:
return 0
#middle row
if board[3]==-1 and board[4]==-1 and board[5]==0:
return 5
if board[3]==-1 and board[4]==0 and board[5]==-1:
return 4
if board[3]==0 and board[4]==-1 and board[5]==-1:
return 3
#bottom row
if board[6]==-1 and board[7]==-1 and board[8]==0:
return 8
if board[6]==-1 and board[7]==0 and board[8]==-1:
return 7
if board[6]==0 and board[7]==-1 and board[8]==-1:
return 6
#verticle
#left colom
if board[0]==-1 and board[3]==-1 and board[6]==0:
return 6
if board[0]==-1 and board[3]==0 and board[6]==-1:
return 3
if board[0]==0 and board[3]==-1 and board[6]==-1:
return 0
#middle colom
if board[1]==-1 and board[4]==-1 and board[7]==0:
return 7
if board[1]==-1 and board[4]==0 and board[7]==-1:
return 4
if board[1]==0 and board[4]==-1 and board[7]==-1:
return 1
#right colom
if board[2]==-1 and board[5]==-1 and board[8]==0:
return 8
if board[2]==-1 and board[5]==0 and board[8]==-1:
return 5
if board[2]==0 and board[5]==-1 and board[8]==-1:
return 2
#diagonals
if board[0]==-1 and board[4]==-1 and board[8]==0:
return 8
if board[0]==-1 and board[4]==0 and board[8]==-1:
return 4
if board[0]==0 and board[4]==-1 and board[8]==-1:
return 0
if board[2]==-1 and board[4]==-1 and board[6]==0:
return 6
if board[2]==-1 and board[4]==0 and board[6]==-1:
return 4
if board[2]==0 and board[4]==-1 and board[6]==-1:
return 2
return -1
def block():
# top row
if board[0] == 1 and board[1] == 1 and board[2] == 0:
return 2
if board[0] == 1 and board[1] == 0 and board[2] == 1:
return 1
if board[0] == 0 and board[1] == 1 and board[2] == 1:
return 0
# middle row
if board[3] == 1 and board[4] == 1 and board[5] == 0:
return 5
if board[3] == 1 and board[4] == 0 and board[5] == 1:
return 4
if board[3] == 0 and board[4] == 1 and board[5] == 1:
return 3
# bottom row
if board[6] == 1 and board[7] == 1 and board[8] == 0:
return 8
if board[6] == 1 and board[7] == 0 and board[8] == 1:
return 7
if board[6] == 0 and board[7] == 1 and board[8] == 1:
return 6
# verticle
# left colom
if board[0] == 1 and board[3] == 1 and board[6] == 0:
return 6
if board[0] == 1 and board[3] == 0 and board[6] == 1:
return 3
if board[0] == 0 and board[3] == 1 and board[6] == 1:
return 0
# middle colom
if board[1] == 1 and board[4] == 1 and board[7] == 0:
return 7
if board[1] == 1 and board[4] == 0 and board[7] == 1:
return 4
if board[1] == 0 and board[4] == 1 and board[7] == 1:
return 1
# right colom
if board[2] == 1 and board[5] == 1 and board[8] == 0:
return 8
if board[2] == 1 and board[5] == 0 and board[8] == 1:
return 5
if board[2] == 0 and board[5] == 1 and board[8] == 1:
return 2
# diagonals
if board[0] == 1 and board[4] == 1 and board[8] == 0:
return 8
if board[0] == 1 and board[4] == 0 and board[8] == 1:
return 4
if board[0] == 0 and board[4] == 1 and board[8] == 1:
return 0
if board[2] == 1 and board[4] == 1 and board[6] == 0:
return 6
if board[2] == 1 and board[4] == 0 and board[6] == 1:
return 4
if board[2] == 0 and board[4] == 1 and board[6] == 1:
return 2
return -1
def opposetCorner():
if board[0] == 1 and board[8] == 0:
return 8
if board[8]==1 and board[0]==0:
return 0
if board[2]==1 and board[6]==0:
return 6
if board[6]==1 and board[2]==0:
return 2
return -1
def emptyCorner():
if board[8] == 0:
return 8
if board[0]==0:
return 0
if board[6]==0:
return 6
if board[2]==0:
return 2
return -1
def emptySide():
if board[1] == 0:
return 1
if board[3]==0:
return 3
if board[5]==0:
return 5
if board[7]==0:
return 7
return -1