Staggerbot
The class Staggerbot
represents the “brains” of robots whose algorithms are not
sophisticated enough to beat a game of Viinaharava. These robots stagger from place to place
harum-scarum, relying on a (pseudo)random number generator.
- Value parameters:
- body
the unlucky robot body whose actions the staggerbot brain will control
- name
the name of the staggerbot
- randomSeed
the seed that feeds the random generator that guides the bot
Value members
Concrete methods
Moves the robot. A staggerbot selects a random direction and tries to move to
the adjacent square in that direction. If there is a wall or another robot in
that square, the robot collides with it and does not change squares (as per
the moveTowards
method of its body, possibly colliding
and breaking itself or another robot).
Moves the robot. A staggerbot selects a random direction and tries to move to
the adjacent square in that direction. If there is a wall or another robot in
that square, the robot collides with it and does not change squares (as per
the moveTowards
method of its body, possibly colliding
and breaking itself or another robot).
If the staggerbot collides with something, it remains facing whatever it collided with and ends its turn there. In case the staggerbot did not collide with anything, it again selects a new random direction and turns to face that direction (which may or may not be the direction it just moved in).
Note: this means that if (and only if) the staggerbot did not collide, it picks two random directions per turn, one to move in and one to face in.
The staggerbot selects each random direction using the following algorithm:
-
Use an indexed collection containing all the four
CompassDir
objects in clockwise order, starting with north. -
Pick a random index. Use the
nextInt
method inscala.util.Random
that produces a pseudorandom integer that is at least zero and lower than the method’s integer parameter. -
Select the direction found at the random index.
A particular staggerbot always uses the same instance of Random
to generate numbers.
It does not create a new random number generator every time this method is called.
Each staggerbot has its own Random
object.
This method assumes that it is called only if the robot is not broken or stuck.
Inherited methods
Moves the robot one square forwards, if there is nothing there. If that square isn’t empty, the robot does not move, so this method never causes a collision.
Moves the robot one square forwards, if there is nothing there. If that square isn’t empty, the robot does not move, so this method never causes a collision.
- Returns:
true
if the move was successful and the robot is now in the next square,false
if it was blocked- Inherited from:
- RobotBrain
Controls the robot body’s actions for a single turn. If the brain considers the robot to be stuck (see isStuck), this method does nothing. If not stuck, the brain calls its own moveBody method, which carries out the actual robot actions.
Controls the robot body’s actions for a single turn. If the brain considers the robot to be stuck (see isStuck), this method does nothing. If not stuck, the brain calls its own moveBody method, which carries out the actual robot actions.
- Inherited from:
- RobotBrain
Returns the direction this robot is facing in.
Returns the direction this robot is facing in.
- Inherited from:
- RobotBrain
Determines whether or not the robot brain considers the robot to be stuck. A brain considers the robot stuck if and only if all the squares surrounding the robot contain unpassable obstacles, as defined by the mayAdvance method. Only the four nearest squares in the main compass directions are considered.
Determines whether or not the robot brain considers the robot to be stuck. A brain considers the robot stuck if and only if all the squares surrounding the robot contain unpassable obstacles, as defined by the mayAdvance method. Only the four nearest squares in the main compass directions are considered.
- Inherited from:
- RobotBrain
Returns the location of this robot in its robot world.
Returns the location of this robot in its robot world.
- Inherited from:
- RobotBrain
Returns the coordinates that point at the square that is immediately in front of this robot.
Returns the coordinates that point at the square that is immediately in front of this robot.
- Inherited from:
- RobotBrain
Checks the the square that neighbors this robot in the given direction to see if it contains something that the robot brain considers an obstacle. This method is abstract; different kinds of robot brains will have different definitions of what counts as an obstacle.
Checks the the square that neighbors this robot in the given direction to see if it contains something that the robot brain considers an obstacle. This method is abstract; different kinds of robot brains will have different definitions of what counts as an obstacle.
This default implementation (which may be overriden by subtypes) does not consider anything to be an obstacle. Robots that rely on this default implementation are willing to move in any direction and never consider themselves to be stuck (see isStuck).
- Inherited from:
- RobotBrain
Changes the robot’s name to the given one.
Changes the robot’s name to the given one.
Note to students: In Scala, a method whose name ends in an underscore and an
equals sign — like this one’s — can be called using a special syntax. For instance,
this method can be called either with the statement bot.name_=("Suzy")
or simply
with an assignment statement: bot.name = "Suzy"
. You won’t find many uses for this
in O1, but it’s nice to know nonetheless.
- Inherited from:
- RobotBrain
Returns the brain of the robot immediately in front of this robot. The
brain is returned in an Option
wrapper; None
is returned if there
is no robot in that square or if the robot that is there has no brain.
Returns the brain of the robot immediately in front of this robot. The
brain is returned in an Option
wrapper; None
is returned if there
is no robot in that square or if the robot that is there has no brain.
- Inherited from:
- RobotBrain
Returns the square that is immediately in front of this robot.
Returns the square that is immediately in front of this robot.
- Inherited from:
- RobotBrain
Returns a textual representation of the robot (which is the robot’s name).
Returns a textual representation of the robot (which is the robot’s name).
- Definition Classes
- RobotBrain -> Any
- Inherited from:
- RobotBrain
Returns the world that this robot is located in.
Returns the world that this robot is located in.
- Inherited from:
- RobotBrain