-
Notifications
You must be signed in to change notification settings - Fork 0
/
osero.py
358 lines (336 loc) · 16 KB
/
osero.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import const
import agent
import sys
import pygame
from time import sleep
from pygame.locals import *
import sys
import numpy as np
SCR_RECT = Rect(0,0,const.SCR_X,const.SCR_y)
green=(55,255,55)
r_green=(125,155,125)
white=(255,255,255)
brack=(0,0,0)
red=(255,0,0)
gray=(100,100,100)
purple=(155,155,255)
d_purple=(255,0,255)
class Osero:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(SCR_RECT.size)
self.SWITCH=0
while True:
pygame.display.update()
self.draw_back()
self.amount = self.count_storn()
self.draw_txt(self.screen,self.amount)
self.next_draw()
self.storn_draw(self.screen)
pygame.display.set_caption("osero")
self.event()
def draw_back(self):
pygame.draw.rect(self.screen, (white), Rect(0,0,750,500),0)
pygame.draw.rect(self.screen, (green), Rect(10,10,500-15,500-15),0)
pygame.draw.rect(self.screen, (brack), Rect(10,10,500-15,500-15),5)
pygame.draw.rect(self.screen, (purple), Rect(510,20,230,450),0)
pygame.draw.rect(self.screen, (white), Rect(520,30,210,120),0)
pygame.draw.rect(self.screen, (brack), Rect(520,30,210,120),5)
pygame.draw.rect(self.screen, (white), Rect(520,160,210,120),0)
pygame.draw.rect(self.screen, (brack), Rect(520,160,210,120),5)
pygame.draw.rect(self.screen, (white), Rect(520,290,210,120),0)
pygame.draw.rect(self.screen, (brack), Rect(520,290,210,120),5)
for i in range(8):
pygame.draw.line(self.screen, (brack), (15+const.CS*(i+1),15), (15+const.CS*(i+1),485),3)
pygame.draw.line(self.screen, (brack), (15,15+const.CS*(i+1)), (485,15+const.CS*(i+1)),3)
def draw_txt(self,screen,amount):
sysfont_big = pygame.font.SysFont(None, 80)
sysfont_small = pygame.font.SysFont(None, 50)
self.white_win = sysfont_small.render("white win!!",True, (0,55,155))
self.brack_win = sysfont_small.render("brack win!!",True, (0,55,155))
self.draw = sysfont_small.render("draw game!!",True, (0,55,155))
self.white_num = sysfont_big.render(str(amount[0]),True, (brack))
self.brack_num = sysfont_big.render(str(amount[1]),True, (brack))
self.hihun = sysfont_big.render("-",True, (brack))
self.pass_txt = sysfont_big.render("pass", True, (red))
self.replay_txt= sysfont_big.render("replay", True,(d_purple))
self.turn_white = sysfont_small.render("white turn", True, (brack))
self.turn_brack = sysfont_small.render("brack turn", True, (brack))
self.screen.blit(self.pass_txt, (560,190))
self.screen.blit(self.replay_txt, (540,320))
self.screen.blit(self.brack_num, (650,40))
self.screen.blit(self.white_num, (550,40))
self.screen.blit(self.hihun, (620,40))
if self.SWITCH == 0:
self.screen.blit(self.turn_brack, (540,100))
elif self.SWITCH == 1:
self.screen.blit(self.turn_white, (540,100))
if (amount[0]>amount[1] and amount[2]==64) or (amount[1]==0):
self.screen.blit(self.brack_win, (530,430))
elif (amount[0]<amount[1] and amount[2]==64) or (amount[0]==0):
self.screen.blit(self.white_win, (530,430))
elif (amount[0]==amount[1] and amount[2]==64):
self.screen.blit(self.draw, (530,430))
def storn_draw(self,screen):
for i in range(8):
for j in range(8):
if const.storn_position[j,i]==0:
pygame.draw.circle(self.screen, (green), (45+60*(i),45+60*(j)), 23)
elif const.storn_position[j,i]==1:
pygame.draw.circle(self.screen, (brack), (45+60*(i),45+60*(j)), 23)
elif const.storn_position[j,i]==2:
pygame.draw.circle(self.screen, (white), (45+60*(i),45+60*(j)), 23)
elif const.storn_position[j,i]==3:
pygame.draw.circle(self.screen, (gray), (45+60*(i),45+60*(j)), 5)
def mouse_get_position(self):
mouse_pressed = pygame.mouse.get_pressed()
if mouse_pressed[0]:
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
return(self.mouse_x,self.mouse_y)
def count_storn(self):
self.amount = [0 ,0 ,0]#brack,white,all
for i in range(8):
for j in range(8):
if const.storn_position[j,i] == 1:
self.amount[0] +=1
elif const.storn_position[j,i] == 2:
self.amount[1] +=1
self.amount[2] = self.amount[0]+self.amount[1]
return self.amount
def next_draw(self):
DX=[1,1,0,-1,-1,-1,0,1]
DY=[0,1,1,1,0,-1,-1,-1]
frag=1
if self.SWITCH==0:
for i in range(8):
for j in range(8):
for (dx,dy) in zip (DX,DY):
k = 1
count_reverse = 1
while True:
if (j+dx*k<0)or(j+dx*k>7)or(i+dy*k<0)or(i+dy*k>7):
count_reverse=1
break
if const.storn_position[i+dy*k][j+dx*k] == 0 or const.storn_position[i+dy*k][j+dx*k] == 3:
count_reverse=1
break
if const.storn_position[i+dy*k][j+dx*k] == 2:
count_reverse+=1
if const.storn_position[i+dy*k][j+dx*k] == 1:
break
k+=1
if count_reverse==1:
frag+=1
if frag<9 and const.storn_position[i,j]==0:
const.storn_position[i,j]=3
if frag==9 and const.storn_position[i,j]==3:
const.storn_position[i,j]=0
frag=1
if self.SWITCH==1:
for i in range(8):
for j in range(8):
for (dx,dy) in zip (DX,DY):
k = 1
count_reverse = 1
while True:
if (j+dx*k<0)or(j+dx*k>7)or(i+dy*k<0)or(i+dy*k>7):
count_reverse=1
break
if const.storn_position[i+dy*k][j+dx*k] == 0 or const.storn_position[i+dy*k][j+dx*k] == 3:
count_reverse=1
break
if const.storn_position[i+dy*k][j+dx*k] == 1:
count_reverse+=1
if const.storn_position[i+dy*k][j+dx*k] == 2:
break
k+=1
if count_reverse==1:
frag+=1
if frag<9 and const.storn_position[i,j]==0:
const.storn_position[i,j]=3
if frag==9 and const.storn_position[i,j]==3:
const.storn_position[i,j]=0
frag=1
def reverse(self,last_position):
DX=[1,1,0,-1,-1,-1,0,1]
DY=[0,1,1,1,0,-1,-1,-1]
frag=1
if self.SWITCH==0:
for (dx,dy) in zip (DX,DY):
i = 1
count_reverse = 1
while True:
if (last_position[1]+dx*i<0)or(last_position[1]+dx*i>7)or(last_position[0]+dy*i<0)or(last_position[0]+dy*i>7):
count_reverse=1
break
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 0:
count_reverse=1
break
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 2:
count_reverse+=1
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 1:
break
i+=1
for i in range(count_reverse):
const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i]=1
if count_reverse==1:
frag+=1
if self.SWITCH==1:
for (dx,dy) in zip (DX,DY):
i = 1
count_reverse = 1
while True:
if (last_position[1]+dx*i<0)or(last_position[1]+dx*i>7)or(last_position[0]+dy*i<0)or(last_position[0]+dy*i>7):
count_reverse=1
break
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 0:
count_reverse=1
break
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 1:
count_reverse+=1
if const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i] == 2:
break
i+=1
for i in range(count_reverse):
const.storn_position[last_position[0]+dy*i][last_position[1]+dx*i]=2
if count_reverse==1:
frag+=1
return frag
def event(self):
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_SPACE:
print(const.storn_position)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
mouse_position=self.mouse_get_position()
for i in range(8):
for j in range(8):
if mouse_position[0]>(20+const.CS*i) and mouse_position[0]<(60+const.CS*i) and mouse_position[1]>(25+const.CS*j) and mouse_position[1]<(60+const.CS*j):
self.last_position=[j,i]
if self.SWITCH==1 and const.storn_position[j,i]==3:
const.storn_position[j,i]=2
count_reverse=self.reverse(self.last_position)
if count_reverse!=9:
self.SWITCH=0
else: const.storn_position[j,i]=0
if self.SWITCH==0 and const.storn_position[j,i]==3:
const.storn_position[j,i]=1
count_reverse=self.reverse(self.last_position)
if count_reverse!=9:
self.SWITCH=1
else: const.storn_position[j,i]=0
if mouse_position[0]>525 and mouse_position[0]<725 and mouse_position[1]>295 and mouse_position[1]<405:
const.storn_position = np.asarray([[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,2,0,0,0],
[0,0,0,2,1,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]])
if mouse_position[0]>525 and mouse_position[0]<725 and mouse_position[1]>165 and mouse_position[1]<275:
if self.SWITCH==1:
self.SWITCH=0
else:
self.SWITCH=1
class VS_AI(Osero):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(SCR_RECT.size)
self.SWITCH=0
while True:
pygame.display.update()
self.draw_back()
self.amount = self.count_storn()
self.draw_txt(self.screen,self.amount)
self.next_draw()
self.storn_draw(self.screen)
pygame.display.set_caption("osero")
self.single_event()
def AI_action(self):
stock=-51
for i in range(8):
for j in range(8):
if const.storn_position[i,j] == 3 and stock<=const.value_storn[i,j]:
stock=const.value_storn[i,j]
AI_select=[i,j]
if stock==-51:
self.SWITCH=0
print("AI can't put!!")
if self.SWITCH==1:
const.storn_position[[AI_select[0]],[AI_select[1]]]=2
self.reverse(AI_select)
# pygame.draw.circle(screen, (r_green), (45+60*(AI_select[0]),45+60*(AI_select[1])), 40)
self.SWITCH=0
def single_event(self):
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
if event.type==KEYDOWN:
if event.key==K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_SPACE:
print(const.storn_position)
if event.type == MOUSEBUTTONDOWN and event.button == 1:
mouse_position=self.mouse_get_position()
for i in range(8):
for j in range(8):
if mouse_position[0]>(20+const.CS*i) and mouse_position[0]<(60+const.CS*i) and mouse_position[1]>(25+const.CS*j) and mouse_position[1]<(60+const.CS*j):
self.last_position=[j,i]
if self.SWITCH==0 and const.storn_position[j,i]==3:
const.storn_position[j,i]=1
count_reverse=self.reverse(self.last_position)
if count_reverse!=9:
self.SWITCH=1
self.next_draw()
self.AI_action()
else: const.storn_position[j,i]=0
if mouse_position[0]>525 and mouse_position[0]<725 and mouse_position[1]>295 and mouse_position[1]<405:
const.storn_position = np.asarray([[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,2,1,0,0,0],
[0,0,0,1,2,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]])
if mouse_position[0]>525 and mouse_position[0]<725 and mouse_position[1]>165 and mouse_position[1]<275:
if self.SWITCH==1:
self.SWITCH=0
else:
self.SWITCH=1
self.next_draw()
self.AI_action()
class GA(Osero):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(SCR_RECT.size)
self.SWITCH=0
while True:
pygame.display.update()
self.draw_back()
self.amount = self.count_storn()
self.draw_txt(self.screen,self.amount)
self.next_draw()
self.storn_draw(self.screen)
pygame.display.set_caption("osero")
self.event()
args=sys.argv
args.append('main')
if args[1] in 'AI':
if __name__ == '__main__':
VS_AI()
elif args[1] in 'GA':
if __name__ == '__main__':
GA()
elif args[1] in 'main':
if __name__ == '__main__':
Osero()