def evaluate_moves_by_mlp(self, moves, my_fields, other_fields): max_move_index = 0 max_move_value = 0 for idx, move_die in enumerate(moves): die, move = move_die my_fields_after = copy.copy(my_fields) other_fields_after = copy.copy(other_fields) try: Game.apply_move(my_fields_after, other_fields_after, move) inputs = Board.prepare_any_inputs(my_fields_after, other_fields_after) outputs = self.mlp.run_input(inputs) if outputs[0] > max_move_value: max_move_value = max_move_value max_move_index = idx except Exception as e: continue return moves[max_move_index]
def move(self, dice): self.last_my_fields_before_move = copy.copy(self.my_fields) self.last_other_fields_before_move = copy.copy(self.other_fields) if dice[0] == dice[1]: final_moves = [] my_fields_after = copy.copy(self.my_fields) other_fields_after = copy.copy(self.other_fields) for _ in range(4): moves = self.generate_possible_moves(dice[0], my_fields_after, other_fields_after) if len(moves) == 0: return final_moves #print("Possible moves:\n{}".format(moves)) used_die, best_move = self.evaluate_moves_by_mlp( moves, my_fields_after, other_fields_after) #print("Best move: {}, {}".format(best_move, used_die)) final_moves.append(best_move) try: Game.apply_move(my_fields_after, other_fields_after, best_move) except Exception as e: #print("dice {}, best_move {}".format(dice, best_move)) self.print_intermediate_board(my_fields_after, other_fields_after) raise e #self.print_intermediate_board(my_fields_after, other_fields_after) return final_moves else: final_moves = [] moves = [] moves.extend( self.generate_possible_moves(dice[0], self.my_fields, self.other_fields)) moves.extend( self.generate_possible_moves(dice[1], self.my_fields, self.other_fields)) #print("Possible moves:\n{}".format(moves)) if len(moves) == 0: return final_moves used_die, best_move = self.evaluate_moves_by_mlp( moves, self.my_fields, self.other_fields) #print("Best move: {}, {}".format(best_move, used_die)) final_moves.append(best_move) my_fields_after = copy.copy(self.my_fields) other_fields_after = copy.copy(self.other_fields) Game.apply_move(my_fields_after, other_fields_after, best_move) #self.print_intermediate_board(my_fields_after, other_fields_after) other_die = dice[0] if dice[0] == used_die: other_die = dice[1] moves = self.generate_possible_moves(other_die, my_fields_after, other_fields_after) #print("Possible moves:\n{}".format(moves)) if len(moves) > 0: used_die, best_move = self.evaluate_moves_by_mlp( moves, my_fields_after, other_fields_after) #print("Best move: {}, {}".format(best_move, used_die)) final_moves.append(best_move) return final_moves