Simplify code and improve setup.
This commit is contained in:
parent
4e695f3a7e
commit
3d1f6f4ec8
|
@ -35,20 +35,17 @@ pub struct Board {
|
||||||
|
|
||||||
impl Board {
|
impl Board {
|
||||||
pub fn new(size: BoardSize) -> Board {
|
pub fn new(size: BoardSize) -> Board {
|
||||||
|
|
||||||
let board_size = size.get_size();
|
let board_size = size.get_size();
|
||||||
let width = board_size / 2;
|
let width = board_size / 2;
|
||||||
let height = board_size;
|
let height = board_size;
|
||||||
let starting_rows = board_size / 2 - 1;
|
let starting_rows = board_size / 2 - 1;
|
||||||
|
|
||||||
let playing_tiles = (0..width * height)
|
let playing_tiles = (0..width * height)
|
||||||
.map(|tile|{
|
.map(|tile| {
|
||||||
let row = tile / width;
|
let row = tile / width;
|
||||||
match row {
|
match row {
|
||||||
r if r < starting_rows => BoardTile::Piece(
|
r if r < starting_rows => BoardTile::Piece(Piece::LightPawn),
|
||||||
Piece::LightPawn),
|
r if r > height - starting_rows - 1 => BoardTile::Piece(Piece::DarkPawn),
|
||||||
r if r > height - starting_rows - 1 => BoardTile::Piece(
|
|
||||||
Piece::DarkPawn),
|
|
||||||
_ => BoardTile::Empty,
|
_ => BoardTile::Empty,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -70,7 +67,6 @@ static COLUMN_COORDINATE_BORDER: &str = " ABCDEFGHIJKL";
|
||||||
|
|
||||||
impl fmt::Display for Board {
|
impl fmt::Display for Board {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
|
||||||
let width = self.dimensions.get_size();
|
let width = self.dimensions.get_size();
|
||||||
|
|
||||||
// Create the top level border.
|
// Create the top level border.
|
||||||
|
@ -78,31 +74,25 @@ impl fmt::Display for Board {
|
||||||
writeln!(formatter, "{}", column_border_chars)?;
|
writeln!(formatter, "{}", column_border_chars)?;
|
||||||
|
|
||||||
for row in 0..width {
|
for row in 0..width {
|
||||||
|
|
||||||
write!(formatter, "{}", (row + 1).to_string())?;
|
write!(formatter, "{}", (row + 1).to_string())?;
|
||||||
if row % 2 == 0 {
|
|
||||||
write!(formatter, " ")?;
|
|
||||||
}
|
|
||||||
|
|
||||||
for col in 0..(width / 2) {
|
for col in 0..(width / 2) {
|
||||||
|
|
||||||
let idx = self.get_index(row, col);
|
let idx = self.get_index(row, col);
|
||||||
|
let token = match &self.tiles[idx] {
|
||||||
let token = match self.tiles[idx] {
|
BoardTile::Piece(x) => match x {
|
||||||
BoardTile::Piece(Piece::LightPawn) => "○",
|
Piece::LightPawn => "○",
|
||||||
BoardTile::Piece(Piece::LightKing) => "⍟",
|
Piece::LightKing => "⍟",
|
||||||
BoardTile::Piece(Piece::DarkPawn) => "●",
|
Piece::DarkPawn => "●",
|
||||||
BoardTile::Piece(Piece::DarkKing) => "✪",
|
Piece::DarkKing => "✪",
|
||||||
|
},
|
||||||
BoardTile::Empty => "◰",
|
BoardTile::Empty => "◰",
|
||||||
};
|
};
|
||||||
|
|
||||||
let adjacent = if row % 2 == 0 && col == (width / 2) - 1 {
|
if row % 2 == 0 {
|
||||||
""
|
write!(formatter, " {}", token)?;
|
||||||
} else {
|
} else {
|
||||||
" "
|
write!(formatter, "{} ", token)?;
|
||||||
};
|
}
|
||||||
|
|
||||||
write!(formatter, "{}{}", token, adjacent)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
write!(formatter, "\n")?;
|
write!(formatter, "\n")?;
|
||||||
|
@ -135,7 +125,6 @@ impl Game {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::Board;
|
use crate::Board;
|
||||||
|
@ -143,61 +132,55 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_international_board_setup() {
|
fn initial_international_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
ABCDEFGHIJ\n\
|
||||||
" \
|
1 ○ ○ ○ ○ ○\n\
|
||||||
ABCDEFGHIJ\n\
|
2○ ○ ○ ○ ○ \n\
|
||||||
1 ○ ○ ○ ○ ○\n\
|
3 ○ ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ ○ \n\
|
4○ ○ ○ ○ ○ \n\
|
||||||
3 ○ ○ ○ ○ ○\n\
|
5 ◰ ◰ ◰ ◰ ◰\n\
|
||||||
4○ ○ ○ ○ ○ \n\
|
6◰ ◰ ◰ ◰ ◰ \n\
|
||||||
5 ◰ ◰ ◰ ◰ ◰\n\
|
7 ● ● ● ● ●\n\
|
||||||
6◰ ◰ ◰ ◰ ◰ \n\
|
8● ● ● ● ● \n\
|
||||||
7 ● ● ● ● ●\n\
|
9 ● ● ● ● ●\n\
|
||||||
8● ● ● ● ● \n\
|
10● ● ● ● ● \n\
|
||||||
9 ● ● ● ● ●\n\
|
";
|
||||||
10● ● ● ● ● \n\
|
|
||||||
";
|
|
||||||
assert_eq!(Board::new(BoardSize::International).to_string(), expected);
|
assert_eq!(Board::new(BoardSize::International).to_string(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_american_board_setup() {
|
fn initial_american_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
ABCDEFGH\n\
|
||||||
" \
|
1 ○ ○ ○ ○\n\
|
||||||
ABCDEFGH\n\
|
2○ ○ ○ ○ \n\
|
||||||
1 ○ ○ ○ ○\n\
|
3 ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ \n\
|
4◰ ◰ ◰ ◰ \n\
|
||||||
3 ○ ○ ○ ○\n\
|
5 ◰ ◰ ◰ ◰\n\
|
||||||
4◰ ◰ ◰ ◰ \n\
|
6● ● ● ● \n\
|
||||||
5 ◰ ◰ ◰ ◰\n\
|
7 ● ● ● ●\n\
|
||||||
6● ● ● ● \n\
|
8● ● ● ● \n\
|
||||||
7 ● ● ● ●\n\
|
";
|
||||||
8● ● ● ● \n\
|
|
||||||
";
|
|
||||||
assert_eq!(Board::new(BoardSize::American).to_string(), expected);
|
assert_eq!(Board::new(BoardSize::American).to_string(), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_canadian_board_setup() {
|
fn initial_canadian_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
ABCDEFGHIJKL\n\
|
||||||
" \
|
1 ○ ○ ○ ○ ○ ○\n\
|
||||||
ABCDEFGHIJKL\n\
|
2○ ○ ○ ○ ○ ○ \n\
|
||||||
1 ○ ○ ○ ○ ○ ○\n\
|
3 ○ ○ ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ ○ ○ \n\
|
4○ ○ ○ ○ ○ ○ \n\
|
||||||
3 ○ ○ ○ ○ ○ ○\n\
|
5 ○ ○ ○ ○ ○ ○\n\
|
||||||
4○ ○ ○ ○ ○ ○ \n\
|
6◰ ◰ ◰ ◰ ◰ ◰ \n\
|
||||||
5 ○ ○ ○ ○ ○ ○\n\
|
7 ◰ ◰ ◰ ◰ ◰ ◰\n\
|
||||||
6◰ ◰ ◰ ◰ ◰ ◰ \n\
|
8● ● ● ● ● ● \n\
|
||||||
7 ◰ ◰ ◰ ◰ ◰ ◰\n\
|
9 ● ● ● ● ● ●\n\
|
||||||
8● ● ● ● ● ● \n\
|
10● ● ● ● ● ● \n\
|
||||||
9 ● ● ● ● ● ●\n\
|
11 ● ● ● ● ● ●\n\
|
||||||
10● ● ● ● ● ● \n\
|
12● ● ● ● ● ● \n\
|
||||||
11 ● ● ● ● ● ●\n\
|
";
|
||||||
12● ● ● ● ● ● \n\
|
|
||||||
";
|
|
||||||
assert_eq!(Board::new(BoardSize::Canadian).to_string(), expected);
|
assert_eq!(Board::new(BoardSize::Canadian).to_string(), expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue