1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::Error;
use std::fmt;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

const N: usize = 15;
const Q: usize = 1 + N / 2;

const _DEFAULT_QUARTER_BOARD: [&str; Q] = [
    "3l -- -- -- 3w -- -- 2l",
    "-- 2l -- -- -- 3l -- --",
    "-- -- 2w -- -- -- 2l --",
    "-- -- -- 3l -- -- -- 2w",
    "3w -- -- -- 2w -- 2l --",
    "-- 3l -- -- -- 3l -- --",
    "-- -- 2l -- 2l -- -- --",
    "2l -- -- 2w -- -- -- ss",
];

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum Cell {
    NoBonus,
    Start,
    LetterBonus(u32),
    WordBonus(u32),
}

type Inner = [[Cell; N]; N];
/// Wordfeud board grid, consisting of 15x15 (normal or bonus) squares.
///
/// A bonus square has a 2x or 3x letter bonus, or a 2x or 3x word bonus.
/// The center square at (7,7) is the "start" square, and must be used in the first turn.
#[derive(Debug, Clone, PartialEq)]
pub struct Grid(Inner);

impl Deref for Grid {
    type Target = Inner;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Grid {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl fmt::Display for Grid {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self.to_strings().join("\n"))
    }
}

use Cell::{LetterBonus, NoBonus, Start, WordBonus};

impl fmt::Display for Cell {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            NoBonus => write!(f, "--"),
            Start => write!(f, "ss"),
            LetterBonus(n) => write!(f, "{}l", n),
            WordBonus(n) => write!(f, "{}w", n),
        }
    }
}

impl FromStr for Cell {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "--" => Ok(NoBonus),
            "ss" => Ok(Start),
            "2l" => Ok(LetterBonus(2)),
            "3l" => Ok(LetterBonus(3)),
            "2w" => Ok(WordBonus(2)),
            "3w" => Ok(WordBonus(3)),
            _ => Err(Error::GridParseError(String::from(s))),
        }
    }
}

impl Grid {
    /// Create a new empty grid 15x15 cells with no bonus.
    fn empty() -> Grid {
        Grid([[NoBonus; N]; N])
    }

    /// Create a symmetrical `wordfeud` board by mirroring a quarter board
    /// horizontally and vertically
    fn expand_quarter_board(qb: &[&str; Q]) -> Grid {
        let mut board = Grid::empty();
        for (i, row) in qb.iter().enumerate() {
            let row = row.split(' ').collect::<Vec<&str>>();
            assert!(row.len() == Q);
            for (j, c) in row.iter().enumerate() {
                let val = c.parse().unwrap();
                board[i][j] = val;
                board[N - i - 1][j] = val;
                board[i][N - j - 1] = val;
                board[N - i - 1][N - j - 1] = val;
            }
        }
        board
    }

    /// Create default wordfeud grid
    /// ## Example
    /// ```
    /// # use wordfeud_solver::Grid;
    /// let grid = Grid::default();
    /// println!("{}", grid);
    /// ```
    pub fn default() -> Grid {
        Grid::expand_quarter_board(&_DEFAULT_QUARTER_BOARD)
    }

    /// Get board cells as a vec of 15 strings
    pub fn to_strings(&self) -> Vec<String> {
        self.iter()
            .map(|row| {
                row.iter()
                    .map(Cell::to_string)
                    .collect::<Vec<String>>()
                    .join(" ")
            })
            .collect::<Vec<_>>()
    }

    /// Create a `Grid` from strings
    /// Parameter `grid` must have 15 rows, each row consisting of 15 elements joined by spaces.
    ///
    /// ## Errors
    /// If `grid` has wrong dimensions, or elements can not be parsed as a `Cell`.
    /// ## Examples
    /// ```
    /// # use wordfeud_solver::{Grid, Error};
    /// let grid_strings = &[
    /// "3l -- -- -- 3w -- -- 2l -- -- 3w -- -- -- 3l",
    /// "-- 2l -- -- -- 3l -- -- -- 3l -- -- -- 2l --",
    /// "-- -- 2w -- -- -- 2l -- 2l -- -- -- 2w -- --",
    /// "-- -- -- 3l -- -- -- 2w -- -- -- 3l -- -- --",
    /// "3w -- -- -- 2w -- 2l -- 2l -- 2w -- -- -- 3w",
    /// "-- 3l -- -- -- 3l -- -- -- 3l -- -- -- 3l --",
    /// "-- -- 2l -- 2l -- -- -- -- -- 2l -- 2l -- --",
    /// "2l -- -- 2w -- -- -- ss -- -- -- 2w -- -- 2l",
    /// "-- -- 2l -- 2l -- -- -- -- -- 2l -- 2l -- --",
    /// "-- 3l -- -- -- 3l -- -- -- 3l -- -- -- 3l --",
    /// "3w -- -- -- 2w -- 2l -- 2l -- 2w -- -- -- 3w",
    /// "-- -- -- 3l -- -- -- 2w -- -- -- 3l -- -- --",
    /// "-- -- 2w -- -- -- 2l -- 2l -- -- -- 2w -- --",
    /// "-- 2l -- -- -- 3l -- -- -- 3l -- -- -- 2l --",
    /// "3l -- -- -- 3w -- -- 2l -- -- 3w -- -- -- 3l",   
    /// ];
    /// let grid =  Grid::from_strings(grid_strings)?;
    /// assert_eq!(grid.len(), 15);
    /// assert_eq!(grid[0].len(), 15);
    /// # Ok::<(), Error>(())
    /// ```
    pub fn from_strings<S: AsRef<str>>(grid: &[S]) -> Result<Grid, Error> {
        if grid.len() != N {
            return Err(Error::InvalidRowCount(grid.len()));
        }
        let mut board = Grid::empty();
        for (i, row) in grid.iter().enumerate() {
            let row = row.as_ref();
            let cells: Vec<&str> = row.split(' ').collect();
            if cells.len() != N {
                return Err(Error::InvalidRowLength(String::from(row), cells.len()));
            }
            for (j, &cell) in cells.iter().enumerate() {
                let val = cell.parse()?;
                board[i][j] = val;
            }
        }
        Ok(board)
    }
}

#[cfg(test)]
mod tests {
    pub use super::*;

    #[test]
    fn test_grid_from_array() -> Result<(), Error> {
        let grid = Grid::default();
        let grid_as_strings = grid.to_strings();
        println!("{:#?}", grid.to_strings());
        assert_eq!(Grid::from_strings(&grid_as_strings)?, grid);
        Ok(())
    }
}