| CIT
594 Example RoboTalk Program Spring 2007, David Matuszek |
Here is a sample RoboTalk program. The program has the robot move to the bottom right corner, then collect all the fuel, armor, and zap guns it can find, finally depositing them in the top left corner (row = 1, column = 1, since all outside table cells contain "wall").
This program does not use RoboTalk methods. That is, it's a main program only.
turn south
move 100
turn east
move 100
turn around
holdingFuel = 0
holdingArmor = 0
holdingGun = 0
while row != 1 or column != 1 {
if seeing FUEL {
move distance
take FUEL
holdingFuel = holdingFuel + 1
}
if seeing ARMOR {
move distance
take ARMOR
holdingArmor = holdingArmor + 1
}
if seeing ZAP_GUN {
move distance
take ZAP_GUN
holdingGun = holdingGun + 1
}
if seeing WALL and facing west {
if row = 1 {
move distance - 1
}
else {
turn around
}
}
else {
if seeing WALL and facing east {
move distance - 1
turn north
move 1
turn west
}
}
}
repeat holdingFuel {
drop FUEL
}
repeat holdingArmor {
drop ARMOR
}
repeat holdingGun {
drop ZAP_GUN
}
|
Unfortunately, due to a problem in the grammar, obvious uses of and, or,
and
not did not work properly. I tried to write the program without
using these, but it got really ugly, so I decided to change the grammar (again).
If you want to use this program for testing, you can incorporate the changes
below, but you don't have to if you don't want to. Your code might be enough
like mine that you can just use the methods below without any changes; if
not, the changes should be simple ones.
As you can see, the change simply involves moving the code for seeing, smelling,
and facing from <condition> to <logical
factor>.
| Original | Corrected |
|---|---|
<condition> ::= <logicalCondition>
| "seeing" <thing>
| "smelling" <thing>
| "facing" <direction> |
<condition> ::= <logicalCondition> |
boolean isCondition() {
if (isLogicalCondition()) return true;
if (keyword("facing")) {
if (isDirection()) {
makeTree(2, 1);
return true;
}
else error("Missing \"Direction\" in logical condition.");
}
if (keyword("seeing") || keyword("smelling")) {
if (isThing()) {
makeTree(2, 1);
return true;
}
else error("Missing \"Thing\" in logical condition.");
}
return false;
} |
boolean isCondition() {
return isLogicalCondition();
} |
| Original | Corrected |
|---|---|
<logical factor> ::= <comparison> |
<logical factor> ::= <comparison>
| "seeing" <thing>
| "smelling" <thing>
| "facing"<direction> |
boolean isLogicalFactor() {
return isComparison();
} |
boolean isLogicalFactor() {
if (isComparison()) return true;
if (keyword("facing")) {
if (isDirection()) {
makeTree(2, 1);
return true;
}
else error("Missing \"Direction\" in logical condition.");
}
if (keyword("seeing") || keyword("smelling")) {
if (isThing()) {
makeTree(2, 1);
return true;
}
else error("Missing \"Thing\" in logical condition.");
}
return false;
} |