Add tray and tile pool

This commit is contained in:
Joel Therrien 2023-08-02 20:00:01 -07:00
parent 55f96f32f5
commit e3ba7ac440
6 changed files with 164 additions and 6 deletions

View file

@ -7,4 +7,4 @@ edition = "2021"
[dependencies] [dependencies]
csv = "1.2.2" csv = "1.2.2"
rand = {version = "0.8.5", features = ["small_rng"]}

View file

@ -50,10 +50,10 @@ impl Coordinates {
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct Letter { pub struct Letter {
text: char, pub text: char,
points: u32, pub points: u32,
ephemeral: bool, pub ephemeral: bool,
is_blank: bool, pub is_blank: bool,
} }
impl Letter { impl Letter {
@ -65,6 +65,27 @@ impl Letter {
is_blank: false, is_blank: false,
} }
} }
pub fn new(text: Option<char>, 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)] #[derive(Debug)]

View file

@ -1,3 +1,53 @@
use rand::prelude::SliceRandom;
use rand::{Rng};
use crate::board::Letter;
pub const GRID_LENGTH: u8 = 15; pub const GRID_LENGTH: u8 = 15;
pub const TRAY_LENGTH: u8 = 7; pub const TRAY_LENGTH: u8 = 7;
pub const ALL_LETTERS_BONUS: u32 = 50; pub const ALL_LETTERS_BONUS: u32 = 50;
pub fn standard_tile_pool<R: Rng>(rng: Option<&mut R>) -> Vec<Letter> {
let mut letters = Vec::new();
fn add_n_times(vector: &mut Vec<Letter>, 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
}

View file

@ -2,4 +2,5 @@
pub mod constants; pub mod constants;
pub mod board; pub mod board;
pub mod dictionary; pub mod dictionary;
pub mod player_interaction;

86
src/player_interaction.rs Normal file
View file

@ -0,0 +1,86 @@
use crate::board::Letter;
pub mod ai;
#[derive(Debug)]
pub struct Tray {
pub letters: Vec<Option<Letter>>
}
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<Letter>) {
for letter in self.letters.iter_mut() {
if letter.is_none() {
*letter = standard_tile_pool.pop();
}
}
}
pub fn remove(&mut self, indices: Vec<u8>) {
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());
}
}

View file