/
robot.js
73 lines (64 loc) · 1.81 KB
/
robot.js
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
"use strict";
async function main(tank) {
// auxiliary functions
const startAction = async tank => {
await tank.drive(Math.floor(Math.random() * 359), 45);
}
const initRadarRotate = async (counter) => {
let scan = await tank.scan(counter, 8);
if (scan) {return {
distance: scan,
degree: counter,
target: true,
}}
counter += 15;
return initRadarRotate(counter)
}
const radarChase = async (tank, degree) => {
let right = await tank.scan(degree + 15, 10);
let left = await tank.scan(degree - 15, 10);
if (!right && !left) {return}
// target the nearest enemy
degree = right < left ? degree + 10 : degree - 10;
// if you find one, chase him like a panther
await tank.drive(degree, 65);
// FIRE!
await tank.shoot(degree, 500);
// the wall!
await dodgeWall(tank);
return radarChase(tank, degree);
}
const dodgeWall = async (tank) => {
let yCoordinate = await tank.getY();
let xCoordinate = await tank.getX();
if (yCoordinate >= 700) {
await tank.drive(270, 50);
} else if (yCoordinate <= 300) {
await tank.drive(90, 50);
}
if (xCoordinate >= 700) {
await tank.drive(180, 50);
} else if (yCoordinate <= 300) {
await tank.drive(0, 50);
}
}
// main loop
while (true) {
// start slow, play dumb
if(!await tank.getSpeed()) {
await startAction(tank);
}
// who put that wall over there?!
await dodgeWall(tank);
// use your radary senses to locate enemy
let { distance, degree, target } = await initRadarRotate(0);
if (target) {
// if you find one, chase him like a panther
await tank.drive(degree, 75);
// FIRE!
await tank.shoot(degree, distance);
// where are you!
await radarChase(tank, degree);
}
}
}