1 /*
2  * Copyright (c) 2001-2021 Mathew A. Nelson and Robocode contributors
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * https://robocode.sourceforge.io/license/epl-v10.html
7  */
8 package sample;
9 
10 
11 import robocode.*;
12 
13 import java.awt.*;
14 
15 
16 /**
17  * Crazy - a sample robot by Mathew Nelson.
18  * <p>
19  * This robot moves around in a crazy pattern.
20  *
21  * @author Mathew A. Nelson (original)
22  * @author Flemming N. Larsen (contributor)
23  */
24 public class Crazy extends AdvancedRobot {
25 	boolean movingForward;
26 
27 	/**
28 	 * run: Crazy's main run function
29 	 */
run()30 	public void run() {
31 		// Set colors
32 		setBodyColor(new Color(0, 200, 0));
33 		setGunColor(new Color(0, 150, 50));
34 		setRadarColor(new Color(0, 100, 100));
35 		setBulletColor(new Color(255, 255, 100));
36 		setScanColor(new Color(255, 200, 200));
37 
38 		// Loop forever
39 		while (true) {
40 			// Tell the game we will want to move ahead 40000 -- some large number
41 			setAhead(40000);
42 			movingForward = true;
43 			// Tell the game we will want to turn right 90
44 			setTurnRight(90);
45 			// At this point, we have indicated to the game that *when we do something*,
46 			// we will want to move ahead and turn right.  That's what "set" means.
47 			// It is important to realize we have not done anything yet!
48 			// In order to actually move, we'll want to call a method that
49 			// takes real time, such as waitFor.
50 			// waitFor actually starts the action -- we start moving and turning.
51 			// It will not return until we have finished turning.
52 			waitFor(new TurnCompleteCondition(this));
53 			// Note:  We are still moving ahead now, but the turn is complete.
54 			// Now we'll turn the other way...
55 			setTurnLeft(180);
56 			// ... and wait for the turn to finish ...
57 			waitFor(new TurnCompleteCondition(this));
58 			// ... then the other way ...
59 			setTurnRight(180);
60 			// .. and wait for that turn to finish.
61 			waitFor(new TurnCompleteCondition(this));
62 			// then back to the top to do it all again
63 		}
64 	}
65 
66 	/**
67 	 * onHitWall:  Handle collision with wall.
68 	 */
onHitWall(HitWallEvent e)69 	public void onHitWall(HitWallEvent e) {
70 		// Bounce off!
71 		reverseDirection();
72 	}
73 
74 	/**
75 	 * reverseDirection:  Switch from ahead to back &amp; vice versa
76 	 */
reverseDirection()77 	public void reverseDirection() {
78 		if (movingForward) {
79 			setBack(40000);
80 			movingForward = false;
81 		} else {
82 			setAhead(40000);
83 			movingForward = true;
84 		}
85 	}
86 
87 	/**
88 	 * onScannedRobot:  Fire!
89 	 */
onScannedRobot(ScannedRobotEvent e)90 	public void onScannedRobot(ScannedRobotEvent e) {
91 		fire(1);
92 	}
93 
94 	/**
95 	 * onHitRobot:  Back up!
96 	 */
onHitRobot(HitRobotEvent e)97 	public void onHitRobot(HitRobotEvent e) {
98 		// If we're moving the other robot, reverse!
99 		if (e.isMyFault()) {
100 			reverseDirection();
101 		}
102 	}
103 }
104