Example #1
0
 def play(self, board, player, step, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     #Launch
     if (step == 1 or step == 2):
         self.gametime = time_left
     if (step == 1):
         # Hard codage de la 1�re action pour �viter une perte de temps
         return (3, 8, 4, 7)
     #Init minmax
     newBoard = avalam.Board(board.get_percepts(player == avalam.PLAYER2))
     state = (newBoard, player, step)
     self.maxMinMaxDepth = calculate_maxMinMaxDepth(
         step, time_left, self.gametime, newBoard.estimate_depth_safety())
     self.maxTimeForMinMax = datetime.datetime.now() + datetime.timedelta(
         0, getTimeSituation(step, time_left, self.gametime))
     #Debug
     print("Depth :", self.maxMinMaxDepth)
     print("Time :", getTimeSituation(step, time_left, self.gametime))
     print("Time left : ", time_left)
     #Minmax
     minmaxRes = minimax.search(state, self, time_left)
     print(minmaxRes)
     if minmaxRes == None:
         self.maxMinMaxDepth = 2
         minmaxRes = minimax.search(state, self, time_left)
     return minmaxRes
 def play(self, board, player, step, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     #Launch
     if (step == 1 or step == 2):
         self.gametime = time_left
     if (step == 1):
         # Hard codage de la 1�re action pour �viter une perte de temps
         return (3, 8, 4, 7)
     #Init minmax
     newBoard = avalam.Board(board.get_percepts(player==avalam.PLAYER2))
     state = (newBoard, player, step)
     self.maxMinMaxDepth = calculate_maxMinMaxDepth(step, time_left, self.gametime, newBoard.estimate_depth_safety())
     self.maxTimeForMinMax = datetime.datetime.now() + datetime.timedelta(0, getTimeSituation(step, time_left, self.gametime))
     #Debug
     print("Depth :", self.maxMinMaxDepth)
     print("Time :", getTimeSituation(step, time_left, self.gametime))
     print("Time left : ", time_left)
     #Minmax
     minmaxRes = minimax.search(state, self, time_left)
     print(minmaxRes)
     if minmaxRes == None:
         self.maxMinMaxDepth = 2
         minmaxRes = minimax.search(state, self, time_left)
     return minmaxRes
Example #3
0
    def play(self, percepts, step, time_left):
        
        ###BENCH

        #return self.eval_speed(percepts,step,time_left);

        if step % 2 == 0:
            player = -1 # Red
        else:
            player = 1  #Yellow
        # Game is new reset vars 
        if step == 1 or step == 2:
            self.step            = 0
            self.max_depth       = 4
            self.start_time      = 0
            self.time_slot       = 0
            self.time_left       = 0

        state = (Board(percepts), player)
        self.player = player
        self.step = step
        self.time_left = time_left
        self.start_time = time.time()
        i = 15 - step #15 because we rarely have more than 15 steps to compute for a player
        if i <=0:
            i = 1
        self.time_slot = math.floor(time_left / (i))  # Time_left / Number of steps left (approx.)
        return minimax.search(state, self, True)
Example #4
0
File: player.py Project: bend/AI
 def play(self, percepts, step, time_left):
     if step % 2 == 0:
         player = -1
     else:
         player = 1
     state = (Board(percepts), player)
     return minimax.search(state, self)
    def get_action(self, state, last_action, time_left):
        self.last_action = last_action
        self.time_left = time_left
        self.max_time = time_left
        self.current_depth = 0
        self.start_time = time()
        if self.total_time == 0:
            self.total_time = time_left
        self.max_time = time_left
        best_move = 1
        # print(time_left)

        # Iterative deepening
        while time(
        ) - self.start_time < self.max_time and self.current_depth < self.max_depth:
            #print(time() - self.start_time)
            best_move = minimax.search(state, self)
            self.current_depth += 1
        #print("Finish")
        #print(self.current_depth)
        #print("Time elapsed during smart agent play:", time() - self.start_time)

        l1 = []
        l2 = []
        for pawn in [0, 1, 2, 3, 4]:
            l1.append(state.get_pawn_advancement(self.id, pawn))
            l2.append(state.get_pawn_advancement(1 - self.id, pawn))
        print('{} {} {}'.format(l1, l2, best_move))

        return best_move
Example #6
0
 def get_action(self, state, last_action, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     return minimax.search(state, self)
    def get_action(self, state, last_action, time_left):
        self.last_action = last_action
        self.time_left = time_left
        self.current_depth = 0
        self.start_time = time()
        if self.total_time == 0:
            self.total_time = time_left
        if time_left / self.total_time > 0.2:
            self.max_time = 0.03 * self.total_time
        else:
            self.max_time = 0.03 * self.total_time * (
                time_left / (0.2 * self.total_time))**2
        best_move = 1
        # print(time_left)

        # Iterative deepening
        while time(
        ) - self.start_time < self.max_time and self.current_depth < self.max_depth:
            #print(time() - self.start_time)
            best_move = minimax.search(state, self)
            self.current_depth += 1
        #print("Finish")
        #print(self.current_depth)
        #print("Time elapsed during smart agent play:", time() - self.start_time)

        return best_move
Example #8
0
File: player.py Project: bend/AI
 def play(self, percepts, step, time_left):
     if step % 2 == 0:
         player = -1
     else:
         player = 1
     state = (Board(percepts), player)
     return minimax.search(state, self)
Example #9
0
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        """if step == 1 :
            self.passed=True
            return (3,3,4,3)
        if self.passed==False and step==2:
            return (4,3,3,3)"""

        self.player = player
        self.time_left = time_left
        newBoard = avalam.Board(board.get_percepts(player == avalam.PLAYER2))
        state = (newBoard, player, step)
        start_time = time.time()
        result = minimax.search(state, self)
        # print('towerDifferentColourFilter =', self.towerDifferentColourFilter(player,board,action))
        interval = time.time() - start_time
        self.totalTime += interval

        print("Decision Time:", interval)
        print("Total time:", self.totalTime)
        return result
Example #10
0
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        """if step == 1 :
            self.passed=True
            return (3,3,4,3)
        if self.passed==False and step==2:
            return (4,3,3,3)"""
        start_time = time.time()
        self.player = player
        self.time_left = time_left
        newBoard = avalam.Board(board.get_percepts(player == avalam.PLAYER2))
        state = (newBoard, player, step)
        result = minimax.search(state, self)
        #print('towerDifferentColourFilter =', self.towerDifferentColourFilter(player,board,action))

        interval = time.time() - start_time
        self.totalTime += interval

        print('Decision Time:', interval)
        print('Total time:', self.totalTime)
        return result
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        # Rebuild state with board possibly inverted depending on whose turn it is.
        self.we_play_as_player_2 = ((step%2)==0)
        newBoard = avalam.Board(board.get_percepts(player==avalam.PLAYER2))
        state = (newBoard, player, step)

        # If this is step1. We play from our hardcoded opening move.
        if step==1:
            return (4, 8, 3, 8)

        # Reset time control variables.
        allocated_time = self.allocate_time_for_move(state, time_left, step)
        self.time_limit_expired = False
        self.it_dep_time_limit = time.clock()+allocated_time
        prev_prev_depth_duration = 0
        prev_depth_duration = 0

        # Reset caches.
        self.minimax_values_cache = MinimaxValuesCache()
        self.state_evaluations_cache = {}

        # Perform iterative deepening
        self.performing_it_dep = True
        self.current_depth_limit = 0
        self.should_stop_iterative_deepening = False
        best_action = None
        previous_best_action = None

        while not self.time_limit_expired and \
              not self.should_stop_iterative_deepening and \
              (self.it_dep_time_limit - time.clock()) > self.time_prediction_for_step(step,prev_prev_depth_duration,prev_depth_duration):
            # We stop if none of the branches was stopped due to iterative
            # depth limit. It means they were all stopped because of
            # reaching a terminal state.

            self.should_stop_iterative_deepening = True
            self.current_depth_limit+=1
            previous_best_action = best_action

            prev_prev_depth_duration = prev_depth_duration
            start_time = time.clock()
            best_action = minimax.search(state, self)
            prev_depth_duration = time.clock()-start_time

        # The last move might come from a time cutted-search at depth N. In this
        # case, depending on when the search was time-cutted, the last move
        # obtained from depth N could actually be worse than the previous move
        # obtained after entirely completing the N-1 depth. So we always return
        # the previous best move if the search was time-cutted.
        if self.time_limit_expired:
            best_action =  previous_best_action

        self.minimax_values_cache = None
        return best_action
Example #12
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     state = (dict_to_board(percepts), player)
     return minimax.search(state, self)
Example #13
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     state = (dict_to_board(percepts), player)
     return minimax.search(state, self)
Example #14
0
 def play(self, percepts, step, time_left):
     if step % 2 == 0:
         player = -1
     else:
         player = 1
     state = (Board(percepts), player)
     self.step = step
     self.time_left = time_left
     print(time_left)
     return minimax.search(state, self, True)
Example #15
0
 def play(self, board, player, step, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     self.time_left = time_left
     state = (board, player, step)
     return minimax.search(state, self)
Example #16
0
 def play(self, board, player, step, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.time_left = time_left
     newBoard = avalam.Board(board.get_percepts(player==avalam.PLAYER2))
     state = (newBoard, player, step)
     return minimax.search(state, self)
Example #17
0
 def play(self, board, player, step, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.time_left = time_left
     newBoard = avalam.Board(board.get_percepts(player == avalam.PLAYER2))
     state = (newBoard, player, step)
     return minimax.search(state, self)
Example #18
0
 def play(self, percepts, step, time_left):
     if step % 2 == 0:
         player = -1
     else:
         player = 1
     state = (Board(percepts), player)
     self.step = step
     self.time_left = time_left
     print(time_left)
     return minimax.search(state, self, True)
Example #19
0
 def get_action(self, state, last_action, time_left):
     self.last_action = last_action
     self.time_left = time_left
     print('good', time_left)
     if time_left > 200:
         self.depth = 3
     elif time_left > 20:
         self.depth = 2
     else:
         self.depth = 1
     return minimax.search(state, self)
Example #20
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     state = (dict_to_board(percepts), player)
     self.getCurrentDepth(state[0],state[1])
     value=minimax.search(state, self)
     print("play value : " , value)
     return value
Example #21
0
 def play(self, percepts, step, time_left):
     # We are always the yellow player
     board = Board(percepts)
     nodes_before = self.nodes
     t0 = time.clock()
     result = minimax.search(board, self)
     # result = minimax.search(board, self, prune=False)
     t1 = time.clock()
     print("Nodes visited:", self.nodes - nodes_before)
     print("Time required:", t1 - t0)
     print("Result: ", result)
     return result
Example #22
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     state = (dict_to_board(percepts), player)
     self.getCurrentDepth(state[0], state[1])
     value = minimax.search(state, self)
     print("play value : ", value)
     return value
Example #23
0
 def get_action(self, state, last_action, time_left):
     """This function is used to play a move according
     to the board, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     if state.turn in [0, 1]:
         if state.is_empty((0, 1)):
             return tuple(("place", (0, 1), 0))
         else:
             return tuple(("place", (0, 3), 0))
     self.last_action = last_action
     return minimax.search(state, self)
Example #24
0
    def get_action(self, state, last_action, time_left):
        self.last_action = last_action
        self.time_left = time_left
        self.count = 0
        self.threshold = 0

        # If we have enough time, we start the threshold at a 'high' value: 5
        if self.time_left > 450:  # Half the time
            self.threshold = 5

        number_of_moves_left = 0
        for i in range(5):
            if state.is_pawn_finished(self.id, i):
                continue
            if state.is_pawn_returning(self.id,
                                       i):  # Returing path for the pawn
                number_of_moves_left += ceil(
                    (12 - state.get_pawn_advancement(self.id, i)) /
                    squadro_state.MOVES_RETURN[self.id][i])
            else:
                number_of_moves_left += ceil(
                    (6 - state.get_pawn_advancement(self.id, i)) /
                    squadro_state.MOVES[self.id][i])
                number_of_moves_left += ceil(
                    6 / squadro_state.MOVES_RETURN[self.id][i])
        number_of_moves_left *= 3
        self.time_left_that_research = self.time_left / number_of_moves_left
        self.time_begin_research = time()
        for pawn in range(5):
            if state.is_pawn_finished(self.id, pawn):
                self.count += 1
            if state.is_pawn_finished(1 - self.id, pawn):
                self.count += 1

        tmp = 0
        best = None
        while time(
        ) - self.time_begin_research < self.time_left_that_research / 1.5:
            self.threshold += 1
            print('threshold', self.threshold)
            value, tmp, best = minimax.search(state, self)
            print('value', value)
            print('Future actions')
            cur = self.id
            for i in best:
                print(cur, i)
                cur = 1 - cur
            print('---')
        print('\n')
        return tmp
Example #25
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     board = dict_to_board(percepts)
     state = (board, player)
     if step < 7 and board.nb_walls[0]+board.nb_walls[1] == 20 :
         (x, y) = board.get_shortest_path(self.player)[0]
         return ('P', x, y)
     else :
         return minimax.search(state, self)
Example #26
0
    def play(self, percepts, step, time_left):
        if step % 2 == 0:
            player = -1
        else:
            player = 1
#        start = time.time()
        state = (Board(percepts), player)
        m = minimax.search(state, self)

#        print("nombre de noeuds explorés : ", self.count)
#        print("nombre de noeuds explorés par depth : ", self.depthCount)
#        self.count = 0
#        depthCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
#        print("temps elapsed : ", format(time.time()-start))
        return m
Example #27
0
    def get_action(self, state, last_action, time_left):
        self.last_action = last_action
        self.time_left = time_left  # in seconds
        interval = 2**(self.n + 1)
        if state.turn <= 2 * (state.size - 1):
            self.behaviour = True
        else:
            self.behaviour = False
            # update maxDepth
            if time_left <= self.total_time / interval:
                self.n += 1
                interval2 = 2**self.n
                self.maxDepth = max(self.maxDepth / interval2, self.minDepth)

        return minimax.search(state, self)
Example #28
0
    def play(self, percepts, step, time_left):
        if step % 2 == 0:
            player = -1
        else:
            player = 1
        start = time.time()
        state = (Board(percepts), player)
        m = minimax.search(state, self)

        #        print("nombre de noeuds explorés : ", self.count)
        #        print("nombre de noeuds explorés par depth : ", self.depthCount)
        #        self.count = 0
        #        depthCount = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        print("temps elapsed : ", format(time.time() - start))
        return m
Example #29
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     self.step = step
     start = time.time()
     state = (dict_to_board(percepts), player)
     end = time.time()
     print("dict: " + str(end - start))
     start = time.time()
     ret = minimax.search(state, self)
     end = time.time()
     print("minimax: " + str(end - start))
     return ret
Example #30
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     self.step = step
     start = time.time()
     state = (dict_to_board(percepts), player)
     end = time.time()
     print("dict: " + str(end - start))
     start = time.time()
     ret = minimax.search(state, self)
     end = time.time()
     print("minimax: " + str(end - start))
     return ret
Example #31
0
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        """if step == 1 :
            self.passed=True
            return (3,3,4,3)
        if self.passed==False and step==2:
            return (4,3,3,3)"""
        start_time = time.time() 
        self.player=player
        self.time_left = time_left
        newBoard = avalam.Board(board.get_percepts(player==avalam.PLAYER2))
        state = (newBoard, player, step)
        result=minimax.search(state,self)

        return result
Example #32
0
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        """if step == 1 :
            self.passed=True
            return (3,3,4,3)
        if self.passed==False and step==2:
            return (4,3,3,3)"""
        start_time = time.time()
        self.player = player
        self.time_left = time_left
        newBoard = avalam.Board(board.get_percepts(player == avalam.PLAYER2))
        state = (newBoard, player, step)
        result = minimax.search(state, self)

        return result
Example #33
0
    def play(self, board, player, step, time_left):
        """This function is used to play a move according
        to the board, player and time left provided as input.
        It must return an action representing the move the player
        will perform.
        """
        import minimax
        self.player = player

        print("time_left: {}".format(time_left))

        if self.previous_time:
            self.previous_time = self.time_left
        else:
            self.previous_time = time_left
        self.time_left = time_left
        state = (board, player, step)

        ret = minimax.search(state, self)
        # print("ret: {}".format(ret))
        return ret
Example #34
0
    def get_action(self, state, last_action, time_left):
        self.last_action = last_action
        self.time_left = time_left
        self.count = 0
        self.threshold = 0

        # If we have enough time, we start the threshold at a 'high' value: 5
        if self.time_left > 450:  # Half the time
            self.threshold = 5

        number_of_moves_left = 0
        for i in range(5):
            if state.is_pawn_finished(self.id, i):
                continue
            if state.is_pawn_returning(self.id,
                                       i):  # Returing path for the pawn
                number_of_moves_left += ceil(
                    (12 - state.get_pawn_advancement(self.id, i)) /
                    squadro_state.MOVES_RETURN[self.id][i])
            else:
                number_of_moves_left += ceil(
                    (6 - state.get_pawn_advancement(self.id, i)) /
                    squadro_state.MOVES[self.id][i])
                number_of_moves_left += ceil(
                    6 / squadro_state.MOVES_RETURN[self.id][i])
        number_of_moves_left *= 2
        self.time_left_that_research = self.time_left / number_of_moves_left
        self.time_begin_research = time()
        for pawn in range(5):
            if state.is_pawn_finished(self.id, pawn):
                self.count += 1
            if state.is_pawn_finished(1 - self.id, pawn):
                self.count += 1
        #iterative deepening
        tmp = 0
        while time(
        ) - self.time_begin_research < self.time_left_that_research / 1.5:
            self.threshold += 1
            tmp = minimax.search(state, self)
        return tmp
Example #35
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     self.step = step
     start = time.time()
     board = dict_to_board(percepts)
     board.get_shortest_path(PLAYER1)
     board.get_shortest_path(PLAYER2)
     state = (board, player)
     end = time.time()
     print("dict: " + str(end - start))
     start = time.time()
  #   n = minimax.nodes(state,  self)
     #print ('nodes')
     #for node in n:
     #    print ("node " + str(node))
     ret = minimax.search(state,  0, self)
     end = time.time()
     print("minimax: " + str(end - start))
     return ret
Example #36
0
 def play(self, percepts, player, step, time_left):
     """This function is used to play a move according
     to the percepts, player and time left provided as input.
     It must return an action representing the move the player
     will perform.
     """
     self.player = player
     self.step = step
     start = time.time()
     board = dict_to_board(percepts)
     board.get_shortest_path(PLAYER1)
     board.get_shortest_path(PLAYER2)
     state = (board, player)
     end = time.time()
     print("dict: " + str(end - start))
     start = time.time()
     #   n = minimax.nodes(state,  self)
     #print ('nodes')
     #for node in n:
     #    print ("node " + str(node))
     ret = minimax.search(state, 0, self)
     end = time.time()
     print("minimax: " + str(end - start))
     return ret
 def get_action(self, state, last_action, time_left):
     self.last_action = last_action
     self.time_left = time_left
     return minimax.search(state, self)
Example #38
0
    def play(self, percepts, step, time_left):
        
        self.time_left = time_left
        self.timer = time()
        self.step = step
        #print(self.actualdepth)

        if step % 2 == 0:
            player = -1
        else:
            player = 1
        board = Board(percepts)
        state = (board, player)
        
        #print(step)
        
        self.previousSearchedBoard = None
        # MustDo before !
        mustDo = []
        for i in range(board.rows) :
            for j in range(board.columns):
                actions = board.get_tower_actions(i,j)
                for action in actions :
                    t_1 = [board.m[action[0]][action[1]][0]]
                    t_2 = [board.m[action[2]][action[3]][0]]
                    t_1.extend(board.get_tower(board.m[action[0]][action[1]]))
                    t_2.extend(board.get_tower(board.m[action[2]][action[3]]))
                    move = Action(t_1,t_2)
                    if move.is_a_pattern(self.mustDoDic):
                        mustDo.append([action,move.weight])
        if len(mustDo) > 0:
            action_to_play = 0
            weight = 0
            for elem in mustDo :
                if elem[1] > weight :
                    weight = elem[1]
                    action_to_play = elem[0]
            #print(mustDo)
            if action_to_play == 0 :
                print("INVALIDE PLAY in PLAY")
                print(mustDo)
                self.previousboard = board.clone().play_action(mustDo[0][0])
                return mustDo[0][0]
            else:
                self.previousboard = board.clone().play_action(action_to_play)
                return action_to_play                
        else :
#            maxtime = (self.time_left - 9) / ((1 + (37/(self.step+1))**1.75)/2)
#            print("maxtime", maxtime)
#            print("timeleft", self.time_left)
#            print("step", self.step)
            if step < 10:
                counteraction = False
                if self.previousboard:
                    differenttowers = self.get_diff_towers(board)
                    #self.timeout = False
                    counteraction = self.get_counter_action(board.get_percepts(), board, differenttowers, player)
                    #self.check_timeout()
                self.timeout = False
                subboardaction = self.get_sub_board_action(board, player)
                self.check_timeout()
                if counteraction:
                    if counteraction[1] and subboardaction[1]:
                        if counteraction[0] > subboardaction[0]:
                            action = counteraction[1]
                        else:
                            action = subboardaction[1]
                    elif counteraction[1]:
                        action = counteraction[1]
                    elif subboardaction[1]:
                        action = subboardaction[1]
                    else:
                        self.symetricDic = dict()
                        self.timeout = False
                        action = minimax.search(state, self)
                        self.check_timeout()
                elif subboardaction[1]:
                    action = subboardaction[1]
                else:
                    self.symetricDic = dict()
                    self.timeout = False
                    action = minimax.search(state, self)
                    self.check_timeout()
                self.previousboard = board.clone().play_action(action)
            else:
                self.symetricDic = dict()
                self.timeout = False
                action = minimax.search(state, self)
                self.check_timeout()
            return action 
Example #39
0
    def play(self, percepts, step, time_left):
        
        self.time_left = time_left
        self.timer = time()
        self.step = step
        print(self.time_left)
#        if step == 2:
#            init_previousboard()
        if step % 2 == 0:
            player = -1
        else:
            player = 1
        board = Board(percepts)
        state = (board, player)
        
#        subboardaction = self.get_sub_board_action(board, player)
#        return subboardaction[1]
#        if self.previousboard:
#            differenttowers = self.get_diff_towers(board)
#            counteraction = self.get_counter_action(board.get_percepts(), board, differenttowers, player)
#            self.previousboard = board.clone().play_action(counteraction[1])
#            return counteraction[1]
        
        self.previousSearchedBoard = None
        # MustDo before !
        mustDo = []
        for i in range(board.rows) :
            for j in range(board.columns):
                actions = board.get_tower_actions(i,j)
                for action in actions :
                    t_1 = [board.m[action[0]][action[1]][0]]
                    t_2 = [board.m[action[2]][action[3]][0]]
                    t_1.extend(board.get_tower(board.m[action[0]][action[1]]))
                    t_2.extend(board.get_tower(board.m[action[2]][action[3]]))
                    move = Action(t_1,t_2)
                    if move.is_a_pattern(self.mustDoDic):
                        mustDo.append([action,move.weight])
        if len(mustDo) > 0:
            action_to_play = 0
            weight = 0
            for elem in mustDo :
                if elem[1] > weight :
                    weight = elem[1]
                    action_to_play = elem[0]
            print(mustDo)
            if action_to_play == 0 :
                print("INVALIDE PLAY in PLAY")
                print(mustDo)
                self.previousboard = board.clone().play_action(mustDo[0][0])
                return mustDo[0][0]
            else:
                self.previousboard = board.clone().play_action(action_to_play)
                return action_to_play                
        else :
            return minimax.search(state, self)
            counteraction = False
            if self.previousboard:
                differenttowers = self.get_diff_towers(board)
                counteraction = self.get_counter_action(board.get_percepts(), board, differenttowers, player)
            subboardaction = self.get_sub_board_action(board, player)
            #print(subboardaction)
            if counteraction:
                if counteraction[1] and subboardaction[1]:
                    if counteraction[0] > subboardaction[0]:
                        action = counteraction[1]
                    else:
                        action = subboardaction[1]
                elif counteraction[1]:
                    action = counteraction[1]
                else:
                    action = subboardaction[1]
            elif subboardaction[1]:
                action = subboardaction[1]
            else:
                action = minimax.search(state, self)
            #action = minimax.search(state, self)
            self.previousboard = board.clone().play_action(action)
            return action 
Example #40
0
 def play(self, percepts, step, time_left):
     # We are always the yellow player
     board = Board(percepts)
     return minimax.search(board, self)
Example #41
0
    def play(self, percepts, step, time_left):

        self.time_left = time_left
        self.timer = time()
        self.step = step
        print(self.time_left)
        #        if step == 2:
        #            init_previousboard()
        if step % 2 == 0:
            player = -1
        else:
            player = 1
        board = Board(percepts)
        state = (board, player)

        #        subboardaction = self.get_sub_board_action(board, player)
        #        return subboardaction[1]
        #        if self.previousboard:
        #            differenttowers = self.get_diff_towers(board)
        #            counteraction = self.get_counter_action(board.get_percepts(), board, differenttowers, player)
        #            self.previousboard = board.clone().play_action(counteraction[1])
        #            return counteraction[1]

        self.previousSearchedBoard = None
        # MustDo before !
        mustDo = []
        for i in range(board.rows):
            for j in range(board.columns):
                actions = board.get_tower_actions(i, j)
                for action in actions:
                    t_1 = [board.m[action[0]][action[1]][0]]
                    t_2 = [board.m[action[2]][action[3]][0]]
                    t_1.extend(board.get_tower(board.m[action[0]][action[1]]))
                    t_2.extend(board.get_tower(board.m[action[2]][action[3]]))
                    move = Action(t_1, t_2)
                    if move.is_a_pattern(self.mustDoDic):
                        mustDo.append([action, move.weight])
        if len(mustDo) > 0:
            action_to_play = 0
            weight = 0
            for elem in mustDo:
                if elem[1] > weight:
                    weight = elem[1]
                    action_to_play = elem[0]
            print(mustDo)
            if action_to_play == 0:
                print("INVALIDE PLAY in PLAY")
                print(mustDo)
                self.previousboard = board.clone().play_action(mustDo[0][0])
                return mustDo[0][0]
            else:
                self.previousboard = board.clone().play_action(action_to_play)
                return action_to_play
        else:
            return minimax.search(state, self)
            counteraction = False
            if self.previousboard:
                differenttowers = self.get_diff_towers(board)
                counteraction = self.get_counter_action(
                    board.get_percepts(), board, differenttowers, player)
            subboardaction = self.get_sub_board_action(board, player)
            #print(subboardaction)
            if counteraction:
                if counteraction[1] and subboardaction[1]:
                    if counteraction[0] > subboardaction[0]:
                        action = counteraction[1]
                    else:
                        action = subboardaction[1]
                elif counteraction[1]:
                    action = counteraction[1]
                else:
                    action = subboardaction[1]
            elif subboardaction[1]:
                action = subboardaction[1]
            else:
                action = minimax.search(state, self)
            #action = minimax.search(state, self)
            self.previousboard = board.clone().play_action(action)
            return action
Example #42
0
 def play(self, percepts, step, time_left):
     board = Board(percepts)
     return minimax.search(board, self)