From e3ba7ac440c8cd7456ed59f5d06835bcfdf76628 Mon Sep 17 00:00:00 2001 From: Joel Therrien Date: Wed, 2 Aug 2023 20:00:01 -0700 Subject: [PATCH] Add tray and tile pool --- Cargo.toml | 2 +- src/board.rs | 29 ++++++++++-- src/constants.rs | 52 +++++++++++++++++++++- src/lib.rs | 1 + src/player_interaction.rs | 86 ++++++++++++++++++++++++++++++++++++ src/player_interaction/ai.rs | 0 6 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 src/player_interaction.rs create mode 100644 src/player_interaction/ai.rs diff --git a/Cargo.toml b/Cargo.toml index 4d5e9dd..8b37f87 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] csv = "1.2.2" - +rand = {version = "0.8.5", features = ["small_rng"]} diff --git a/src/board.rs b/src/board.rs index 0a00748..05752b2 100644 --- a/src/board.rs +++ b/src/board.rs @@ -50,10 +50,10 @@ impl Coordinates { #[derive(Debug, Copy, Clone)] pub struct Letter { - text: char, - points: u32, - ephemeral: bool, - is_blank: bool, + pub text: char, + pub points: u32, + pub ephemeral: bool, + pub is_blank: bool, } impl Letter { @@ -65,6 +65,27 @@ impl Letter { is_blank: false, } } + + pub fn new(text: Option, points: u32) -> Letter { + match text { + None => { + Letter { + text: ' ', + points, + ephemeral: true, + is_blank: true, + } + } + Some(text) => { + Letter { + text, + points, + ephemeral: true, + is_blank: false, + } + } + } + } } #[derive(Debug)] diff --git a/src/constants.rs b/src/constants.rs index 7e3d29f..08d5b77 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,3 +1,53 @@ +use rand::prelude::SliceRandom; +use rand::{Rng}; +use crate::board::Letter; + pub const GRID_LENGTH: u8 = 15; pub const TRAY_LENGTH: u8 = 7; -pub const ALL_LETTERS_BONUS: u32 = 50; \ No newline at end of file +pub const ALL_LETTERS_BONUS: u32 = 50; + +pub fn standard_tile_pool(rng: Option<&mut R>) -> Vec { + let mut letters = Vec::new(); + + fn add_n_times(vector: &mut Vec, letter: Letter, n: usize) { + for _ in 0..n { + vector.push(letter); + } + } + + add_n_times(&mut letters, Letter::new(Some('A'), 1), 9); + add_n_times(&mut letters, Letter::new(Some('B'), 3), 2); + add_n_times(&mut letters, Letter::new(Some('C'), 3), 2); + add_n_times(&mut letters, Letter::new(Some('D'), 2), 4); + add_n_times(&mut letters, Letter::new(Some('E'), 1), 12); + add_n_times(&mut letters, Letter::new(Some('F'), 4), 2); + add_n_times(&mut letters, Letter::new(Some('G'), 2), 3); + add_n_times(&mut letters, Letter::new(Some('H'), 4), 2); + add_n_times(&mut letters, Letter::new(Some('I'), 1), 9); + add_n_times(&mut letters, Letter::new(Some('J'), 8), 1); + add_n_times(&mut letters, Letter::new(Some('K'), 5), 1); + add_n_times(&mut letters, Letter::new(Some('L'), 1), 4); + add_n_times(&mut letters, Letter::new(Some('M'), 3), 2); + add_n_times(&mut letters, Letter::new(Some('N'), 1), 6); + add_n_times(&mut letters, Letter::new(Some('O'), 1), 8); + add_n_times(&mut letters, Letter::new(Some('P'), 3), 2); + add_n_times(&mut letters, Letter::new(Some('Q'), 10), 1); + add_n_times(&mut letters, Letter::new(Some('R'), 1), 6); + add_n_times(&mut letters, Letter::new(Some('S'), 1), 4); + add_n_times(&mut letters, Letter::new(Some('T'), 1), 6); + add_n_times(&mut letters, Letter::new(Some('U'), 1), 4); + add_n_times(&mut letters, Letter::new(Some('V'), 4), 2); + add_n_times(&mut letters, Letter::new(Some('W'), 4), 2); + add_n_times(&mut letters, Letter::new(Some('X'), 8), 1); + add_n_times(&mut letters, Letter::new(Some('Y'), 4), 2); + add_n_times(&mut letters, Letter::new(Some('Z'), 10), 1); + + add_n_times(&mut letters, Letter::new(None, 0), 2); + + if rng.is_some() { + letters.shuffle(rng.unwrap()); + } + + letters + +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 108ab85..3df9d6c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,4 +2,5 @@ pub mod constants; pub mod board; pub mod dictionary; +pub mod player_interaction; diff --git a/src/player_interaction.rs b/src/player_interaction.rs new file mode 100644 index 0000000..dd0d825 --- /dev/null +++ b/src/player_interaction.rs @@ -0,0 +1,86 @@ +use crate::board::Letter; + +pub mod ai; + +#[derive(Debug)] +pub struct Tray { + pub letters: Vec> +} + +impl Tray { + pub fn new(tray_length: u8) -> Self { + let mut letters = Vec::with_capacity(tray_length as usize); + for _ in 0..tray_length { + letters.push(None); + } + Tray { + letters + } + } + + pub fn fill(&mut self, standard_tile_pool: &mut Vec) { + for letter in self.letters.iter_mut() { + if letter.is_none() { + *letter = standard_tile_pool.pop(); + } + } + } + + pub fn remove(&mut self, indices: Vec) { + for i in indices { + *self.letters.get_mut(i as usize).unwrap() = None; + } + } + + +} + +#[cfg(test)] +mod tests { + use super::*; + + + #[test] + fn test_tray() { + let mut letters = vec![ + Letter::new(Some('E'), 3), + Letter::new(Some('O'), 2), + Letter::new(Some('J'), 1) + ]; + + let mut tray = Tray::new(5); + + assert_eq!(tray.letters.len(), 5); + for letter in &tray.letters { + assert!(letter.is_none()); + } + + *tray.letters.get_mut(3).unwrap() = Some(Letter::new(Some('Z'), 10)); + + println!("{:?}", tray); + + assert!(tray.letters.get(3).unwrap().is_some()); + + tray.fill(&mut letters); + assert!(letters.is_empty()); + + println!("{:?}", tray); + + assert_eq!(tray.letters.get(0).unwrap().unwrap().text, 'J'); + assert_eq!(tray.letters.get(1).unwrap().unwrap().text, 'O'); + assert_eq!(tray.letters.get(2).unwrap().unwrap().text, 'E'); + assert_eq!(tray.letters.get(3).unwrap().unwrap().text, 'Z'); + assert!(tray.letters.get(4).unwrap().is_none()); + + tray.remove(vec![3, 4]); + + assert_eq!(tray.letters.get(0).unwrap().unwrap().text, 'J'); + assert_eq!(tray.letters.get(1).unwrap().unwrap().text, 'O'); + assert_eq!(tray.letters.get(2).unwrap().unwrap().text, 'E'); + assert!(tray.letters.get(3).unwrap().is_none()); + assert!(tray.letters.get(4).unwrap().is_none()); + + + } + +} \ No newline at end of file diff --git a/src/player_interaction/ai.rs b/src/player_interaction/ai.rs new file mode 100644 index 0000000..e69de29