Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. use self::Color::*;
  2. use self::Kind::*;
  3.  
  4. use std::fmt;
  5.  
  6. #[derive(Clone)]
  7. struct Piece {
  8. kind: Kind,
  9. color: Color,
  10. }
  11.  
  12. #[derive(Clone)]
  13. enum Kind {
  14. King,
  15. Queen,
  16. Reich,
  17. Horse,
  18. Tower,
  19. Pawn,
  20. }
  21. #[derive(Clone)]
  22. enum Color {
  23. Black,
  24. White,
  25. }
  26.  
  27. impl Piece {
  28. fn invert_color(&self) -> Self {
  29. match self.color {
  30. Black => Self {
  31. color: White,
  32. ..self.clone()
  33. },
  34. White => Self {
  35. color: Black,
  36. ..self.clone()
  37. },
  38. }
  39. }
  40. }
  41.  
  42. struct Board {
  43. zones: Vec<Vec<Option<Piece>>>,
  44. }
  45.  
  46. impl Board {
  47. fn new() -> Self {
  48. let first_row: Vec<Option<Piece>> = [Tower, Horse, Reich, King, Queen, Reich, Horse, Tower]
  49. .iter()
  50. .map(|k| {
  51. Some(Piece {
  52. kind: k.to_owned(),
  53. color: White,
  54. })
  55. })
  56. .collect();
  57. let second_row: Vec<Option<Piece>> = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn]
  58. .iter()
  59. .map(|k| {
  60. Some(Piece {
  61. kind: k.to_owned(),
  62. color: White,
  63. })
  64. })
  65. .collect();
  66.  
  67. let mut zones = vec![];
  68.  
  69. zones.push(first_row.clone());
  70. zones.push(second_row.clone());
  71.  
  72. for _ in 0..4 {
  73. zones.push([None, None, None, None, None, None, None, None].to_vec());
  74. }
  75.  
  76. zones.push(
  77. second_row
  78. .iter()
  79. .map(|p| {
  80. if let Some(p) = p {
  81. Some(p.to_owned().invert_color())
  82. } else {
  83. p.to_owned()
  84. }
  85. })
  86. .collect(),
  87. );
  88. zones.push(
  89. first_row
  90. .iter()
  91. .map(|p| {
  92. if let Some(p) = p {
  93. Some(p.to_owned().invert_color())
  94. } else {
  95. p.to_owned()
  96. }
  97. })
  98. .collect(),
  99. );
  100.  
  101. Self { zones }
  102. }
  103. }
  104.  
  105. impl fmt::Display for Board {
  106. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  107. for row in self.zones.iter() {
  108. for zone in row {
  109. match zone {
  110. None => write!(f, " ")?,
  111. Some(piece) => match (&piece.kind, &piece.color) {
  112. (King, White) => write!(f, "♔")?,
  113. (King, Black) => write!(f, "♚")?,
  114. (Queen, White) => write!(f, "♕")?,
  115. (Queen, Black) => write!(f, "♛")?,
  116. (Reich, White) => write!(f, "♗")?,
  117. (Reich, Black) => write!(f, "♝")?,
  118. (Horse, White) => write!(f, "♘")?,
  119. (Horse, Black) => write!(f, "♞")?,
  120. (Tower, White) => write!(f, "♖")?,
  121. (Tower, Black) => write!(f, "♜")?,
  122. (Pawn, White) => write!(f, "♙")?,
  123. (Pawn, Black) => write!(f, "♟")?,
  124. },
  125. }
  126. }
  127. writeln!(f)?;
  128. }
  129. Ok(())
  130. }
  131. }
  132.  
  133. fn main() {
  134. let chess = Board::new();
  135. print!("{}", chess);
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement