I have to create a 20 x 20 array (turtle program), I am sure you guys get these alot.. I have been working on this for way too long and I am not getting it.. I have read the chapter again and still am lost.. here is the code I have so far. I can't figure out where and how to get it to show the menu options so you can tell what you have the option of hitting to get the "turtle" to go where you want it and also how to display the grid after the turtle has moved... please help.. thank you,

#include <iostream>
#include <string>

using namespace std;

int penup(int &pen);
int pendown(int &pen);
int left_turn(int &dir);
int right_turn(int &dir);
int move(int &current_y,int &current_x);
int getcommand(int &command);

const int y=20;
const int x=20;
int dir=1;
int floor[y][x];
int current_y=0;
int current_x=0;
int pen=1;
int moves;
int command;
int i;
int j;


int main()
{

int again=1;

if(again==1)
{
int dir=1;
int floor[y][x];
int current_y=0;
int current_x=0;
int pen=1;
}


for(i=0;i<=y;++i)
{
for(j=0;j<=x;++j)
{
floor[i][j]=0;
}
}


while(again!=2)
{
do
{
if(pen==1)
{
cout << "current pen position is up!" << endl;
}
else
{
cout << "current pen position is down!" << endl;
}
if(dir==0)
{
cout<< "current direction is up!" << endl;
}
else if(dir==1)
{
cout << "current direction is right!" << endl;
}
else if (dir==2)
{
cout << "current direction is down!" << endl;
}
else if(dir==3)
{
cout << "current direction is left!" << endl;
}
cout << "current position are: (" << current_x << " ," <<
current_y << ")\n\n";

cout << "please enter your command: ";
cin >> command;
cout << "\n\n";

getcommand(command);


}while(command!=9);

cout << "do you want to draw again? (1=yes , 2=no)";
cin >> again;

if(again!=1||2)
{
while(again!=1||2)
{
cout << "That was an incorrect input!\n" << "do you want to draw again? (1=yes , 2=no)";
cin>>again;
}
}


}
return 0;

}

int getcommand(int &command)
{
if (command==1)
{
penup(pen);
}
else if (command==2)
{
pendown(pen);
}
else if (command==3)
{
right_turn(dir);
}
else if (command==4)
{
left_turn(dir);
}
else if (command==5)
{
cout << "please enter how many moves you want: ";
cin >> moves;
move(current_y,current_x);
}
else if (command==6)
{
system("cls");
for(i=0;i<=y;++i)
{
for(j=0;j<=x;++j)
{
if(floor[i][j]==0)
{
cout << " ";
}
else if(floor[i][j]==1)
{
cout << "*";
}
}
cout << endl;
}
}
else if (command==9)
{
}

else
{
cout << "you did not put a correct command. please try again.";

}
return command;
}


int penup(int &pen)
{
pen=1;

return pen;
}
int pendown(int &pen)
{
pen=2;

return pen;
}
int left_turn(int &dir)
{
--dir;
if(dir<0)
{
dir=3;
}
return dir;
}
int right_turn(int &dir)
{
++dir;
if(dir>3)
{
dir=0;
}
return dir;
}
int move(int &current_y,int &current_x)
{
if(pen==1)
{
if(dir==0)
{
while((moves>0)&&(current_y>=0))
{
--current_y;
--moves;
}
}
else if(dir==1)
{
while((moves>0)&&(current_x<=19))
{

++current_x;
--moves;
}
}
else if(dir==2)
{
while((moves>0)&&(current_y<=19))
{

++current_y;
--moves;
}
}
else if(dir==3)
{
while((moves>0)&&(current_x>=0))
{

--current_x;
--moves;
}
}
}
else if(pen==2)
{
if(dir==0)
{
while((moves>0)&&(current_y>=0))
{

floor[current_y][current_x]=1;
--current_y;
--moves;
}
}
else if(dir==1)
{
while((moves>0)&&(current_x<=19))
{

floor[current_y][current_x]=1;
++current_x;
--moves;
}
}
else if(dir==2)
{
while((moves>0)&&(current_y<=19))
{

floor[current_y][current_x]=1;
++current_y;
--moves;
}
}
else if(dir==3)
{
while((moves>0)&&(current_x>=0))
{

floor[current_y][current_x]=1;
--current_x;
--moves;
}
}

}
return current_y,current_x;
}

Recommended Answers

All 4 Replies

I'd put a call to a function called showMenu() in getCommand() before doing your sequential if/elses. Then you can write out your menu in showMenu().

you can't do use the logical OR operator like this:

if(again!=1||2)

while(again!=1||2)

It has to be:
if(again != 1 || again != 2)

while(again != 1 || again != 2)

Once you have the pen down so you mark any changes you place the int representing the turtle at the floor[current_x][current_)y] coordinate. Then use nested for loop to display each int in the board, placing newline char strategically to get a board appearance as opposed to a big long line.

Member Avatar for iamthwee

Wow your indentation is horrendous. How do you expect to fix any problems with a style like that? :eek:

return current_y,current_x;

You can only return one value from a function. Use [reference] parameters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.