100 likes | 187 Views
Figure8Bot. Problem. Create a robot that moves in a tight figure-8 pattern, firing whenever it sees an opponent. (a) A figure-8 pattern can be defined in 2 ways: either as a circuit in the shape of 2 adjacent circles:. Problem. (b) a circuit composed of 2 lines and 2 arcs: Hints
E N D
Problem • Create a robot that moves in a tight figure-8 pattern, firing whenever it sees an opponent. • (a) A figure-8 pattern can be defined in 2 ways: either as a circuit in the shape of 2 adjacent circles:
Problem • (b) a circuit composed of 2 lines and 2 arcs: • Hints • This is no job for an ordinary • robot...you will need to extend • AdvancedRobot. • The code for the SpinBot and Crazy sample robots can give you some ideas for how to do this. Look at the method calls in the run() methods of these, then examine their API references to determine how it works. • For part (a), you do not need to do any maths at all. The robocode API allows for a very easy non-mathematical approach • For part (b), work out the distance of the straight line sections, in terms of the length of the arcs, on paper to begin with. It may be helpful to know that the turning circle of a robot with velocity 5 is 290 units.
Solution – Turning /** * SpinBot's run method - Circle */ public void run() { while (true) { // Tell the game that when we take move, // we'll also want to turn right... a lot. setTurnRight(10000); // Limit our speed to 5 setMaxVelocity(5); // Start moving (and turning) ahead(10000); // Repeat. } }
Changing Direction /** * run: Crazy's main run function */ public void run() { while (true) { setAhead(40000); movingForward = true; setTurnRight(90); waitFor(new TurnCompleteCondition(this)); setTurnLeft(180); waitFor(new TurnCompleteCondition(this)); setTurnRight(180); waitFor(new TurnCompleteCondition(this)); } }
Solution A • (a) For this task, all we have to do is make the robot drive once around one circle, the once around the other circle; then repeat. If we assume that the robot begins on the intersection of the 2 circles, facing up, then what we need to do is turn 360 degrees clockwise, then 360 degrees anti-clockwise, moving ahead all along. • /** * Figure8's run method */ public void run() { setMaxVelocity(5); // just to make the turning circle smaller while (true) { setAhead(10000); // we want to be on the move at all times setTurnRight(360); // first, turn clockwise waitFor(new TurnCompleteCondition(this)); // wait until we're done turning setTurnLeft(360); // second, turn anti-clockwise waitFor(new TurnCompleteCondition(this)); // wait until we're done turning } }
Solution B • For this question, we first have to know exactly how long the straight sections of the path are. If we do not use the correct lengths, the robot will not move in a figure-8 pattern exactly; it will drift across the battle field. • Examine the diagram again:
Solution B • We can see that the length of the 2 straight sections is 2R, for the radius R of the turning circle. The circumference of the turning circle is given in the hints as 290 for a robot of velocity 5. This was discovered using a simple AdvancedRobot: • /** * This method determines the circumference of a robot's turning circle. */ public void run() { int distance = 10000; setMaxVelocity(5); while(true) { setTurnRight(360); setAhead(distance); waitFor(new TurnCompleteCondition(this)); double circumference = distance - getDistanceRemaining(); out.println("Circumference is " + circumference); } }
The Math Stuff • Recall that R = Circumference / (2*pi) = 46.15... So the distance = 2R = 92.030986699329929475925827560583 (approximately ;) • /** * Figure8's run method */ public void run() { double distance = 92.030986699329929475925827560583; setMaxVelocity(5); // just to make the turning circle smaller while (true) { setAhead(290); // this could be bigger, since the next setAhead cancels this one. setTurnRight(270); waitFor(new TurnCompleteCondition(this)); setAhead(distance); waitFor(new MoveCompleteCondition(this)); setAhead(290); setTurnLeft(270); waitFor(new TurnCompleteCondition(this)); setAhead(distance); waitFor(new MoveCompleteCondition(this)); } }
Which is better? • Robot vs. AdvancedRobot • Test it out • Where is B’s blind spot?