def is_possible_move(board: Board, current: Point, new: Point): x = current.get_x() y = current.get_y() if board.is_barrier_at(x, y): return False nx = new.get_x() ny = new.get_y() if (abs(nx - x) + abs(ny - y)) > 1: return False if board.is_barrier_at(nx, ny): return False if (ny - y == -1) and (not board.has_ladder_at(x, y)): return False yd = y + 1 if ( (ny - y != 1) and (not board.is_barrier_at(x, yd)) and (not board.has_ladder_at(x, yd)) and (not board.has_ladder_at(x, y)) and (not board.has_pipe_at(x, y)) ): return False return True
def eyes_possible(gcb: Board): global cost_map TIME = time() def _print_cost_map(): global cost_map print_map = {} for point in cost_map: print_map[point] = int(cost_map[point]) result = "" for y in range(gcb.get_size()[0]): for x in range(gcb.get_size()[1]): try: result += number_into_char[print_map[(x, y)] % len(number_into_char)] except: if (y == 0 or y == gcb.get_size()[0]-1 or x == 0 or x == gcb.get_size()[1]-1): result += '*' else: result += ' ' result += '\n' print(result) cost_map = {} # elem = cost hero = (gcb.get_my_position().get_x(), gcb.get_my_position().get_y()) cost_map[hero] = 59 for point in gcb.get_gold_positions(): cost_map[(point.get_x(), point.get_y())] = 1 check = {} check[0] = check_moves(gcb, gcb.get_my_position()) visited = set() visited.add(gcb.get_my_position()) for iter in range(0, 300): check[iter+1] = [] for point in check[iter]: for new_point in check_moves(gcb, point): if not (new_point in visited): check[iter+1].append(new_point) visited.add(new_point) for point in check[iter]: cost_map[(point.get_x(), point.get_y())] = 0 for point in gcb.get_gold_positions(): if (cost_map[(point.get_x(), point.get_y())] == 0): cost_map[(point.get_x(), point.get_y())] = 9 else: cost_map[(point.get_x(), point.get_y())] = 1 _print_cost_map() print(time() - TIME) return LoderunnerAction.DO_NOTHING
def on_message(self, ws, message): board = Board(message.lstrip("board=")) board.print_board() action = self.on_turn(board) self.__send(action.value)
def turn(gcb: Board): if gcb.has_ladder_at(gcb.get_my_position().get_x(), gcb.get_my_position().get_y()): action_id = random.randint(0, len(LoderunnerAction)-2) else: action_id = random.randint(0, len(LoderunnerAction) - 7) return list(LoderunnerAction)[action_id]
def optimal_bfs(gcb: Board): global x_max, y_max, map, cost_map, task_list, hero y_max = 0 x_max = 0 def parse_map(): global x_max, y_max, map, cost_map map_copy = gcb._line_by_line().split('\n') map = {} x_max = len(map_copy[0]) y_max = len(map_copy[1]) for y in range(y_max): for x in range(x_max): map[(x, y)] = map_copy[y][x] def print_cost_map(): global x_max, y_max, map, cost_map print_map = {} for point in cost_map: print_map[point] = int(10.0 * (cost_map[point][0] - 1) / cost_map[point][1] + 0.5) result = "" for y in range(y_max): for x in range(x_max): try: result += number_into_char[print_map[(x, y)] % len(number_into_char)] except: if (y == 0 or y == y_max - 1 or x == 0 or x == x_max - 1): result += '*' else: result += ' ' result += '\n' print(result) def calc_next_ceil(): global map, cost_map, task_list point = task_list.get() point = list(point) if (map[(point[0], point[1])] in {' ', '◄', '►'} and map[(point[0], point[1] + 1)] in {'H', '#', '☼', ')', '(', 'U', 'Э', 'Є'}): if (map[(point[0] - 1, point[1])] in {' ', 'H', '~', '$', '&', '@'} and not ((point[0] - 1, point[1]) in cost_map.keys())): new_cost = list(cost_map[(point[0], point[1])]) new_cost[1] += 1 if (map[(point[0] - 1, point[1])] == '$'): new_cost[0] = 1 if (map[(point[0] - 1, point[1])] == '&'): new_cost[0] = 5 if (map[(point[0] - 1, point[1])] == '@'): new_cost[0] = 10 cost_map[(point[0] - 1, point[1])] = new_cost task_list.put((point[0] - 1, point[1])) if (map[(point[0] + 1, point[1])] in {' ', 'H', '~', '$', '&', '@'} and not ((point[0] + 1, point[1]) in cost_map.keys())): new_cost = list(cost_map[(point[0], point[1])]) new_cost[1] += 1 if (map[(point[0] + 1, point[1])] == '$'): new_cost[0] = 1 if (map[(point[0] + 1, point[1])] == '&'): new_cost[0] = 5 if (map[(point[0] + 1, point[1])] == '@'): new_cost[0] = 10 cost_map[(point[0] + 1, point[1])] = new_cost task_list.put((point[0] + 1, point[1])) if (map[(point[0], point[1])] in {' ', ']', '['} and map[(point[0], point[1] + 1)] in {' ', '$', '&', '@'}): if (not ((point[0], point[1] + 1) in cost_map.keys())): new_cost = list(cost_map[(point[0], point[1])]) new_cost[1] += 1 if (map[(point[0], point[1] + 1)] == '$'): new_cost[0] = 1 if (map[(point[0], point[1] + 1)] == '&'): new_cost[0] = 5 if (map[(point[0], point[1] + 1)] == '@'): new_cost[0] = 10 cost_map[(point[0], point[1] + 1)] = new_cost task_list.put((point[0], point[1] + 1)) if (map[(point[0], point[1])] in {'Y', 'H'}): if (map[(point[0], point[1] + 1)] in {' ', 'Y', 'H', '~', '$', '&', '@'} and not ((point[0], point[1] + 1) in cost_map.keys())): new_cost = list(cost_map[(point[0], point[1])]) new_cost[1] += 1 if (map[(point[0], point[1] + 1)] == '$'): new_cost[0] = 1 if (map[(point[0], point[1] + 1)] == '&'): new_cost[0] = 5 if (map[(point[0], point[1] + 1)] == '@'): new_cost[0] = 10 cost_map[(point[0], point[1] + 1)] = new_cost task_list.put((point[0], point[1] + 1)) if (map[(point[0], point[1] - 1)] in {' ', 'Y', 'H', '~', '$', '&', '@'} and not ((point[0], point[1] - 1) in cost_map.keys())): new_cost = list(cost_map[(point[0], point[1])]) new_cost[1] += 1 if (map[(point[0], point[1] - 1)] == '$'): new_cost[0] = 1 if (map[(point[0], point[1] - 1)] == '&'): new_cost[0] = 5 if (map[(point[0], point[1] - 1)] == '@'): new_cost[0] = 10 cost_map[(point[0], point[1] - 1)] = new_cost task_list.put((point[0], point[1] - 1)) task_list = queue.Queue() parse_map() cost_map = {} # elem = [cost, time] hero = (gcb.get_my_position().get_x(), gcb.get_my_position().get_y()) cost_map[hero] = [1, 1] task_list.put(hero) while (not task_list.empty()): calc_next_ceil() print_cost_map() return list(LoderunnerAction)[6]