def decideAction(self, robot, board, actions): ''' Walks towards the other robot and tries to run into it. ''' # get the list of robots from the board robots = board.getRobots() robotKeys = list(robots.keys()) # temporary hacky workaround # get the other robot (future versions should find the closest robot) closest = robots[robotKeys[0]] # if first robot is this robot, get second robot if robotKeys[0] == robot.name: closest = robots[robotKeys[1]] # get current distance to target robot currentDistance = manhattanDistance(robot, closest) # get coordinates of space in front of robot forwardPos = forwardCoords(robot.xPosition, robot.yPosition, robot.rotation) # get distance of forwardPos from target forwardDistance = manhattanDistance(forwardPos, closest) # if forward gets robot closer to target, move forward if forwardDistance < currentDistance: return 'MOVE_FORWARD' # otherwise turn right else: return 'TURN_RIGHT'
def decideAction(self, robot, board, actions): ''' Returns the alias to a random movement function in the robot class ''' choice = random.choice(actions) if (choice == 'MOVE_FORWARD'): forwardLoc = utility.forwardCoords(robot.xPosition, robot.yPosition, robot.rotation, board) # if going to move off the board, turn if forwardLoc is None: return 'TURN_RIGHT' # do other stuff isOccupied = board.occupied(forwardLoc[0],forwardLoc[1]) if (isOccupied is None): #space in front is outside the board return 'TURN_RIGHT' if (isOccupied): #if something is in front, shoot at it return 'SHOOT_PROJECTILE' else: #if something isn't in front, move forward return 'MOVE_FORWARD' return choice
def moveForward(self, board): ''' Moves the robot forward one space in the current direction. If it runs into a wall, it will simply not move. @param board: A reference to the board object. This is used for checking bounds and collisions. ''' # logging.debug("%s is moving forward" % (self.name)) # get the coordinates in front of the robot coords = forwardCoords(self.xPosition, self.yPosition, self.rotation, board) # if out of bounds, do nothing if coords is None: # logging.debug('{} is off the board'.format(self.name)) # debugging output # exit return # =================== # TEST FOR COLLISIONS # =================== # retrieve list of actors in new space collidingActors = board.occupied(coords[0], coords[1]) # if there are one or more actors in the new space if len(collidingActors) > 0: # debugging output # logging.debug('actors in front of {}: {}'.format(self.name, str(collidingActors))) # iterate over actors in space for actor in collidingActors: # debugging output # logging.debug(self.name + " to collide with actor " + actor) # if actor is robot, damage robot and destroy self if actor in board.getRobots(): # debugging output # logging.debug(self.name + " colliding with robot " + actor) # run collision logic board.collision(self.name, actor) # exit without moving return # no collisions, move to new coords if (self.health > 0): self.xPosition = coords[0] self.yPosition = coords[1]
def takeAction(self, board): ''' Moves the projectile forward one space in the current direction. If it runs into a wall, it will destroy itself. @param board: A reference to the board object. This is used for checking bounds and collidingActors. ''' # logging.debug("%s is moving forward" % (self.name)) # get the coordinates in front of the robot coords = forwardCoords(self.xPosition, self.yPosition, self.rotation, board) # if out of bounds, destroy self if coords is None: # debugging output # logging.debug('{} is off the board'.format(self.name)) self.health = 0 return # =================== # TEST FOR COLLISIONS # =================== # retrieve list of actors in new space collidingActors = board.occupied(coords[0], coords[1]) if len(collidingActors) > 0: # logging.debug('actors in front of {}: {}'.format(self.name, str(collidingActors))) # iterate over actors in space for actor in collidingActors: # logging.debug(self.name + " to collide with actor " + actor) # if actor is robot, damage robot and destroy self if actor in board.getRobots(): # logging.debug(self.name + " colliding with robot " + actor) # run collision logic board.collision(self.name, actor) # reset health to 0 to ensure correct destruction self.health = 0 elif actor in board.getProjectiles(): # actor is a projectile # if both are not facing the same direction, both are destroyed # logging.debug(self.name + " colliding with projectile " + actor) # test if both are facing same direction if not (board.actors[actor].rotation == self.rotation): # collide both board.collision(self.name, actor) # projectile still on the board if (self.health > 0): self.xPosition = coords[0] self.yPosition = coords[1]