diff --git a/SafariChess_AI.py b/SafariChess_AI.py index 92a6e24..0a1b9cc 100644 --- a/SafariChess_AI.py +++ b/SafariChess_AI.py @@ -12,7 +12,7 @@ piece_score = {"7": 120, "1": 100, "6": 120, "5": 80, "4": 60,"3": 70,"2": 100} mouse_score = [[13, 13, 13, 13, 12, 11, 10, 9, 8], [25, 20, 15, 13, 12, 12, 10, 9, 8], [50, 25, 20, 12, 11, 11, 10, 9, 8], - [100000, 50, 20, 11, 9, 9, 9, 9, 0], + [1919810, 50, 20, 11, 9, 9, 9, 9, 0], [50, 25, 20, 9, 8, 8, 8, 8, 8], [25, 20, 15, 9, 8, 8, 8, 8, 8], [11, 11, 10, 8, 8, 8, 8, 8, 8]] @@ -34,7 +34,7 @@ mouse_score = [[13, 13, 13, 13, 12, 11, 10, 9, 8], eagle_score = [[14,13,10,12,14,13,10,8,7], [15,15,14,0,0,0,9,8,6], [50,20,20,0,0,0,7,5,4], - [100000,50,20,13,12,11,10,8,0], + [1919810,50,20,13,12,11,10,8,0], [50,20,20,0,0,0,7,5,4], [15,12,15,0,0,0,9,8,6], [14,12,10,13,14,13,10,8,7]] @@ -45,7 +45,7 @@ fox_score = [[ 11, 12, 14, 13, 12, 11, 10, 8, 8], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], - [100000, 50, 20, 13, 12, 11, 11, 10, + [1919810, 50, 20, 13, 12, 11, 11, 10, 0], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], @@ -60,7 +60,7 @@ wolf_score = [[ 11, 12, 14, 13, 12, 11, 10, 8, 8], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], - [100000, 50, 20, 13, 12, 11, 11, 10, + [1919810, 50, 20, 13, 12, 11, 11, 10, 0], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], @@ -75,7 +75,7 @@ leopard_score = [[ 11, 12, 14, 13, 12, 11, 10, 8 8], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], - [100000, 50, 20, 13, 12, 11, 11, 10, + [1919810, 50, 20, 13, 12, 11, 11, 10, 0], [ 50, 20, 20, 0, 0, 0, 10, 9, 10], @@ -84,29 +84,13 @@ leopard_score = [[ 11, 12, 14, 13, 12, 11, 10, 8 [ 11, 12, 14, 13, 12, 11, 10, 8, 8]] -tiger_score = [[ 20, 20, 18, 15, 12, 11, 14, 12, - 5], - [ 40, 25, 30, 0, 0, 0, 16, 12, - 12], - [ 150, 40, 30, 0, 0, 0, 16, 12, - 12], - [100000, 150, 20, 15, 15, 15, 9, 12, - 0], - [ 150, 40, 30, 0, 0, 0, 16, 12, - 12], - [ 40, 25, 30, 0, 0, 0, 16, 12, - 12], - [ 20, 20, 18, 15, 12, 11, 14, 12, - 5]] -# tiger_score is not used in the game... - lion_score = [[ 20, 20, 18, 15, 12, 11, 14, 12, 5], [ 40, 25, 30, 0, 0, 0, 16, 12, 12], [ 50, 40, 30, 0, 0, 0, 16, 12, 12], - [100000, 50, 20, 15, 15, 15, 9, 12, + [1919810, 50, 20, 15, 15, 15, 9, 12, 0], [ 50, 40, 30, 0, 0, 0, 16, 12, 12], @@ -121,7 +105,7 @@ elephant_score = [[ 20, 20, 18, 15, 12, 11, 14, 1 12], [ 50, 40, 30, 0, 0, 0, 16, 12, 12], - [100000, 50, 20, 15, 15, 15, 9, 12, + [1919810, 50, 20, 15, 15, 15, 9, 12, 0], [ 50, 40, 30, 0, 0, 0, 16, 12, 12], diff --git a/SafariChess_AI_mcst.py b/SafariChess_AI_mcst.py new file mode 100644 index 0000000..e69de29 diff --git a/SafariChess_Gamev1.0.py b/SafariChess_Gamev1.0.py index ad30ad6..7dbb0d9 100644 --- a/SafariChess_Gamev1.0.py +++ b/SafariChess_Gamev1.0.py @@ -40,7 +40,7 @@ IMAGES = {} bias_top = 100 #棋盘的上边距 bias_left = 20 #棋盘的左边距 #网络道具 -general_countdown = 10 +general_countdown = 40 client = None networkMsg = None server = None @@ -70,7 +70,7 @@ def listenFromServer(): def startNetworkServices(): global client,server,port,addr,connection client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - server = '192.168.43.183' + server = '127.0.0.1' port = 50005 addr = (server,port) connection = client.connect(addr) @@ -282,7 +282,10 @@ def MainMenu(): mode = 1 run = False if button_2.rect.collidepoint(event.pos): + activate_AI = input("activate_AI? y/[n]: ") mode = 2 + if activate_AI == 'y': + mode = 4 run = False if button_3.rect.collidepoint(event.pos): mode = 3 @@ -495,7 +498,7 @@ def main(mode): if lastNetworkMsg != networkMsg:#handle #! 无法解决的问题:判断延迟导致无法同时接收来自服务器的两条转发消息。 #! 尝试让输方发送消息解决上述问题。 - print('catch new msg: ',networkMsg) + print('handling new msg: ',networkMsg) lastNetworkMsg = networkMsg #networkMsg中保存当前字典 if 'status' in networkMsg.keys(): #Login_handler @@ -517,9 +520,9 @@ def main(mode): elif networkMsg['request'] == 'stop': game_state.win_person = 'b' if networkMsg['side'] == 1 else 'r' - pass + elif networkMsg['request'] == 'report': - foul = networkMsg['side'] + foul = MySide otherQuitJson = { 'type': 3, @@ -539,13 +542,14 @@ def main(mode): game_over = True #Move handler - elif 'src' and 'dst' in networkMsg.keys(): + elif 'src' and 'dst' in networkMsg.keys():# 接受对手行棋 theMove = backend.Move([networkMsg['src']['y'],networkMsg['src']['x']],[networkMsg['dst']['y'],networkMsg['dst']['x']],game_state.board) + #print(theMove) violate_175 = game_state.seventeen_five_violation(goal=theMove.get_goal(),color=theMove.get_turn(), showTrace= True) - violate_73 = game_state.seventy_three_violation(goal=theMove.get_goal(), color = theMove.get_turn(), showTrace= True) + violate_73 = game_state.seventy_three_violation(goal=theMove.get_goal(), color=theMove.get_turn(), showTrace= True) if not type(violate_175) == bool: print("player {} foul 17-5".format(theMove.get_turn())) - print("Recent 17 Move: {}".format(violate_175)) + print("Recent Moves: {}".format(violate_175)) reportJson = { "type": 2, "msg": { @@ -555,9 +559,10 @@ def main(mode): } } client.send(json.dumps(reportJson).encode('utf-8')) + foul = theMove.get_turn() if not type(violate_73) == bool: print("player {} foul 7-3".format(theMove.get_turn())) - print("Recent 7 Move: {}".format(violate_73)) + print("Recent Moves: {}".format(violate_73)) reportJson = { "type": 2, "msg": { @@ -567,6 +572,7 @@ def main(mode): } } client.send(json.dumps(reportJson).encode('utf-8')) + foul = theMove.get_turn() game_state.makeMove(theMove) valid_moves = game_state.getAllMoves() other_stage = not other_stage @@ -746,13 +752,18 @@ def main(mode): game_over = True #print('game over label confirmed.') - if game_over and other_joined: + if time_out_side != None: + showGameOverText(screen,"player "+ ("b" if time_out_side==1 else "r")+" timed out") #超时了 + elif foul is not None: + showGameOverText(screen,"player "+ ("b" if foul == 1 else "r") + "'s foul play") + game_over = True + elif game_over and other_joined: if (game_state.win_person == ''): game_state.conquer() showGameOverText(screen,"player "+game_state.win_person+" wins") - if game_over and not other_joined: + elif game_over and not other_joined: showGameOverText(screen,"player "+ ("b" if MySide==0 else "r") + " quitted the game") #对手退出了 - if time_out_side != None: - showGameOverText(screen,"player "+ ("b" if time_out_side==1 else "r")+" timed out") #超时了 + + showText(screen,bigfont,game_state.color(),700,350) clock.tick(50) pg.display.flip() diff --git a/SafariChess_backend.py b/SafariChess_backend.py index 4871e36..8ad5fd8 100644 --- a/SafariChess_backend.py +++ b/SafariChess_backend.py @@ -23,7 +23,7 @@ class Move: return self.moveID == __o.moveID return False def __str__(self): - return f"{self.start_row=},"+f"{self.start_col=},"+f"{self.end_row=},"+f"{self.end_col=}" + return f"{self.start_row=}, "+f"{self.start_col=}, "+f"{self.end_row=}, "+f"{self.end_col=}" def get_goal(self): return (self.end_row, self.end_col) def get_start(self): @@ -115,7 +115,7 @@ class GameState: try: if visit_counts[goal] >= 3: if showTrace: - return goal + return (goal, [move.get_goal() for move in past7Moves]) else: return False else: @@ -128,16 +128,20 @@ class GameState: # showTrace为是否查看轨迹 #获取近17步操作 + RECENT_COUNT = 17 + PERMIT_RANGE = 5 + # print("using params: {}, {}, {}".format(goal, color, showTrace)) past17Moves = [] for i in range(len(self.move_log) - 1, -1, -1): if self.move_log[i].get_turn() == color: past17Moves.append(self.move_log[i]) - if len(past17Moves) > 17: + if len(past17Moves) > RECENT_COUNT: break - if len(past17Moves) >= 17: - past17Moves = past17Moves[-17:] + if len(past17Moves) >= RECENT_COUNT: + past17Moves = past17Moves[-RECENT_COUNT:] else: return True + #print("past17Movesloc: {}".format([move.get_goal() for move in past17Moves])) #判断是否为同一棋子的移动 piece = past17Moves[0].get_piece() for i in range(len(past17Moves)): @@ -148,21 +152,23 @@ class GameState: for move in past17Moves: if move.get_goal() in (self.blue_trap_loc + self.red_trap_loc): return True - try: - visit_locs.append(move.get_goal()) - except KeyError: - visit_locs.append(move.get_goal()) + this_loc = move.get_goal() + if this_loc not in visit_locs: + visit_locs.append(this_loc) + try: - if showTrace and len(visit_locs) <= 5: + if showTrace and len(visit_locs) <= PERMIT_RANGE: if goal in visit_locs: return (goal,visit_locs) else: return True - elif len(visit_locs)<=5: + elif len(visit_locs)<=PERMIT_RANGE: if goal in visit_locs: return False else: return True + else: + return True except: return True @@ -213,7 +219,7 @@ class GameState: self.moveFunctions[self.board[row][col][1]](row,col,moves) valid_moves = [] for move in moves: # 己方躲避17-5和7-3 - if self.seventy_three_violation(goal=move.get_goal_chess_comb(), color=self.color()) and self.seventeen_five_violation(goal=move.get_goal_chess_comb(), color=self.color()): + # if self.seventy_three_violation(goal=move.get_goal_chess_comb(), color=self.color()) and self.seventeen_five_violation(goal=move.get_goal_chess_comb(), color=self.color()): valid_moves.append(move) if len(valid_moves) == 0: print('warning: no available moves at side {}'.format('b' if not self.red_to_move else 'r')) diff --git a/__pycache__/SafariChess_AI.cpython-39.pyc b/__pycache__/SafariChess_AI.cpython-39.pyc index c51eb7b..7a4717a 100644 Binary files a/__pycache__/SafariChess_AI.cpython-39.pyc and b/__pycache__/SafariChess_AI.cpython-39.pyc differ diff --git a/__pycache__/SafariChess_Classes.cpython-39.pyc b/__pycache__/SafariChess_Classes.cpython-39.pyc index a59e445..f4a2724 100644 Binary files a/__pycache__/SafariChess_Classes.cpython-39.pyc and b/__pycache__/SafariChess_Classes.cpython-39.pyc differ diff --git a/__pycache__/SafariChess_backend.cpython-39.pyc b/__pycache__/SafariChess_backend.cpython-39.pyc index c22b96f..7ee8bb5 100644 Binary files a/__pycache__/SafariChess_backend.cpython-39.pyc and b/__pycache__/SafariChess_backend.cpython-39.pyc differ diff --git a/network/config.txt b/network/config.txt index d599145..b34e174 100644 --- a/network/config.txt +++ b/network/config.txt @@ -1,5 +1,5 @@ -SOCKET_HOST=192.168.43.183 +SOCKET_HOST=127.0.0.1 SOCKET_PORT=50005 MAX_WAITING_TIME=600 -MAX_THNIKING_TIME=10 +MAX_THNIKING_TIME=400 MAX_TOTAL_TIME=1000