Repository navigation
Expand file tree
/
Copy pathbasic.rs
More file actions
42 lines (41 loc) · 1.54 KB
/
Copy pathbasic.rs
File metadata and controls
42 lines (41 loc) · 1.54 KB
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
use slither_sim::{Action, Config, Simulator};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut world = Simulator::new(slither_sim::WorldOptions {
seed: 7,
config: Config::default(),
population: slither_sim::InitialPopulation::Placed {
ids: (0..4).map(|slot| slot.to_string()).collect(),
placement: slither_sim::Placement::TestRing,
},
})?;
// One buffer for every decision: `observe_all_into` refills its vectors.
let mut observations = Vec::new();
for _ in 0..300 {
world.observe_all_into(&mut observations);
let actions: Vec<_> = observations
.iter()
.map(|observation| {
observation.as_ref().map(|observation| {
let head = observation.me.head;
let heading = observation
.food
.iter()
.min_by(|a, b| {
head.distance(a.point())
.total_cmp(&head.distance(b.point()))
})
.map_or((-head.y).atan2(-head.x), |food| {
(food.y - head.y).atan2(food.x - head.x)
});
Action {
heading,
boost: false,
}
})
})
.collect();
world.step(&actions)?;
}
println!("time={:.3}s living={}", world.time(), world.living());
Ok(())
}