You can debug robocode in three ways:
1. Output Monitoring Program
This is the simplest and most efficient debugging method. Add system. Out. println (…) to your code (...) Or system. Error. println (...), And monitor your code through output.
All system outputs are output to the robocode monitor. On the battlefield, click a tank name on the right of the screen to open the robocode monitor:
2. debug through degug
We all know that we can break points in eclipse for debugging. If we want to debug our tanks, we only need to break the breakpoint and start robocode in debug mode.
3. Draw on the battlefield for debugging
This is the most distinctive debugging method of robocode. In this way, you can achieve the following debugging results:
Every time I was shocked by the tank JK. Mega. drussgt (the yellow guy in the middle! I downloaded this tank from the Internet. The drussgt tank in draws a garden centered around the tank on the screen, draws the expected trajectory of the other party, and draws the possible coverage of the enemy bullet (dark blue line ). The drussgt tank is really smart and powerful! It quickly moves along the beautiful arc, and almost no one else can touch it. But at the same time, it can predict the opponent's motion track based on the opponent's movement direction and speed, and carry out attacks with a forward amount!
Maybe this is irrelevant to what we are going to talk about today. All I want to talk about is how the debugging lines on the screen are drawn.
It is not complicated. In the robot class, there is an event method calledOnpaint(Graphics2d g ). In this method, we can use the graphics2d object passed in by this method to plot on the battlefield. The following tanks are used to mark the opponent's position and draw a straight line between the opponent and the opponent:
Public class myrobot extends robot {// stores the coordinates of the last detected enemy: int scannedx = integer. min_value; int scannedy = integer. min_value; Public void run () {// keep the radar rotating while (true) {turnradarright (360 );}} // when the radar detects the enemy, execute this method public void onscannedrobot (scannedrobotevent e) {// calculate the angle of the target double angle = math. toradians (getheading () + E. getbearing () % 360); // calculate the target coordinate scannedx = (INT) (getx () + math. sin (angle) * E. getdistance (); scannedy = (INT) (Gety () + math. cos (angle) * E. getdistance ();} // draw the target location and connection line between the target and the target on the battlefield: Public void onpaint (graphics2d g) {G. setcolor (new color (0xff, 0x00, 0x00, 0x80); G. drawline (scannedx, scannedy, (INT) getx (), (INT) Gety (); G. fillrect (scannedx-20, scannedy-20, 40, 40 );}}
The final result is as follows:
However, the onpaint event is disabled by default. To enable the onpaint event, you need to click the paint button in the monitoring window.