def searchOneNode(self, xy): # 演绎 sg_ = Snaker_game(self.sg.map, self.sg.body, self.sg.food) path_str = self.currentNode.path_str + self.get_n_by_xy( self.currentNode.xy, xy) sg_.run_list(path_str) sg_c = judgment(sg_.map) # 如果不在openList中,就加入openlist if self.nodeInOpenlist(xy) == False: node = Node(xy, self.currentNode) # 添加路径 node.path_str = path_str # H值计算 node.set_ghc(self.endNode, sg_c) self.openList.append(node) # 如果在openList中,判断currentNode到当前点的G是否更小 # 如果更小,就重新计算g值,并且改变father else: nodeTmp = self.getNodeFromOpenList(xy) if sg_c < nodeTmp.c: nodeTmp.father = self.currentNode nodeTmp.set_ghc(self.endNode, sg_c) nodeTmp.path_str = path_str elif sg_c == nodeTmp.c and self.currentNode.g + 1 < nodeTmp.g: nodeTmp.father = self.currentNode nodeTmp.set_ghc(self.endNode, sg_c) nodeTmp.path_str = path_str return
def searchOneNode(self, xy): # 如果不在openList中,就加入openlist if self.nodeInOpenlist(xy) == False: node = Node(xy, self.currentNode) # 添加路径 node.path_str = node.father.path_str + self.get_n_by_xy( node.father.xy, node.xy) sg_ = Snaker_game(self.sg.map, self.sg.body, self.sg.food) sg_.run_list(node.path_str) # H值计算 node.set_ghc(self.endNode, judgment(sg_.map)) self.openList.append(node) # 如果在openList中,判断currentNode到当前点的G是否更小 # 如果更小,就重新计算g值,并且改变father else: nodeTmp = self.getNodeFromOpenList(xy) sg_ = Snaker_game(self.sg.map, self.sg.body, self.sg.food) sg_.run_list(self.currentNode.path_str + self.get_n_by_xy(self.currentNode.xy, xy)) if self.currentNode.g + 1 < nodeTmp.g: nodeTmp.g = self.currentNode.g + 1 nodeTmp.father = self.currentNode # 添加路径 nodeTmp.path_str = nodeTmp.father.path_str + self.get_n_by_xy( nodeTmp.father.xy, nodeTmp.xy) elif judgment(sg_.map) < nodeTmp.c: nodeTmp.g = self.currentNode.g + 1 nodeTmp.father = self.currentNode # 添加路径 nodeTmp.path_str = nodeTmp.father.path_str + self.get_n_by_xy( nodeTmp.father.xy, nodeTmp.xy) return
def mimang(self): x, y = self.game.body[-1] f_x, f_y = self.game.food ok_list = [-1, 'W', 99999999] for n in "WASD": sg = Snaker_game(self.game.map, self.game.body, self.game.food) sg.run_list(n) dx, dy = self.get_dxy(n) if self.game.map[x + dx][y + dy] in [0, 2]: jm = judgment(sg.map) if jm < ok_list[2]: ok_list = [abs(x + dx - f_x) + abs(y + dy - f_y), n, jm] elif jm == ok_list[2] and abs(x + dx - f_x) + abs( y + dy - f_y) > ok_list[0]: ok_list = [abs(x + dx - f_x) + abs(y + dy - f_y), n, jm] return ok_list[1]
def __init__(self, game): self.game = game self.state = '' # 觅食范围,即食物在这个范围才去吃 # 随着蛇身越来越长,我们将觅食距离也缩短以保证蛇的安全 self.food_r = game_w_n + game_h_n - 4 # 与game同步 self.synchro() self.snaker_game = Snaker_game(self.game.map, self.game.body, self.game.food)
def zhuiwei(self): x, y = self.game.body[-1] w_x, w_y = self.game.body[0] ok_list = [] for n in "WASD": dx, dy = self.get_dxy(n) if self.game.map[x + dx][y + dy] in [0, 2]: # 安全 sg = Snaker_game(self.game.map, self.game.body, self.game.food) sg.run(n) fen = judgment(sg.map) t_x, t_y = sg.body[-1] w_x, w_y = sg.body[0] wei = abs(w_x - t_x) + abs(w_y - t_y) ok_list.append([-wei, fen, n]) elif (x + dx, y + dy) == self.game.body[0]: fen = judgment(self.game.map) wei = abs(w_x - x) + abs(w_y - y) ok_list.append([-wei, fen, n]) ok_list = sorted(ok_list, key=(lambda x: [x[1] + x[0]])) print('追尾排序:', ok_list) return ok_list
def searchNear(self): x, y = self.currentNode.xy if not self.is_static: # 更新map sg_ = Snaker_game(self.sg.map, self.sg.body, self.sg.food) sg_.run_list(self.currentNode.path_str) self.map2d = sg_.map for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]: # 忽略封闭列表 if self.nodeInCloselist((x + dx, y + dy)): continue if self.map2d[x + dx][y + dy] in [0, 2]: self.searchOneNode((x + dx, y + dy)) elif sg_.body[0] == (x + dx, y + dy): # print('前方是尾巴{},可以走'.format(sg_.body[0])) self.searchOneNode((x + dx, y + dy)) else: for dx, dy in [(-1, 0), (0, -1), (1, 0), (0, 1)]: # 忽略封闭列表 if self.nodeInCloselist((x + dx, y + dy)): continue if self.map2d[x + dx][y + dy] in [0, 2]: self.searchOneNode((x + dx, y + dy))
def get_next(self): x, y = self.game.body[-1] f_x, f_y = self.game.food if len(self.game.body) <= max(game_w_n, game_h_n) - 1: # 蛇身很短,用贪心算法 return self.tanxin() else: sg = Snaker_game(self.game.map, self.game.body, self.game.food) to_food = self.is_connect((x, y), self.game.food, sg) if to_food != False: # 食物可到达 # 判断吃食物后能否追尾 sg = Snaker_game(self.game.map, self.game.body, self.game.food) temp_xy = sg.body[0] start_xy = sg.body[-1] sg.run_list(to_food) to_tail = self.is_connect(sg.body[-1], sg.body[0], sg, char=to_food[-1]) if to_tail != False: # print('=================================') # print('食物可到达,且能追尾,吃食前头坐标{},食坐标{}'.format(start_xy,self.game.food)) # print('吃食后食物坐标{},吃食后地图:(路径:{})'.format(sg.food,to_food)) # print(sg.map.T) # 能追尾 self.game.strategy = '觅食,方向{},追尾路径{},吃食前尾巴坐标{},吃食后尾巴坐标{}'.format( to_food, to_tail, temp_xy, sg.body[0]) return to_food else: # 吃食物很危险,开始追尾 zw_list = self.zhuiwei() if len(zw_list) == 1: return zw_list[0][2] for i in zw_list: sg = Snaker_game(self.game.map, self.game.body, self.game.food) sg.run(i[2]) to_tail = self.is_connect(sg.body[-1], sg.body[0], sg, char=i[2]) if to_tail != False: self.game.strategy = '追尾1.0,方向{},尾巴坐标{}'.format( i[2] + to_tail, sg.body[0]) return i[2] sg = Snaker_game(self.game.map, self.game.body, self.game.food) to_tail = self.is_connect((x, y), sg.body[0], sg) if to_tail != False: self.game.strategy = '追尾1.1,方向{},尾巴坐标{}'.format( to_tail, sg.body[0]) return to_tail[:1] else: # 陷入困境 # return self.kunjin() self.game.strategy = '迷茫' return self.mimang() else: # 食物不可达,准备追尾 sg = Snaker_game(self.game.map, self.game.body, self.game.food) to_tail = self.is_connect((x, y), sg.body[0], sg) if to_tail != False: zw_list = self.zhuiwei() if len(zw_list) == 1: return zw_list[0][2] for i in zw_list: sg = Snaker_game(self.game.map, self.game.body, self.game.food) sg.run(i[2]) to_tail = self.is_connect(sg.body[-1], sg.body[0], sg, char=i[2]) if to_tail != False: self.game.strategy = '追尾2.0,方向{},尾巴坐标{}'.format( i[2] + to_tail, sg.body[0]) return i[2] self.game.strategy = '追尾2.1,方向{},尾巴坐标{}'.format( to_tail, sg.body[0]) return to_tail[:1] else: # 陷入困境 self.game.strategy = '困境' return self.kunjin()