summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-10 23:37:15 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-10 23:37:15 +0200
commit1cd5247ccbd34bb76ab9d1fe80e67f6319fa000c (patch)
tree9cc2e8aab32887f86614e795cb98cfa5ea1ff4dd
parentf3f798441e56afec9e6357c96274f90bf4ea6947 (diff)
Initial bad strategy
-rw-r--r--src/main.rs19
-rw-r--r--src/state.rs28
2 files changed, 46 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index 74428c3..92b0a1b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -26,5 +26,22 @@ fn main() {
}
fn choose_command(state: &GameState) -> Command {
- Command::Accelerate
+ 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::Nothing]);
+ (player_move, state)
+ })
+ .max_by(|(_, state_1), (_, state_2)| {
+ state_1.players[0]
+ .position
+ .x
+ .cmp(&state_2.players[0].position.x)
+ .then(state_1.players[0].speed.cmp(&state_2.players[0].speed))
+ })
+ .unwrap()
+ .0;
+ naive_result
}
diff --git a/src/state.rs b/src/state.rs
index 1dfa21d..6285e75 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,6 +1,7 @@
use crate::command::Command;
use crate::consts::*;
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GameStatus {
Continue,
PlayerOneWon,
@@ -8,6 +9,7 @@ pub enum GameStatus {
Draw, // Until I add score I guess
}
+#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GameState {
pub status: GameStatus,
pub players: [Player; 2],
@@ -17,6 +19,7 @@ pub struct GameState {
pub finish_lines: Vec<Position>,
}
+#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Player {
pub position: Position,
pub next_position: Position,
@@ -110,6 +113,31 @@ impl GameState {
);
}
}
+
+ pub fn valid_moves(&self, player_index: usize) -> Vec<Command> {
+ let player = &self.players[player_index];
+ let mut result = Vec::with_capacity(7);
+ result.push(Command::Nothing);
+ if player.speed < SPEED_4 {
+ result.push(Command::Accelerate);
+ }
+ if player.speed > SPEED_0 {
+ result.push(Command::Decelerate);
+ }
+ if player.position.y > 0 {
+ result.push(Command::TurnLeft);
+ }
+ if player.position.y < WIDTH {
+ result.push(Command::TurnRight);
+ }
+ if player.boosts > 0 {
+ result.push(Command::UseBoost);
+ }
+ if player.oils > 0 {
+ result.push(Command::UseOil);
+ }
+ result
+ }
}
impl Player {