added seeding colors

This commit is contained in:
JOLIMAITRE Matthieu 2022-04-04 16:22:04 +03:00
parent 855cdbd235
commit 772b85b0b7
2 changed files with 27 additions and 13 deletions

View file

@ -10,6 +10,7 @@ pub struct Rules {
spawn_per_turn: usize, spawn_per_turn: usize,
controller: Box<dyn Controller>, controller: Box<dyn Controller>,
clear_term: bool, clear_term: bool,
color_seed: u16,
} }
impl Rules { impl Rules {
@ -36,6 +37,7 @@ impl Default for Rules {
spawn_per_turn: 1, spawn_per_turn: 1,
controller: Box::new(PlayerController::new()), controller: Box::new(PlayerController::new()),
clear_term: true, clear_term: true,
color_seed: 35,
} }
} }
} }
@ -77,10 +79,11 @@ impl Game {
size, size,
spawn_per_turn, spawn_per_turn,
clear_term, clear_term,
color_seed,
} = rules; } = rules;
Self { Self {
board: Grid::new(size), board: Grid::new(size, color_seed),
controller, controller,
spawn_per_turn, spawn_per_turn,
clear_term, clear_term,

View file

@ -39,9 +39,11 @@ impl Tile {
const TILE_LENGTH: usize = 7; const TILE_LENGTH: usize = 7;
const TILE_HEIGHT: usize = 3; const TILE_HEIGHT: usize = 3;
pub fn display(&self) -> String { pub fn display(&self, color_seed: u16) -> String {
match self.value { 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 => [ None => [
// empty tile // empty tile
" ", " ", " ", " ", " ", " ",
@ -73,8 +75,8 @@ impl Tile {
} }
} }
fn color_representation(text: String, value: usize) -> String { fn color_representation(text: String, value: usize, color_seed: u16) -> String {
let color = Self::hashed_color(value); let color = Self::hashed_color(value, color_seed);
let color_code = color::Bg(color); let color_code = color::Bg(color);
let reset_code = color::Bg(color::Reset); 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(); let mut hasher = DefaultHasher::new();
value.hash(&mut hasher); (value + color_seed as usize).hash(&mut hasher);
let hash = hasher.finish(); let hash = hasher.finish();
// SAFETY: // SAFETY:
// there are no logic that relies on the value of the outputted numbers, thus it is safe to create them without constructors // 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() .try_into()
.unwrap(); .unwrap();
let mut remaining = 300f64; let mut remaining = 255f64;
let r = Self::take_fraction(&mut remaining, frac_a, 200.) as u8; let r = Self::take_fraction(&mut remaining, frac_a, 150.) as u8;
let g = Self::take_fraction(&mut remaining, frac_b, 200.) as u8; let g = Self::take_fraction(&mut remaining, frac_b, 150.) as u8;
let b = remaining as u8; let b = remaining as u8;
color::Rgb(r, g, b) color::Rgb(r, g, b)
} }
@ -119,17 +121,22 @@ impl Tile {
pub struct Grid { pub struct Grid {
size: usize, size: usize,
tiles: Vec<Vec<Tile>>, tiles: Vec<Vec<Tile>>,
color_seed: u16,
} }
impl Grid { impl Grid {
/// ///
/// constructor /// constructor
/// ///
pub fn new(size: usize) -> Self { pub fn new(size: usize, color_seed: u16) -> Self {
let tiles = (0..size) let tiles = (0..size)
.map(|_| (0..size).map(|_| Tile::new_empty()).collect()) .map(|_| (0..size).map(|_| Tile::new_empty()).collect())
.collect(); .collect();
Self { size, tiles } Self {
size,
tiles,
color_seed,
}
} }
/// ///
@ -195,7 +202,11 @@ impl Grid {
let tiles: Vec<Vec<_>> = self let tiles: Vec<Vec<_>> = self
.tiles .tiles
.iter() .iter()
.map(|row| row.iter().map(|tile| tile.display()).collect()) .map(|row| {
row.iter()
.map(|tile| tile.display(self.color_seed))
.collect()
})
.collect(); .collect();
let row_representations: Vec<_> = tiles let row_representations: Vec<_> = tiles
.iter() .iter()