Ryan Bayne
asked on
Making Game Robot Teleport Randomly In Room Using Random Co-Ordinates
Been struggling with this one for a long long time and its driving me crazy. I'm trying to make a robot jump around the room randomly. It will hold the current co-ordinates until it finds new ones that doesnt have an object on them. While checking random co-ordinates it will be positioned to the side of the square. 0= up, 1=right, 2=down and 3= up. At the moment its working for UP only and I cant get it to start jumping about randomly. The positioning is on the square before the new co-ordinated one so this position will change depending on which way the robot faces.
Can anyone tell me anything seriously wrong with this code and help me out. Man its like 5am here in UK lol I been up all night with this. Heres my code...
import SE111aClasses.*;
public class Tut14_1 {
public static void main(String args[ ]) {
// make the objects
MyRobot robby = new MyRobot(); // robby of class MyRobot
Room room = new Room(9); // Room 9 has some obstacles
Picture picture = new Picture(room,robby);
GUI gui = new GUI(); // gui for main method
int i=0;
int j=0;
int k=0;
// two for loops to make robby change tile and encounter obstacles
for(i=1;i<=10;i++)
{
for (j=5;j<15;j++)
{
// move robby to the tile (i,j) if it is empty
robby.goTo(room,i,j);
picture.draw(room,robby);
}
}
for (k=0; k<10; k++)
{
// teleport robby 10 times around the room
robby.teleport(room);
picture.draw(room,robby);
System.out.println("Telepo rt request sent");
//gui.putText("pause"); // pause between each teleport
}
}
}
import SE111aClasses.*;
import java.util.Random;
public class MyRobot extends Robot {
GUI gui = new GUI();
// method to move robby from current position to position (x,y) if (x,y) is empty
// if the tile is full (i.e. contains an obstacle) robby should not move
// message displayed to show if robby is moving to square or unable to move
public void goTo(Room room, int x, int y)
{
int keepx=0; // variables to hold the robot's current position
int keepy=0;
int dir=0;
keepx=get_xpos(); // use the robot methods to get x and y positions
keepy=get_ypos();
dir = get_dir();
// setX(x); // use the robot methods to set the position of the robot
// setY(y-1); // to (x,y-1), one y position lower to enable look ahead
if (dir==0)
{setX(x);
setY(y-1);}
else if (dir==1)
{setX(x-1);
setY(y);}
else if (dir==2)
{setX(x);
setY(y+1);}
else
{setX(x+1);
setY(y);}
if (obstacle_ahead(room)) // if there is an obstacle ahead
{
setX(keepx); // put robot back to position before goTo method
setY(keepy);
System.out.println("Obstac le was ahead"); //confirms obastacle
}
else // put the robot in the correct, free y position
{
setX(x);
setY(y);
System.out.println("There was no Obstacle was ahead");//Confirms no obstacle
} // this method ONLY works for robby when he is facing up
}
public void teleport(Room room) //While obstacle ahead teleport to new location
{
int x=0;
int y=0;
do {
Random rand = new Random();
x = rand.nextInt(18) + 1;
System.out.println("Method (Teleport) Random X choosen =" + x);
y = rand.nextInt(18) + 1;
System.out.println("Method (Teleport) Random Y choosen =" + y);
} while (isObstacleInWay(room,x,y) );
}
public boolean isObstacleInWay(Room room, int x, int y)
{ System.out.println("isObst acleInWay check"); //Check when this runs
//int dir = get_dir();
//int keepx=0; // variables to hold the robot's current position
//int keepy=0;
//keepx=get_xpos();
// keepy=get_ypos();
//if ( (x != keepx) && (y !=keepy) )
//{
// return false;
//}
// else
//{
return true;
//}
}
} // end class myRobot
Can anyone tell me anything seriously wrong with this code and help me out. Man its like 5am here in UK lol I been up all night with this. Heres my code...
import SE111aClasses.*;
public class Tut14_1 {
public static void main(String args[ ]) {
// make the objects
MyRobot robby = new MyRobot(); // robby of class MyRobot
Room room = new Room(9); // Room 9 has some obstacles
Picture picture = new Picture(room,robby);
GUI gui = new GUI(); // gui for main method
int i=0;
int j=0;
int k=0;
// two for loops to make robby change tile and encounter obstacles
for(i=1;i<=10;i++)
{
for (j=5;j<15;j++)
{
// move robby to the tile (i,j) if it is empty
robby.goTo(room,i,j);
picture.draw(room,robby);
}
}
for (k=0; k<10; k++)
{
// teleport robby 10 times around the room
robby.teleport(room);
picture.draw(room,robby);
System.out.println("Telepo
//gui.putText("pause"); // pause between each teleport
}
}
}
import SE111aClasses.*;
import java.util.Random;
public class MyRobot extends Robot {
GUI gui = new GUI();
// method to move robby from current position to position (x,y) if (x,y) is empty
// if the tile is full (i.e. contains an obstacle) robby should not move
// message displayed to show if robby is moving to square or unable to move
public void goTo(Room room, int x, int y)
{
int keepx=0; // variables to hold the robot's current position
int keepy=0;
int dir=0;
keepx=get_xpos(); // use the robot methods to get x and y positions
keepy=get_ypos();
dir = get_dir();
// setX(x); // use the robot methods to set the position of the robot
// setY(y-1); // to (x,y-1), one y position lower to enable look ahead
if (dir==0)
{setX(x);
setY(y-1);}
else if (dir==1)
{setX(x-1);
setY(y);}
else if (dir==2)
{setX(x);
setY(y+1);}
else
{setX(x+1);
setY(y);}
if (obstacle_ahead(room)) // if there is an obstacle ahead
{
setX(keepx); // put robot back to position before goTo method
setY(keepy);
System.out.println("Obstac
}
else // put the robot in the correct, free y position
{
setX(x);
setY(y);
System.out.println("There was no Obstacle was ahead");//Confirms no obstacle
} // this method ONLY works for robby when he is facing up
}
public void teleport(Room room) //While obstacle ahead teleport to new location
{
int x=0;
int y=0;
do {
Random rand = new Random();
x = rand.nextInt(18) + 1;
System.out.println("Method
y = rand.nextInt(18) + 1;
System.out.println("Method
} while (isObstacleInWay(room,x,y)
}
public boolean isObstacleInWay(Room room, int x, int y)
{ System.out.println("isObst
//int dir = get_dir();
//int keepx=0; // variables to hold the robot's current position
//int keepy=0;
//keepx=get_xpos();
// keepy=get_ypos();
//if ( (x != keepx) && (y !=keepy) )
//{
// return false;
//}
// else
//{
return true;
//}
}
} // end class myRobot
> public boolean isObstacleInWay(Room room, int x, int y)
it appears to always return true, is that intened?
And once you have random x,y you don't appear to actually do anything with the new values
it appears to always return true, is that intened?
And once you have random x,y you don't appear to actually do anything with the new values
ASKER
I dont understand anything thats not coded similiar to the little Java I know.
Sorry but thanks for trying to help
Sorry but thanks for trying to help
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
isObstacleInTheWay returns obviously boolean true or false but I was just testing by forcing either or.
Can you post Room and Robot classes
ASKER
robby works without the GoTo and teleport methods. But he only moves UP/FORWARD and jumps over obstacles/walls once at the top of the screen he moves on to the next colum and starts again.
The program should really make him turn but I could decide where is best. Then thats where the facing comes. The numbers are used to guide the code into know where to place the robby however its like a temp move you wont see the robot actually doing that. Then it uses the code that is already there and already works, to check ahead and move to the square.
The program should really make him turn but I could decide where is best. Then thats where the facing comes. The numbers are used to guide the code into know where to place the robby however its like a temp move you wont see the robot actually doing that. Then it uses the code that is already there and already works, to check ahead and move to the square.
ASKER
Do you mean actually post the files? A link to download them?
ASKER
Thanks for your time by way its always apreciated Its like 6am i been doing this for hours!!! lol
ASKER
Objects I checked out your website. I cant remember seeing a website giving details on getting workexperiance which is someting I hear a lot of graduates have problems with when it comes to getting a job. pretty cool im impressed.
> public boolean isObstacleInWay(Room room, int x, int y)
This method should be calling some method in room to check the contents of the cell.
teleport should be comething like:
public void teleport(Room room) //While obstacle ahead teleport to new location
{
int x=0;
int y=0;
Random rand = new Random();
do {
x = rand.nextInt(18) + 1;
System.out.println("Method (Teleport) Random X choosen =" + x);
y = rand.nextInt(18) + 1;
System.out.println("Method (Teleport) Random Y choosen =" + y);
} while (isObstacleInWay(room,x,y) );
setX(x);
setY(y);
}
I'm assume you add 1 to x and y because cell coords start at 1 and not 0. If this is not the case it does not look like u should be adding 1.
This method should be calling some method in room to check the contents of the cell.
teleport should be comething like:
public void teleport(Room room) //While obstacle ahead teleport to new location
{
int x=0;
int y=0;
Random rand = new Random();
do {
x = rand.nextInt(18) + 1;
System.out.println("Method
y = rand.nextInt(18) + 1;
System.out.println("Method
} while (isObstacleInWay(room,x,y)
setX(x);
setY(y);
}
I'm assume you add 1 to x and y because cell coords start at 1 and not 0. If this is not the case it does not look like u should be adding 1.
ASKER
The coordinates do start from 1. The room is 18 squares wide.
And I understand that. Offcourse so if there is not an obstacle in the way it will set the co-ordinates. I've placing setX and SetY all over the place because I knew
they werent getting used.
Well thats a step forward FINALLY! Its amazing how no sleep and a little progress can make you feel like calling someone a genius when they help u lol (Thats u)
And I understand that. Offcourse so if there is not an obstacle in the way it will set the co-ordinates. I've placing setX and SetY all over the place because I knew
they werent getting used.
Well thats a step forward FINALLY! Its amazing how no sleep and a little progress can make you feel like calling someone a genius when they help u lol (Thats u)
ASKER
OK So I got him jumping about like a mad robot when I put the boolean method into play. NICE! something happened he landed on a wall and stopped. I'll need to test it more.
ASKER
OK he jumps on walls so its using random co-ordinates but its skipping a whole process which is GoTo I THINK!
How do I call the GoTo inside the Teleport process to check the new x,y co-ordinates?
How do I call the GoTo inside the Teleport process to check the new x,y co-ordinates?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I dont get it. it doesnt avoid going on obstacles at all now not even at the beginning which is code that did work.
ASKER
I know what its doing. Its stopping 2 squares before the object, stating their is an object ahead and jumping the square before the real object.
ASKER
Most of the code from goTo also has to put in isObstacleInTheWay but your comments helped me a lot sometimes you just need to bounce it off someone.
Thanks very much.
Thanks very much.
> Most of the code from goTo also has to put in isObstacleInTheWay
why, isObstacleInTheWay() should just need to check the cell contents. It doesn't need to actually move the robot.
why, isObstacleInTheWay() should just need to check the cell contents. It doesn't need to actually move the robot.
try out here..it may help you..
http://robowiki.net/cgi-bin/robowiki?CrazyTracker
R.K