示例#1
0
 def execute(self):
     if self.actions.final_action == self.opt_action['Forward']:
         api.move_forward()
     elif self.actions.final_action == self.opt_action['Left']:
         api.turn_left()
     elif self.actions.final_action == self.opt_action['Right']:
         api.turn_right()
     elif self.actions.final_action == self.opt_action['Back']:
         api.move_backward()
     elif self.actions.final_action == self.opt_action['Fire']:
         api.fire_cannon()
     elif self.actions.final_action == self.opt_action['LookBack']:
         api.turn_left()
示例#2
0
    def random_turn(self):

        """
        Method that generates a random turn with equal
        probability to the left or to the right.
        """

        direction = random.choice(['left', 'right'])

        if direction == 'left':
            api.turn_left()
            return "Left turn"
        elif direction == 'right':
            api.turn_right()
            return "Right turn"
        else:
            return "No turn"
示例#3
0
    def avoid(self):

        """
        Method executes manouver to avoid collision.
        Turns the way with the most space.
        """

        space_to_left = api.lidar_left()
        space_to_right = api.lidar_right()

        if space_to_left > space_to_right:
            api.turn_left()
            return "Left turn"
        elif space_to_left < space_to_right:
            api.turn_right()
            return "Right turn"
        else:
            self.random_turn()
            return "Random turn"
示例#4
0
    def turn(self, surroundings):
        """
        Tanken vrider sig åt det håll det finns mest utrymme åt.
        Efter den har vridit sig så ändras tankens riktning.

        :param surroundings: Avstånd till omgivning från tanken baserat från åskådarens perspektiv
        """
        new_dir = ''

        if self.direction == 'up':
            if surroundings[2] > 1 and surroundings[2] > surroundings[3]:
                api.turn_left()
                new_dir = 'left'
            else:
                api.turn_right()
                new_dir = 'right'

        elif self.direction == 'left':
            if surroundings[1] > 1 and surroundings[1] > surroundings[0]:
                api.turn_left()
                new_dir = 'down'
            else:
                api.turn_right()
                new_dir = 'up'

        elif self.direction == 'right':
            if surroundings[0] > 1 and surroundings[0] > surroundings[1]:
                api.turn_left()
                new_dir = 'up'
            else:
                api.turn_right()
                new_dir = 'down'

        elif self.direction == 'down':
            if surroundings[3] > 1 and surroundings[3] > surroundings[2]:
                api.turn_left()
                new_dir = 'right'
            else:
                api.turn_right()
                new_dir = 'left'

        self.direction = new_dir
示例#5
0
    def update(self):
        """
        Executes a single step of the tank's programming. The tank can only
        move, turn, or fire its cannon once per turn. Between each update, the
        tank's engine remains running and consumes 1 fuel. This function will be
        called repeatedly until there are no more targets left on the grid, or
        the tank runs out of fuel.
        """
        global tankdir
        global maxcol, maxrow, mincol, minrow

        self.Map[pos.col][pos.row] = EMPTY

        if (api.identify_target()):
            front_object = TARGET
        else:
            front_object = BLOCK
        self.fuelBefore = self.fuelNow
        self.fuelNow = api.current_fuel()

        #if (self.fuelBefore- self.fuelNow>7): # got shot!!! ESCAPE
        #    if (api.lidar_back()>1 and (api.lidar_left()==1 or api.lidar_right()==1 ) ):
        #        api.move_backward()
        # Todo: Write your code here!
        # front (no turn)

        # Another scaping strategy
        #if (api.lidar_left() == 1  and api.lidar_right()==1 and api.lidar_front() > 1 ):
        #    api.move_forward()
        tempdir = tankdir
        for i in range(1, api.lidar_front()):
            self.Map[pos.col + (i * tempdir.col)][pos.row +
                                                  (i * tempdir.row)] = EMPTY
        tmpcol = pos.col + (api.lidar_front() * tempdir.col)
        tmprow = pos.row + (api.lidar_front() * tempdir.row)
        if self.Map[tmpcol][tmprow] != BLOCK:
            self.Map[tmpcol][tmprow] = front_object

        # back (turn twice)
        tempdir = update_dir(update_dir(tankdir, RIGHT), RIGHT)
        for i in range(1, api.lidar_back()):
            self.Map[pos.col + (i * tempdir.col)][pos.row +
                                                  (i * tempdir.row)] = EMPTY
        tmpcol = pos.col + (api.lidar_back() * tempdir.col)
        tmprow = pos.row + (api.lidar_back() * tempdir.row)
        if self.Map[tmpcol][tmprow] != BLOCK:
            self.Map[tmpcol][tmprow] = OBJECT

        tempdir = update_dir(tankdir, LEFT)
        for i in range(1, api.lidar_left()):
            self.Map[pos.col + (i * tempdir.col)][pos.row +
                                                  (i * tempdir.row)] = EMPTY
        tmpcol = pos.col + (api.lidar_left() * tempdir.col)
        tmprow = pos.row + (api.lidar_left() * tempdir.row)
        if self.Map[tmpcol][tmprow] != BLOCK:
            self.Map[tmpcol][tmprow] = OBJECT

        tempdir = update_dir(tankdir, RIGHT)
        for i in range(1, api.lidar_right()):
            self.Map[pos.col + (i * tempdir.col)][pos.row +
                                                  (i * tempdir.row)] = EMPTY
        tmpcol = pos.col + (api.lidar_right() * tempdir.col)
        tmprow = pos.row + (api.lidar_right() * tempdir.row)
        if self.Map[tmpcol][tmprow] != BLOCK:
            self.Map[tmpcol][tmprow] = OBJECT

        ## update surrounding walls as blocker
        for i in range(0, NUM_COLS):
            for j in range(0, NUM_ROWS):
                if (self.Map[i][j] == EMPTY):
                    if i < mincol:
                        mincol = i
                    if i > maxcol:
                        maxcol = i
                    if j < minrow:
                        minrow = j
                    if j > maxrow:
                        maxrow = j

        if maxcol - mincol == 9:
            for j in range(0, NUM_ROWS):
                self.Map[mincol - 1][j] = BLOCK
                self.Map[maxcol + 1][j] = BLOCK
            for i in range(0, NUM_COLS):
                self.Map[i][minrow - 1] = BLOCK
                self.Map[i][maxrow + 1] = BLOCK
        if maxcol - mincol == 19:
            for j in range(0, NUM_ROWS):
                self.Map[mincol - 1][j] = BLOCK
                self.Map[maxcol + 1][j] = BLOCK
            for i in range(0, NUM_COLS):
                self.Map[i][minrow - 1] = BLOCK
                self.Map[i][maxrow + 1] = BLOCK

        # print( "position", pos.col, pos.row, tankdir.col, tankdir.row,
        #    "lf", api.lidar_front(), "lb", api.lidar_back(), "ll", api.lidar_left(), "lr", api.lidar_right())
        self.visit[pos.col][pos.row].extend((tankdir.col, tankdir.row))
        if (api.identify_target() and api.lidar_front() < 6):
            api.fire_cannon()
        else:
            maxdep = MAX_DEPTH  # api.current_fuel()
            (dist, move) = self.NearestCost(pos, tankdir, 0, maxdep)
            # print("best move", (dist, move))
            if (move == FORWARD):
                api.move_forward()
                x = self.pos_forward(pos, tankdir, 1)
                pos.col = x.col
                pos.row = x.row

            if (move == RIGHT):
                tankdir = update_dir(tankdir, RIGHT)
                api.turn_right()
            if (move == LEFT):
                tankdir = update_dir(tankdir, LEFT)
                api.turn_left()