The most general kind of computation is defined by a Turing Machine, invented by the mathematician Alan Turing. The word "computable" means able to be computed by a Turing Machine. Modern computers and computer languges simulate finite Turing Machines. Karel J. Robot is no different. Actually we don't need the full power of Karel to simulate the operation of a Turing Machine. Actually there are many equivalent definitions of Turing Machines. Most universities have a course called something like Computability and Automata that explain this in detail. There are also several such books.
Here we will use the following characterization of a Turing Machine.
You can read more about Turing Machines at http://plato.stanford.edu/entries/turing-machine/.
The tape will be first street and first avenue, with the origin as the zero cell. A single Robot with an infinite number of beepers in its beeper bag will be the tape head. Its initial position is the origin. We read the current tape position by asking if there is a beeper on the current corner. We can either place a beeper (after clearing the corner) or just clear the corner to simulate writing a 1 or a blank. The program will consist of a set of mutually recursive methods defined by a set of rules to be given below. These methods represent both the states and the program. A robot changes its state by sending itself the message corresponding to that state.
First we need some infrastructure. These define how the "head" moves and how it writes on the tape. We will also show the finalState.
public void putBlank() { while(nextToABeeper()) pickBeeper(); } public void putOne() { putBlank(); putBeeper(); } public void moveLeft() { faceWest(); if (frontIsClear()) { move(); } else { turnRight(); move(); } } public void moveRight() { faceSouth(); if(frontIsClear()) { move(); } else { turnLeft(); move(); } } public void finalState() { turnOff(); }
With this infrastructure the states of the machine are a set of methods that might be called state1, state2, etc. The body of the method defines how the machine behaves in that state for any value on the tape. The first message sent to the robot is always state1 (the initial state). One of your states is the final state and it simply executes a turnOff instruction.
The bodies of all the other states consists of an if statement with an else part. The test is always on nextToABeeper. The if part represents seeing a 1 on the tape and the else part seeing a blank. Within each part the structure is identical with up to three statements in each part with the statements that appear always in the following order.
For example state 1 might look like:
public void state1() { if(nextToABeeper()) { putBlank(); moveRight(); state3(); } else { moveLeft(); state7(); } }
A Turing machine can run infinitely (never halt) or it can enter the final state and halt. Don't forget to provide a final state which isn't like these, but just sends turnOff.
Notes on implementation. You could create a TuringMachine class that extends Robot and has the infrastructure methods (along with turnRight, etc.) but no state methods except the finalState method. Then, to build an individual turing machine, create a new class that extends TuringMachine and provides the (other) state methods. Then create one of these at the origin with an infinite number of beepers and send it the state1 message. Of course you must create a world that has some beepers (perhaps) along first street and/or first avenue.
Last updated: October 12, 2003