private static TurtleDirection NewDirection(TurtleDirection facing, TurtleCommands turnCommand) { if (turnCommand == TurtleCommands.TurnRight) { switch (facing) { case TurtleDirection.UP: return(TurtleDirection.RIGHT); case TurtleDirection.DOWN: return(TurtleDirection.LEFT); case TurtleDirection.LEFT: return(TurtleDirection.UP); case TurtleDirection.RIGHT: return(TurtleDirection.DOWN); default: return(facing); } } else { switch (facing) { case TurtleDirection.UP: return(TurtleDirection.LEFT); case TurtleDirection.DOWN: return(TurtleDirection.RIGHT); case TurtleDirection.LEFT: return(TurtleDirection.DOWN); case TurtleDirection.RIGHT: return(TurtleDirection.UP); default: return(facing); } } }
static void Execute(int[,] turtleProgram) { //a 20x20 array floor initialized to 0's int[,] floor = new int[20, 20]; char [,] displayFloor = new char [20, 20]; ClearFloor(floor); //turtle properties bool penDown = false; TurtleDirection facing = TurtleDirection.RIGHT; int xCoord = 0; int yCoord = 0; //control variables int moveCounter = 0; int commandIndex = 0; TurtleCommands nextCommand = (TurtleCommands)turtleProgram[commandIndex, 0]; while (nextCommand != TurtleCommands.End) { //execute command switch (nextCommand) { case TurtleCommands.PenUp: //raises the turtle's pen penDown = false; break; case TurtleCommands.PenDown: //lowers the turtle's pen penDown = true; break; case TurtleCommands.TurnRight: //turns the turtle right 90 degrees facing = NewDirection(facing, TurtleCommands.TurnRight); break; case TurtleCommands.TurnLeft: //turns the turtle left 90 degrees facing = NewDirection(facing, TurtleCommands.TurnLeft); break; case TurtleCommands.Move: //moves the turtle the indicated number of spaces switch (facing) { case TurtleDirection.UP: moveCounter = turtleProgram[commandIndex, 1]; //get number of spaces to move. This code is repeated for each of the directions. if (yCoord > 0) //check to see if at edge of floor { while (moveCounter > 0) //check for moves remaining { if (penDown) //check for pen down { floor[xCoord, yCoord] = 1; } if (yCoord > 0) //check if movement has caused turtle to reach end of floor. //required due to movement near end of loop { yCoord--; } moveCounter--; } } break; case TurtleDirection.DOWN: moveCounter = turtleProgram[commandIndex, 1]; if (yCoord < 19) { while (moveCounter > 0) { if (penDown) { floor[xCoord, yCoord] = 1; } if (yCoord < 19) { yCoord++; } moveCounter--; } } break; case TurtleDirection.LEFT: moveCounter = turtleProgram[commandIndex, 1]; if (xCoord > 0) { while (moveCounter > 0) { if (penDown) { floor[xCoord, yCoord] = 1; } if (xCoord > 0) { xCoord--; } moveCounter--; } } break; case TurtleDirection.RIGHT: moveCounter = turtleProgram[commandIndex, 1]; if (xCoord < 19) { while (moveCounter > 0) { if (penDown) { floor[xCoord, yCoord] = 1; } if (xCoord < 19) { xCoord++; } moveCounter--; } } break; default: Console.WriteLine("Something went wrong in Execute(move command)"); break; } break; case TurtleCommands.Print: //creates secondary array of characters that mirrors the floor and prints it for (int x = 0; x < floor.GetUpperBound(0); x++) { for (int y = 0; y < floor.GetUpperBound(1); y++) { if (floor[x, y] == 0) { displayFloor[x, y] = ' '; } else { displayFloor[x, y] = '*'; } } } for (int x = 0; x < displayFloor.GetUpperBound(0); x++) { for (int y = 0; y < displayFloor.GetUpperBound(1); y++) { if (x != 19) { Console.Write(displayFloor[x, y]); } else { Console.WriteLine(displayFloor[x, y]); } } } break; case TurtleCommands.End: Console.WriteLine("END OF PROGRAM"); break; default: Console.WriteLine("Something went wrong in Execute (unknown command)"); break; } commandIndex++; //move control variable nextCommand = (TurtleCommands)turtleProgram[commandIndex, 0]; //change which command is being evaluated } }