how can i make this robot to moves

May 26, 2011 at 4:40am
no code
Last edited on Jun 12, 2011 at 12:59am
May 26, 2011 at 5:06am
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
//This is how I would approach this problem
class Robot {
	static MIN_X, MAX_X, MIN_Y, MAX_Y;
	int x, y;

 public:
	Robot() { x = 0; y = 0; }
	Robot(int x, int y) { this->x = x; this->y = y; }

	int getX() { return x; }
	int getY() { return Y; }

	bool move(int east, int south) {
		int _x = x + east, _y = y + south;
		if( MIN_X <= _x && _x <= MAX_X && MIN_Y <= _y && _y <= MAX_Y ) {
			x = _x;
			y = _y;
			return true;
		}
		return false;
	}

	bool moveNorth() { return move(0, -1); }
	bool moveSouth() { return move(0, 1); }
	bool moveEast() { return move(1, 0); }
	bool moveWest() { return move(-1, 0); }
	bool moveNorthEast() { return move(1, -1); }
	bool moveNorthWest() { return move(-1, -1); }
	bool moveSouthEast() { return move(1, 1); }
	bool moveSouthWest() { return move(-1, 1); }
};

Robot::MIN_X = 1;
Robot::MAX_X = 19;
Robot::MIN_Y = 1;
Robot::MAX_Y = 19;
May 26, 2011 at 5:30am
You might want to think about using a two dimensional array here.
std::string grid[10][10];

Also, why don't you create a class Robot to maintain the state of the Robot's movement. IE: x and y.

You could create a class for your grid which contains internal information about each room and
it's contents. Add methods to class Robot such as move(), displayLocation(), etc.

Take advantage of object oriented programming here, this is what it was designed for. :)
May 26, 2011 at 7:50am

nocode
Last edited on Jun 12, 2011 at 1:00am
May 26, 2011 at 10:24am
I would make two constant arrays of size 8, containing the horizontal and vertical movements for each of the 8 commands.
Then for each motion, just read the necessart move in x andY from the tables and add that toe the current position instead of having multiple if statements
May 27, 2011 at 1:37am
Can anyone rewrite my code in her/his owns way according to problem statement so that it easy for me to follow and understand
Thanks for your help and kindness
Last edited on May 27, 2011 at 1:40am
May 27, 2011 at 2:13am
I don't have the time (or will) to rewrite your whole project. I already gave you a usable class http://cplusplus.com/forum/beginner/43707/#msg236369 for your robot. Please check out the tutorial if you need to learn more about OOP. I can be more helpful if you try it yourself and run into a more specific problem.
Topic archived. No new replies allowed.