Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* ----------------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <martinv@students.zcu.cz> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return. Martin Vítek
* ----------------------------------------------------------------------------------
*/
package cz.zcu.martinv.MyRobocodeRobots;
import robocode.*;
import java.awt.Color;
import java.util.ArrayList;
/**
* Simple robot, which is realy bad.
* Robots bahaviour is based on hard-coded behaviour and state machine.
*
* @author Martin Vítek
*/
public class BadBot extends AdvancedRobot
{
private enum States
{
SCANNING, APROACHING, FIGHTING, RUNNING, RAMMING, UNCOLISIONING;
}
private RobotStatus status;
private States state;
private ArrayList<EnemyRobotStatus> enemyRobots;
/**
* Main method - here is all robot logic.
* It's called by RoboCode.
*/
@Override
public void run()
{
out.format("Hi, I'm your bad robot!%n");
//Robot initialization
setBodyColor(Color.GREEN);
setRadarColor(Color.WHITE);
setGunColor(Color.BLACK);
setScanColor(Color.YELLOW);
setAdjustGunForRobotTurn(true); //Gun movement is independent on body
setAdjustRadarForGunTurn(true); //Radar movement is independent on body
setAdjustRadarForRobotTurn(true); //Radar movement is independent on gun
enemyRobots = new ArrayList<EnemyRobotStatus>(2);
//Start scaning other robots
state = States.SCANNING;
while(true)
{
switch(state)
{
case SCANNING: scanning();
break;
case APROACHING: aproaching();
break;
/*
case FIGHTING: fighting();
break;
case RUNNING: running();
break;
case RAMMING: ramming();
break;
case UNCOLISIONING: uncolisioning();
break;
*/
}
}
}
/**
* This method is called when another robot is scanned.
* It's called by RoboCode.
* @param e passed by RoboCode
*/
@Override
public void onScannedRobot(ScannedRobotEvent e)
{
out.format("Found: %s%n", e.getName());
switch(state)
{
case SCANNING: scanningOnScannedRobot(e);
break;
case APROACHING: aproachingOnScannedRobot(e);
break;
}
//scan();
}
/**
* This method is called every turn.
* It's called by RoboCode.
* @param e passed by RoboCode
*/
@Override
public void onStatus(StatusEvent e)
{
status = e.getStatus();
//checkWalls();
}
@Override
public void onBulletHit(BulletHitEvent e)
{
}
@Override
public void onHitByBullet(HitByBulletEvent e)
{
}
@Override
public void onHitRobot(HitRobotEvent e)
{
}
@Override
public void onHitWall(HitWallEvent e)
{
turnRight(180.0);
}
/**
* This method is called when this robot wins.
* It's called by RoboCode.
* @param e passed by RoboCode
*/
@Override
public void onWin(WinEvent e)
{
turnRight(360);
}
private void scanning()
{
stop();
turnRadarRight(36.0);
}
private void scanningOnScannedRobot(ScannedRobotEvent e)
{
if (enemyRobots.contains(new EnemyRobotStatus(e))) return;
enemyRobots.add(new EnemyRobotStatus(e));
//We have all robots in current battle
if (enemyRobots.size() == getOthers())
{
out.format("We have scanned all (%d) enemy robots%n", enemyRobots.size());
state = States.APROACHING;
}
}
private void aproaching()
{
ahead(500.0);
}
private void aproachingOnScannedRobot(ScannedRobotEvent e)
{
}
private void checkWalls()
{
if ( (status.getX() < 1.8*getWidth()) ||
(status.getX() > (getBattleFieldWidth()-1.8*getWidth())) ||
(status.getY() < 1.8*getHeight()) ||
(status.getY() > (getBattleFieldHeight()-1.8*getHeight())) )
{
stop();
turnRight(90.0);
ahead(40000);
}
}
}