2023-07-25 02:50:40 +00:00
|
|
|
use std::collections::HashSet;
|
2023-07-22 23:43:49 +00:00
|
|
|
use std::fmt;
|
|
|
|
use std::fmt::{Formatter, Write};
|
2023-07-25 02:50:40 +00:00
|
|
|
use std::str::FromStr;
|
2023-07-22 23:43:49 +00:00
|
|
|
|
|
|
|
pub const GRID_LENGTH: u8 = 15;
|
|
|
|
pub const TRAY_LENGTH: u8 = 7;
|
|
|
|
pub const ALL_LETTERS_BONUS: u32 = 50;
|
2023-07-22 03:18:06 +00:00
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
enum Direction {
|
|
|
|
Row, Column
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Direction {
|
|
|
|
fn invert(&self) -> Self {
|
|
|
|
match &self {
|
|
|
|
Direction::Row => {Direction::Column}
|
|
|
|
Direction::Column => {Direction::Row}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub struct Coordinates (pub u8, pub u8);
|
|
|
|
|
|
|
|
impl Coordinates {
|
|
|
|
fn add(&self, direction: Direction, i: i8) -> Option<Self> {
|
|
|
|
let proposed = match direction {
|
|
|
|
Direction::Column => {(self.0 as i8, self.1 as i8+i)}
|
|
|
|
Direction::Row => {(self.0 as i8+i, self.1 as i8)}
|
|
|
|
};
|
|
|
|
|
|
|
|
if proposed.0 < 0 || proposed.0 >= GRID_LENGTH as i8 || proposed.1 < 0 || proposed.1 >= GRID_LENGTH as i8 {
|
|
|
|
None
|
|
|
|
} else{
|
|
|
|
Some(Coordinates(proposed.0 as u8, proposed.1 as u8))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increment(&self, direction: Direction) -> Option<Self>{
|
|
|
|
self.add(direction, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decrement(&self, direction: Direction) -> Option<Self>{
|
|
|
|
self.add(direction, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_to_index(&self) -> usize {
|
|
|
|
(self.0 + GRID_LENGTH*self.1) as usize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:18:06 +00:00
|
|
|
#[derive(Debug)]
|
2023-07-28 04:25:50 +00:00
|
|
|
pub struct Letter {
|
|
|
|
text: char,
|
|
|
|
points: u32,
|
|
|
|
ephemeral: bool,
|
|
|
|
is_blank: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Letter {
|
|
|
|
pub fn new_fixed(text: char, points: u32) -> Self {
|
|
|
|
Letter {
|
|
|
|
text,
|
|
|
|
points,
|
|
|
|
ephemeral: false,
|
|
|
|
is_blank: false,
|
|
|
|
}
|
|
|
|
}
|
2023-07-22 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-22 23:43:49 +00:00
|
|
|
pub enum CellType {
|
2023-07-22 03:18:06 +00:00
|
|
|
Normal,
|
|
|
|
DoubleWord,
|
|
|
|
DoubleLetter,
|
|
|
|
TripleLetter,
|
|
|
|
TripleWord,
|
|
|
|
Start,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
2023-07-22 23:43:49 +00:00
|
|
|
pub struct Cell {
|
|
|
|
pub value: Option<Letter>,
|
2023-07-22 03:18:06 +00:00
|
|
|
cell_type: CellType,
|
2023-07-28 04:25:50 +00:00
|
|
|
coordinates: Coordinates,
|
2023-07-22 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-25 02:50:40 +00:00
|
|
|
pub struct Dictionary {
|
|
|
|
words: Vec<String>,
|
|
|
|
scores: Vec<f64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Dictionary {
|
|
|
|
fn new() -> Self {
|
|
|
|
let mut reader = csv::Reader::from_path("resources/dictionary.csv").unwrap();
|
|
|
|
let mut words: Vec<String> = Vec::new();
|
|
|
|
let mut scores: Vec<f64> = Vec::new();
|
|
|
|
|
|
|
|
for result in reader.records() {
|
|
|
|
let record = result.unwrap();
|
|
|
|
words.push(record.get(0).unwrap().to_string());
|
|
|
|
let score = record.get(1).unwrap();
|
|
|
|
scores.push(f64::from_str(score).unwrap());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary {
|
|
|
|
words,
|
|
|
|
scores,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter_to_sub_dictionary(&self, proportion: f64) -> Self {
|
|
|
|
let mut words: Vec<String> = Vec::new();
|
|
|
|
let mut scores: Vec<f64> = Vec::new();
|
|
|
|
|
|
|
|
for (word, score) in self.words.iter().zip(self.scores.iter()) {
|
|
|
|
if *score >= proportion {
|
|
|
|
words.push(word.clone());
|
|
|
|
scores.push(*score);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary {words, scores}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn substring_set(&self) -> HashSet<&str> {
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
|
|
|
|
for word in self.words.iter() {
|
|
|
|
for j in 0..word.len() {
|
|
|
|
for k in (j+1)..(word.len()+1) {
|
|
|
|
set.insert(&word[j..k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:18:06 +00:00
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Board {
|
|
|
|
cells: Vec<Cell>,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Word<'a> {
|
|
|
|
cells: Vec<&'a Cell>,
|
|
|
|
coords: Coordinates,
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:18:06 +00:00
|
|
|
impl Board {
|
2023-07-22 23:43:49 +00:00
|
|
|
pub fn new() -> Self {
|
2023-07-22 03:18:06 +00:00
|
|
|
let mut cells = Vec::new();
|
|
|
|
|
|
|
|
|
|
|
|
/// Since the board is symmetrical in both directions for the purposes of our logic we can keep our coordinates in one corner
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `x`: A coordinate
|
|
|
|
///
|
|
|
|
/// returns: u8 The coordinate mapped onto the lower-half
|
|
|
|
fn map_to_corner(x: u8) -> u8 {
|
|
|
|
return if x > GRID_LENGTH / 2 {
|
|
|
|
GRID_LENGTH - x - 1
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
for i_orig in 0..GRID_LENGTH {
|
|
|
|
let i = map_to_corner(i_orig);
|
|
|
|
for j_orig in 0..GRID_LENGTH {
|
|
|
|
let j = map_to_corner(j_orig);
|
2023-07-22 03:18:06 +00:00
|
|
|
|
|
|
|
let mut typee = CellType::Normal;
|
|
|
|
|
|
|
|
// double word scores are diagonals
|
|
|
|
if i == j {
|
|
|
|
typee = CellType::DoubleWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Triple letters
|
|
|
|
if (i % 4 == 1) && j % 4 == 1 && !(i == 1 && j == 1) {
|
|
|
|
typee = CellType::TripleLetter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Double letters
|
|
|
|
if (i % 4 == 2) && (j % 4 == 2) && !(
|
|
|
|
i == 2 && j == 2
|
|
|
|
) {
|
|
|
|
typee = CellType::DoubleLetter;
|
|
|
|
}
|
|
|
|
if (i.min(j) == 0 && i.max(j) == 3) || (i.min(j)==3 && i.max(j) == 7) {
|
|
|
|
typee = CellType::DoubleLetter;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Triple word scores
|
|
|
|
if (i % 7 == 0) && (j % 7 == 0) {
|
|
|
|
typee = CellType::TripleWord;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start
|
|
|
|
if i == 7 && j == 7 {
|
|
|
|
typee = CellType::Start;
|
|
|
|
}
|
|
|
|
|
|
|
|
cells.push(Cell {
|
|
|
|
cell_type: typee,
|
|
|
|
value: None,
|
2023-07-28 04:25:50 +00:00
|
|
|
coordinates: Coordinates(i, j),
|
2023-07-22 03:18:06 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Board {cells}
|
|
|
|
}
|
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
pub fn get_cell(&self, coordinates: Coordinates) -> Result<&Cell, &str> {
|
|
|
|
if coordinates.0 >= GRID_LENGTH || coordinates.1 >= GRID_LENGTH {
|
2023-07-22 03:18:06 +00:00
|
|
|
Err("x & y must be within the board's coordinates")
|
|
|
|
} else {
|
2023-07-28 04:25:50 +00:00
|
|
|
let index = coordinates.map_to_index();
|
|
|
|
Ok(self.cells.get(index).unwrap())
|
2023-07-22 03:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-22 23:43:49 +00:00
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
pub fn get_cell_mut(&mut self, coordinates: Coordinates) -> Result<&mut Cell, &str> {
|
|
|
|
if coordinates.0 >= GRID_LENGTH || coordinates.1 >= GRID_LENGTH {
|
2023-07-22 23:43:49 +00:00
|
|
|
Err("x & y must be within the board's coordinates")
|
|
|
|
} else {
|
2023-07-28 04:25:50 +00:00
|
|
|
let index = coordinates.map_to_index();
|
|
|
|
Ok(self.cells.get_mut(index).unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn score_move(&self) -> Result<u32, &str> {
|
|
|
|
// We don't assume that the move is valid, so let's first establish that
|
|
|
|
|
|
|
|
|
|
|
|
// Let's first establish what rows and columns tiles were played in
|
|
|
|
let mut rows_played = HashSet::with_capacity(15);
|
|
|
|
let mut columns_played = HashSet::with_capacity(15);
|
|
|
|
let mut tiles_played = 0;
|
|
|
|
|
|
|
|
for x in 0..GRID_LENGTH {
|
|
|
|
for y in 0..GRID_LENGTH {
|
|
|
|
let coords = Coordinates(x, y);
|
|
|
|
let cell = self.get_cell(coords).unwrap();
|
|
|
|
match &cell.value {
|
|
|
|
Some(value) => {
|
|
|
|
if value.ephemeral {
|
|
|
|
rows_played.insert(x);
|
|
|
|
columns_played.insert(y);
|
|
|
|
tiles_played += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows_played.is_empty() {
|
|
|
|
return Err("Tiles need to be played")
|
|
|
|
} else if rows_played.len() > 1 && columns_played.len() > 1 {
|
|
|
|
return Err("Tiles need to be played on one row or column")
|
|
|
|
}
|
|
|
|
|
|
|
|
let direction = if rows_played.len() > 1 {
|
|
|
|
Direction::Column
|
|
|
|
} else {
|
|
|
|
Direction::Row
|
|
|
|
};
|
|
|
|
|
|
|
|
let starting_row = *rows_played.iter().min().unwrap();
|
|
|
|
let starting_column = *columns_played.iter().min().unwrap();
|
|
|
|
|
|
|
|
let mut starting_coords = Coordinates(starting_row, starting_column);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// At this point we now know that we're at the start of the word and we have the direction.
|
|
|
|
// Now we'll head forward and look for every word that intersects one of the played tiles
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_word(&self, mut start_coords: Coordinates, direction: Direction) -> Option<Word> {
|
|
|
|
// let's see how far we can backtrack to the start of the word
|
|
|
|
let mut times_moved = 0;
|
|
|
|
loop {
|
|
|
|
let one_back = start_coords.add(direction, -times_moved);
|
|
|
|
match one_back {
|
|
|
|
None => { break }
|
|
|
|
Some(new_coords) => {
|
|
|
|
let cell = self.get_cell(new_coords).unwrap();
|
|
|
|
if cell.value.is_some(){
|
|
|
|
times_moved += 1;
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if times_moved == 0 {
|
|
|
|
return None;
|
2023-07-22 23:43:49 +00:00
|
|
|
}
|
2023-07-28 04:25:50 +00:00
|
|
|
|
|
|
|
start_coords = start_coords.add(direction, -times_moved + 1).unwrap();
|
|
|
|
|
|
|
|
// since we moved and we know that start_coords has started on a letter, we know we have a word
|
|
|
|
// we'll now keep track of the cells that form it
|
|
|
|
let mut cells = Vec::with_capacity(GRID_LENGTH as usize);
|
|
|
|
cells.push(self.get_cell(start_coords).unwrap());
|
|
|
|
|
|
|
|
loop {
|
|
|
|
let position = start_coords.add(direction, cells.len() as i8);
|
|
|
|
match position {
|
|
|
|
None => {break}
|
|
|
|
Some(x) => {
|
|
|
|
let cell = self.get_cell(x).unwrap();
|
|
|
|
match cell.value {
|
|
|
|
None => {break}
|
|
|
|
Some(_) => {
|
|
|
|
cells.push(cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(Word {
|
|
|
|
cells,
|
|
|
|
coords: start_coords,
|
|
|
|
})
|
2023-07-22 23:43:49 +00:00
|
|
|
}
|
2023-07-28 04:25:50 +00:00
|
|
|
|
|
|
|
|
2023-07-22 23:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Board {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
|
|
|
|
|
|
|
let mut str = String::new();
|
|
|
|
|
|
|
|
let normal = "\x1b[48;5;174m\x1b[38;5;0m";
|
|
|
|
let triple_word = "\x1b[48;5;196m\x1b[38;5;0m";
|
|
|
|
let double_word = "\x1b[48;5;204m\x1b[38;5;0m";
|
|
|
|
let triple_letter = "\x1b[48;5;21m\x1b[38;5;15m";
|
|
|
|
let double_letter = "\x1b[48;5;51m\x1b[38;5;0m";
|
|
|
|
str.write_char('\n').unwrap();
|
|
|
|
|
|
|
|
for x in 0..GRID_LENGTH {
|
|
|
|
for y in 0..GRID_LENGTH {
|
2023-07-28 04:25:50 +00:00
|
|
|
let coords = Coordinates(x, y);
|
|
|
|
|
|
|
|
let cell = self.get_cell(coords).unwrap();
|
2023-07-22 23:43:49 +00:00
|
|
|
|
|
|
|
let color = match cell.cell_type {
|
|
|
|
CellType::Normal => {normal}
|
|
|
|
CellType::DoubleWord => {double_word}
|
|
|
|
CellType::DoubleLetter => {double_letter}
|
|
|
|
CellType::TripleLetter => {triple_letter}
|
|
|
|
CellType::TripleWord => {triple_word}
|
|
|
|
CellType::Start => {double_word}
|
|
|
|
};
|
|
|
|
|
|
|
|
let content = match &cell.value {
|
|
|
|
None => {' '}
|
2023-07-28 04:25:50 +00:00
|
|
|
Some(letter) => {letter.text}
|
2023-07-22 23:43:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
str.write_str(color).unwrap();
|
|
|
|
str.write_char(content).unwrap();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
str.write_str("\x1b[0m\n").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
write!(f, "{}", str)
|
|
|
|
|
|
|
|
}
|
2023-07-22 03:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cell_types() {
|
|
|
|
let board = Board::new();
|
|
|
|
|
2023-07-28 04:25:50 +00:00
|
|
|
assert!(matches!(board.get_cell(Coordinates(0, 0)).unwrap().cell_type, CellType::TripleWord));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(1, 0)).unwrap().cell_type, CellType::Normal));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(0, 1)).unwrap().cell_type, CellType::Normal));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(1, 1)).unwrap().cell_type, CellType::DoubleWord));
|
|
|
|
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(13, 13)).unwrap().cell_type, CellType::DoubleWord));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(14, 14)).unwrap().cell_type, CellType::TripleWord));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(11, 14)).unwrap().cell_type, CellType::DoubleLetter));
|
|
|
|
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(7, 7)).unwrap().cell_type, CellType::Start));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(8, 6)).unwrap().cell_type, CellType::DoubleLetter));
|
|
|
|
assert!(matches!(board.get_cell(Coordinates(5, 9)).unwrap().cell_type, CellType::TripleLetter));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_word_finding() {
|
|
|
|
let mut board = Board::new();
|
|
|
|
|
|
|
|
board.get_cell_mut(Coordinates(8, 6)).unwrap().value = Some(Letter::new_fixed('J', 0));
|
|
|
|
board.get_cell_mut(Coordinates(8, 7)).unwrap().value = Some(Letter::new_fixed('O', 0));
|
|
|
|
board.get_cell_mut(Coordinates(8, 8)).unwrap().value = Some(Letter::new_fixed( 'E', 0));
|
|
|
|
board.get_cell_mut(Coordinates(8, 9)).unwrap().value = Some(Letter::new_fixed( 'L', 0));
|
|
|
|
|
|
|
|
board.get_cell_mut(Coordinates(0, 0)).unwrap().value = Some(Letter::new_fixed('I', 0));
|
|
|
|
board.get_cell_mut(Coordinates(1, 0)).unwrap().value = Some(Letter::new_fixed('S', 0));
|
|
|
|
|
|
|
|
board.get_cell_mut(Coordinates(3, 0)).unwrap().value = Some(Letter::new_fixed('C', 0));
|
|
|
|
board.get_cell_mut(Coordinates(4, 0)).unwrap().value = Some(Letter::new_fixed('O', 0));
|
|
|
|
board.get_cell_mut(Coordinates(5, 0)).unwrap().value = Some(Letter::new_fixed('O', 0));
|
|
|
|
board.get_cell_mut(Coordinates(6, 0)).unwrap().value = Some(Letter::new_fixed('L', 0));
|
|
|
|
|
|
|
|
|
|
|
|
board.get_cell_mut(Coordinates(9, 8)).unwrap().value = Some(Letter::new_fixed( 'G', 0));
|
|
|
|
board.get_cell_mut(Coordinates(10, 8)).unwrap().value = Some(Letter::new_fixed( 'G', 0));
|
|
|
|
|
|
|
|
fn word_to_text(word: Word) -> String {
|
|
|
|
let mut text = String::with_capacity(word.cells.len());
|
|
|
|
for cell in word.cells {
|
|
|
|
text.push(cell.value.as_ref().unwrap().text);
|
|
|
|
}
|
|
|
|
|
|
|
|
text
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in vec![6, 7, 8, 9] {
|
|
|
|
println!("x is {}", x);
|
|
|
|
let first_word = board.find_word(Coordinates(8, x), Direction::Column);
|
|
|
|
match first_word {
|
|
|
|
None => {panic!("Expected to find word JOEL")}
|
|
|
|
Some(x) => {
|
|
|
|
assert_eq!(x.coords.0, 8);
|
|
|
|
assert_eq!(x.coords.1, 6);
|
|
|
|
|
|
|
|
assert_eq!(word_to_text(x), "JOEL");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let single_letter_word = board.find_word(Coordinates(8, 9), Direction::Row);
|
|
|
|
match single_letter_word {
|
|
|
|
None => {panic!("Expected to find letter L")}
|
|
|
|
Some(x) => {
|
|
|
|
assert_eq!(x.coords.0, 8);
|
|
|
|
assert_eq!(x.coords.1, 9);
|
|
|
|
|
|
|
|
assert_eq!(word_to_text(x), "L");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in vec![0, 1] {
|
|
|
|
println!("x is {}", x);
|
|
|
|
let word = board.find_word(Coordinates(x, 0), Direction::Row);
|
|
|
|
match word {
|
|
|
|
None => {panic!("Expected to find word IS")}
|
|
|
|
Some(x) => {
|
|
|
|
assert_eq!(x.coords.0, 0);
|
|
|
|
assert_eq!(x.coords.1, 0);
|
|
|
|
|
|
|
|
assert_eq!(word_to_text(x), "IS");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for x in vec![3, 4, 5, 6] {
|
|
|
|
println!("x is {}", x);
|
|
|
|
let word = board.find_word(Coordinates(x, 0), Direction::Row);
|
|
|
|
match word {
|
|
|
|
None => {panic!("Expected to find word COOL")}
|
|
|
|
Some(x) => {
|
|
|
|
assert_eq!(x.coords.0, 3);
|
|
|
|
assert_eq!(x.coords.1, 0);
|
|
|
|
|
|
|
|
assert_eq!(word_to_text(x), "COOL");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let no_word = board.find_word(Coordinates(2, 0), Direction::Row);
|
|
|
|
assert!(no_word.is_none());
|
|
|
|
|
|
|
|
let word = board.find_word(Coordinates(10, 8), Direction::Row);
|
|
|
|
match word {
|
|
|
|
None => {panic!("Expected to find word EGG")}
|
|
|
|
Some(x) => {
|
|
|
|
assert_eq!(x.coords.0, 8);
|
|
|
|
assert_eq!(x.coords.1, 8);
|
|
|
|
|
|
|
|
assert_eq!(word_to_text(x), "EGG");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-22 03:18:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
2023-07-25 02:50:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dictionary() {
|
|
|
|
let dictionary = Dictionary::new();
|
|
|
|
|
|
|
|
assert_eq!(dictionary.words.len(), dictionary.scores.len());
|
|
|
|
assert_eq!(dictionary.words.len(), 279429);
|
|
|
|
|
|
|
|
assert_eq!(dictionary.words.get(0).unwrap(), "AA");
|
|
|
|
assert_eq!(dictionary.words.get(9).unwrap(), "AARDVARK");
|
|
|
|
|
|
|
|
assert!((dictionary.scores.get(9).unwrap() - 0.5798372).abs() < 0.0001)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_dictionary_sets() {
|
|
|
|
let dictionary = Dictionary {
|
|
|
|
words: vec!["JOEL".to_string(), "JOHN".to_string(), "XYZ".to_string()],
|
|
|
|
scores: vec![0.7, 0.5, 0.1],
|
|
|
|
};
|
|
|
|
|
|
|
|
let dictionary = dictionary.filter_to_sub_dictionary(0.3);
|
|
|
|
assert_eq!(dictionary.words.len(), 2);
|
|
|
|
assert_eq!(dictionary.words.get(0).unwrap(), "JOEL");
|
|
|
|
assert_eq!(dictionary.words.get(1).unwrap(), "JOHN");
|
|
|
|
|
|
|
|
let set = dictionary.substring_set();
|
|
|
|
|
|
|
|
assert!(set.contains("J"));
|
|
|
|
assert!(set.contains("O"));
|
|
|
|
assert!(set.contains("E"));
|
|
|
|
assert!(set.contains("L"));
|
|
|
|
assert!(set.contains("H"));
|
|
|
|
assert!(set.contains("N"));
|
|
|
|
|
|
|
|
assert!(set.contains("JO"));
|
|
|
|
assert!(set.contains("OE"));
|
|
|
|
assert!(set.contains("EL"));
|
|
|
|
assert!(set.contains("OH"));
|
|
|
|
assert!(set.contains("HN"));
|
|
|
|
|
|
|
|
assert!(set.contains("JOE"));
|
|
|
|
assert!(set.contains("OEL"));
|
|
|
|
assert!(set.contains("JOH"));
|
|
|
|
assert!(set.contains("OHN"));
|
|
|
|
|
|
|
|
|
|
|
|
assert!(!set.contains("XY"));
|
|
|
|
assert!(!set.contains("JH"));
|
|
|
|
assert!(!set.contains("JE"));
|
|
|
|
|
|
|
|
}
|
2023-07-22 03:18:06 +00:00
|
|
|
}
|