2019-03-01 09:04:11 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
pub enum Piece {
|
|
|
|
LightPawn,
|
2019-02-28 09:06:24 -05:00
|
|
|
LightKing,
|
2019-03-01 09:04:11 -05:00
|
|
|
DarkPawn,
|
2019-02-28 09:06:24 -05:00
|
|
|
DarkKing,
|
|
|
|
}
|
|
|
|
|
2019-03-01 09:04:11 -05:00
|
|
|
pub enum BoardTile {
|
2019-02-28 09:06:24 -05:00
|
|
|
Empty,
|
2019-03-01 09:04:11 -05:00
|
|
|
Piece(Piece),
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum BoardSize {
|
|
|
|
American,
|
|
|
|
International,
|
|
|
|
Canadian,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BoardSize {
|
|
|
|
fn get_size(&self) -> u32 {
|
|
|
|
match &self {
|
|
|
|
BoardSize::American => 8,
|
|
|
|
BoardSize::International => 10,
|
|
|
|
BoardSize::Canadian => 12,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Board {
|
|
|
|
dimensions: BoardSize,
|
|
|
|
tiles: Vec<BoardTile>,
|
2019-02-28 09:06:24 -05:00
|
|
|
}
|
|
|
|
|
2019-03-01 09:04:11 -05:00
|
|
|
impl Board {
|
|
|
|
pub fn new(size: BoardSize) -> Board {
|
|
|
|
|
|
|
|
let board_size = size.get_size();
|
|
|
|
let width = board_size / 2;
|
|
|
|
let height = board_size / 2;
|
|
|
|
let starting_rows = board_size / 2 - 1;
|
|
|
|
|
|
|
|
let playing_tiles = (0..width * height)
|
|
|
|
.map(|tile|{
|
|
|
|
let row = tile / width;
|
|
|
|
match row {
|
|
|
|
r if r < starting_rows => BoardTile::Piece(
|
|
|
|
Piece::LightPawn),
|
|
|
|
r if r > height - starting_rows => BoardTile::Piece(
|
|
|
|
Piece::DarkPawn),
|
|
|
|
_ => BoardTile::Empty,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
Board {
|
|
|
|
dimensions: size,
|
|
|
|
tiles: playing_tiles,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 11:04:06 -05:00
|
|
|
static COLUMN_COORDINATE_BORDER: &str = " ABCDEFGHIJKL";
|
|
|
|
|
2019-03-01 09:04:11 -05:00
|
|
|
impl fmt::Display for Board {
|
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
|
|
|
|
let width = self.dimensions.get_size();
|
|
|
|
|
2019-03-01 11:04:06 -05:00
|
|
|
// Create the top level border.
|
|
|
|
let mut column_border_chars = COLUMN_COORDINATE_BORDER.chars();
|
|
|
|
for _ in 0..width + 1 {
|
|
|
|
write!(formatter, "{}", column_border_chars.next().unwrap())?;
|
|
|
|
}
|
|
|
|
write!(formatter, "\n")?;
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-03-01 09:04:11 -05:00
|
|
|
// for {}
|
|
|
|
//
|
|
|
|
// let square = match tile {
|
|
|
|
// Piece::LightPawn => "♙",
|
|
|
|
// Piece::LightKing => "♔",
|
|
|
|
// Piece::DarkPawn => "♟",
|
|
|
|
// Piece::DarkKing => "♚",
|
|
|
|
// };
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2019-02-28 09:06:24 -05:00
|
|
|
|
|
|
|
|
2019-02-28 08:39:27 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2019-03-01 11:04:06 -05:00
|
|
|
use crate::Board;
|
|
|
|
use crate::BoardSize;
|
|
|
|
|
2019-02-28 08:39:27 -05:00
|
|
|
#[test]
|
2019-03-01 11:04:06 -05:00
|
|
|
fn setup_international_board() {
|
|
|
|
|
|
|
|
let expected = r#" ABCDEFGHIJ
|
|
|
|
"#;
|
|
|
|
assert_eq!(Board::new(BoardSize::International).to_string(), expected);
|
2019-02-28 08:39:27 -05:00
|
|
|
}
|
|
|
|
}
|