diff --git a/rs48_lib/Cargo.toml b/rs48_lib/Cargo.toml index 45b16d6..4811655 100644 --- a/rs48_lib/Cargo.toml +++ b/rs48_lib/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rs48_lib" -version = "1.2.0" +version = "1.3.0" edition = "2021" description = "components of rs48" license = "MIT" @@ -8,4 +8,5 @@ authors = ["JOLIMAITRE Matthieu "] [dependencies] rand = "0.8" +rayon = "1.5.3" termion = "1.5" diff --git a/rs48_lib/src/controller/player.rs b/rs48_lib/src/controller/player.rs index 27839c1..a9f3547 100644 --- a/rs48_lib/src/controller/player.rs +++ b/rs48_lib/src/controller/player.rs @@ -4,14 +4,9 @@ use termion::{event::Key, input::TermRead, raw::IntoRawMode}; use super::{Controller, ControllerError, Move}; use crate::game::Game; +#[derive(Debug, Default)] pub struct PlayerController; -impl PlayerController { - pub fn new() -> Self { - Self - } -} - impl Controller for PlayerController { fn next_move(&mut self, _game: &Game) -> Result { let stdin = stdin(); diff --git a/rs48_lib/src/controller/random.rs b/rs48_lib/src/controller/random.rs index bb21104..0490130 100644 --- a/rs48_lib/src/controller/random.rs +++ b/rs48_lib/src/controller/random.rs @@ -3,14 +3,9 @@ use rand::random; use super::{Controller, ControllerError, Move}; use crate::game::Game; +#[derive(Debug, Default)] pub struct RandomController; -impl RandomController { - pub fn new() -> Self { - Self - } -} - impl Controller for RandomController { fn next_move(&mut self, _game: &Game) -> Result { let movement = random(); diff --git a/rs48_lib/src/controller/simulated.rs b/rs48_lib/src/controller/simulated.rs index 73aa53e..41826a7 100644 --- a/rs48_lib/src/controller/simulated.rs +++ b/rs48_lib/src/controller/simulated.rs @@ -1,12 +1,11 @@ +use std::ops::Add; + +use rayon::prelude::{IntoParallelIterator, ParallelIterator}; + use crate::{game::Game, prelude::RandomController}; use super::{Controller, ControllerError, Move}; -pub enum Objective { - Score, - TileCount, -} - pub struct SimulatedController { simulations_per_move: usize, length_of_simulation: usize, @@ -26,35 +25,33 @@ impl Controller for SimulatedController { let initial_score = game.get_score(); let mut scores: Vec<_> = Move::all() - .into_iter() + .into_par_iter() .map(|initial_move| { - let sim_scores = (0..self.simulations_per_move).map(|_| { + let sim_scores = (0..self.simulations_per_move).into_par_iter().map(|_| { let mut game = game.clone(); + game.turn(initial_move.clone()).ok(); - let mut controller = RandomController::new(); + let mut controller = RandomController::default(); for _ in 1..self.length_of_simulation { let movement = controller.next_move(&game).ok(); let result = movement.and_then(|movement| game.turn(movement).ok()); if result.is_none() { - continue; + break; } } game.get_score() - initial_score }); - let mut res = 0; - let mut it = 0; - for score in sim_scores { - it += 1; - res += score; - } - - (initial_move, res / it) + let sim_scores: Vec<_> = sim_scores.collect(); + let avg = + sim_scores.iter().cloned().reduce(Add::add).unwrap_or(0) / sim_scores.len(); + (initial_move, avg) }) .collect(); - scores.sort_by(|(_, a), (_, b)| a.cmp(b)); + scores.sort_by(|(_, a), (_, b)| b.cmp(a)); - let (m, _) = scores.last().unwrap(); + dbg!(&scores); + let (m, _) = scores.first().unwrap(); Ok(m.clone()) } } diff --git a/rs48_lib/src/game.rs b/rs48_lib/src/game.rs index d3275f5..467e2ce 100644 --- a/rs48_lib/src/game.rs +++ b/rs48_lib/src/game.rs @@ -69,7 +69,7 @@ impl Game { let Rules { size, spawn_per_turn, - } = rules.clone(); + } = rules; Self { board: Grid::new(size),