From 772b85b0b75e8c968b6c575d60966acc77bd74f9 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Mon, 4 Apr 2022 16:22:04 +0300 Subject: [PATCH] added seeding colors --- src/lib/game.rs | 5 ++++- src/lib/grid.rs | 35 +++++++++++++++++++++++------------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/src/lib/game.rs b/src/lib/game.rs index 76c82aa..23ffde1 100644 --- a/src/lib/game.rs +++ b/src/lib/game.rs @@ -10,6 +10,7 @@ pub struct Rules { spawn_per_turn: usize, controller: Box, clear_term: bool, + color_seed: u16, } impl Rules { @@ -36,6 +37,7 @@ impl Default for Rules { spawn_per_turn: 1, controller: Box::new(PlayerController::new()), clear_term: true, + color_seed: 35, } } } @@ -77,10 +79,11 @@ impl Game { size, spawn_per_turn, clear_term, + color_seed, } = rules; Self { - board: Grid::new(size), + board: Grid::new(size, color_seed), controller, spawn_per_turn, clear_term, diff --git a/src/lib/grid.rs b/src/lib/grid.rs index 9e1dab0..275f73f 100644 --- a/src/lib/grid.rs +++ b/src/lib/grid.rs @@ -39,9 +39,11 @@ impl Tile { const TILE_LENGTH: usize = 7; const TILE_HEIGHT: usize = 3; - pub fn display(&self) -> String { + pub fn display(&self, color_seed: u16) -> String { match self.value { - Some(value) => Self::color_representation(Self::display_number(value), value), + Some(value) => { + Self::color_representation(Self::display_number(value), value, color_seed) + } None => [ // empty tile " ", " ", " ", @@ -73,8 +75,8 @@ impl Tile { } } - fn color_representation(text: String, value: usize) -> String { - let color = Self::hashed_color(value); + fn color_representation(text: String, value: usize, color_seed: u16) -> String { + let color = Self::hashed_color(value, color_seed); let color_code = color::Bg(color); let reset_code = color::Bg(color::Reset); @@ -88,9 +90,9 @@ impl Tile { // [ | | ] - fn hashed_color(value: usize) -> color::Rgb { + fn hashed_color(value: usize, color_seed: u16) -> color::Rgb { let mut hasher = DefaultHasher::new(); - value.hash(&mut hasher); + (value + color_seed as usize).hash(&mut hasher); let hash = hasher.finish(); // SAFETY: // there are no logic that relies on the value of the outputted numbers, thus it is safe to create them without constructors @@ -101,9 +103,9 @@ impl Tile { .try_into() .unwrap(); - let mut remaining = 300f64; - let r = Self::take_fraction(&mut remaining, frac_a, 200.) as u8; - let g = Self::take_fraction(&mut remaining, frac_b, 200.) as u8; + let mut remaining = 255f64; + let r = Self::take_fraction(&mut remaining, frac_a, 150.) as u8; + let g = Self::take_fraction(&mut remaining, frac_b, 150.) as u8; let b = remaining as u8; color::Rgb(r, g, b) } @@ -119,17 +121,22 @@ impl Tile { pub struct Grid { size: usize, tiles: Vec>, + color_seed: u16, } impl Grid { /// /// constructor /// - pub fn new(size: usize) -> Self { + pub fn new(size: usize, color_seed: u16) -> Self { let tiles = (0..size) .map(|_| (0..size).map(|_| Tile::new_empty()).collect()) .collect(); - Self { size, tiles } + Self { + size, + tiles, + color_seed, + } } /// @@ -195,7 +202,11 @@ impl Grid { let tiles: Vec> = self .tiles .iter() - .map(|row| row.iter().map(|tile| tile.display()).collect()) + .map(|row| { + row.iter() + .map(|tile| tile.display(self.color_seed)) + .collect() + }) .collect(); let row_representations: Vec<_> = tiles .iter()