This repository was archived by the owner on May 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWES_Main.py
385 lines (332 loc) · 16.5 KB
/
WES_Main.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import time
import pygame as p
import WES_Engine
from PastVersionsForAIAlgorithm import ai_algorithm_showcase as ai
import ai_algorithm_56642728 as ai2
import ctypes
import jpype
#os.environ['JAVA_HOME'] = r'C:/Program Files/Java/jdk-19'
class WES(object):
def __init__(self):
self.WIDTH = self.HEIGHT = 512
self.DIMENSIONS = 5 # dimensions of a chess board are 5x5
self.SQ_SIZE = self.HEIGHT // self.DIMENSIONS
self.MAX_FPS = 15 #for animations
self.IMAGES = {}
self.LANGUAGE = "PYTHON" #{C++, JAVA, PYTHON}
self.PLAY_ROLE = [0, 0] # index: 0 for player 1 as wolf and 1 for player 2 as sheep. value: 0 for AI and 1 for human.
self.initial_board = [
['1', '1', '1', '1', '1'],
['1', '1', '1', '1', '1'],
['0', '1', '0', '1', '0'],
['0', '0', '0', '0', '0'],
['0', '2', '0', '2', '0']] # the index of each chess is their real position in pygame
'''
Initialize a global dictionary of images. This will be called exactly once in the main
'''
self.round = 0
self.current_round = 'round' + str(self.round) + '/'
# return board status by reading the board file
def read_board(self, filename):
board_digits = list()
board_current = list()
with open(filename, 'r+') as fin:
board_digits = fin.read().split('\n')
for item in board_digits:
items = item.split(',')
board_current.append(items)
return board_current
def loadImages(self):
pieces = ['2', '1']
for piece in pieces:
if piece == '2':
self.IMAGES[piece] = p.transform.scale(p.image.load('images/'+'bW'+'.png'), (self.SQ_SIZE, self.SQ_SIZE))
else:
self.IMAGES[piece] = p.transform.scale(p.image.load('images/'+'wS'+'.png'), (self.SQ_SIZE, self.SQ_SIZE))
#Note: We can access an image by saying IMAGES[piece], the size can be reset by SQ_SIZE * shape
'''
Responsible for all graphics within a current game state
'''
'''
make move by human manipulation
'''
def click_move(self, e, sqSelection, playerClicks, gs, moveMade):
if e.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos() # (x, y) ->location of mouse[[0,0],...[4,4]] from left top to right bottom
col = location[0] // self.SQ_SIZE
row = location[1] // self.SQ_SIZE
if sqSelection == (row, col): # the user clicked the same square twice
sqSelection = () # deselect
playerClicks = [] # clear player click
else:
sqSelection = (row, col)
playerClicks.append(sqSelection) # append for both 1st and 2nd clicks
if len(playerClicks) == 2: # after 2nd click
move = WES_Engine.Move(playerClicks[0], playerClicks[1], gs.board)
if gs.getValidMoves(move, moveMade):
#print('finished')
gs.makeMove(move)
moveMade = not moveMade
# print(moveMade, '1111')
sqSelection = () # reset user clicks
playerClicks = []
else:
sqSelection = () # reset user clicks
playerClicks = []
return gs, moveMade, sqSelection, playerClicks
def drawGameState(self, screen, gs):
self.drawBoard(screen)
self.drawPieces(screen, gs.board)
'''
Draw the squares on the board
'''
def drawBoard(self, screen):
colors = [p.Color('lightgreen'), p.Color('lightblue')]
for r in range(self.DIMENSIONS):
for c in range(self.DIMENSIONS):
color = colors[(r + c) % 2]
p.draw.rect(screen, color, p.Rect(c*self.SQ_SIZE, r*self.SQ_SIZE, self.SQ_SIZE, self.SQ_SIZE))
'''
Draw the pieces on the board using the current game state on board
'''
def drawPieces(self, screen, board):
for r in range(self.DIMENSIONS):
for c in range(self.DIMENSIONS):
piece = board[r][c]
if piece != '0':
screen.blit(self.IMAGES[piece], p.Rect(c*self.SQ_SIZE, r*self.SQ_SIZE, self.SQ_SIZE, self.SQ_SIZE))
'''
ai_algorithm, input is the board status, output the start position and end position of a chess
'''
def ai_algorithm(self, gs, count, moveMade): # or replace the gs with filename
record_file = self.current_round + 'state_' + str(count-1) +'.txt'
if self.LANGUAGE == "PYTHON":
start_row, start_col, end_row, end_col = ai.AIAlgorithm(record_file, moveMade)
elif self.LANGUAGE == "C++":
# Compile a .cpp to .so: g++ --shared -o aiAlgorithm.so aiAlgorithm.cpp
# Load the shared library
#lib = ctypes.CDLL('./c++/aiAlgorithm.so') # If Python >= 3.8, please use this coomand: lib = ctypes.CDLL('./c++/aiAlgorithm.so', winmode=0)
lib = ctypes.CDLL(r'C:\Users\weicwang2\OneDrive - City University of Hong Kong - Student\Desktop\GAME\GAME\c++\aiAlgorithm.so', winmode=0)
lib.AIAlgorithm_c.restype = ctypes.POINTER(ctypes.c_long) #ctypes.POINTER(ctypes.c_int)
lib.AIAlgorithm_c.argtypes = [ctypes.c_char_p, ctypes.c_bool]
result = lib.AIAlgorithm_c(ctypes.c_char_p(record_file.encode('utf-8')), moveMade)
# result = lib.ai_algorithm(record_file, moveMade)
move = [result[i] for i in range(4)]
del result
start_row, start_col, end_row, end_col = move[0], move[1], move[2], move[3]
elif self.LANGUAGE == "JAVA":
# compile a .java file to .jar:
# 1. javac AIAlgorithm.java
# 2. jar cf AIAlgorithm.jar AIAlgorithm.class
# Create a Java object that corresponds to the AI_Algorithm class
jarpath = r'./java/AIAlgorithm.jar' # the path to jar file
JVMPath = jpype.getDefaultJVMPath()
Djava = "-Djava.class.path=" + jarpath
if not jpype.isJVMStarted():
jpype.startJVM(JVMPath, "-ea", Djava)
JDClass = jpype.JClass("AIAlgorithm")
jd = JDClass()
# Call the ai_algorithm function
result = jd.ai_algorithm(record_file, moveMade)
# Convert the result to a Python list
result = list(result)
start_row, start_col, end_row, end_col = result[0], result[1], result[2], result[3]
return start_row, start_col, end_row, end_col
def ai2_algorithm(self, gs, count, moveMade): # or replace the gs with filename
record_file = self.current_round + 'state_' + str(count-1) +'.txt'
if self.LANGUAGE == "PYTHON":
start_row, start_col, end_row, end_col = ai2.AIAlgorithm(record_file, moveMade)
elif self.LANGUAGE == "C++":
# Compile a .cpp to .so: g++ --shared -o aiAlgorithm.so aiAlgorithm.cpp
# Load the shared library
#lib = ctypes.CDLL('./c++/aiAlgorithm.so') # If Python >= 3.8, please use this coomand: lib = ctypes.CDLL('./c++/aiAlgorithm.so', winmode=0)
lib = ctypes.CDLL(r'C:\Users\weicwang2\OneDrive - City University of Hong Kong - Student\Desktop\GAME\GAME\c++\aiAlgorithm.so', winmode=0)
lib.AIAlgorithm_c.restype = ctypes.POINTER(ctypes.c_long) #ctypes.POINTER(ctypes.c_int)
lib.AIAlgorithm_c.argtypes = [ctypes.c_char_p, ctypes.c_bool]
result = lib.AIAlgorithm_c(ctypes.c_char_p(record_file.encode('utf-8')), moveMade)
# result = lib.ai_algorithm(record_file, moveMade)
move = [result[i] for i in range(4)]
del result
start_row, start_col, end_row, end_col = move[0], move[1], move[2], move[3]
elif self.LANGUAGE == "JAVA":
# compile a .java file to .jar:
# 1. javac AIAlgorithm.java
# 2. jar cf AIAlgorithm.jar AIAlgorithm.class
# Create a Java object that corresponds to the AI_Algorithm class
jarpath = r'./java/AIAlgorithm.jar' # the path to jar file
JVMPath = jpype.getDefaultJVMPath()
Djava = "-Djava.class.path=" + jarpath
if not jpype.isJVMStarted():
jpype.startJVM(JVMPath, "-ea", Djava)
JDClass = jpype.JClass("AIAlgorithm")
jd = JDClass()
# Call the ai_algorithm function
result = jd.ai_algorithm(record_file, moveMade)
# Convert the result to a Python list
result = list(result)
start_row, start_col, end_row, end_col = result[0], result[1], result[2], result[3]
return start_row, start_col, end_row, end_col
def ai_move(self, gs, count, moveMade):
start_row, start_col, end_row, end_col = self.ai_algorithm(gs, count, moveMade)
start = (int(start_row), int(start_col))
end = (int(end_row), int(end_col))
move = WES_Engine.Move(start, end, gs.board)
# print(move.getChessNotation())
# print(gs.getValidMoves(move, moveMade))
# print(gs.board[move.startRow][move.startCol] == '2')
# print(move)
if gs.getValidMoves(move, moveMade):
gs.makeMove(move)
moveMade = not moveMade
else:
#print(moveMade)
#print(start, '\n', end)
#print(gs.board)
raise ValueError('The move is invalid')
return gs, moveMade
def ai2_move(self, gs, count, moveMade):
start_row, start_col, end_row, end_col = self.ai2_algorithm(gs, count, moveMade)
start = (int(start_row), int(start_col))
end = (int(end_row), int(end_col))
move = WES_Engine.Move(start, end, gs.board)
# print(move.getChessNotation())
if gs.getValidMoves(move, moveMade):
gs.makeMove(move)
moveMade = not moveMade
else:
#print(moveMade)
#print(start, '\n', end)
#print(gs.board)
raise ValueError('The move is invalid')
return gs, moveMade
def board_record(self, gs, count_n):
board = gs.board
record_name = self.current_round + 'state_' + str(count_n) + '.txt'
board_str = ''
for item in board:
count = 0
for chess in item:
if count < 4:
board_str += chess + ','
count += 1
else:
board_str += chess + '\n'
with open(record_name, 'w+') as fin:
fin.write(board_str)
return count_n + 1
'''
The main driver for our code, This will handle user input and updating the graphics
'''
def main(self):
p.init()
screen = p.display.set_mode((self.WIDTH, self.HEIGHT))
clock = p.time.Clock()
screen.fill(p.Color('white'))
gs = WES_Engine.WES_State()
moveMade = True # True for wolf and False for Sheep
self.loadImages() # only load images once before the while loop
running = True
sqSelection = () # no square is selected, keep track of the last click of the user{tuple: (row, col)}
playerClicks = [] # keey track of player clicks {two tuples:[beginning position, ending position]}
initial_file = 'state_0.txt'
font = p.font.init()
my_font = p.font.SysFont('Comic Sans MS', 30)
p.display.set_caption('Wolves Eat Sheep')
count = 1
self.drawGameState(screen, gs)
count_mode = True
while running:
for e in p.event.get():
if e.type == p.QUIT:
running = False
if moveMade:
if self.PLAY_ROLE[0] == 1:
gs, moveMade = self.ai2_move(gs, count, moveMade)
#print('2222', moveMade, playerClicks)
if count_mode == moveMade:
count = self.board_record(gs, count)
count_mode = not count_mode
status = self.check_winner(gs, clock, screen, my_font, p)
if status:
return True
elif self.PLAY_ROLE[0] == 0:
gs, moveMade = self.ai_move(gs, count, moveMade)
if count_mode == moveMade:
count = self.board_record(gs, count)
count_mode = not count_mode
status = self.check_winner(gs, clock, screen, my_font, p)
if status:
return True
else:
raise ValueError('The setting of playing is wrong')
if not moveMade:
if self.PLAY_ROLE[1] == 1:
gs, moveMade = self.ai2_move(gs, count, moveMade)
#print('1111', moveMade, playerClicks)
if count_mode == moveMade:
count = self.board_record(gs, count)
count_mode = not count_mode
status = self.check_winner(gs, clock, screen, my_font, p)
if status:
return True
elif self.PLAY_ROLE[1] == 0:
gs, moveMade = self.ai_move(gs, count, moveMade)
if count_mode == moveMade:
count = self.board_record(gs, count)
count_mode = not count_mode
status = self.check_winner(gs, clock, screen, my_font, p)
if status:
return True
else:
raise ValueError('The setting of playing is wrong')
# self.drawGameState(screen, gs)
# clock.tick(self.MAX_FPS)
# winner = gs.checkWinning()
# if winner != 0:
# if winner == 1:
# surface_1 = my_font.render('Wolves Win!', False, (220, 0, 0))
# surface_2 = my_font.render('Input mode for A New Game', False, (220, 0, 0))
# screen.blit(surface_1, (1.7 * self.SQ_SIZE, 2 * self.SQ_SIZE))
# screen.blit(surface_2, (0.2 * self.SQ_SIZE, 2.4 * self.SQ_SIZE))
# p.display.flip()
# time.sleep(5)
# p.quit()
# return True
# if winner == 2:
# surface_1 = my_font.render('Sheep Win!', False, (220, 0, 0))
# surface_2 = my_font.render('Input mode for A New Game', False, (220, 0, 0))
# screen.blit(surface_1, (1.7 * self.SQ_SIZE, 2 * self.SQ_SIZE))
# screen.blit(surface_2, (0.2 * self.SQ_SIZE, 2.4 * self.SQ_SIZE))
# p.display.flip()
# time.sleep(5)
# p.quit()
# return True
# p.display.flip()
'''
Winner Judgement
'''
def check_winner(self, gs, clock, screen, my_font, p):
self.drawGameState(screen, gs)
clock.tick(self.MAX_FPS)
winner = gs.checkWinning()
if winner != 0:
if winner == 1:
surface_1 = my_font.render('Wolves Win!', False, (220, 0, 0))
surface_2 = my_font.render('Input mode for A New Game', False, (220, 0, 0))
screen.blit(surface_1, (1.7 * self.SQ_SIZE, 2 * self.SQ_SIZE))
screen.blit(surface_2, (0.2 * self.SQ_SIZE, 2.4 * self.SQ_SIZE))
p.display.flip()
time.sleep(5)
p.quit()
return True
if winner == 2:
surface_1 = my_font.render('Sheep Win!', False, (220, 0, 0))
surface_2 = my_font.render('Input mode for A New Game', False, (220, 0, 0))
screen.blit(surface_1, (1.7 * self.SQ_SIZE, 2 * self.SQ_SIZE))
screen.blit(surface_2, (0.2 * self.SQ_SIZE, 2.4 * self.SQ_SIZE))
p.display.flip()
time.sleep(5)
p.quit()
return True
else:
return False