/***************************************************
Write a program that allows the user to enter a
sequence of "moves" and prints out their position
after the moves. Moves are:
left - means a 90 deg counter clockwise turn (in place)
right - means a 90 deg clockwise turn (in place)
step - means step forward 1 unit
stop - terminates the sequence of moves.
The initial position is (0,0) facing North (up).
So the sequence "left step step left step right stop"
would leave you at (-2,-1).
***************************************************/
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Initialize
int x, y;
string direction, next;
x = y = 0;
direction = "North";
cout << "Enter moves: ";
cin >> next;
// Loop processing the "next" command at each iteration
while(next != "stop")
{
if (next == "left" && direction == "North")
{
direction = "West";
}
else if (next == "left" && direction == "West")
{
direction = "South";
}
else if (next == "left" && direction == "South")
{
direction = "East";
}
else if (next == "left" && direction == "East")
{
direction = "North";
}
else if (next == "right" && direction == "North")
{
direction = "East";
}
else if (next == "right" && direction == "East")
{
direction = "South";
}
else if (next == "right" && direction == "South")
{
direction = "West";
}
else if (next == "right" && direction == "West")
{
direction = "North";
}
else if (next == "step" && direction == "North")
{
y = y + 1;
}
else if (next == "step" && direction == "West")
{
x = x - 1;
}
else if (next == "step" && direction == "South")
{
y = y - 1;
}
else if (next == "step" && direction == "East")
{
x = x + 1;
}
// Read next command
cin >> next;
}
// Print final position
cout << "Final position is "
<< '(' << x << ',' << y << ')' << endl;
return 0;
}