Hi everyone,
I have a question regarding reading commands from an array for a turtle graphics program.
5, x - Move forward x number of spaces (x being the next number in the array, ie. 5, 5)
4 - Turn right
9 - End commands (sentinel value)
If you have this array:
int commands[] = { 5, 5, 4, 5, 9, 9 }
These commands cause the turtle to move 5 spaces in whatever direction he's facing (the first 5 invokes the move command, while the next 5 in the array tells the turtle how many spaces to move). Then the next command is 4 which tells the turtle to turn right. Then the following 5, 9 tells the turtle to move 9 spaces forward.
Now, my problem is I have a for loop that reads these commands:
void TurtleGraphics::processTurtleMoves( const int commands[] )
{
for ( int cmd = 0; commands[ cmd ] != 9; cmd++ )
{
switch ( commands[ cmd ] )
{
case 4: // turn right
tDirection += 3;
if ( tDirection > 12 )
{
tDirection -= 12;
}
break;
case 5: //move forward x number of spaces
int steps = commands[ cmd++ ];
for ( int stepsTaken = 0; stepsTaken < steps; stepsTaken++ )
{
switch ( tDirection )
{
case 3: // turtle is facing right
if ( penPos == 1 )
{
m_Floor[ tRow ][ tCol ] = 1;
}
tCol++
break;
case 6: // turtle is facing down
if ( penPos == 1 )
{
m_Floor[ tRow ][ tCol ] = 1;
}
tRow++;
break;
case 9: // turtle is facing left
if ( penPos == 1 )
{
m_Floor[ tRow ][ tCol ] = 1;
}
if ( tRow-- >= 0 )
{
cout << "Out of bounds" << endl;
}
tRow--;
break;
case 12: // turtle is facing up
if ( penPos == 1 )
{
m_Floor[ tRow ][ tCol ] = 1;
}
tCol++;
break;
}
}
break;
case 9: // end of data (sentinel)
break;
}
}
This code is simplifiied to just the area I'm confused about. I don't get how to read in the "second" command when case 5 (move forward) is chosen. I'm thinking I have to increment cmd so the read in command is the digit after the current one, and then read the value in cmd + 1 and use this value to process the number of steps the turtle should take.
So the question is:
How do you read in the second value in the commands 5, 5 and then process the next command, 4, and then 5, 9, and then 9 to end? Help please! ^^