# LightBot 2

One of the main themes of this course is to think computationally in order to figure out how to solve complex problems using principles from computer science. To introduce this main theme, we will play an online game called LightBot 2. The goal of LightBot 2 is to program a virtual robot to light one or more square on a floor that may be uneven or have other surprises.

Light-Bot 2 contains a set of standard levels divided into four categories: basic, recursion, conditionals, and expert. If you have not already, you should go and play the first few levels in the basic, recursion, and conditionals categories of Light-Bot 2 before proceeding.

The rest of this document contains:

### Rules for Programming Lightbot

In the game, the Lightbot is programmed by arranging icons representing discrete actions onto a grid. The possible actions are:

 step right left jump light f1 f2 break

• step - Lightbot moves forward one step if this does not take it off the grid and the next tile is at the same level as the current tile.

• right - Lightbot turns right (clockwise) 90 degrees but stays on the current tile.

• left - Lightbot turns left (counter-clockwise) 90 degrees but stays on the current tile.

• jump - Lightbot jumps forward one step if the next tile is either one level higher or any number of levels lower than the current tile.

• light - Lightbot lights up the tile it is standing on if it has a deep blue color. If it is on a green tile, this causes Lightbot to "teleport" to another green tile as shown on the grid. If it is on a blue, purple, or orange tile, then its body color changes to that color. If it already that color, it changes its body color back to gray.

• f1 - Lightbot starts executing the commands shown in the Function 1 area. Once it is done, it returns back to where it left off in the command sequence (just after the f1 command).

• f2 - Lightbot starts executing the commands shown in the Function 2 area. Once it is done, it returns back to where it left off in the command sequence (just after the f2 command).

• break - Lightbot stops execution of the current method and returns back to where it left off in the previous method it was running. If it executes a break in main, then Lightbot stops.

A command (left, right, jump, etc.) can be "colored" so that it only occurs if Lightbot is the given color when it tries to execute that command. For example, the command

will cause Lightbot to jump only if it is currently blue. Otherwise, Lightbot doesn't jump. (You can create a colored command by first dragging the command into the robot's program area and then clicking the multicolor box and clicking the command until it turns into the color you want.)

After the player finishes building the program, they press the "Run" button and watch Lightbot operate under the control of the program. Commands are grouped into three procedures: a "main method", a "function 1", and a "function 2". When executing a procedure Lightbot, generally, executes each command of the procedure left-to-right, top-to-bottom. That is, Lightbot first performs the left-most action in the top row of that procedure, then proceeds performing the actions left-to-right within each row, moves to the left-most command of the next lower row after completing each row, and finally stops when the end of the procedure is reached. (The "f1", "f2" and "break" commands alter the order of execution, but even when such commands are present, this left-to-right, top-to-bottom ordering defines each procedure's "command sequence".)

### Representing Lightbot Programs Textually

Although programs can be written in a variety of notations — including graphical ones, like in the Light-Bot online game — it is most common to use represent programs textually. That is, most programs are represented as a sequence of letters numbers, spaces, and other character symbols, arranged according to particular rules that define what sequences are allowed and what they mean. We will see this later for programs written in Ruby, but we can also rewrite our Light-Bot programs in a textual form.

For example, consider the Lightbot puzzle with its solution program and the corresponding text file shown in Figure 1 below.

It is in the textual form that you are required to submit your solutions to Lightbot puzzles. Specifically, you should create a plain text file (ending with an extension of .txt) for each puzzle consisting of four parts (a header, a main method, a function 1, and a function 2). These parts should be separated by blank lines.

The header will consist of three lines. The first of which, will begin with "Name:" and followed by your name. The second will have "Course: 15-110". The third will have "Section: " followed by the letter for your lab section.

For each procedure, begin with a line containing "main:", "f1:", or "f2:", respectively, and then continue with a list of commands for that procedure. (If there are no commands for f1, put a blank line immediately after the "f1:", and continue with the next procedure. If there are no commands for the f2 procedure, simply end the file after the "f2:".) The commands for each procedure should be listed one per line so that reading them top-to-bottom results in the same command sequence as reading the commands left-to-right, top-to-bottom in the Light-bot game. Commands that would be colored gray in the Light-bot game should consist of a single word in all lowercase characters: "step", "right", "left", "jump", "light", "f1", "f2", or "break". Commands that would be colored in the Light-bot game (indicating an action that should only be performed when Lightbot is that color) should be written as three words on a single line. These should be written using one of the command words, followed by the word "if", followed by the color, either "blue", "orange", or "purple". For example, the command-icon:

would be written in this textual form as the line "jump if blue".

 ```Name: Jeffery von Ronne Course: 15-110 Section: Z main: f1 left f2 left left step light step light f1: light step if purple jump if blue break if orange f1 f2: step light step light right f2 if orange ```
Figure 1 A Lightbot game solution and its corresponding text file

It is important to follow these formatting requirements precisely, because the course assistants will be using a testing program to automatically grade your Lightbot programs. This testing program will only be able to determine whether your program solves the puzzle if your program is correctly formatted.

### Testing the format of your Lightbot file

We have created a tool for checking for common problems in the formatting of Lightbot files. On an Andrew Linux machine (e.g., in the Gates Hall Cluster), the tool can be used with the following procedure:

1. Open up the "Terminal" program under "Applications". This will give you an shell in which you can type commands and the system will respond.

2. If you put your lightbot file in a directory other than your home directory, use the cd command to change the shell in the terminal window to use that directory as its "current directory."

The ls command can be used to list the files in the current directory. Use the ls command (type ls and then enter) to check whether you've changed the current directory to the one that contains your Lightbot file.

3. Execute the checker with the following command:

`~vonronne/public/15110/bin/lightbotchecker.py`

The checker will ask you to enter the file name for your lightbot file and the maximum number of commands allowed for main, f1 and f2 respectively. This is not the number of commands you used, but how many commands the puzzle allows you to enter for each method.

Once you enter the required information, the output will look like the following if your Lightbot file has no formatting errors:

```Enter filename: puzzle1.txt
Maximum commands allowed for main: 16
Maximum commands allowed for f1: 8
Maximum commands allowed for f2: 8

running light-bot checker
note: resolving problems early in the code may fix other problems,
so start at the top and go down

** main, f1, and f2 labels...

** Processing main
done checking main function

** Processing f1
done checking f1 function

** Processing f2
done checking f2 function

```

Otherwise it will give you an error message, indicating what kind of problem your code has.

NOTE: The checker tool does not check to see if your answer is correct. Instead, it only checks to see if you've formatted your file correctly based on the rules given above.

If you are not using the Andrew Linux environment, you may still be able to run the Lightbot format checker if you have Python installed on your computer. We, however, are unable to provide detailed instructions for using them on other computers.