1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
#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");
}
|