summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2020-04-19 10:40:19 +0200
committerJustin Wernick <justin@worthe-it.co.za>2020-04-19 10:40:19 +0200
commit556cb56d46b9d9bc8ec1ce0ba09ab5bc38c6022b (patch)
tree07f31737890aa8f8a1925b4371aba8a702d940f7
parent91c144969f6dd2daa7d9fb2e5d59059ecd556b79 (diff)
Change to proper sets
-rw-r--r--Cargo.toml2
-rw-r--r--src/consts.rs22
-rw-r--r--src/global_json.rs31
-rw-r--r--src/json.rs33
-rw-r--r--src/state.rs33
-rw-r--r--vroomba-analysis/src/main.rs1
6 files changed, 64 insertions, 58 deletions
diff --git a/Cargo.toml b/Cargo.toml
index d522a41..6158188 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,4 +11,4 @@ serde_json = "1.0"
[profile.release]
lto = true
-# debug = true \ No newline at end of file
+debug = true \ No newline at end of file
diff --git a/src/consts.rs b/src/consts.rs
index 69c9fe8..8010eba 100644
--- a/src/consts.rs
+++ b/src/consts.rs
@@ -1,14 +1,14 @@
-pub const SPEED_0: usize = 0;
-pub const SPEED_1: usize = 3;
-pub const SPEED_2: usize = 6;
-pub const SPEED_3: usize = 8;
-pub const SPEED_4: usize = 9;
-pub const SPEED_BOOST: usize = 15;
+pub const SPEED_0: u16 = 0;
+pub const SPEED_1: u16 = 3;
+pub const SPEED_2: u16 = 6;
+pub const SPEED_3: u16 = 8;
+pub const SPEED_4: u16 = 9;
+pub const SPEED_BOOST: u16 = 15;
-pub const BOOST_DURATION: usize = 5;
+pub const BOOST_DURATION: u8 = 5;
-pub const MIN_Y: usize = 1;
-pub const HEIGHT: usize = 4;
-pub const MAX_Y: usize = MIN_Y + HEIGHT;
+pub const MIN_Y: u8 = 1;
+pub const HEIGHT: u8 = 4;
+pub const MAX_Y: u8 = MIN_Y + HEIGHT;
-pub const WIDTH: usize = 1500;
+pub const WIDTH: u16 = 1500;
diff --git a/src/global_json.rs b/src/global_json.rs
index 189a97b..101a085 100644
--- a/src/global_json.rs
+++ b/src/global_json.rs
@@ -1,3 +1,4 @@
+use std::convert::TryInto;
use std::fs::File;
use std::io::prelude::*;
use std::rc::Rc;
@@ -20,7 +21,7 @@ pub fn read_state_from_global_json_file(filename: &str) -> Result<GameState> {
let mut content = String::new();
file.read_to_string(&mut content)?;
let json_state: JsonState = serde_json::from_str(content.as_ref())?;
- Ok(json_state.to_game_state())
+ json_state.to_game_state()
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -37,11 +38,11 @@ pub struct JsonState {
pub struct JsonPlayer {
// id: usize,
position: JsonPosition,
- speed: usize,
+ speed: u16,
// state: JsonPlayerState,
powerups: Vec<JsonPowerup>,
// boosting: bool,
- boost_counter: usize,
+ boost_counter: u8,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -55,8 +56,8 @@ pub struct JsonBlock {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonPosition {
- block_number: usize,
- lane: usize,
+ lane: u8,
+ block_number: u16,
}
// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -96,10 +97,10 @@ pub enum JsonSurfaceObject {
}
impl JsonState {
- fn to_game_state(&self) -> GameState {
- GameState {
+ fn to_game_state(&self) -> Result<GameState> {
+ Ok(GameState {
status: GameStatus::Continue,
- players: [self.players[0].to_player(), self.players[1].to_player()],
+ players: [self.players[0].to_player()?, self.players[1].to_player()?],
obstacles: Rc::new(
self.blocks
.iter()
@@ -131,13 +132,13 @@ impl JsonState {
.map(|cell| cell.position.to_position())
.collect(),
),
- }
+ })
}
}
impl JsonPlayer {
- fn to_player(&self) -> Player {
- Player {
+ fn to_player(&self) -> Result<Player> {
+ Ok(Player {
position: self.position.to_position(),
next_position: self.position.to_position(),
speed: self.speed,
@@ -146,14 +147,16 @@ impl JsonPlayer {
.powerups
.iter()
.filter(|powerup| **powerup == JsonPowerup::Oil)
- .count(),
+ .count()
+ .try_into()?,
boosts: self
.powerups
.iter()
.filter(|powerup| **powerup == JsonPowerup::Boost)
- .count(),
+ .count()
+ .try_into()?,
finished: false,
- }
+ })
}
}
diff --git a/src/json.rs b/src/json.rs
index fa90e06..6cdc48f 100644
--- a/src/json.rs
+++ b/src/json.rs
@@ -1,3 +1,4 @@
+use std::convert::TryInto;
use std::fs::File;
use std::io::prelude::*;
use std::rc::Rc;
@@ -14,7 +15,7 @@ pub fn read_state_from_json_file(filename: &str) -> Result<GameState> {
let mut content = String::new();
file.read_to_string(&mut content)?;
let json_state: JsonState = serde_json::from_str(content.as_ref())?;
- Ok(json_state.to_game_state())
+ json_state.to_game_state()
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -32,11 +33,11 @@ pub struct JsonState {
pub struct JsonPlayer {
// id: usize,
position: JsonPosition,
- speed: usize,
+ speed: u16,
// state: JsonPlayerState,
powerups: Vec<JsonPowerup>,
// boosting: bool,
- boost_counter: usize,
+ boost_counter: u8,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -44,7 +45,7 @@ pub struct JsonPlayer {
pub struct JsonOpponent {
// id: usize,
position: JsonPosition,
- speed: usize,
+ speed: u16,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -58,8 +59,8 @@ pub struct JsonWorldMapCell {
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct JsonPosition {
- x: usize,
- y: usize,
+ y: u8,
+ x: u16,
}
// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
@@ -99,10 +100,10 @@ pub enum JsonSurfaceObject {
}
impl JsonState {
- fn to_game_state(&self) -> GameState {
- GameState {
+ fn to_game_state(&self) -> Result<GameState> {
+ Ok(GameState {
status: GameStatus::Continue,
- players: [self.player.to_player(), self.opponent.to_player()],
+ players: [self.player.to_player()?, self.opponent.to_player()],
obstacles: Rc::new(
self.world_map
.iter()
@@ -138,13 +139,13 @@ impl JsonState {
.map(|cell| cell.position.to_position())
.collect(),
),
- }
+ })
}
}
impl JsonPlayer {
- fn to_player(&self) -> Player {
- Player {
+ fn to_player(&self) -> Result<Player> {
+ Ok(Player {
position: self.position.to_position(),
next_position: self.position.to_position(),
speed: self.speed,
@@ -153,14 +154,16 @@ impl JsonPlayer {
.powerups
.iter()
.filter(|powerup| **powerup == JsonPowerup::Oil)
- .count(),
+ .count()
+ .try_into()?,
boosts: self
.powerups
.iter()
.filter(|powerup| **powerup == JsonPowerup::Boost)
- .count(),
+ .count()
+ .try_into()?,
finished: false,
- }
+ })
}
}
diff --git a/src/state.rs b/src/state.rs
index 78f8267..b898e4b 100644
--- a/src/state.rs
+++ b/src/state.rs
@@ -1,5 +1,6 @@
use crate::command::Command;
use crate::consts::*;
+use std::collections::BTreeSet;
use std::rc::Rc;
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
@@ -14,27 +15,27 @@ pub enum GameStatus {
pub struct GameState {
pub status: GameStatus,
pub players: [Player; 2],
- pub obstacles: Rc<Vec<Position>>,
- pub powerup_oils: Rc<Vec<Position>>,
- pub powerup_boosts: Rc<Vec<Position>>,
- pub finish_lines: Rc<Vec<Position>>,
+ pub obstacles: Rc<BTreeSet<Position>>,
+ pub powerup_oils: Rc<BTreeSet<Position>>,
+ pub powerup_boosts: Rc<BTreeSet<Position>>,
+ pub finish_lines: Rc<BTreeSet<Position>>,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct Player {
pub position: Position,
pub next_position: Position,
- pub speed: usize,
- pub boost_remaining: usize,
- pub oils: usize,
- pub boosts: usize,
+ pub speed: u16,
+ pub boost_remaining: u8,
+ pub oils: u16,
+ pub boosts: u16,
pub finished: bool,
}
-#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
+#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Position {
- pub x: usize,
- pub y: usize,
+ pub y: u8,
+ pub x: u16,
}
impl GameState {
@@ -93,7 +94,7 @@ impl GameState {
self.players[player_index].oils = self.players[player_index].oils.saturating_sub(1);
let player_position = self.players[player_index].position;
let mut obstacles = (*self.obstacles).clone();
- obstacles.push(Position {
+ obstacles.insert(Position {
x: player_position.x.saturating_sub(1),
y: player_position.y,
});
@@ -233,10 +234,10 @@ impl Player {
fn move_along(
&mut self,
- obstacles: &[Position],
- powerup_oils: &[Position],
- powerup_boosts: &[Position],
- finish_lines: &[Position],
+ obstacles: &BTreeSet<Position>,
+ powerup_oils: &BTreeSet<Position>,
+ powerup_boosts: &BTreeSet<Position>,
+ finish_lines: &BTreeSet<Position>,
) {
let start_x = self.position.x.saturating_add(1);
for x in start_x..=self.next_position.x {
diff --git a/vroomba-analysis/src/main.rs b/vroomba-analysis/src/main.rs
index 3abcfc6..a1145b7 100644
--- a/vroomba-analysis/src/main.rs
+++ b/vroomba-analysis/src/main.rs
@@ -53,7 +53,6 @@ fn main() {
// .map(|node| node.last_command)
// .collect::<Vec<_>>()
// );
- println!("{:?}", shortest_path.0);
println!("{}", shortest_path.1);
}