robot module
import direction class Robot(object): ''' The class Robot represents robots (or "bots") which inhabit two dimensional grid worlds. More specifically, each instance of the class represents a "robot body" (or "chassis" or "hardware") and basic functionality of such a robot. Each "robot body" is associated with a "robot brain" that controls the body and determines what functionality is activated and when. A robot is equipped with the various capabilities: - It can sense its own surroundings (location, facing, the world that it is in). - It can move forward. - It can spin around in any one of the four main compass directions. - It can sense whether it is broken or not, and whether it is "stuck" in a square between four walls. When a robot's take_turn() method is called, it uses its "brain" to figure out what to do (move, turn about, etc.). Robots with different kinds of brains behave differently. See the documentation of RobotWorld and RobotBrain. ''' def __init__(self, name): ''' Creates a new robot with the given name. The newly created robot is initially just a "dumb shell" until it's given a brain later using the method set_brain(). If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the name of the bot: string See set_brain(RobotBrain) ''' self.set_name(name) self.world = None # fixed value self.location = None # most-recent holder self.destroyed = False # flag self.facing = None # most-recent holder def set_name(self, name): ''' Sets the name of the robot. If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the new name of the robot: string ''' if not name: self.name = "Incognito" # most-recent holder else: self.name = name def get_name(self): ''' Returns the robot name : string ''' return self.name def set_brain(self, new_brain): ''' Sets a "brain" (or AI) for the robot (replacing any brain previously set, if any): spinbot, lovebot, drunkbot, ... Parameter new_brain is the artificial intelligence that controls the robot: spinbot/lovebot/drunkbot/... object ''' self.brain = new_brain def get_brain(self): ''' Returns the "brain" (or AI) of the robot: spinbot/lovebot/drunkbot/... object ''' return self.brain def get_world(self): ''' Returns the world in which the bot is, or None if the robot has not been placed in any robot world: RobotWorld ''' return self.world def get_location(self): ''' Returns the current location of the robot in the robot world, or None if the robot has not been placed in any robot world: Coordinates See get_location_square() ''' return self.location def get_location_square(self): ''' Returns the square that the robot is in: Square See get_location() ''' return self.get_world().get_square(self.get_location()) def get_facing(self): ''' Returns the direction the robot is facing: tuple ''' return self.facing def destroy(self): ''' Breaks the robot, rendering it unoperational. See fix() and take_turn() ''' self.destroyed = True def fix(self): ''' Fixes the robot. See destroy() ''' self.destroyed = False def is_broken(self): ''' Determines whether the robot is broken or not. A robot is broken if it has been broken with the method destroy() and not fixed since, or if it is lacking a brain. Returns the boolean value which states whether the bot is broken: boolean ''' return self.destroyed or self.get_brain() is None def is_stuck(self): ''' Determines whether the robot is stuck or not, i.e., are there any squares that the robot could move into. This is done by examining the four adjacent squares (diagonally adjacent squares are not considered). If there is a wall in all directions, the robot is considered stuck. Also, if the robot has not yet been placed in any robot world, it is considered to be stuck. Returns a boolean value that states whether the bot is stuck or not: boolean See take_turn() ''' world = self.get_world() if world is None: return True for value in direction.get_values(): # most-recent holder if not world.get_square(self.get_location().get_neighbor(value)).is_wall_square(): return False return True def set_world(self, world, location, facing): ''' Places the robot in the given robot world at the specified coordinates. Note! This method is supposed to be used from the addRobot method in the RobotWorld class, which makes sure that the robot is appropriately recorded as being part of the robot world. Parameter world is the robot world in which the bot is placed: RobotWorld Parameter location is the coordinates at which the bot is placed: Coordinates Parameter facing is the direction the robot is facing initially : tuple Returns False if the square at the given location is not empty or the robot is already located in some world (the given one or some other world), True otherwise: boolean See RobotWorld.add_robot(Robot, Coordinates, Direction) ''' target_square = world.get_square(location) if not target_square.is_empty() or self.get_world() is not None: return False else: self.world = world self.location = location self.facing = facing return True def spin(self, new_facing): ''' Turns the robot in the specified direction, if the robot is intact. If the robot is broken, the method does nothing. Parameter new_facing is the new facing direction of the robot: tuple ''' if not self.is_broken(): self.facing = new_facing def move(self, direction): ''' Changes the place of the robot within its current world from the current square to the square next to it in the given direction. The direction does not necessarily have to be the same one that the robot is originally facing in. This method changes the robot to face the direction it moves in. A broken robot can't move. Two robots can never be in the same square, neither can a robot and a wall. If the square at the given coordinates is not empty, a collision occurs instead and the robot does not move (but still turns to face whatever it collided with). If the moving robot collides with a wall, the robot itself breaks. If a moving robot collides with another robot, the other robot breaks and the moving robot stays intact. Parameter direction is the direction to move in: tuple Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' if self.is_broken(): return False target = self.get_location().get_neighbor(direction) current_square = self.get_location_square() target_square = self.get_world().get_square(target) self.spin(direction) if target_square.is_empty(): current_square.remove_robot() self.location = target target_square.set_robot(self) return True elif target_square.get_robot() is not None: target_square.get_robot().destroy() return False else: # collided with wall self.destroy() return False def move_forward(self): ''' Moves the robot forward one square. This is equivalent to calling self.move(self.get_facing()). See move(Direction) Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' return self.move(self.get_facing()) def take_turn(self): ''' Gives the robot a turn to act. An unstuck, unbroken robot, however, consults its brain to find out what to do. This is done by calling the brain's method move_body(). Here is a general outline of what happens during a robot's turn: 1. The robot checks its sensors to see if it is stuck or broken. (If it is, it doesn't do anything.) 2. If not, it call's it's brain's move_body() method, leaving it up to the brain to decide what happens next. 3. The move_body() method will then determine how the robot behaves, usually calling various methods of the robot's body (i.e., the Robot object whose turn it is), e.g. move, spin. However, it is not up to the Robot object to decide which of its methods get called - that depends on the implementation of the robot's brain. See is_broken() See is_stuck() See RobotBrain.move_body() ''' if not self.is_stuck() and not self.is_broken(): self.brain.move_body()
Classes
class Robot
The class Robot represents robots (or "bots") which inhabit two dimensional grid worlds. More specifically, each instance of the class represents a "robot body" (or "chassis" or "hardware") and basic functionality of such a robot. Each "robot body" is associated with a "robot brain" that controls the body and determines what functionality is activated and when.
A robot is equipped with the various capabilities:
-
It can sense its own surroundings (location, facing, the world that it is in).
-
It can move forward.
-
It can spin around in any one of the four main compass directions.
-
It can sense whether it is broken or not, and whether it is "stuck" in a square between four walls.
When a robot's take_turn() method is called, it uses its "brain" to figure out what to do (move, turn about, etc.). Robots with different kinds of brains behave differently.
See the documentation of RobotWorld and RobotBrain.
class Robot(object): ''' The class Robot represents robots (or "bots") which inhabit two dimensional grid worlds. More specifically, each instance of the class represents a "robot body" (or "chassis" or "hardware") and basic functionality of such a robot. Each "robot body" is associated with a "robot brain" that controls the body and determines what functionality is activated and when. A robot is equipped with the various capabilities: - It can sense its own surroundings (location, facing, the world that it is in). - It can move forward. - It can spin around in any one of the four main compass directions. - It can sense whether it is broken or not, and whether it is "stuck" in a square between four walls. When a robot's take_turn() method is called, it uses its "brain" to figure out what to do (move, turn about, etc.). Robots with different kinds of brains behave differently. See the documentation of RobotWorld and RobotBrain. ''' def __init__(self, name): ''' Creates a new robot with the given name. The newly created robot is initially just a "dumb shell" until it's given a brain later using the method set_brain(). If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the name of the bot: string See set_brain(RobotBrain) ''' self.set_name(name) self.world = None # fixed value self.location = None # most-recent holder self.destroyed = False # flag self.facing = None # most-recent holder def set_name(self, name): ''' Sets the name of the robot. If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the new name of the robot: string ''' if not name: self.name = "Incognito" # most-recent holder else: self.name = name def get_name(self): ''' Returns the robot name : string ''' return self.name def set_brain(self, new_brain): ''' Sets a "brain" (or AI) for the robot (replacing any brain previously set, if any): spinbot, lovebot, drunkbot, ... Parameter new_brain is the artificial intelligence that controls the robot: spinbot/lovebot/drunkbot/... object ''' self.brain = new_brain def get_brain(self): ''' Returns the "brain" (or AI) of the robot: spinbot/lovebot/drunkbot/... object ''' return self.brain def get_world(self): ''' Returns the world in which the bot is, or None if the robot has not been placed in any robot world: RobotWorld ''' return self.world def get_location(self): ''' Returns the current location of the robot in the robot world, or None if the robot has not been placed in any robot world: Coordinates See get_location_square() ''' return self.location def get_location_square(self): ''' Returns the square that the robot is in: Square See get_location() ''' return self.get_world().get_square(self.get_location()) def get_facing(self): ''' Returns the direction the robot is facing: tuple ''' return self.facing def destroy(self): ''' Breaks the robot, rendering it unoperational. See fix() and take_turn() ''' self.destroyed = True def fix(self): ''' Fixes the robot. See destroy() ''' self.destroyed = False def is_broken(self): ''' Determines whether the robot is broken or not. A robot is broken if it has been broken with the method destroy() and not fixed since, or if it is lacking a brain. Returns the boolean value which states whether the bot is broken: boolean ''' return self.destroyed or self.get_brain() is None def is_stuck(self): ''' Determines whether the robot is stuck or not, i.e., are there any squares that the robot could move into. This is done by examining the four adjacent squares (diagonally adjacent squares are not considered). If there is a wall in all directions, the robot is considered stuck. Also, if the robot has not yet been placed in any robot world, it is considered to be stuck. Returns a boolean value that states whether the bot is stuck or not: boolean See take_turn() ''' world = self.get_world() if world is None: return True for value in direction.get_values(): # most-recent holder if not world.get_square(self.get_location().get_neighbor(value)).is_wall_square(): return False return True def set_world(self, world, location, facing): ''' Places the robot in the given robot world at the specified coordinates. Note! This method is supposed to be used from the addRobot method in the RobotWorld class, which makes sure that the robot is appropriately recorded as being part of the robot world. Parameter world is the robot world in which the bot is placed: RobotWorld Parameter location is the coordinates at which the bot is placed: Coordinates Parameter facing is the direction the robot is facing initially : tuple Returns False if the square at the given location is not empty or the robot is already located in some world (the given one or some other world), True otherwise: boolean See RobotWorld.add_robot(Robot, Coordinates, Direction) ''' target_square = world.get_square(location) if not target_square.is_empty() or self.get_world() is not None: return False else: self.world = world self.location = location self.facing = facing return True def spin(self, new_facing): ''' Turns the robot in the specified direction, if the robot is intact. If the robot is broken, the method does nothing. Parameter new_facing is the new facing direction of the robot: tuple ''' if not self.is_broken(): self.facing = new_facing def move(self, direction): ''' Changes the place of the robot within its current world from the current square to the square next to it in the given direction. The direction does not necessarily have to be the same one that the robot is originally facing in. This method changes the robot to face the direction it moves in. A broken robot can't move. Two robots can never be in the same square, neither can a robot and a wall. If the square at the given coordinates is not empty, a collision occurs instead and the robot does not move (but still turns to face whatever it collided with). If the moving robot collides with a wall, the robot itself breaks. If a moving robot collides with another robot, the other robot breaks and the moving robot stays intact. Parameter direction is the direction to move in: tuple Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' if self.is_broken(): return False target = self.get_location().get_neighbor(direction) current_square = self.get_location_square() target_square = self.get_world().get_square(target) self.spin(direction) if target_square.is_empty(): current_square.remove_robot() self.location = target target_square.set_robot(self) return True elif target_square.get_robot() is not None: target_square.get_robot().destroy() return False else: # collided with wall self.destroy() return False def move_forward(self): ''' Moves the robot forward one square. This is equivalent to calling self.move(self.get_facing()). See move(Direction) Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' return self.move(self.get_facing()) def take_turn(self): ''' Gives the robot a turn to act. An unstuck, unbroken robot, however, consults its brain to find out what to do. This is done by calling the brain's method move_body(). Here is a general outline of what happens during a robot's turn: 1. The robot checks its sensors to see if it is stuck or broken. (If it is, it doesn't do anything.) 2. If not, it call's it's brain's move_body() method, leaving it up to the brain to decide what happens next. 3. The move_body() method will then determine how the robot behaves, usually calling various methods of the robot's body (i.e., the Robot object whose turn it is), e.g. move, spin. However, it is not up to the Robot object to decide which of its methods get called - that depends on the implementation of the robot's brain. See is_broken() See is_stuck() See RobotBrain.move_body() ''' if not self.is_stuck() and not self.is_broken(): self.brain.move_body()
Ancestors (in MRO)
- Robot
- __builtin__.object
Instance variables
var destroyed
var facing
var location
var world
Methods
def __init__(
self, name)
Creates a new robot with the given name. The newly created robot is initially just a "dumb shell" until it's given a brain later using the method set_brain().
If the given name is None or an empty string, the name is set to "Incognito".
Parameter name is the name of the bot: string
See set_brain(RobotBrain)
def __init__(self, name): ''' Creates a new robot with the given name. The newly created robot is initially just a "dumb shell" until it's given a brain later using the method set_brain(). If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the name of the bot: string See set_brain(RobotBrain) ''' self.set_name(name) self.world = None # fixed value self.location = None # most-recent holder self.destroyed = False # flag self.facing = None # most-recent holder
def destroy(
self)
Breaks the robot, rendering it unoperational.
See fix() and take_turn()
def destroy(self): ''' Breaks the robot, rendering it unoperational. See fix() and take_turn() ''' self.destroyed = True
def fix(
self)
Fixes the robot.
See destroy()
def fix(self): ''' Fixes the robot. See destroy() ''' self.destroyed = False
def get_brain(
self)
Returns the "brain" (or AI) of the robot: spinbot/lovebot/drunkbot/... object
def get_brain(self): ''' Returns the "brain" (or AI) of the robot: spinbot/lovebot/drunkbot/... object ''' return self.brain
def get_facing(
self)
Returns the direction the robot is facing: tuple
def get_facing(self): ''' Returns the direction the robot is facing: tuple ''' return self.facing
def get_location(
self)
Returns the current location of the robot in the robot world, or None if the robot has not been placed in any robot world: Coordinates
See get_location_square()
def get_location(self): ''' Returns the current location of the robot in the robot world, or None if the robot has not been placed in any robot world: Coordinates See get_location_square() ''' return self.location
def get_location_square(
self)
Returns the square that the robot is in: Square
See get_location()
def get_location_square(self): ''' Returns the square that the robot is in: Square See get_location() ''' return self.get_world().get_square(self.get_location())
def get_name(
self)
Returns the robot name : string
def get_name(self): ''' Returns the robot name : string ''' return self.name
def get_world(
self)
Returns the world in which the bot is, or None if the robot has not been placed in any robot world: RobotWorld
def get_world(self): ''' Returns the world in which the bot is, or None if the robot has not been placed in any robot world: RobotWorld ''' return self.world
def is_broken(
self)
Determines whether the robot is broken or not. A robot is broken if it has been broken with the method destroy() and not fixed since, or if it is lacking a brain.
Returns the boolean value which states whether the bot is broken: boolean
def is_broken(self): ''' Determines whether the robot is broken or not. A robot is broken if it has been broken with the method destroy() and not fixed since, or if it is lacking a brain. Returns the boolean value which states whether the bot is broken: boolean ''' return self.destroyed or self.get_brain() is None
def is_stuck(
self)
Determines whether the robot is stuck or not, i.e., are there any squares that the robot could move into. This is done by examining the four adjacent squares (diagonally adjacent squares are not considered). If there is a wall in all directions, the robot is considered stuck. Also, if the robot has not yet been placed in any robot world, it is considered to be stuck.
Returns a boolean value that states whether the bot is stuck or not: boolean
See take_turn()
def is_stuck(self): ''' Determines whether the robot is stuck or not, i.e., are there any squares that the robot could move into. This is done by examining the four adjacent squares (diagonally adjacent squares are not considered). If there is a wall in all directions, the robot is considered stuck. Also, if the robot has not yet been placed in any robot world, it is considered to be stuck. Returns a boolean value that states whether the bot is stuck or not: boolean See take_turn() ''' world = self.get_world() if world is None: return True for value in direction.get_values(): # most-recent holder if not world.get_square(self.get_location().get_neighbor(value)).is_wall_square(): return False return True
def move(
self, direction)
Changes the place of the robot within its current world from the current square to the square next to it in the given direction. The direction does not necessarily have to be the same one that the robot is originally facing in. This method changes the robot to face the direction it moves in. A broken robot can't move.
Two robots can never be in the same square, neither can a robot and a wall. If the square at the given coordinates is not empty, a collision occurs instead and the robot does not move (but still turns to face whatever it collided with). If the moving robot collides with a wall, the robot itself breaks. If a moving robot collides with another robot, the other robot breaks and the moving robot stays intact.
Parameter direction is the direction to move in: tuple
Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean
def move(self, direction): ''' Changes the place of the robot within its current world from the current square to the square next to it in the given direction. The direction does not necessarily have to be the same one that the robot is originally facing in. This method changes the robot to face the direction it moves in. A broken robot can't move. Two robots can never be in the same square, neither can a robot and a wall. If the square at the given coordinates is not empty, a collision occurs instead and the robot does not move (but still turns to face whatever it collided with). If the moving robot collides with a wall, the robot itself breaks. If a moving robot collides with another robot, the other robot breaks and the moving robot stays intact. Parameter direction is the direction to move in: tuple Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' if self.is_broken(): return False target = self.get_location().get_neighbor(direction) current_square = self.get_location_square() target_square = self.get_world().get_square(target) self.spin(direction) if target_square.is_empty(): current_square.remove_robot() self.location = target target_square.set_robot(self) return True elif target_square.get_robot() is not None: target_square.get_robot().destroy() return False else: # collided with wall self.destroy() return False
def move_forward(
self)
Moves the robot forward one square. This is equivalent to calling self.move(self.get_facing()).
See move(Direction)
Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean
def move_forward(self): ''' Moves the robot forward one square. This is equivalent to calling self.move(self.get_facing()). See move(Direction) Returns a boolean value indicating if the movement was successful (False means that either the robot was broken to begin with or that it collided with something instead of successfully moving): boolean ''' return self.move(self.get_facing())
def set_brain(
self, new_brain)
Sets a "brain" (or AI) for the robot (replacing any brain previously set, if any): spinbot, lovebot, drunkbot, ...
Parameter new_brain is the artificial intelligence that controls the robot: spinbot/lovebot/drunkbot/... object
def set_brain(self, new_brain): ''' Sets a "brain" (or AI) for the robot (replacing any brain previously set, if any): spinbot, lovebot, drunkbot, ... Parameter new_brain is the artificial intelligence that controls the robot: spinbot/lovebot/drunkbot/... object ''' self.brain = new_brain
def set_name(
self, name)
Sets the name of the robot. If the given name is None or an empty string, the name is set to "Incognito".
Parameter name is the new name of the robot: string
def set_name(self, name): ''' Sets the name of the robot. If the given name is None or an empty string, the name is set to "Incognito". Parameter name is the new name of the robot: string ''' if not name: self.name = "Incognito" # most-recent holder else: self.name = name
def set_world(
self, world, location, facing)
Places the robot in the given robot world at the specified coordinates. Note! This method is supposed to be used from the addRobot method in the RobotWorld class, which makes sure that the robot is appropriately recorded as being part of the robot world.
Parameter world is the robot world in which the bot is placed: RobotWorld
Parameter location is the coordinates at which the bot is placed: Coordinates
Parameter facing is the direction the robot is facing initially : tuple
Returns False if the square at the given location is not empty or the robot is already located in some world (the given one or some other world), True otherwise: boolean
See RobotWorld.add_robot(Robot, Coordinates, Direction)
def set_world(self, world, location, facing): ''' Places the robot in the given robot world at the specified coordinates. Note! This method is supposed to be used from the addRobot method in the RobotWorld class, which makes sure that the robot is appropriately recorded as being part of the robot world. Parameter world is the robot world in which the bot is placed: RobotWorld Parameter location is the coordinates at which the bot is placed: Coordinates Parameter facing is the direction the robot is facing initially : tuple Returns False if the square at the given location is not empty or the robot is already located in some world (the given one or some other world), True otherwise: boolean See RobotWorld.add_robot(Robot, Coordinates, Direction) ''' target_square = world.get_square(location) if not target_square.is_empty() or self.get_world() is not None: return False else: self.world = world self.location = location self.facing = facing return True
def spin(
self, new_facing)
Turns the robot in the specified direction, if the robot is intact. If the robot is broken, the method does nothing.
Parameter new_facing is the new facing direction of the robot: tuple
def spin(self, new_facing): ''' Turns the robot in the specified direction, if the robot is intact. If the robot is broken, the method does nothing. Parameter new_facing is the new facing direction of the robot: tuple ''' if not self.is_broken(): self.facing = new_facing
def take_turn(
self)
Gives the robot a turn to act. An unstuck, unbroken robot, however, consults its brain to find out what to do. This is done by calling the brain's method move_body(). Here is a general outline of what happens during a robot's turn: 1. The robot checks its sensors to see if it is stuck or broken. (If it is, it doesn't do anything.) 2. If not, it call's it's brain's move_body() method, leaving it up to the brain to decide what happens next. 3. The move_body() method will then determine how the robot behaves, usually calling various methods of the robot's body (i.e., the Robot object whose turn it is), e.g. move, spin. However, it is not up to the Robot object to decide which of its methods get called - that depends on the implementation of the robot's brain.
See is_broken() See is_stuck() See RobotBrain.move_body()
def take_turn(self): ''' Gives the robot a turn to act. An unstuck, unbroken robot, however, consults its brain to find out what to do. This is done by calling the brain's method move_body(). Here is a general outline of what happens during a robot's turn: 1. The robot checks its sensors to see if it is stuck or broken. (If it is, it doesn't do anything.) 2. If not, it call's it's brain's move_body() method, leaving it up to the brain to decide what happens next. 3. The move_body() method will then determine how the robot behaves, usually calling various methods of the robot's body (i.e., the Robot object whose turn it is), e.g. move, spin. However, it is not up to the Robot object to decide which of its methods get called - that depends on the implementation of the robot's brain. See is_broken() See is_stuck() See RobotBrain.move_body() ''' if not self.is_stuck() and not self.is_broken(): self.brain.move_body()