Added basic display for board
This commit is contained in:
parent
b874bb99d4
commit
4e4912ac3f
2 changed files with 94 additions and 10 deletions
80
src/lib.rs
80
src/lib.rs
|
@ -1,15 +1,18 @@
|
||||||
const GRID_LENGTH: u8 = 15;
|
use std::fmt;
|
||||||
const TRAY_LENGTH: u8 = 7;
|
use std::fmt::{Formatter, Write};
|
||||||
const ALL_LETTERS_BONUS: u32 = 50;
|
|
||||||
|
pub const GRID_LENGTH: u8 = 15;
|
||||||
|
pub const TRAY_LENGTH: u8 = 7;
|
||||||
|
pub const ALL_LETTERS_BONUS: u32 = 50;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Letter {
|
pub enum Letter {
|
||||||
FixedLetter{text: char, points: u32},
|
FixedLetter{text: char, points: u32},
|
||||||
Blank(char)
|
Blank(char)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum CellType {
|
pub enum CellType {
|
||||||
Normal,
|
Normal,
|
||||||
DoubleWord,
|
DoubleWord,
|
||||||
DoubleLetter,
|
DoubleLetter,
|
||||||
|
@ -19,19 +22,19 @@ enum CellType {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Cell {
|
pub struct Cell {
|
||||||
value: Option<Letter>,
|
pub value: Option<Letter>,
|
||||||
cell_type: CellType,
|
cell_type: CellType,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Board {
|
pub struct Board {
|
||||||
cells: Vec<Cell>,
|
cells: Vec<Cell>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let mut cells = Vec::new();
|
let mut cells = Vec::new();
|
||||||
|
|
||||||
|
|
||||||
|
@ -98,7 +101,7 @@ impl Board {
|
||||||
Board {cells}
|
Board {cells}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_cell(&self, x: u8, y: u8) -> Result<&Cell, &str> {
|
pub fn get_cell(&self, x: u8, y: u8) -> Result<&Cell, &str> {
|
||||||
if x >= GRID_LENGTH || y >= GRID_LENGTH {
|
if x >= GRID_LENGTH || y >= GRID_LENGTH {
|
||||||
Err("x & y must be within the board's coordinates")
|
Err("x & y must be within the board's coordinates")
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,6 +109,63 @@ impl Board {
|
||||||
Ok(self.cells.get(coord).unwrap())
|
Ok(self.cells.get(coord).unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_cell_mut(&mut self, x: u8, y: u8) -> Result<&mut Cell, &str> {
|
||||||
|
if x >= GRID_LENGTH || y >= GRID_LENGTH {
|
||||||
|
Err("x & y must be within the board's coordinates")
|
||||||
|
} else {
|
||||||
|
let coord = (x + GRID_LENGTH*y) as usize;
|
||||||
|
Ok(self.cells.get_mut(coord).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
let cell = self.get_cell(x, y).unwrap();
|
||||||
|
|
||||||
|
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 => {' '}
|
||||||
|
Some(letter) => {*match letter {
|
||||||
|
Letter::FixedLetter {text, points} => {text},
|
||||||
|
Letter::Blank(text) => {text}
|
||||||
|
}}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
str.write_str(color).unwrap();
|
||||||
|
str.write_char(content).unwrap();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
str.write_str("\x1b[0m\n").unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
write!(f, "{}", str)
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
24
src/main.rs
Normal file
24
src/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use WordGrid::{Board, Letter};
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut board = Board::new();
|
||||||
|
|
||||||
|
let c1 = board.get_cell_mut(8, 6).unwrap();
|
||||||
|
c1.value = Some(Letter::FixedLetter {text: 'J', points: 0});
|
||||||
|
board.get_cell_mut(8, 7).unwrap().value = Some(Letter::FixedLetter {text: 'o', points: 0});
|
||||||
|
board.get_cell_mut(8, 8).unwrap().value = Some(Letter::FixedLetter {text: 'e', points: 0});
|
||||||
|
board.get_cell_mut(8, 9).unwrap().value = Some(Letter::FixedLetter {text: 'l', points: 0});
|
||||||
|
|
||||||
|
board.get_cell_mut(9, 8).unwrap().value = Some(Letter::FixedLetter {text: 'i', points: 0});
|
||||||
|
board.get_cell_mut(9, 9).unwrap().value = Some(Letter::FixedLetter {text: 's', points: 0});
|
||||||
|
|
||||||
|
board.get_cell_mut(10, 8).unwrap().value = Some(Letter::FixedLetter {text: 'c', points: 0});
|
||||||
|
board.get_cell_mut(10, 9).unwrap().value = Some(Letter::FixedLetter {text: 'o', points: 0});
|
||||||
|
board.get_cell_mut(10, 10).unwrap().value = Some(Letter::FixedLetter {text: 'o', points: 0});
|
||||||
|
board.get_cell_mut(10, 11).unwrap().value = Some(Letter::FixedLetter {text: 'l', points: 0});
|
||||||
|
|
||||||
|
println!("{}", board);
|
||||||
|
|
||||||
|
//println!("\x1b[38;5;196m Hello my name is Joel");
|
||||||
|
//println!("\x1b[48;5;20m Hello my name is Joel");
|
||||||
|
}
|
Loading…
Reference in a new issue