__CLEANBOT__ How does it work?
-
CLEANBOT utilizes the BrainF Interpreter which uses 8 commands. The commands are a single character which are:
+ - , . [ ] < >
Let's compare each command with Fuze's code.
+ Command
The + command Increments the current Cell which the Pointer points to. Pointers are basically an index. So one could think of them as RAM[PTR] where the Pointer indexes RAM. So when we use the above command we perform the action in Fuze:RAM[PTR] += 1
Compared to BrainF we use + to perform that same action.
- Command
The - command decrements the current Cell. In Fuze:RAM[PTR] -= 1
As with the + command we only need - to perform this action.
, Command
In other BrainF interpreters there are different features that are incorporated into the language. One BrainF interpreter differs from another in terms of its implementation of its commands and the , is one of those implementations. In CLEANBOT the , command halts the interpreter and waits for the user to insert a number into RAM. After the number as been typed pressing ENTER will insert that number into RAM[PTR].. Command
Normally in a BrainF interpreter the period is used to print the contents of RAM[PTR]. In CLEANBOT however, this is changed to sending instructions to the robot to follow 5 commands. These commands are:1 -> Move robot forward 2 -> Turn the robot to the left 3 -> Turn the robot to the right 4 -> Pick up the Ascii characters around the map (Except #) 5 -> Drop a block (#) to hinder an enemy from catching the robot
<> Commands
The < and > commands are used to move the Pointer left (<) or right (>) across RAM. In Fuze the > command will be shown as:PTR += 1 RAM[PTR]
The same is done for the < command:
PTR -= 1 RAM[PTR]
[ Command
The [ command pushes the PC (Program Counter) to the STACK.
The Program Counter is pretty much a Pointer. Except this Pointer points to ROM where all the instructions are kept. When we write code for example:+++
This when it is translated into Bytecode will be inserted into ROM:
ROM = [+,+,+]
The PC extracts the instruction and looks to the next instruction.
In Fuze this will look like:PC = 0 ROM = [+,+,+] instruction = ROM[PC] PC += 1
Now with the explanation of the PC out of the way. Let's talk about the STACK. The Stack is a data structure which has the specified rules:
1) You can only push and pop data to and from the Stack. 2) You cannot insert data anywhere in the Stack, all data must be pushed or popped to and from the top of the Stack.
In CLEANBOT the stack is used to save the PC wherever a [ command is read.
] Command
The ] command is used to Pop the PC from the Stack and store it back into the PC itself. This is what make the VM jump to a specific location in ROM. However, the jumps only happen when and only when the current RAM[PTR] is greater than zero. This is equivalent to Fuze code:If RAM[PTR] > 0 then SP -= 1//This is called the Stack Pointer PC = STACK[SP] endif
In CLEANBOT this can help when writing repetitive code. An example of this loop command would be:
+++.--[.]
The above code example of BrainF in CLEANBOT increments RAM[0] three times and then send that value to the robot. Looking at the instructions of CLEANBOT above we see that 3 -> Turns the robot to the right. So now the robot is facing East as its starting position will always face North.
CODE EXAMPLE:
In the maps there is a 3x3 square of (#) symbolizing walls on the map. If we wanted to traverse around this square like wall the code below shows in detail how to code this:/* @ -> starting position .###. .#.#. .###. */ First we set up the robot's direction. We want the robot to move to the right and then circle the 3x3 square. +++. This code will turn the robot to the right one time. Since the robot's starting position is always North when we turn to the right the robot will now be facing East Now we set up our RAM slots 1 and 2. RAM[1] will hold the CLEANBOT command (1) to move forward while RAM[2] will hold the amount of times we need to move to reach the edge of the square. >+>++++ Now let us start the loop. [[-<.>]+++.+] Pay attention to the square brackets and notice that they have matching closing square brackets This code above will maneuver the robot around the 3x3 square for infinity.
I hope that this helps you guys in understanding how CLEANBOT works and will have fun playing this game.
-
Great explanation. It is starting to make sense now!
-
-
CLEANBOT is really something unique!