Turtle Graphics

I have the most of this program set up but I do not know how to set it up in two classes like I need to for the project.


The Logo language which is popular among elementary school children 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 our shapes as it moves, while the pen is up, the turtle moves about freely without writing anything. In this problem, you will simulate the operation of the turtle and create a computerized sketchpad as well.

Use a 20-by-20 array floor that is initialized to false. 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 below:

As the turtle moves with the pen down, set the appropriate elements of array floor to true. When the 6 command (print) is given, whether there is a true 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 graphic language.

Project requirement

Please develop two classes and separate the interface from implementation. One class called TurtleGraphics in which you at least implement the following functions:

+ function that allows the turtle turn right

+ function that allows the turtle turn left

+ function to get commands from user (using keyboard)

+ function to move pen

+ function to print the array to draw interesting shapes

Another class is called Main in which you create a TurtleGraphic object, call the get command function to get all the command and process each command accordingly. At the end, print out the array to draw shapes








#include <stdio.h>
#include <conio.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//-Prototypes-

int loadArray(int [],int );
void docommands(int [],int [][30],int );
void printArray(int [][30]);
void trnrgt(int *);
void trnlft(int *);
int moveit(int *,int ,int [],int [][30],int *,int *,int);
void doc(void);

void main(void)
{
int commands[100]={0};
int position[30][30]={{0},{0}};int count=0;

system("CLS");
printf("This is a turtle graphics program.");
doc(); //how to use the program
//ount=loadArray(commands,count); //count is how many numbers were entered
docommands(commands,position,count);
system ("PAUSE");

}
int loadArray(int array[],int temp){
int x;
for(x=0;;x++) {
temp++; //temp is the count
printf("Enter command : ");
scanf("%d",&array[x]);
if (array[x]==9)
break ;
if (array[x]==7 || array[x]==8){
printf("Invalid entry !!!Try Again\n");
x--;//erase what we just wrote
temp--;//decrement our count
}
if (array[x]<= 0 || array[x]>9){
printf("Invalid entry !!!Try Again\n");
x--;
temp--;
}
if (array[x]==5) {
scanf(",%d",&array[x+1]);
x++; //move to next element
temp++; //increae count
}
}
return temp;
}
void docommands(int array[],int ddarray[][30],int c){
int x,y,pendwn,count,post=1,bounds1=0,bounds2=0;
for (x=0;x<=c;x++){
switch (array[x]){
case 1: pendwn = 0;
break ;
case 2:pendwn = 1;
break;
case 3:trnrgt(&post);
break;
case 4:trnlft(&post);
break;
case 5:x=moveit(&post,x,array,ddarray,&bounds1,&bounds2,pendwn);
break;
case 6:printArray(ddarray);
break;
case 9:break;
}
}
}
void printArray(int array [][30]){
int x,col,rows;
for (rows=0;rows <30;rows++){
for (col=0;col<30;col++)
printf("%c" ,array[rows][col]);
printf("\n");
}
}
int moveit(int *post,int x,int array[],int ddarray[][30],int *bounds1,int *bounds2,int pendwn){
int y,b,bnds1,bnds2;
x++;
bnds1=*bounds1; //watch the bounds for going up and down
bnds2=*bounds2; //watch the bounds for going sideways
switch (*post){
case 1:
if (bnds2<30){
if (array[x]>29)
array[x]=29;
for(y=0;y<(array[x]);y++){
if (pendwn){
ddarray[bnds1][bnds2]=42;
bnds2++;
}
else {
ddarray[bnds1][bnds2]=32;
bnds2++;
}
}
}
break;
case 2: {
if (array[x]>29)
array[x]=29;
for (y=(array[x]);y>0;y--){
if (bnds2>0){
if (bnds1>=30)
bnds1=19;
if (pendwn){
ddarray[bnds1][bnds2]=42;
bnds2--;
}
else {
ddarray[bnds1][bnds2]= 32;
bnds2--;
}
}
else
break;
}
break;
case 3:{
if (array[x]>29)
array[x]=29;
for (y =0;y<(array[x]);y++){
if (bnds1>0){
if (pendwn){
ddarray[bnds1][bnds2]=42;
bnds1--;
}
else{
ddarray[bnds1][bnds2]=32;
bnds1--;
}
}
}
break;
}
case 4:{
if (array[x]>29)
array[x]=29;
for (y=0;y < (array[x]);y++){
if (bnds2>29)
bnds2=19;
if (pendwn){
ddarray[bnds1][bnds2]= 42;
bnds1++;
}
else{
ddarray[bnds1][bnds2]= 32;
bnds1++;
}
}
break;
}
}
}
*bounds1=bnds1;
*bounds2=bnds2;
return x;
}
void trnlft(int *post){
switch (*post){
case 1:*post = 3; // 1 = east
break;
case 2:*post = 4; // 2 = west
break;
case 3:*post = 2; // 3 = north
break;
case 4:*post = 1; //4 = south
break;
}
}
void trnrgt(int *post){
switch (*post){
case 1: *post = 4;
break;
case 2: *post = 3;
break;
case 3: *post = 1;
break;
case 4: *post = 2;
break;
}
}
void doc (void){
printf("This program will draw a series of * with the commands that ");
printf("are entered. \nHere are the commands : \n\n");
printf("1 pushes your pen up meaning leaving no traces.\n");
printf("2 puts the pen down to leave traces.\n");
printf("3 turns right.\n");
printf("4 turns left.\n");
printf("5 moves after entering a 5 you have to specify the number of moves.\n");
printf(" for example : 5,10 you have to put the coma before the number.\n");
printf("6 prints the result on the screen.\n");
printf("9 ends the program.\n\n");
}
Topic archived. No new replies allowed.