コード例 #1
0
 def sorted_moves(self, state, agent, heuristic_type):
     if agent is True:
         moves = get_legal_moves(state.board, state.my_location)
     else:
         moves = get_legal_moves(state.board, state.rival_location)
     # Change here?
     moves.sort(key=(lambda move: self.get_heuristic_for_move(state, move, agent, heuristic_type)))
     return moves
コード例 #2
0
 def find_longest_route(self, state, isMe):
     max = 0
     if isMe == True:
         for d in get_legal_moves(state.board, state.my_location):
             temp = self.find_longest_route_aux(state.board, state.my_location, d, 0)
             if temp > max:
                 max = temp
     else:
         for d in get_legal_moves(state.board, state.rival_location):
             temp = self.find_longest_route_aux(state.board, state.rival_location, d, 0)
             if temp > max:
                 max = temp
     return max
コード例 #3
0
 def find_longest_route_aux(self, board, curr_pos, new_pos, depth):
     temp_board = board.copy()
     temp_board[curr_pos[0]][curr_pos[1]] = -1
     if depth == 5:
         return 0
     max = 0
     if not self.can_I_move(temp_board, new_pos):
         return 0
     for d in get_legal_moves(board, new_pos):
         temp = 1 + self.find_longest_route_aux(temp_board, new_pos, d, depth + 1)
         if temp > max:
             max = temp
     return max
コード例 #4
0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        start_time = time.time()
        minimax_ret = 0
        iteration_time = 0
        depth = 1
        state = State(self.board, self.penalty_score, players_score[0], players_score[1], self.cur_fruits, self.turn)
        succ = self.get_legal_moves
        utility = self.calc_score
        preform_move = self.preform_move

        if players_score[0] - players_score[1] > self.penalty_score: #If it is worthy to end the game
            # print("Yessss, ", players_score[0], " ", players_score[1], " ", self.penalty_score)
            while time.time() - start_time < time_limit + 8:# We want to get to fine, end the game and win
                # minimax_ret = MiniMax(succ=succ,utility=utility, perform_move= preform_move).search(state=state, depth=depth, maximizing_player=True)
                minimax_ret = self.get_legal_moves(state.board, state.my_location)[0]
                minimax_ret = (0, self.calc_direction(state.my_location, minimax_ret))

            new_pos = (state.my_location[0] + minimax_ret[1][0], state.my_location[1] + minimax_ret[1][1])
            self.board[state.my_location[0]][state.my_location[1]] = -1
            self.board[new_pos[0]][new_pos[1]] = 1
            self.turn += 1
            return minimax_ret[1]

        #TODO: check if correct upperbound

        while 4 * iteration_time < time_limit - (time.time() - start_time) and time.time() - start_time < time_limit:  #total time = iter_time + 3*iter_time (the upper bound of the running time)
            moves = get_legal_moves(state.board, state.my_location)
            minimax_ret = [1, 2]
            if len(moves) == 1:
                minimax_ret[0] = None
                minimax_ret[1] = calc_direction(state.my_location, moves[0])
                break

            start_iteration = time.time()
            minimax_ret = MiniMax(succ=succ,utility=utility, perform_move= preform_move).search(state=state, depth=depth, maximizing_player=True)
            #print('depth        ', depth)
            iteration_time = time.time() - start_iteration
            depth += 1
        
        new_pos = (state.my_location[0] + minimax_ret[1][0], state.my_location[1] + minimax_ret[1][1])
        self.board[state.my_location[0]][state.my_location[1]] = -1
        self.board[new_pos[0]][new_pos[1]] = 1
        self.turn += 1
        return minimax_ret[1]
コード例 #5
0
 def just_get_any_legal_location(self, state: State):
     loc = get_legal_moves(state.board, state.my_location)[0]
     return calc_direction(state.my_location, loc)
コード例 #6
0
    def make_move(self, time_limit, players_score):
        """Make move with this Player.
        input:
            - time_limit: float, time limit for a single turn.
        output:
            - direction: tuple, specifing the Player's movement, chosen from self.directions
        """
        #TODO: erase the following line and implement this function.
        have_same_time = min(len(self.board), len(self.board[0])) / 2
        time_per_turn = self.time_tmp * 2 // (3 * have_same_time)
        print("Turn ", self.turn, " time: ", time_per_turn)
        time_per_turn = min(time_per_turn, time_limit)
        start_time = time.time()
        minimax_ret = 0
        iteration_time = 0
        depth = 1

        state = State(self.board, self.penalty_score, players_score[0],
                      players_score[1], self.cur_fruits, self.turn)

        if players_score[0] - players_score[
                1] > self.penalty_score:  # If it is worthy to end the game
            # print("Yessss, ", players_score[0], " ", players_score[1], " ", self.penalty_score)
            #print("AAAA")
            while time.time(
            ) - start_time < time_limit + 8:  # We want to get to fine, end the game and win
                minimax_ret = self.ab.get_legal_moves(state.board,
                                                      state.my_location)[0]
                minimax_ret = (0,
                               self.ab.calc_direction(state.my_location,
                                                      minimax_ret))

            new_pos = (state.my_location[0] + minimax_ret[1][0],
                       state.my_location[1] + minimax_ret[1][1])
            self.board[state.my_location[0]][state.my_location[1]] = -1
            self.board[new_pos[0]][new_pos[1]] = 1
            self.turn += 1
            return minimax_ret[1]

        while 4 * iteration_time < time_limit - (time.time(
        ) - start_time) and time.time(
        ) - start_time < time_per_turn:  # total time = iter_time + 3*iter_time (the upper bound of the running time)
            moves = get_legal_moves(state.board, state.my_location)
            minimax_ret = [1, 2]
            if len(moves) == 1:
                minimax_ret[0] = None
                minimax_ret[1] = calc_direction(state.my_location, moves[0])
                break

            start_iteration = time.time()
            minimax_ret = self.player.search(state=state,
                                             depth=depth,
                                             maximizing_player=True)
            iteration_time = time.time() - start_iteration
            depth += 1

        new_pos = (state.my_location[0] + minimax_ret[1][0],
                   state.my_location[1] + minimax_ret[1][1])
        self.board[state.my_location[0]][state.my_location[1]] = -1
        self.board[new_pos[0]][new_pos[1]] = 1
        self.turn += 1
        time_passed = time.time() - start_time
        self.time_left -= time_passed
        if (1 + self.turn) % have_same_time == 0:
            self.time_tmp = self.time_left
        return minimax_ret[1]