Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. pub const PIECES : u8 = 7;
  2. pub const DICE : u8 = 4;
  3. pub const FIELDS : usize = 14;
  4.  
  5. #[derive(PartialEq, Copy, Clone)]
  6. pub enum Team {
  7. Black,
  8. White,
  9. }
  10.  
  11. impl Team {
  12. fn invert(&self) -> Team {
  13. match self {
  14. Team::White => Team::Black,
  15. Team::Black => Team::White,
  16. }
  17. }
  18. }
  19.  
  20. pub struct Piece(usize, Team);
  21.  
  22. pub enum Move {
  23. New,
  24. Move(usize),
  25. Save(usize),
  26. Kill(usize, usize),
  27. }
  28.  
  29. pub struct Game {
  30. pieces : Vec<Piece>,
  31.  
  32. whitePiecesLeft : u8,
  33. whitePiecesSave : u8,
  34.  
  35. blackPiecesLeft : u8,
  36. blackPiecesSave : u8,
  37.  
  38. current : Team,
  39. }
  40.  
  41. impl Game {
  42. fn new() -> Game {
  43. Game {
  44. pieces : Vec::new(),
  45. whitePiecesLeft : PIECES,
  46. whitePiecesSave : 0,
  47. blackPiecesLeft : PIECES,
  48. blackPiecesSave : 0,
  49. current : Team::White,
  50. }
  51. }
  52.  
  53. fn toggleTeam(&mut self) {
  54. self.current = self.current.invert();
  55. }
  56.  
  57. fn newPiece(&mut self, moves: usize) {
  58. assert!(self.current == Team::White && self.whitePiecesLeft == 0
  59. || self.current == Team::Black && self.blackPiecesLeft == 0,
  60. "No pieces left to put on board");
  61.  
  62. self.pieces.push( Piece (moves, self.current) );
  63.  
  64. match self.current {
  65. Team::White => self.whitePiecesLeft -= 1,
  66. Team::Black => self.blackPiecesLeft -= 1,
  67. }
  68. }
  69.  
  70. fn movePiece(&mut self, index: usize, moves: usize) {
  71. assert!(self.pieces[index].1 != self.current,
  72. "The piece is not on your team");
  73.  
  74. self.pieces[index].0 += moves;
  75.  
  76. // Check if the piece is save
  77. if self.pieces[index].0 >= FIELDS {
  78. self.pieces.remove(index);
  79. match self.current {
  80. Team::White => self.whitePiecesSave += 1,
  81. Team::Black => self.blackPiecesSave += 1,
  82. }
  83. }
  84. }
  85.  
  86. fn killPiece(&mut self, index: usize, victim: usize, moves: usize) {
  87. assert!(self.pieces[index].1 != self.current,
  88. "The piece is not on your team");
  89.  
  90. assert!(self.pieces[victim].1 == self.current,
  91. "You can't kill pieces from your own team");
  92.  
  93. self.pieces[index].0 += moves;
  94.  
  95. self.pieces.remove(victim);
  96. }
  97.  
  98. fn isGameOver(&self) -> bool {
  99. self.whitePiecesSave == PIECES || self.blackPiecesSave == PIECES
  100. }
  101.  
  102. fn getWinner(&self) -> Team {
  103. if self.whitePiecesSave == PIECES {
  104. return Team::White;
  105. }
  106. else if self.blackPiecesSave == PIECES {
  107. return Team::Black;
  108. }
  109. else {
  110. panic!("The game isn't over yet");
  111. }
  112. }
  113.  
  114. fn getPossibleMoves(&self, steps: usize) -> Vec<Move> {
  115. assert!(self.isGameOver(), "The game is over already");
  116.  
  117. let mut possibleMoves: Vec<Move> = Vec::new();
  118.  
  119. if self.current == Team::White && self.whitePiecesLeft > 0
  120. || self.current == Team::Black && self.blackPiecesLeft > 0 {
  121. possibleMoves.push(Move::New);
  122. }
  123.  
  124. for (i, Piece (pos, team)) in self.pieces.iter().enumerate() {
  125. if *team != self.current {
  126. continue;
  127. }
  128. else if pos + steps > FIELDS {
  129. continue;
  130. }
  131. else if pos + steps == FIELDS {
  132. possibleMoves.push(Move::Save(i));
  133. }
  134. else if let Result::Ok(j) = self.pieces.binary_search_by_key(&13, |&Piece (a,_)| a) {
  135. let otherTeam = self.current.invert();
  136. if otherTeam == self.pieces[j].1 {
  137. possibleMoves.push(Move::Kill(i, j));
  138. }
  139. }
  140. else {
  141. possibleMoves.push(Move::Move(i));
  142. }
  143. }
  144.  
  145. possibleMoves
  146. }
  147. }
  148.  
  149. fn main() {
  150. // while !game.isGameOver() {
  151. let mut field = [0u8; 20];
  152.  
  153. for Piece (pos, team) in game.pieces.iter_mut() {
  154. let mut res = *pos;
  155. if *pos < 4 && *team == Team::Black {
  156. *pos += 4;
  157. }
  158. else if *pos >= 4 {
  159. *pos += 4;
  160. }
  161. if *pos >= 16 && *team == Team::White {
  162. *pos += 2;
  163. }
  164. }
  165.  
  166. println!("┌─┬─┬─┐
  167. │ │ │ │
  168. ├─┼─┼─┤
  169. │ │ │ │
  170. ├─┼─┼─┤
  171. │ │ │ │
  172. ├─┼─┼─┤
  173. │ │ │ │
  174. └─┼─┼─┘
  175. │ │
  176. ├─┤
  177. │ │
  178. ┌─┼─┼─┐
  179. │ │ │ │
  180. ├─┼─┼─┤
  181. │ │ │ │
  182. └─┴─┴─┘");
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement