Deitel & Deitel, C How to Program 6th Edition, Exercise 6.23 (Turtle Graphics)

/*6.23 (Turtle Graphics) The Logo language, which is particularly popular among personal computer
users, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks
around the room under the control of a C program. The turtle holds a pen in one of two positions,
up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the
turtle moves about freely without writing anything. In this problem you’ll simulate the operation of
the turtle and create a computerized sketchpad as well.
Use a 50-by-50 array floor which is initialized to zeros. Read commands from an array that
contains them. Keep track of the current position of the turtle at all times and whether the pen is
currently up or down. Assume that the turtle always starts at position 0, 0 of the floor with its pen
up. The set of turtle commands your program must process are shown in Fig. 6.24. Suppose that
the turtle is somewhere near the center of the floor. The following “program” would draw and print
a 12-by-12 square:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the
6 command (print) is given, wherever there is a 1 in the array, display an asterisk, or some other
character you choose. Wherever there is a zero, display a blank. Write a program to implement the
turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting
shapes. Add other commands to increase the power of your turtle graphics language.
Command Meaning
1 Pen up
2 Pen down
3 Turn right
4 Turn left
5,10 Move forward 10 spaces(or a number other than 10)
6 Print the 50-by-50 array
9 End of data (sentinel)
*/


#include <stdio.h>

enum face { UP, RIGHT, DOWN, LEFT };
enum pen { OFF, ON };
enum commandos { EMPTY, PENUP, PENDOWN, TURNRIGHT, TURNLEFT, GO, PRINT, OUT };

int main() {
int i, x = 0, y = 0, k;
int floor[50][50] = { 0 };
enum commandos commands[] = {PENDOWN, GO, 5, TURNRIGHT, GO, 5, TURNLEFT, GO, 5, TURNRIGHT, GO, 20, PRINT, OUT};
enum face looks = RIGHT;
enum pen pens = OFF;

for (i = 0; commands[i] != OUT; i++) {
switch (commands[i]) {
case 1:
pens = OFF;
break;
case 2:
pens = ON;
break;
case 3:
looks++;
break;
case 4:
looks--;
break;
case 5:
i++;
switch (pens) {
case OFF:
switch (looks) {
case UP:
x -= commands[i];
break;
case RIGHT:
y += commands[i];
break;
case DOWN:
x += commands[i];
break;
case LEFT:
y -= commands[i];
break;
}
break;
case ON:
switch (looks) {
case UP:
for (k = 0; k < commands[i]; k++) {
x--;
floor[x][y] = 1;
}
break;
case RIGHT:
for (k = 0; k < commands[i]; k++) {
y++;
floor[x][y] = 1;
}
break;
case DOWN:
for (k = 0; k < commands[i]; k++) {
x++;
floor[x][y] = 1;
}
break;
case LEFT:
for (k = 0; k < commands[i]; k++) {
y--;
floor[x][y] = 1;
}
break;
}
break;
}
break;
case 6:
for (x = 0; x < 50; x++) {
for (y = 0; y < 50; y++)
printf("%d", floor[x][y]);
printf("\n");
}
break;
}
}
return 0;
}


Let me know in the comments if this page helped you somehow.

It is nice to know if my work helped someone, thanks :)

Comments

Post a Comment

Popular posts from this blog

Deitel & Deitel, C How to Program 6th Edition, Exercise 6.22 (Total Sales)

Deitel & Deitel, C How to Program 6th Edition, Exercise 5.47 (Craps Game Modification)