Simplify code and improve setup.

This commit is contained in:
Dorian 2019-03-04 08:27:22 -05:00
parent 4e695f3a7e
commit 3d1f6f4ec8
1 changed files with 53 additions and 70 deletions

View File

@ -35,20 +35,17 @@ pub struct Board {
impl Board {
pub fn new(size: BoardSize) -> Board {
let board_size = size.get_size();
let width = board_size / 2;
let height = board_size;
let starting_rows = board_size / 2 - 1;
let playing_tiles = (0..width * height)
.map(|tile|{
.map(|tile| {
let row = tile / width;
match row {
r if r < starting_rows => BoardTile::Piece(
Piece::LightPawn),
r if r > height - starting_rows - 1 => BoardTile::Piece(
Piece::DarkPawn),
r if r < starting_rows => BoardTile::Piece(Piece::LightPawn),
r if r > height - starting_rows - 1 => BoardTile::Piece(Piece::DarkPawn),
_ => BoardTile::Empty,
}
})
@ -70,7 +67,6 @@ static COLUMN_COORDINATE_BORDER: &str = " ABCDEFGHIJKL";
impl fmt::Display for Board {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let width = self.dimensions.get_size();
// Create the top level border.
@ -78,31 +74,25 @@ impl fmt::Display for Board {
writeln!(formatter, "{}", column_border_chars)?;
for row in 0..width {
write!(formatter, "{}", (row + 1).to_string())?;
if row % 2 == 0 {
write!(formatter, " ")?;
}
for col in 0..(width / 2) {
let idx = self.get_index(row, col);
let token = match self.tiles[idx] {
BoardTile::Piece(Piece::LightPawn) => "",
BoardTile::Piece(Piece::LightKing) => "",
BoardTile::Piece(Piece::DarkPawn) => "",
BoardTile::Piece(Piece::DarkKing) => "",
let token = match &self.tiles[idx] {
BoardTile::Piece(x) => match x {
Piece::LightPawn => "",
Piece::LightKing => "",
Piece::DarkPawn => "",
Piece::DarkKing => "",
},
BoardTile::Empty => "",
};
let adjacent = if row % 2 == 0 && col == (width / 2) - 1 {
""
if row % 2 == 0 {
write!(formatter, " {}", token)?;
} else {
" "
};
write!(formatter, "{}{}", token, adjacent)?;
write!(formatter, "{} ", token)?;
}
}
write!(formatter, "\n")?;
@ -135,7 +125,6 @@ impl Game {
}
}
#[cfg(test)]
mod tests {
use crate::Board;
@ -143,9 +132,7 @@ mod tests {
#[test]
fn initial_international_board_setup() {
let expected =
" \
let expected = " \
ABCDEFGHIJ\n\
1 \n\
2 \n\
@ -163,9 +150,7 @@ mod tests {
#[test]
fn initial_american_board_setup() {
let expected =
" \
let expected = " \
ABCDEFGH\n\
1 \n\
2 \n\
@ -181,9 +166,7 @@ mod tests {
#[test]
fn initial_canadian_board_setup() {
let expected =
" \
let expected = " \
ABCDEFGHIJKL\n\
1 \n\
2 \n\