def _update_prediction_trees(self): i = 0 for prediction_tree in self.prediction_trees: car = self.playfield.goldcars[i] if car.pos is not None: car_node = ai.AiNode_create( GoldcarNodeState(car), TrailNode(car.pos.tile, car.pos.get_in_direction())) car_node.set_playfield(self.playfield) car_node.set_other_trees(self._get_other_prediction_trees(car)) if prediction_tree.root_node is None: prediction_tree.set_root(ai.Node(car_node)) elif prediction_tree.root_node.smartnode.nequals(car_node): prediction_tree.set_root(ai.Node(car_node)) prediction_tree.root_node.smartnode.set_playfield( self.playfield) prediction_tree.root_node.smartnode.carstate = GoldcarNodeState( car) prediction_tree.update() ## if prediction_tree.root_node is not None: ## print prediction_tree.root_node._best_score, i += 1
def bot_take_turn(level): global SIDE print("对手思考中……") root = ai.Node(MAIN_BOARD.board, SIDE, 0) tree = ai.MinimaxTreeSearch(root) strategy = tree.next_move(level) MAIN_BOARD.move(strategy[0], strategy[1], confirm=True) SIDE = switch_side(SIDE)
def main(): with open(sys.argv[1]) as file: data = json.load(file) # TODO: find and print winning action sequence print_utils.print_board(make_board_dict(data)) # make the initial state and the initial node init_state = ai.State(make_state_dict(data, WHITE_PIECE), make_state_dict(data, BLACK_PIECE)) init_node = ai.Node(init_state) # print the winning move sequence print(ai.get_winning_sequence(init_node))
def main(): OUTPUT_FILE = "out/{}x{}_{}gems.csv".format(WIDTH, HEIGHT, len(GEMS)) AGENTS = ORIG_AGENTS.copy() AGENT = AGENTS[0] AGENTS.pop(0) BOARD = [[random.choice(GEMS) for c in range(WIDTH)] for r in range(HEIGHT)] SCORE = MOVES = GEMS_CLEARED = CASCADES = 0 # clear matches while get_matches(BOARD) != []: for match in get_matches(BOARD): for gem in match: BOARD[gem[1]][gem[0]] = random.choice(GEMS) # game loop while True: if PRINT_BOARDS: print("\nscore: ", SCORE) print_board(BOARD) if not get_valid_swaps(BOARD): print("GAME OVER") print_board(BOARD) print("agent: ", AGENT) print("final score: ", SCORE) print("moves: ", MOVES) print("gems cleared: ", GEMS_CLEARED) print("cascades: ", CASCADES) gpm = float(GEMS_CLEARED) / float(MOVES) print("avg gems/move: ", gpm) spm = float(SCORE) / float(MOVES) print("avg score/move: ", spm) if OUTPUT_TO_FILE: with open(OUTPUT_FILE, mode='a') as f: f.write("{},{},{},{},{},{},{}\n".format( AGENT, SCORE, MOVES, GEMS_CLEARED, CASCADES, gpm, spm)) if not AGENTS: exit() else: AGENT = AGENTS[0] AGENTS.pop(0) BOARD = [[random.choice(GEMS) for c in range(WIDTH)] for r in range(HEIGHT)] SCORE = MOVES = GEMS_CLEARED = CASCADES = 0 if USER_INPUT: print("enter pairs to swap: x1 y1 x2 y2") coords = [int(val) for val in input().split(" ")] else: current_node = ai.Node(BOARD, SCORE, 0) coords = current_node.get_next_swap(AGENT) MOVES += 1 # make swap on prospective next board next_board = copy.deepcopy(BOARD) swap_gems(next_board, coords[0], coords[1], coords[2], coords[3]) # check that match is valid matches = get_matches(next_board) if matches == []: continue BOARD = next_board # track if we are cascading settled_once = False # clear matches and increment score until settled while get_matches(BOARD) != []: if settled_once: CASCADES += 1 for match in get_matches(BOARD): for gem in match: SCORE += (10 + (len(match) - 3) * 10) BOARD[gem[1]][gem[0]] = " " GEMS_CLEARED += 1 settled_once = True drop_and_fill(BOARD)
def build_game_tree(self, depth=-1): node = ai.Node()