hshah
Oct 26th, 2004, 03:34 PM
/*
* File: Ex14.java
* Created: 25 October 2004, 21:05
* Author: Hiren Shah
*/
import uk.ac.warwick.dcs.maze.logic.IRobot;
public class Ex14
{
public void controlRobot(IRobot robot)
{
robot.look(IRobot.NORTH);
NorthController(robot);
}
private byte isTargetNorth(IRobot robot)
{
byte result = 0;
int a = robot.getLocationY();
int b = robot.getTargetLocation().y;
if (a>b) result = -1;
else if (a<b) result = 1;
else result = 0;
return result;
}
private byte isTargetEast(IRobot robot)
{
byte result = 0;
int a = robot.getLocationX();
int b = robot.getTargetLocation().x;
if (a>b) result = -1;
else if (a<b) result = 1;
else result = 0;
return result;
}
private int NorthController(IRobot robot)
{
int direction = 0;
int randno;
int d1 = isTargetNorth(robot);
int d2 = isTargetEast(robot);
if (d1 == 1 & d2 == 0)
{ do {
direction = IRobot.AHEAD;
} while (robot.look(IRobot.AHEAD)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.RIGHT;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.AHEAD)==IRobot.WALL);
}
else if (d1 == 1 & d2 == 1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.AHEAD;
else if (randno == 1)
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
do {
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
}
else if (d1 == 1 & d2 == -1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.AHEAD;
else if (randno == 1)
direction = IRobot.LEFT;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
do {
direction = IRobot.LEFT;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
}
else if (d1 == 0 & d2 == 1)
{ do {
direction = IRobot.RIGHT;
} while (robot.look(IRobot.RIGHT)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.AHEAD;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.RIGHT)==IRobot.WALL);
}
else if (d1 == 0 & d2 == -1)
{ do {
direction = IRobot.LEFT;
} while (robot.look(IRobot.LEFT)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.AHEAD;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.LEFT)==IRobot.WALL);
}
else if (d1 == -1 & d2 == 1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.BEHIND;
else if (randno == 1)
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
do {
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
}
else if (d1 == -1 & d2 == 0)
{ do {
direction = IRobot.BEHIND;
} while (robot.look(IRobot.BEHIND)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.RIGHT;
else if (randno == 2)
direction = IRobot.AHEAD;
} while (robot.look(IRobot.BEHIND)==IRobot.WALL);
}
else if (d1 == -1 & d2 == -1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.BEHIND;
else if (randno == 1)
direction = IRobot.LEFT;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
do {
direction = IRobot.LEFT;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
}
return direction;
}
}
Can anyone see anything wrong with that?
* File: Ex14.java
* Created: 25 October 2004, 21:05
* Author: Hiren Shah
*/
import uk.ac.warwick.dcs.maze.logic.IRobot;
public class Ex14
{
public void controlRobot(IRobot robot)
{
robot.look(IRobot.NORTH);
NorthController(robot);
}
private byte isTargetNorth(IRobot robot)
{
byte result = 0;
int a = robot.getLocationY();
int b = robot.getTargetLocation().y;
if (a>b) result = -1;
else if (a<b) result = 1;
else result = 0;
return result;
}
private byte isTargetEast(IRobot robot)
{
byte result = 0;
int a = robot.getLocationX();
int b = robot.getTargetLocation().x;
if (a>b) result = -1;
else if (a<b) result = 1;
else result = 0;
return result;
}
private int NorthController(IRobot robot)
{
int direction = 0;
int randno;
int d1 = isTargetNorth(robot);
int d2 = isTargetEast(robot);
if (d1 == 1 & d2 == 0)
{ do {
direction = IRobot.AHEAD;
} while (robot.look(IRobot.AHEAD)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.RIGHT;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.AHEAD)==IRobot.WALL);
}
else if (d1 == 1 & d2 == 1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.AHEAD;
else if (randno == 1)
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
do {
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
}
else if (d1 == 1 & d2 == -1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.AHEAD;
else if (randno == 1)
direction = IRobot.LEFT;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.AHEAD)!=IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
do {
direction = IRobot.LEFT;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.AHEAD)==IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
}
else if (d1 == 0 & d2 == 1)
{ do {
direction = IRobot.RIGHT;
} while (robot.look(IRobot.RIGHT)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.AHEAD;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.RIGHT)==IRobot.WALL);
}
else if (d1 == 0 & d2 == -1)
{ do {
direction = IRobot.LEFT;
} while (robot.look(IRobot.LEFT)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.AHEAD;
else if (randno == 2)
direction = IRobot.BEHIND;
} while (robot.look(IRobot.LEFT)==IRobot.WALL);
}
else if (d1 == -1 & d2 == 1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.BEHIND;
else if (randno == 1)
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
do {
direction = IRobot.RIGHT;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.RIGHT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.RIGHT)==IRobot.WALL));
}
else if (d1 == -1 & d2 == 0)
{ do {
direction = IRobot.BEHIND;
} while (robot.look(IRobot.BEHIND)!=IRobot.WALL);
do {
randno = (int) Math.round(Math.random()*3);
if (randno == 0)
direction = IRobot.LEFT;
else if (randno == 1)
direction = IRobot.RIGHT;
else if (randno == 2)
direction = IRobot.AHEAD;
} while (robot.look(IRobot.BEHIND)==IRobot.WALL);
}
else if (d1 == -1 & d2 == -1)
{ do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.BEHIND;
else if (randno == 1)
direction = IRobot.LEFT;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
direction = IRobot.BEHIND;
} while ((robot.look(IRobot.BEHIND)!=IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
do {
direction = IRobot.LEFT;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.LEFT)!=IRobot.WALL));
do {
randno = (int) Math.round(Math.random()*2);
if (randno == 0)
direction = IRobot.RIGHT;
else if (randno == 1)
direction = IRobot.AHEAD;
} while ((robot.look(IRobot.BEHIND)==IRobot.WALL) & (robot.look(IRobot.LEFT)==IRobot.WALL));
}
return direction;
}
}
Can anyone see anything wrong with that?