TribalBot

class TribalBot(body: RobotBody, val tribe: Tribe) extends RobotBrain

The class TribalBot represents the “brains” of robots that belong to a “robot tribe” and behave according to the rules of that tribe. Tribal robots consider other tribal robots of the same tribe their friends and all other robots their enemies.

A tribal robot is capable of attacking enemies and thereby converting them to their own tribe. As a consequence, tribes can be quite aggressive as they compete for survival in a robot world.

Robot Actions

Each tribal robot is capable of doing one (but only one) of the following things during its turn:

  • Moving one square forward into an empty floor space.
  • Spinning 90 degrees without moving.
  • Spinning 180 degrees without moving.
  • Attacking — “hacking” — an enemy robot immediately in front of it. (See below.)

Hacking is automatic whenever a tribal robot sees an enemy, but how a tribal robot moves about and spins depends on its tribe. Each tribe has its own program, written in the RoboSpeak language. All members of the tribe follow this program, which defines the sequences of actions that those robots take. See the documentation of class Tribe for details about RoboSpeak.

Hacking

A tribal robot starts its turn by looking at whether there is an enemy robot in front of it. An enemy robot is any robot that is not a member of the same tribe. If there is an enemy robot in the square right in front of the acting tribal bot, then the tribal bot attacks it by “hacking”. Hacking is a kind of brainwashing: it converts the enemy robot into a member of the acting robot’s tribe. The victim will begin executing its new tribe’s RoboSpeak program from a line determined by the hacking robot.

Memory

Each tribal bot has some limited memory resources available, which it draws on when it executes the tribe’s RoboSpeak program. A tribal bot remembers the following information:

  • Where it is: a tribal bot knows which robot body it is controlling, just like any other RobotBrain does, and can therefore determine its location and facing.

  • Where its allegiances lie: a tribal bot knows which tribe it belongs to.

  • What it’s supposed to do next: a tribal bot knows which instruction in its tribe’s RoboSpeak program it is supposed to execute next once it gets its next turn. (This allows the tribal bot to save the program position it is at when it ends a turn. It can later resume program execution where it left off the previous turn.)

  • Which way it’s turning: A tribal bot remembers if it’s supposed to be turning clockwise or counterclockwise when it next makes a turn.

  • Which subprogram calls have been made: A tribal bot has a call stack where it stores frames representing calls to RoboSpeak subprograms. Each such frame is represented by a Frame object. This enables the tribal bot to execute subprograms and return to the correct line in the tribe’s RoboSpeak program whenever the end of a subprogram is reached.

  • The contents of four memory slots (or “registers”) called mem, mem2, radar and hackline. The mem and mem2 slots can be used for any purpose. The intended use of radar is to store the readings produced by the robot’s radar. The hackline slot is meant for configuring hacked robots. See the text below and class Tribe for more details.

Sensors

A tribal bot is (only) capable of seeing the single square directly in front of it. It can determine what there is in that square, but no further.

A tribal bot also has a radar that it can use either short-range to determine how many enemies or friends are located within exactly two steps of the acting robot, or long-range to count the memberships of entire tribes in the robot world. Again, more details can be found in the documentation for class Tribe.

Initial state

When a tribal bot is created, it starts executing its tribal program from the beginning (unless otherwise specified by a hacking robot). Its call stack is initially empty, its memory slots all contain the value 1, and it is considered to be turning clockwise.

As a new tribal bot brain “plugs into” a robot body, it changes the robot’s name. Each new member of a tribe receives a new, rather impersonal name of the form "Tribe#number", e.g., "Tiger#123". The ID number that comes after the hash (#) is unique for every member of a tribe.

Value parameters:
body

the robot body that the tribal bot brain will control

tribe

the tribe that this brain belongs to

See also:
class Object
trait Matchable
class Any

Value members

Concrete methods

def belongsTo(tribe: Tribe): Boolean

Determines whether this robot is a member of the given tribe.

Determines whether this robot is a member of the given tribe.

def boot(startLine: Int): Unit

Resets the robot’s state. All the robot’s memory slots are set to the initial value of 1. When it next gets a turn, the robot starts executing its tribal program from the given line onwards with an empty call stack.

Resets the robot’s state. All the robot’s memory slots are set to the initial value of 1. When it next gets a turn, the robot starts executing its tribal program from the given line onwards with an empty call stack.

Value parameters:
startLine

a line number in the robot’s tribe’s RoboSpeak program; the robot will execute the instruction on this line next

def callSubprogram(callingLine: Int): Unit

Starts the execution of a subprogram. That is to say, creates a new stack frame and places (“pushes”) it on top of the robot’s call stack.

Starts the execution of a subprogram. That is to say, creates a new stack frame and places (“pushes”) it on top of the robot’s call stack.

Value parameters:
callingLine

the line number of the call site. This number is to be stored on the stack so that program execution can resume at the correct location once the robot returns from the subprogram.

Determines the tribe of a robot. Returns the tribe of the given robot wrapped in an Option, or None if the robot is not a tribal bot.

Determines the tribe of a robot. Returns the tribe of the given robot wrapped in an Option, or None if the robot is not a tribal bot.

Returns the brains of all the robots near this one in a specific direction. More specifically, if D is the target direction and S is the square right next to this brain in direction D, this method returns all the robot brains that are either in S or right next to S (orthogonally, not diagonally). That list will always include this robot brain itself. (A total of one to five robot brains will thus be returned.)

Returns the brains of all the robots near this one in a specific direction. More specifically, if D is the target direction and S is the square right next to this brain in direction D, this method returns all the robot brains that are either in S or right next to S (orthogonally, not diagonally). That list will always include this robot brain itself. (A total of one to five robot brains will thus be returned.)

Value parameters:
dirNumber

a number between 0 and 3 that indicates the target direction relative to this robot’s current facing: 0 means the direction the robot is now facing; 1 means 90 degrees clockwise from that facing; 2 means the direction behind the robot, and 3 means 90 degrees counterclockwise from the facing.

Returns the brains of the enemy robot immediately in front of this robot, if there is one. Any robot that is not a friend is an enemy. The brains are wrapped in an Option, and None is returned if there is no robot in the square or if the robot that is there is a friend.

Returns the brains of the enemy robot immediately in front of this robot, if there is one. Any robot that is not a friend is an enemy. The brains are wrapped in an Option, and None is returned if there is no robot in the square or if the robot that is there is a friend.

See also:

Returns the brains of the friendly robot immediately in front of this robot, if there is one. The brains returned in an Option wrapper, and None is returned if there is no robot in the square or if the robot that is there is not a friend.

Returns the brains of the friendly robot immediately in front of this robot, if there is one. The brains returned in an Option wrapper, and None is returned if there is no robot in the square or if the robot that is there is not a friend.

See also:
def hack(): Unit

“Brainwashes” an enemy robot that is currently right in front of the acting robot, assuming there is one there. The victim of the hack will receive a new tribal bot brain of the enemy tribe, which replaces its old brain. Consequently, it will receive a new name and will start behaving differently than before.

“Brainwashes” an enemy robot that is currently right in front of the acting robot, assuming there is one there. The victim of the hack will receive a new tribal bot brain of the enemy tribe, which replaces its old brain. Consequently, it will receive a new name and will start behaving differently than before.

When its next turn comes, the victim will start executing the tribal RoboSpeak program of the attacker’s tribe, using its fresh brain. Anything that the victim’s old brain had in memory (stack frames, memory slot values, etc.) is lost; the victim is now considered to be turning clockwise.

The victim will start executing its new RoboSpeak program from the line indicated by the hacking robot’s hackline memory slot. The default value for this memory slot is 1, so unless the hacking robot’s tribal program specifies otherwise, the victim starts from the first line of RoboSpeak. (See the description of RoboSpeak in class Tribe for more details about how to affect hackline.)

If there is no enemy in front of the acting robot, this method does nothing.

See also:

Determines if the given robot is a friend of this robot. That is, determines if the given robot is a tribal robot of the same tribe (but is not this robot itself).

Determines if the given robot is a friend of this robot. That is, determines if the given robot is a tribal robot of the same tribe (but is not this robot itself).

Determines whether this robot considers the given robot tribe a threat. A tribal bot considers all non-pacifist tribes threats except its own.

Determines whether this robot considers the given robot tribe a threat. A tribal bot considers all non-pacifist tribes threats except its own.

Determines whether this robot considers the given robot to be a threat. A tribal bot considers all members of all non-pacifist tribes except its own to be threats. Non-tribal bots are never considered to be threats.

Determines whether this robot considers the given robot to be a threat. A tribal bot considers all members of all non-pacifist tribes except its own to be threats. Non-tribal bots are never considered to be threats.

Returns the brains of all the robots in the same robot world with this one.

Returns the brains of all the robots in the same robot world with this one.

override def mayAdvance(direction: CompassDir): Boolean

Checks the the square that neighbors the tribal bot in the given direction to see if it contains something that the bot considers an obstacle. To tribal bots, walls are obstacles but other robots aren’t. This behavior overrides the default implementation from class RobotBrain.

Checks the the square that neighbors the tribal bot in the given direction to see if it contains something that the bot considers an obstacle. To tribal bots, walls are obstacles but other robots aren’t. This behavior overrides the default implementation from class RobotBrain.

Definition Classes
def moveBody(): Unit

A tribal robot prioritizes hacking: if there is an enemy present directly in front, the acting bot spends its turn hacking it. (Exception: there are “pacifist” tribes that don’t hack.)

A tribal robot prioritizes hacking: if there is an enemy present directly in front, the acting bot spends its turn hacking it. (Exception: there are “pacifist” tribes that don’t hack.)

If no enemy is present, or if the tribal bot is a pacifist, the bot executes its tribe’s RoboSpeak program until it performs one of the actions that end its turn (or at least tries to perform such an action).

Essentially, this method is responsible for the tribal bot’s equivalent of a fetch-execute cycle during the robot’s turns. In the case of a tribal robot, this involves these two steps:

  1. Execute an instruction.

  2. Fetch the instruction that is to be executed next.

The cycle is repeated until the robot’s turn is over. The turn is over when the robot executes one of the instructions that ends its turn or when the end of the tribe’s program is reached.

Instructions are represented by Instruction objects. The tribal bot object’s job is made easy by the fact that the method execute in class Instruction not only executes the instruction (Step 1 above), but also returns the instruction that is to be executed next (Step 2 above).

Unless otherwise specified, a robot will resume execution where it left off the previous turn.

This method assumes that it is called only if the robot is not broken and not stuck.

See also:

Ends the execution of a subprogram. This means that the topmost stack frame is removed (“popped”) from the robot’s call stack.

Ends the execution of a subprogram. This means that the topmost stack frame is removed (“popped”) from the robot’s call stack.

This method fails with an IllegalStateException in case there are no frames on the stack.

Returns:

a line number that indicates where the subprogram call (whose execution ended) had been called from (and where execution should therefore resume)

Determines whether this robot considers the given robot to be “fodder”, that is, an unthreatening resource. A tribal bot never sees itself as fodder, but does see all other pacifist robots (including ones from its own tribe if it’s a pacifist) as fodder. All non-tribal bots are considered fodder by all tribal bots.

Determines whether this robot considers the given robot to be “fodder”, that is, an unthreatening resource. A tribal bot never sees itself as fodder, but does see all other pacifist robots (including ones from its own tribe if it’s a pacifist) as fodder. All non-tribal bots are considered fodder by all tribal bots.

Determines whether the square immediately in front of the robot contains an enemy robot.

Determines whether the square immediately in front of the robot contains an enemy robot.

See also:

Determines whether the square immediately in front of the robot is an empty floor space.

Determines whether the square immediately in front of the robot is an empty floor space.

Determines whether the square immediately in front of the robot contains an friendly robot.

Determines whether the square immediately in front of the robot contains an friendly robot.

See also:

Determines whether the square immediately in front of the robot is an unpassable obstacle.

Determines whether the square immediately in front of the robot is an unpassable obstacle.

def setSlotValue(slotName: String, newValue: Int): Map[String, Int]

Sets the contents of a named memory slot. The new value replaces the old value stored in the slot

Sets the contents of a named memory slot. The new value replaces the old value stored in the slot

Returns the brains of all the robots within two steps of this one. (See Tribe for a definition of “steps”.)

Returns the brains of all the robots within two steps of this one. (See Tribe for a definition of “steps”.)

def shout(lineNumber: Int): Unit

“Shouts” to all friendly robots nearby. The message reaches all the friendly robots that are within the robot’s short radar: this is equivalent to talking to each of those other robots.

“Shouts” to all friendly robots nearby. The message reaches all the friendly robots that are within the robot’s short radar: this is equivalent to talking to each of those other robots.

Value parameters:
lineNumber

a new line number for the recipients (see talk)

See also:
def slotValue(slotName: String): Int

Returns the contents of a named memory slot.

Returns the contents of a named memory slot.

def spin(): Unit

Spins the robot 90 degrees in whichever direction it is currently turning.

Spins the robot 90 degrees in whichever direction it is currently turning.

See also:

Switches the spinning direction of the tribal bot. If it was previously set to turn clockwise, it now turns counterclockwise, and vice versa. This method does not actually turn the robot, it only affects future spins.

Switches the spinning direction of the tribal bot. If it was previously set to turn clockwise, it now turns counterclockwise, and vice versa. This method does not actually turn the robot, it only affects future spins.

See also:
def talk(lineNumber: Int): Unit

“Talks” to a friendly robot that is currently right in front of the acting robot, if there is a friendly robot there. In practice, this means that the talking robot gives an order to its friendly “listener”, causing the listener to move to the given line number in the two robots’ shared tribal program. The listener is “rebooted” and starts at the given line number with a clean slate, as if it had just been created.

“Talks” to a friendly robot that is currently right in front of the acting robot, if there is a friendly robot there. In practice, this means that the talking robot gives an order to its friendly “listener”, causing the listener to move to the given line number in the two robots’ shared tribal program. The listener is “rebooted” and starts at the given line number with a clean slate, as if it had just been created.

If there is no friendly robot in front of the acting robot, this method does nothing.

Value parameters:
lineNumber

a line number referencing the RoboSpeak program of the two friends’ shared tribe

See also:

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

Returns the robot’s name. If the name has been set to the empty string or contains only whitespace, returns "Incognito" instead.

Returns the robot’s name. If the name has been set to the empty string or contains only whitespace, returns "Incognito" instead.

Inherited from:
RobotBrain
def name_=(newName: String): Unit

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
override def toString: String

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
Inherited from:
RobotBrain

Returns the world that this robot is located in.

Returns the world that this robot is located in.

Inherited from:
RobotBrain

Concrete fields