-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reversi.py
311 lines (288 loc) · 18.1 KB
/
Reversi.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from pygame import display,init, image, font, event, QUIT, KEYDOWN, K_ESCAPE, K_r,K_p, MOUSEBUTTONDOWN, mouse, time, draw, Rect
from os import execv, path
import sys
def resource_path(relative_path):
if hasattr(sys, '_MEIPASS'):
return path.join(sys._MEIPASS, relative_path)
return path.join(path.abspath('.'), relative_path)
class Game:
def __init__ (self):
self.__PointsBlack,self.__PointsWhite = 2,2
self.__possible_moves,self.__Board = [],[]
self.__player = 2
self.__AI = True
for y in range(8):
self.__Board.append([])
for x in range(30,830,100):
self.__Board[y].append([x,y*100+80,0])
self.Board()
def Board(self):
init()
self.__screen = display.set_mode((850,900))
self.__players = [image.load(resource_path("assets/img/Player_White.png")),image.load(resource_path("assets/img/Player_Black.png"))]
self.__Options = image.load(resource_path("assets/img/Player_Possible_move.png"))
self.__font = font.Font(resource_path('assets/fonts/SF-Compact-Rounded-Regular.otf'),24)
self.__won,self.__lost = self.__font.render("WON",True,(255,255,255)),self.__font.render("LOST",True,(255,255,255))
display.set_caption("Reversi")
display.set_icon(image.load(resource_path("assets/img/Icon.ico")))
self.__screen.blits([(image.load(resource_path("assets/img/Board.png")),(0,0)),(self.__players[0],self.__Board[3][3][:2]),(self.__players[1],self.__Board[3][4][:2]),(self.__players[1],self.__Board[4][3][:2]),(self.__players[0],self.__Board[4][4][:2])])
self.__Board[3][3][2],self.__Board[3][4][2],self.__Board[4][3][2],self.__Board[4][4][2]= 1,2,2,1
shadow = self.__font.render("HUMAN", True, (255,255,255,255))
self.__screen.blit(shadow,(23.7,23))
self.Options()
self.listen()
def Options(self):
self.__backup = self.__possible_moves
self.possible_moves()
if self.__backup != self.__possible_moves and self.__possible_moves != []:
for i in self.__backup:
if i != self.__hit and self.__hit in self.__backup:
blit = self.__Board[i[0]][i[1]][:2]
draw.rect(self.__screen, (41,104,33), Rect(blit[0]-1, blit[1]-1, 92, 92))
if self.__player == 2 or (self.__player == 1 and self.__AI == False):
for i in self.__possible_moves:
self.__screen.blit(self.__Options,self.__Board[i[0]][i[1]][:2])
display.flip()
def possible_moves(self):
self.__possible_moves, self.__flip = [],[]
if self.__player == 1:
self.__Color, self.__OppositColor = 1,2
if self.__player == 2:
self.__Color, self.__OppositColor = 2,1
for x in range(8):
for y in range(8):
if self.__Board[y][x][2] == self.__Color:
if y - 1 >= 0 and x - 1 >= 0 and self.__Board[y - 1][x - 1][2] == self.__OppositColor:
List = [y - 1, x - 1]
for i in range(2,10):
y1,x1 = y - i, x - i
if y1 < 0 or x1 < 0 or self.__Board[y1][x1][2] == self.__Color:
break
if self.__Board[y1][x1][2] == 0:
if [y1,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x1])].extend(List)
else:
self.__possible_moves.append([y1,x1]),self.__flip.append(List)
break
if self.__Board[y1][x1][2] == self.__OppositColor:
List.extend([y1,x1])
if y - 1 >= 0 and x >= 0 and self.__Board[y - 1][x][2] == self.__OppositColor:
List = [y - 1, x]
for i in range(2,10):
y1 = y - i
if y1 < 0 or x < 0 or self.__Board[y1][x][2] == self.__Color:
break
if self.__Board[y1][x][2] == 0:
if [y1,x] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x])].extend(List)
else:
self.__possible_moves.append([y1,x]),self.__flip.append(List)
break
if self.__Board[y1][x][2] == self.__OppositColor:
List.extend([y1,x])
if y - 1 >= 0 and x + 1 <= 7 and self.__Board[y - 1][x + 1][2] == self.__OppositColor:
List = [y - 1, x + 1]
for i in range(2,10):
y1,x1 = y - i, x + i
if y1 < 0 or x1 > 7 or self.__Board[y1][x1][2] == self.__Color:
break
if self.__Board[y1][x1][2] == 0:
if [y1,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x1])].extend(List)
else:
self.__possible_moves.append([y1,x1]),self.__flip.append(List)
break
if self.__Board[y1][x1][2] == self.__OppositColor:
List.extend([y1,x1])
if y >= 0 and x - 1 >= 0 and self.__Board[y][x - 1][2] == self.__OppositColor:
List = [y, x - 1]
for i in range(2,10):
x1 = x - i
if y < 0 or x1 < 0 or self.__Board[y][x1][2] == self.__Color:
break
if self.__Board[y][x1][2] == 0:
if [y,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y,x1])].extend(List)
else:
self.__possible_moves.append([y,x1]),self.__flip.append(List)
break
if self.__Board[y][x1][2] == self.__OppositColor:
List.extend([y,x1])
if y >= 0 and x + 1 <= 7 and self.__Board[y][x + 1][2] == self.__OppositColor:
List = [y, x + 1]
for i in range(2,10):
x1 = x + i
if y < 0 or x1 > 7 or self.__Board[y][x1][2] == self.__Color:
break
if self.__Board[y][x1][2] == 0:
if [y,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y,x1])].extend(List)
else:
self.__possible_moves.append([y,x1]),self.__flip.append(List)
break
if self.__Board[y][x1][2] == self.__OppositColor:
List.extend([y,x1])
if y + 1 <= 7 and x - 1 >= 0 and self.__Board[y + 1][x - 1][2] == self.__OppositColor:
List = [y + 1, x - 1]
for i in range(2,10):
y1,x1 = y + i, x - i
if y1 > 7 or x1 < 0 or self.__Board[y1][x1][2] == self.__Color:
break
if self.__Board[y1][x1][2] == 0:
if [y1,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x1])].extend(List)
else:
self.__possible_moves.append([y1,x1]),self.__flip.append(List)
break
if self.__Board[y1][x1][2] == self.__OppositColor:
List.extend([y1,x1])
if y + 1 <= 7 and x >= 0 and self.__Board[y + 1][x][2] == self.__OppositColor:
List = [y + 1,x]
for i in range(2,10):
y1 = y + i
if y1 > 7 or x < 0 or self.__Board[y1][x][2] == self.__Color:
break
if self.__Board[y1][x][2] == 0:
if [y1,x] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x])].extend(List)
else:
self.__possible_moves.append([y1,x]),self.__flip.append(List)
break
if self.__Board[y1][x][2] == self.__OppositColor:
List.extend([y1,x])
if y + 1 <= 7 and x + 1 <= 7 and self.__Board[y + 1][x + 1][2] == self.__OppositColor:
List = [y + 1,x + 1]
for i in range(2,10):
y1,x1 = y + i, x + i
if y1 > 7 or x1 > 7 or self.__Board[y1][x1][2] == self.__Color:
break
if self.__Board[y1][x1][2] == 0:
if [y1,x1] in self.__possible_moves:
self.__flip[self.__possible_moves.index([y1,x1])].extend(List)
else:
self.__possible_moves.append([y1,x1]),self.__flip.append(List)
break
if self.__Board[y1][x1][2] == self.__OppositColor:
List.extend([y1,x1])
if self.__PointsBlack == 2 and self.__PointsWhite == 2 or self.__possible_moves == []:
self.Points()
def Points(self):
self.__PointsBlack,self.__PointsWhite = 0,0
for y in self.__Board:
for x in y:
if x[2] == 1:
self.__PointsWhite += 1
if x[2] == 2:
self.__PointsBlack += 1
draw.rect(self.__screen, (32,32,32), Rect(385, 23, 30, 30)), draw.rect(self.__screen, (32,32,32), Rect(435, 23, 30, 30))
if self.__PointsBlack + self.__PointsWhite != 64 and self.__possible_moves != []:
textPointsBlack = self.__font.render(str(self.__PointsBlack), True, (255,255,255,255))
textPointsWhite = self.__font.render(str(self.__PointsWhite),True,(255,255,255,255))
self.__screen.blits([(textPointsBlack,(412-textPointsBlack.get_rect()[2],23)),(textPointsWhite,(438,23))])
if self.__PointsBlack + self.__PointsWhite == 64 or self.__possible_moves == []:
if self.__PointsBlack > self.__PointsWhite:
self.__screen.blits([(self.__won,(362,23)),(self.__lost,(438,23))])
else:
self.__screen.blits([(self.__lost,(359,23)),(self.__won,(438,23))])
display.flip()
def listen(self):
while True:
if len(self.__possible_moves) == 0:
while True:
for playerevent in event.get():
if playerevent.type == KEYDOWN and playerevent.key == K_ESCAPE or playerevent.type == QUIT:
sys.exit()
if playerevent.type == KEYDOWN and playerevent.key == K_r:
execv(sys.executable, ['Reversi.py'] + sys.argv)
#---------------------------------<AI's Turn>---------------------------------#
if self.__player == 1 and len(self.__possible_moves) > 0 and self.__AI == True:
bestMove = 1
for i in self.__flip:
if len(i) > bestMove:
bestMove,index = len(i),self.__flip.index(i)
time.wait(500)
self.__hit = [self.__possible_moves[index][0],self.__possible_moves[index][1]]
if self.__hit in self.__possible_moves:
position = self.__possible_moves.index(self.__hit)
for z in range(0,len(self.__flip[position]),2):
flip1,flip2 = self.__flip[position][z],self.__flip[position][z+1]
self.__Board[flip1][flip2][2] = self.__Color
self.__screen.blit(self.__players[self.__Color-1],self.__Board[flip1][flip2][:2])
self.__screen.blit(self.__players[self.__Color-1],self.__Board[self.__hit[0]][self.__hit[1]][:2])
self.__Board[self.__hit[0]][self.__hit[1]][2],self.__player = self.__Color, self.__OppositColor
self.Points()
self.Options()
#---------------------------------<AI's Turn>---------------------------------#
if len(self.__possible_moves) > 0 and self.__player == 2 or (self.__player == 1 and self.__AI == False):
for playerevent in event.get():
#--------------------------------------------changeEnemyType----------------------------------------------------#
mousex, mousey = mouse.get_pos()
if mousex >= 755 and mousex <= 824 and mousey >= 28 and mousey <= 46 and self.__AI == True:
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("ROBOT", True, (200,200,200,255))
self.__screen.blit(shadow,(755,23))
display.flip()
if playerevent.type == MOUSEBUTTONDOWN and playerevent.button == 1:
self.__AI = False
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("HUMAN", True, (200,200,200,255))
self.__screen.blit(shadow,(745,23))
display.flip()
break
if mousex >= 744 and mousex <= 824 and mousey >= 28 and mousey <= 46 and self.__AI == False:
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("HUMAN", True, (200,200,200,255))
self.__screen.blit(shadow,(745,23))
display.flip()
if playerevent.type == MOUSEBUTTONDOWN and playerevent.button == 1:
self.__AI = True
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("ROBOT", True, (200,200,200,255))
self.__screen.blit(shadow,(755,23))
display.flip()
break
if (mousex == 750 and (mousey >= 23 and mousey <= 51) or mousex == 829 and (mousey >= 23 and mousey <= 51) or mousey == 23 and (mousex >=750 and mousex <= 829) or mousey == 51 and (mousex >=750 and mousex <= 829)) and self.__AI == True:
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("ROBOT", True, (255,255,255,255))
self.__screen.blit(shadow,(755,23))
display.flip()
break
if (mousex == 742 and (mousey >= 23 and mousey <= 51) or mousex == 829 and (mousey >= 23 and mousey <= 51) or mousey == 23 and (mousex >=750 and mousex <= 829) or mousey == 51 and (mousex >=750 and mousex <= 829)) and self.__AI == False:
draw.rect(self.__screen, (32,32,32),(735,20 , 100, 35))
shadow = self.__font.render("HUMAN", True, (255,255,255,255))
self.__screen.blit(shadow,(745,23))
display.flip()
break
#--------------------------------------------changeEnemyType----------------------------------------------------#
if playerevent.type == KEYDOWN and playerevent.key == K_ESCAPE or playerevent.type ==QUIT:
sys.exit()
if playerevent.type == KEYDOWN and playerevent.key == K_r:
execv(sys.executable, ['Reversi.py'] + sys.argv)
if playerevent.type == MOUSEBUTTONDOWN and playerevent.button == 1:
x,y = mouse.get_pos()
self.__newx, self.__newy = None,None
for xs in range(25,825,100):
if x > xs and x < xs+100:
self.__newx = xs //100
break
for ys in range(75,875,100):
if y > ys and y < ys+100:
self.__newy = ys //100
break
if self.__newx == None or self.__newy == None:
break
self.__hit = [self.__newy,self.__newx]
if self.__hit in self.__possible_moves:
position = self.__possible_moves.index(self.__hit)
for z in range(0,len(self.__flip[position]),2):
flip1,flip2 = self.__flip[position][z],self.__flip[position][z+1]
self.__Board[flip1][flip2][2] = self.__Color
self.__screen.blit(self.__players[self.__Color-1],self.__Board[flip1][flip2][:2])
self.__screen.blit(self.__players[self.__Color-1],self.__Board[self.__hit[0]][self.__hit[1]][:2])
self.__Board[self.__hit[0]][self.__hit[1]][2],self.__player = self.__Color, self.__OppositColor
self.Points()
self.Options()
#-<Main Program>-#
if __name__ == "__main__":
Reversi = Game()
#-<Main Program>-#