Simplify code and improve setup.
This commit is contained in:
parent
4e695f3a7e
commit
3d1f6f4ec8
|
@ -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,61 +132,55 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn initial_international_board_setup() {
|
||||
|
||||
let expected =
|
||||
" \
|
||||
ABCDEFGHIJ\n\
|
||||
1 ○ ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○ ○\n\
|
||||
4○ ○ ○ ○ ○ \n\
|
||||
5 ◰ ◰ ◰ ◰ ◰\n\
|
||||
6◰ ◰ ◰ ◰ ◰ \n\
|
||||
7 ● ● ● ● ●\n\
|
||||
8● ● ● ● ● \n\
|
||||
9 ● ● ● ● ●\n\
|
||||
10● ● ● ● ● \n\
|
||||
";
|
||||
let expected = " \
|
||||
ABCDEFGHIJ\n\
|
||||
1 ○ ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○ ○\n\
|
||||
4○ ○ ○ ○ ○ \n\
|
||||
5 ◰ ◰ ◰ ◰ ◰\n\
|
||||
6◰ ◰ ◰ ◰ ◰ \n\
|
||||
7 ● ● ● ● ●\n\
|
||||
8● ● ● ● ● \n\
|
||||
9 ● ● ● ● ●\n\
|
||||
10● ● ● ● ● \n\
|
||||
";
|
||||
assert_eq!(Board::new(BoardSize::International).to_string(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initial_american_board_setup() {
|
||||
|
||||
let expected =
|
||||
" \
|
||||
ABCDEFGH\n\
|
||||
1 ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○\n\
|
||||
4◰ ◰ ◰ ◰ \n\
|
||||
5 ◰ ◰ ◰ ◰\n\
|
||||
6● ● ● ● \n\
|
||||
7 ● ● ● ●\n\
|
||||
8● ● ● ● \n\
|
||||
";
|
||||
let expected = " \
|
||||
ABCDEFGH\n\
|
||||
1 ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○\n\
|
||||
4◰ ◰ ◰ ◰ \n\
|
||||
5 ◰ ◰ ◰ ◰\n\
|
||||
6● ● ● ● \n\
|
||||
7 ● ● ● ●\n\
|
||||
8● ● ● ● \n\
|
||||
";
|
||||
assert_eq!(Board::new(BoardSize::American).to_string(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initial_canadian_board_setup() {
|
||||
|
||||
let expected =
|
||||
" \
|
||||
ABCDEFGHIJKL\n\
|
||||
1 ○ ○ ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○ ○ ○\n\
|
||||
4○ ○ ○ ○ ○ ○ \n\
|
||||
5 ○ ○ ○ ○ ○ ○\n\
|
||||
6◰ ◰ ◰ ◰ ◰ ◰ \n\
|
||||
7 ◰ ◰ ◰ ◰ ◰ ◰\n\
|
||||
8● ● ● ● ● ● \n\
|
||||
9 ● ● ● ● ● ●\n\
|
||||
10● ● ● ● ● ● \n\
|
||||
11 ● ● ● ● ● ●\n\
|
||||
12● ● ● ● ● ● \n\
|
||||
";
|
||||
let expected = " \
|
||||
ABCDEFGHIJKL\n\
|
||||
1 ○ ○ ○ ○ ○ ○\n\
|
||||
2○ ○ ○ ○ ○ ○ \n\
|
||||
3 ○ ○ ○ ○ ○ ○\n\
|
||||
4○ ○ ○ ○ ○ ○ \n\
|
||||
5 ○ ○ ○ ○ ○ ○\n\
|
||||
6◰ ◰ ◰ ◰ ◰ ◰ \n\
|
||||
7 ◰ ◰ ◰ ◰ ◰ ◰\n\
|
||||
8● ● ● ● ● ● \n\
|
||||
9 ● ● ● ● ● ●\n\
|
||||
10● ● ● ● ● ● \n\
|
||||
11 ● ● ● ● ● ●\n\
|
||||
12● ● ● ● ● ● \n\
|
||||
";
|
||||
assert_eq!(Board::new(BoardSize::Canadian).to_string(), expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue