diff --git a/rs/src/board.rs b/rs/src/board.rs index bfdb25b..a82ee35 100644 --- a/rs/src/board.rs +++ b/rs/src/board.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; #[derive(Clone)] -enum Color { +pub enum Color { Black, White, } @@ -29,8 +29,8 @@ impl Color { #[derive(PartialEq, Eq, Hash, Clone)] pub struct Position { - rank: Rank, - file: File, + pub rank: Rank, + pub file: File, } impl Position { @@ -82,7 +82,7 @@ pub enum File { H, } -trait GridAxis +pub trait GridAxis where Self: Sized, Self: Clone, @@ -174,8 +174,8 @@ impl PieceType { } pub struct Piece { - color: Color, - piece_type: PieceType, + pub color: Color, + pub piece_type: PieceType, } impl Piece { diff --git a/rs/src/main.rs b/rs/src/main.rs index 9155a7c..7bbdb51 100644 --- a/rs/src/main.rs +++ b/rs/src/main.rs @@ -1,17 +1,7 @@ mod board; +mod ui; fn main() { - println!( - "{}", - match board::Board::new() - .get_at(&board::Position::new(board::Rank::_2, board::File::A)) - { - Some(piece) => "Some", - None => "None", - } - ); - // reset -> (ok/fail) - // get_state -> (ok/fail),state - // get_moves,position,[state] -> (ok/fail),position[] - // make_move,[move],[state] -> (ok/fail),state + let board = board::Board::new(); + println!("{}", board.to_string()); } diff --git a/rs/src/ui.rs b/rs/src/ui.rs new file mode 100644 index 0000000..65fff9d --- /dev/null +++ b/rs/src/ui.rs @@ -0,0 +1,59 @@ +use crate::board; +use crate::board::GridAxis; + +impl board::Color { + fn to_string(&self) -> String { + match self { + board::Color::White => "W".to_string(), + board::Color::Black => "B".to_string(), + } + } +} + +impl board::PieceType { + fn to_string(&self) -> String { + match self { + board::PieceType::Pawn => "P".to_string(), + } + } +} + +impl board::Piece { + fn to_string(&self) -> String { + self.piece_type.to_string() + &self.color.to_string() + } +} + +trait GridAxisIO +where + Self: GridAxis, +{ + const ALL_CHARS: [char; 8]; + fn to_string(&self) -> String { + Self::ALL_CHARS[self.get_index() as usize].to_string() + } +} + +impl GridAxisIO for board::Rank { + const ALL_CHARS: [char; Self::ALL_VALUES.len()] = + ['1', '2', '3', '4', '5', '6', '7', '8']; +} + +impl GridAxisIO for board::File { + const ALL_CHARS: [char; Self::ALL_VALUES.len()] = + ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']; +} + +impl board::Position { + fn to_string(&self) -> String { + self.file.to_string() + &self.rank.to_string() + } +} + +impl board::Board { + pub fn to_string(&self) -> String { + self.iter() + .map(|(position, piece)| piece.to_string() + &position.to_string()) + .collect() + } +}