XY position determination

Dec 13, 2008 at 9:21am
I have this simple text bases robot program that creates a robot and enables it to move, turn, etc. My problem is that I cant quite get the XY position of the robot. I created an At() function that determines the XY position of the robot but I dont know what to put in the function. The function is supposed to be of type INT and accepts the X and Y fields of the robot as parameters. Can anyone help me fix this problem? It would be a great help. Thanks.
Dec 13, 2008 at 10:51am
I dont completly get your question...

If the program is text-based, the x and y position is just what you make from them. You could simply use two integers, one for X and the other for Y, and add/substract to them when the player moves. Or you could create a 'board', a twodimensinal array of type bool (or another type) where one of the 'squares' in it is true: the position of the player.

However, I dont see the need of XY positions in a text-based game...?
Dec 13, 2008 at 11:48am
Yeah that's the thing, it's supposed to be text-based but we need to determine the position of the robot. I've already used to integers for the positioning, what I need is a function that can determine where the robot is exactly, like if the robot is at x=5, y= 5; the function will return (5,5). That's my problem because my function doesn't return the position of the robot.
Dec 13, 2008 at 11:57am
So you got a function that accepts two values, eg 5 and 7, and then returns (5,7)? As integer?? That's impossible, since it should hold two values. Maybe an array of integers? A string?

Can you post some code? The function maybe?
Dec 13, 2008 at 12:19pm
Well our instructor told us to create a function called At(). The function code is:

int At(int x, int y);

Now i've been rattling my brains out on how it can return a value.
Dec 13, 2008 at 12:28pm
That doesnt make any sense... Even if it would be possible, wy would you want a function that returns the two values you pass to it? Are you sure you got the assigment right? Maybe it should only display the coördinates, and is it of type void? In what context do you call the function?
Dec 13, 2008 at 12:35pm
I've already solved how to display the coordinates. The function is for checking if the robot has reached the pre-defined goal. Here is a sample main method:

void main(){
clrscr();

Robot r1("MrChubbles", random(10), random(10), random(4));
Robot r2("Super Cool", random(10),random(10),random(4));

r1.Print();
r2.Print();
int goalx = 7, goaly = 9;
cout<<"\nGoal is "<<goalx<<" and "<<goaly<<endl;
while(!r1.At(goalx,goaly) || !r2.At(goalx, goaly))
{
r1.Step();
r1.Print();
r2.Step();
r2.Print();
getch();
}
getch();
}

// The X and Y positions are those in random, the last integer is the direction it is facing. The At() function is supposed to check if the 2 robots has reached the goal (goalx,goaly).
Dec 13, 2008 at 12:49pm
Ah! Now I see!

The function souldnt return the coördinates. It should only tell or the program or the robot has reached its goal. All non-zero values are true; zero is false. If the robot hasn't reached its goal yet, it should return zero (wich would become false in the condition, wich, because of the '!', would become true, so the body of the while-statement would keep on executing). If it has reached its goal, it should return non-zero.
For example, the function may look like this:
1
2
3
4
5
6
7
8
int Robot::At(int goalx, int goaly)
{
    /* let's say x and y are membervariables of class Robot */
    if ( x == goalx && y == goaly)
        return true; //will become 1 when used as integer
    else
        return false; //will become 0 when used as integer
}


I would have used bool for the type of the function, but this is possible to. Maybe there are different ways to reach the goal, eg '1' means 'Has reached main goal' and '2' means 'Has reached secundairy goal'.
Dec 13, 2008 at 1:00pm
I tried the function you gave me and it generated good results. Thanks a lot for the help.
Topic archived. No new replies allowed.