Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use self::Color::*;
- use self::Kind::*;
- use std::fmt;
- #[derive(Clone)]
- struct Piece {
- kind: Kind,
- color: Color,
- }
- #[derive(Clone)]
- enum Kind {
- King,
- Queen,
- Reich,
- Horse,
- Tower,
- Pawn,
- }
- #[derive(Clone)]
- enum Color {
- Black,
- White,
- }
- impl Piece {
- fn invert_color(&self) -> Self {
- match self.color {
- Black => Self {
- color: White,
- ..self.clone()
- },
- White => Self {
- color: Black,
- ..self.clone()
- },
- }
- }
- }
- struct Board {
- zones: Vec<Vec<Option<Piece>>>,
- }
- impl Board {
- fn new() -> Self {
- let first_row: Vec<Option<Piece>> = [Tower, Horse, Reich, King, Queen, Reich, Horse, Tower]
- .iter()
- .map(|k| {
- Some(Piece {
- kind: k.to_owned(),
- color: White,
- })
- })
- .collect();
- let second_row: Vec<Option<Piece>> = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn]
- .iter()
- .map(|k| {
- Some(Piece {
- kind: k.to_owned(),
- color: White,
- })
- })
- .collect();
- let mut zones = vec![];
- zones.push(first_row.clone());
- zones.push(second_row.clone());
- for _ in 0..4 {
- zones.push([None, None, None, None, None, None, None, None].to_vec());
- }
- zones.push(
- second_row
- .iter()
- .map(|p| {
- if let Some(p) = p {
- Some(p.to_owned().invert_color())
- } else {
- p.to_owned()
- }
- })
- .collect(),
- );
- zones.push(
- first_row
- .iter()
- .map(|p| {
- if let Some(p) = p {
- Some(p.to_owned().invert_color())
- } else {
- p.to_owned()
- }
- })
- .collect(),
- );
- Self { zones }
- }
- }
- impl fmt::Display for Board {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- for row in self.zones.iter() {
- for zone in row {
- match zone {
- None => write!(f, " ")?,
- Some(piece) => match (&piece.kind, &piece.color) {
- (King, White) => write!(f, "♔")?,
- (King, Black) => write!(f, "♚")?,
- (Queen, White) => write!(f, "♕")?,
- (Queen, Black) => write!(f, "♛")?,
- (Reich, White) => write!(f, "♗")?,
- (Reich, Black) => write!(f, "♝")?,
- (Horse, White) => write!(f, "♘")?,
- (Horse, Black) => write!(f, "♞")?,
- (Tower, White) => write!(f, "♖")?,
- (Tower, Black) => write!(f, "♜")?,
- (Pawn, White) => write!(f, "♙")?,
- (Pawn, Black) => write!(f, "♟")?,
- },
- }
- }
- writeln!(f)?;
- }
- Ok(())
- }
- }
- fn main() {
- let chess = Board::new();
- print!("{}", chess);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement