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,9 +132,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_international_board_setup() {
|
fn initial_international_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
|
||||||
" \
|
|
||||||
ABCDEFGHIJ\n\
|
ABCDEFGHIJ\n\
|
||||||
1 ○ ○ ○ ○ ○\n\
|
1 ○ ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ ○ \n\
|
2○ ○ ○ ○ ○ \n\
|
||||||
|
@ -163,9 +150,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_american_board_setup() {
|
fn initial_american_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
|
||||||
" \
|
|
||||||
ABCDEFGH\n\
|
ABCDEFGH\n\
|
||||||
1 ○ ○ ○ ○\n\
|
1 ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ \n\
|
2○ ○ ○ ○ \n\
|
||||||
|
@ -181,9 +166,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn initial_canadian_board_setup() {
|
fn initial_canadian_board_setup() {
|
||||||
|
let expected = " \
|
||||||
let expected =
|
|
||||||
" \
|
|
||||||
ABCDEFGHIJKL\n\
|
ABCDEFGHIJKL\n\
|
||||||
1 ○ ○ ○ ○ ○ ○\n\
|
1 ○ ○ ○ ○ ○ ○\n\
|
||||||
2○ ○ ○ ○ ○ ○ \n\
|
2○ ○ ○ ○ ○ ○ \n\
|
||||||
|
|
Loading…
Reference in New Issue