Saludos!
Código
#include <iostream> #include <iomanip> using namespace std; const int floor_size = 20; void mainMenu(void); void turtleGraphics(int [][floor_size], int []); int main(void) { const int max_command = 256; int floor[floor_size][floor_size] = {{false},{false}}; int command[max_command] = {false}; mainMenu(); cout << "\nEnter commands(max " << max_command << "): "; int i = 0; while (command[i - 1] != 9){ cin >> command[i]; i++; } cout << "\n" << endl; turtleGraphics(floor, command); return 0; } void mainMenu(void) { cout << "*****" << setw(20) << "Turtle Graphics" << setw(10) << "*****" << endl; cout << "\nCommand" << setw(20) << "Meaning" << endl; cout << setw(7) << "1" << setw(20) << "Pen up" << endl; cout << setw(7) << "2" << setw(20) << "Pen down" << endl; cout << setw(7) << "3" << setw(20) << "Turn right" << endl; cout << setw(7) << "4" << setw(20) << "Turn left" << endl; cout << setw(7) << "5,10" << setw(20) << "Move forward 10" << "\n" << setw(27) << "spaces (or a number" << "\n" << setw(27) << "other than 10)" << endl; cout << setw(7) << "6" << setw(20) << "Print the" << "\n" << setw(15) << floor_size << "-by-" << floor_size << " array" << endl; cout << setw(7) << "9" << setw(20) << "End of data" << "\n" << setw(27) << "(sentinel)" << endl; } void turtleGraphics(int flr[][floor_size], int cmd[]) { int coord_x = 0, coord_y = 0, move = 0, row, column, i = 0; bool pen = false; while (cmd[i] != 9){ //Uncoment next line to show coord_x and coord_y //cout << "X: " << coord_x << "\nY: " << coord_y << endl; switch(cmd[i]){ case 1: //Don't write pen = false; break; case 2: //Write pen = true; break; case 3: //Turn rigth if (move == 3) move = 0; else move++; break; case 4: //Turn left if (move == 0) move = 3; else move--; break; case 5: // Move turtle i++; switch(move){ //Nested switch (control direction) case 0: //Move down if ((cmd[i] + coord_y) > floor_size) //Check if it is in floor limits cmd[i] = floor_size - coord_y - 1; //If not, set move to the limit for (row = coord_y; row < (cmd[i] + coord_y); row++) for (column = coord_x; column < coord_x + 1; column++) flr[row][column] = pen; coord_y = row; break; case 1: //Move rigth if ((cmd[i] + coord_x) > floor_size) //Check if it is in floor limits cmd[i] = floor_size - coord_x - 1; //If not, set move to the limit for (row = coord_y; row < coord_y + 1; row++) for (column = coord_x; column < (cmd[i] + coord_x); column++) flr[row][column] = pen; coord_x = column; break; case 2: //Move up if (cmd[i] > coord_y) //Check if it is in floor limits cmd[i] = floor_size - 1; //If not, set move to the limit for (row = coord_y; row > (coord_y - cmd[i]); row--) for (column = coord_x; column < coord_x + 1; column++) flr[row][column] = pen; coord_y = row; break; case 3: //Move left if (cmd[i] > coord_x) //Check if it is in floor limits cmd[i] = floor_size - 1; //If not, set move to the limit for (row = coord_y; row < coord_y + 1; row++) for (column = coord_x; column > (coord_x - cmd[i]); column--) flr[row][column] = pen; coord_x = column; break; default: ; } //End nested switch break; case 6: // Print floor for (int i = 0; i < 20; i++){ for (int j = 0; j < 20; j++){ if (flr[i][j] == true) cout << setw(2) << "*"; else cout << setw(2) << " "; } cout << endl; } break; default: ; } i++; } }