summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-13 11:32:57 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-13 11:32:57 +0200
commit790c3c73f909e1e39249303437e34223eef27f1c (patch)
tree6caf9b0b6679bf968b92f4d44c7bdd01ade035b8
parenta516e9a639226505964144d16bffacee82c058a5 (diff)
Split main and lib
-rw-r--r--src/lib.rs52
-rw-r--r--src/main.rs54
2 files changed, 54 insertions, 52 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..902cb73
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,52 @@
+pub mod command;
+pub mod consts;
+pub mod json;
+pub mod state;
+
+use command::*;
+use state::*;
+use std::cmp::Ordering;
+
+pub fn choose_command(state: &GameState) -> Command {
+ /* TODO:
+ * - Simulate both player and opponent moves
+ */
+ let player_moves = state.valid_moves(0);
+ let naive_result = player_moves
+ .into_iter()
+ .map(|player_move| {
+ let mut state = state.clone();
+ state.update([player_move, Command::Accelerate]);
+ (player_move, state)
+ })
+ .flat_map(|(player_move, state)| {
+ state.valid_moves(0).into_iter().map(move |second_move| {
+ let mut second_move_state = state.clone();
+ second_move_state.update([second_move, Command::Accelerate]);
+ (player_move, second_move_state)
+ })
+ })
+ .max_by(|(_, a), (_, b)| compare_states(a, b))
+ .unwrap()
+ .0;
+ naive_result
+}
+
+fn compare_states(a: &GameState, b: &GameState) -> Ordering {
+ if a.status == GameStatus::PlayerOneWon && b.status == GameStatus::PlayerOneWon {
+ a.players[0].speed.cmp(&b.players[0].speed)
+ } else if a.status == GameStatus::PlayerOneWon {
+ Ordering::Greater
+ } else if b.status == GameStatus::PlayerOneWon {
+ Ordering::Less
+ } else {
+ let weighted_position_a = a.players[0].position.x + a.players[0].boosts * 2;
+ let weighted_position_b = b.players[0].position.x + b.players[0].boosts * 2;
+
+ weighted_position_a
+ .cmp(&weighted_position_b)
+ .then(a.players[0].speed.cmp(&b.players[0].speed))
+ .then(a.players[0].position.x.cmp(&b.players[0].position.x))
+ .then(a.players[0].boosts.cmp(&b.players[0].boosts))
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index 9f69b87..a652b8e 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,14 +1,8 @@
-use std::cmp::Ordering;
use std::io::prelude::*;
use std::io::stdin;
-mod command;
-mod consts;
-mod json;
-mod state;
-
-use command::*;
-use state::*;
+use vroomba::command::Command;
+use vroomba::*;
fn main() {
for line in stdin().lock().lines() {
@@ -25,47 +19,3 @@ fn main() {
println!("C;{};{}", round_number, command);
}
}
-
-fn choose_command(state: &GameState) -> Command {
- /* TODO:
- * - Simulate both player and opponent moves
- */
- let player_moves = state.valid_moves(0);
- let naive_result = player_moves
- .into_iter()
- .map(|player_move| {
- let mut state = state.clone();
- state.update([player_move, Command::Accelerate]);
- (player_move, state)
- })
- .flat_map(|(player_move, state)| {
- state.valid_moves(0).into_iter().map(move |second_move| {
- let mut second_move_state = state.clone();
- second_move_state.update([second_move, Command::Accelerate]);
- (player_move, second_move_state)
- })
- })
- .max_by(|(_, a), (_, b)| compare_states(a, b))
- .unwrap()
- .0;
- naive_result
-}
-
-fn compare_states(a: &GameState, b: &GameState) -> Ordering {
- if a.status == GameStatus::PlayerOneWon && b.status == GameStatus::PlayerOneWon {
- a.players[0].speed.cmp(&b.players[0].speed)
- } else if a.status == GameStatus::PlayerOneWon {
- Ordering::Greater
- } else if b.status == GameStatus::PlayerOneWon {
- Ordering::Less
- } else {
- let weighted_position_a = a.players[0].position.x + a.players[0].boosts * 2;
- let weighted_position_b = b.players[0].position.x + b.players[0].boosts * 2;
-
- weighted_position_a
- .cmp(&weighted_position_b)
- .then(a.players[0].speed.cmp(&b.players[0].speed))
- .then(a.players[0].position.x.cmp(&b.players[0].position.x))
- .then(a.players[0].boosts.cmp(&b.players[0].boosts))
- }
-}