我想直接去地图的中心。所以这意味着不要转向地图高度的中心,然后是地图宽度的中心,我想开始并面向中心然后去那里。一旦我在那里,我想停下来。我觉得我应该做的,但它不能正常工作。
1)我在正确的轨道上吗?
2) 有没有更简单的直接去中心的方法?像一种拼写坐标的方法吗?
到目前为止,我有:
public void run() {
// Initialization of the robot should be put here
setColors(Color.black,Color.blue,Color.orange, Color.orange, Color.cyan); // body,gun,radar
double xMiddle = getBattleFieldWidth() / 2;
double yMiddle = getBattleFieldHeight() / 2;
double directionToTurn = 0;
// Robot main loop
while(true) {
if( (int)getX() != xMiddle && (int)getY() != yMiddle ){
if ( (int)getX() > xMiddle && (int)getY() > yMiddle ) { //Quadrant 1
directionToTurn = (int)getHeading() - 225;
System.out.println("Quadrant: ONE ");
} else if ( (int)getX() < xMiddle && (int)getY() > yMiddle ) { //Quadrant 2
directionToTurn = (int)getHeading() - 135;
System.out.println("Quadrant: TWO ");
} else if ( (int)getX() < xMiddle && (int)getY() < yMiddle ) { //Quadrant 3
directionToTurn = (int)getHeading() - 45;
System.out.println("Quadrant: THREE ");
} else if ( (int)getX() > xMiddle && (int)getY() < yMiddle ) { //Quadrant 4
directionToTurn = (int)getHeading() - 315;
System.out.println("Quadrant: FOUR ");
} else if ( (int)getX() > xMiddle && (int)getY() == yMiddle ) { // Right Center
directionToTurn = (int)getHeading() - 270;
System.out.println("Quadrant: Right Center ");
} else if ( (int)getX() < xMiddle && (int)getY() == yMiddle ) { // Left Center
directionToTurn = (int)getHeading() - 90;
System.out.println("Quadrant: Left Center ");
} else if ( (int)getX() == xMiddle && (int)getY() > yMiddle ) { // Top Center
directionToTurn = (int)getHeading() - 180;
System.out.println("Quadrant: Top Center ");
} else if ( (int)getX() == xMiddle && (int)getY() < yMiddle ) { // Bottom Center
directionToTurn = (int)getHeading() - 0;
System.out.println("Quadrant: Bottom Center ");
}
turnLeft( directionToTurn );
System.out.println("Position: ("+(int)(getX())+", "+(int)getY()+"), Facing: "+(int)getHeading() );
ahead(1);
}
}
}