-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.java
190 lines (158 loc) · 6.74 KB
/
Bot.java
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
package za.co.entelect.challenge;
import za.co.entelect.challenge.command.*;
import za.co.entelect.challenge.entities.*;
import za.co.entelect.challenge.enums.PowerUps;
import za.co.entelect.challenge.enums.Terrain;
import java.util.*;
import static java.lang.Math.max;
import java.security.SecureRandom;
public class Bot {
private static final int maxSpeed = 9;
private List<Command> directionList = new ArrayList<>();
private final Random random;
private final static Command ACCELERATE = new AccelerateCommand();
private final static Command LIZARD = new LizardCommand();
private final static Command OIL = new OilCommand();
private final static Command BOOST = new BoostCommand();
private final static Command EMP = new EmpCommand();
private final static Command FIX = new FixCommand();
private final static Command DO_NOTHING = new DoNothingCommand();
private final static Command TURN_RIGHT = new ChangeLaneCommand(1);
private final static Command TURN_LEFT = new ChangeLaneCommand(-1);
public Bot() {
this.random = new SecureRandom();
directionList.add(TURN_LEFT);
directionList.add(TURN_RIGHT);
}
public Command run(GameState gameState) {
Car myCar = gameState.player;
Car opponent = gameState.opponent;
//Basic fix logic
List<Object> frontblocks = getBlocksInFront(myCar.position.lane, myCar.position.block, gameState);
List<Object> rightblocks = getBlocksOnRight(myCar.position.lane, myCar.position.block, gameState);
List<Object> leftblocks = getBlocksOnLeft(myCar.position.lane, myCar.position.block, gameState);
List<Object> nextBlocks = frontblocks.subList(0,1);
//fix first to move
if (myCar.damage >=2){
return FIX;
}
else {
//cek posisi musuh
if (opponent.position.block > myCar.position.block - 5 && opponent.position.block < myCar.position.block + 20) {
if (opponent.position.block > myCar.position.block) {
if (hasPowerUp(PowerUps.EMP, myCar.powerups)) {
return EMP;
} else {
if (hasPowerUp(PowerUps.BOOST, myCar.powerups)) {
return BOOST;
} else {
if (frontblocks.contains(Terrain.MUD) || nextBlocks.contains(Terrain.WALL) || nextBlocks.contains(Terrain.OIL_SPILL)) {
if (hasPowerUp(PowerUps.LIZARD, myCar.powerups)) {
return LIZARD;
} else {
if (rightblocks.contains(Terrain.MUD) || rightblocks.contains(Terrain.WALL) || rightblocks.contains(Terrain.OIL_SPILL)) {
return TURN_LEFT;
} else {
return TURN_RIGHT;
}
}
} else {
//kalo kurang speed accelerate
if (myCar.speed < Bot.maxSpeed) {
return ACCELERATE;
}
}
}
}
} else {
if (opponent.position.block < myCar.position.block && hasPowerUp(PowerUps.OIL, myCar.powerups)) {
return OIL;
} else
return DO_NOTHING;
}
}
}
if (myCar.speed == 0){
return FIX;
}
//Basic avoidance logic
if (frontblocks.contains(Terrain.MUD) || nextBlocks.contains(Terrain.WALL) || nextBlocks.contains(Terrain.OIL_SPILL)) {
if (hasPowerUp(PowerUps.LIZARD, myCar.powerups)) {
return LIZARD;
}
else{
if (rightblocks.contains(Terrain.MUD) || rightblocks.contains(Terrain.WALL) || rightblocks.contains(Terrain.OIL_SPILL)){
return TURN_LEFT;
}
else {
return TURN_RIGHT;
}
}
}
//kalo kurang speed accelerate
if (myCar.speed < Bot.maxSpeed ){
return ACCELERATE;
}
//Basic aggression logic
if (myCar.speed == maxSpeed) {
if (hasPowerUp(PowerUps.OIL, myCar.powerups)) {
return OIL;
}
if (hasPowerUp(PowerUps.EMP, myCar.powerups)) {
return EMP;
}
}
return ACCELERATE;
}
private Boolean hasPowerUp(PowerUps powerCheck, PowerUps[] available) {
for (PowerUps powerUp: available) {
if (powerUp.equals(powerCheck)) {
return true;
}
}
return false;
}
/**
* Returns map of blocks and the objects in the for the current lanes, returns
* the amount of blocks that can be traversed at max speed.
**/
private List<Object> getBlocksInFront(int lane, int block, GameState gameState) {
List<Lane[]> map = gameState.lanes;
List<Object> blocks = new ArrayList<>();
int startBlock = map.get(0)[0].position.block;
Lane[] laneList = map.get(lane - 1);
for (int i = max(block - startBlock, 0); i <= block - startBlock + Bot.maxSpeed; i++) {
if (laneList[i] == null || laneList[i].terrain == Terrain.FINISH) {
break;
}
blocks.add(laneList[i].terrain);
}
return blocks;
}
private List<Object> getBlocksOnLeft(int lane, int block, GameState gameState) {
List<Lane[]> map = gameState.lanes;
List<Object> blocks = new ArrayList<>();
int startBlock2 = map.get(0)[0].position.block;
Lane[] laneList = map.get(lane - 2);
for (int i = block - startBlock2; i <= block - startBlock2 + 15; i++) {
if (laneList[i] == null || laneList[i].terrain == Terrain.FINISH) {
break;
}
blocks.add(laneList[i].terrain);
}
return blocks;
}
private List<Object> getBlocksOnRight(int lane, int block, GameState gameState) {
List<Lane[]> map = gameState.lanes;
List<Object> blocks = new ArrayList<>();
int startBlock3 = map.get(0)[0].position.block;
Lane[] laneList = map.get(lane);
for (int i = block - startBlock3; i <= block - startBlock3 + 15; i++) {
if (laneList[i] == null || laneList[i].terrain == Terrain.FINISH) {
break;
}
blocks.add(laneList[i].terrain);
}
return blocks;
}
}