ask user set a direction or use default value (0,0,'n'). them make it move 100 times randomly.
I am now confuse on the loop inside my class. I don't know where should I add the loop. and also, the output shows my the values that what I have typed. Need your help !
output likes:
0===0---n
#include<iostream>
#include<string>
#include<iomanip>
#include<ctime>
using namespace std;
class bug
{
public:
bug();
bug(int x_pos, int y_pos, char direction);
void turn(char direction);
void move(int x, int y,char direction);
int get_X() const;
int get_Y() const;
char get_direction() const;
private:
int x;
int y;
char direction;
};
bug::bug(int x_pos, int y_pos, char d)
{
x = x_pos;
y = y_pos;
direction = d;
}
bug::bug()
{
x = 0;
y = 0;
direction = 'n';
}
void bug::turn(char direction)
{
int num = 1 + rand() % 3;
if (direction == 'n')
{
if (num == 1)
direction = 'e';
else if (num == 2)
direction = 'w';
else if (num == 3)
direction = 'n';
}
else if (direction == 'w')
{
if (num == 1)
direction = 'w';
else if (num == 2)
direction = 'n';
else if (num == 3)
direction = 's';
}
else if (direction == 's')
{
if (num == 1)
direction = 's';
else if (num == 2)
direction = 'w';
else if (num == 3)
direction = 'e';
}
else if (direction == 'e')
{
if (num == 1)
direction = 'e';
else if (num == 2)
direction = 'n';
else if (num == 3)
direction = 's';
}
}
void bug::move(int x, int y, char direction)
{
if (direction == 'n')
y = y + 1;
else if (direction == 'w')
x = x - 1;
else if (direction == 's')
y = y - 1;
else if (direction == 'e')
x = x + 1;
}
int bug::get_X() const
{
return x;
}
int bug::get_Y() const
{
return y;
}
char bug::get_direction() const
{
return direction;
}
void display(bug start)
{
cout << start.get_X()<<"==="<<start.get_Y() << "---"<<start.get_direction() << endl;
}
int main()
{
srand(time(0));
bug first;
int choice;
cout << "custom mode(1) or default mode(2) ? ";
cin >> choice;
if (choice == 1)
{
int x, y;
char dir;
cout << "the x axis: ";
cin >> x;
cout << "the y axis: ";
cin >> y;
cout << "the direction: ";
cin >> dir;
bug first(x, y, dir);
//first.turn(dir);
//first.move(x,y);
display(first);
}
else
{
for(int j =0;j<100;j++)
{
bug first;
//first.turn;
//first.move();
//for(int j =0;j<100;j++)
display(first);
}
}
system("pause");
}