Advertisement
Guest User

Chess 2

a guest
Apr 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.29 KB | None | 0 0
  1. #include <functional>
  2. #include <iostream>
  3. #include <memory>
  4. #include <string>
  5. #include <vector>
  6.  
  7. using namespace std;
  8.  
  9. class ChessBoard {
  10. private:
  11. bool started = false;
  12.  
  13. public:
  14. enum class Color { WHITE,
  15. BLACK };
  16.  
  17. class Piece {
  18. public:
  19. Piece(Color color) : color(color) {}
  20. virtual ~Piece() {}
  21.  
  22. Color color;
  23. string color_string() const {
  24. if (color == Color::WHITE)
  25. return "white";
  26. else
  27. return "black";
  28. }
  29.  
  30. string color_abbrev() const {
  31. if (color == Color::WHITE)
  32. return "w";
  33. else
  34. return "b";
  35. }
  36.  
  37. /// Return color and type of the chess piece
  38. virtual string type() const = 0;
  39.  
  40. /// Returns true if the given chess piece move is valid
  41. virtual bool valid_move(int from_x, int from_y, int to_x, int to_y) const = 0;
  42.  
  43. /// Return abbreviation of the chess piece
  44. virtual string abbrev() const = 0;
  45. };
  46.  
  47. //Oppgave 1:
  48. class King : public Piece {
  49. public:
  50. const string name = "king";
  51. const string abbreviation = "K";
  52. Color color;
  53. King(const Color color_) : Piece(color_) {}
  54.  
  55. string type() const override {
  56. string type = color_string() + " " + name;
  57. return type;
  58. }
  59. bool valid_move(int from_x, int from_y, int to_x, int to_y) const override {
  60. int diffY = from_y - to_y;
  61. int diffX = from_x - to_x;
  62.  
  63. //Checks if the difference in either direction is smaller than or equal to 1
  64. if ((diffY <= 1 && diffY >= -1) && (diffX <= 1 && diffX >= -1)) {
  65. return true;
  66. }
  67.  
  68. return false;
  69. }
  70. string abbrev() const override {
  71. return abbreviation;
  72. }
  73. };
  74.  
  75. class Knight : public Piece {
  76. public:
  77. const string name = "knight";
  78. const string abbreviation = "N";
  79. Color color;
  80. Knight(const Color color_) : Piece(color_) {}
  81. string type() const override {
  82. string type = color_string() + " " + name;
  83. return type;
  84. }
  85. bool valid_move(int from_x, int from_y, int to_x, int to_y) const override {
  86. int diffY = from_y - to_y;
  87. int diffX = from_x - to_x;
  88.  
  89. //The knight moves either 2 tiles in x direction and 1 tile y direction, or 1 tile in x direction and 2 tiles in y direction
  90. if ((diffY == 2) && (diffX == 1)) {
  91. return true;
  92. } else if ((diffY == -2) && (diffX == 1)) {
  93. return true;
  94. } else if ((diffY == -2) && (diffX == -1)) {
  95. return true;
  96. } else if ((diffY == 2) && (diffX == -1)) {
  97. return true;
  98. } else if ((diffY == 1) && (diffX == 2)) {
  99. return true;
  100. } else if ((diffY == -1) && (diffX == 2)) {
  101. return true;
  102. } else if ((diffY == -1) && (diffX == -2)) {
  103. return true;
  104. } else if ((diffY == 1) && (diffX == -2)) {
  105. return true;
  106. }
  107.  
  108. return false;
  109. }
  110.  
  111. string abbrev() const override {
  112. return abbreviation;
  113. }
  114. };
  115.  
  116. ChessBoard() {
  117. // Initialize the squares stored in 8 columns and 8 rows:
  118. squares.resize(8);
  119. for (auto &square_column : squares)
  120. square_column.resize(8);
  121. }
  122.  
  123. /// 8x8 squares occupied by 1 or 0 chess pieces
  124. vector<vector<unique_ptr<Piece>>> squares;
  125.  
  126. //Funksjonsobjekter
  127. function<void(const Piece &piece, const string &from, const string &to)> on_piece_move;
  128. function<void(const Piece &piece, const string &square)> on_piece_removed;
  129. function<void(Color color)> on_lost_game;
  130. function<void(const Piece &piece, const string &from, const string &to)> on_piece_move_invalid;
  131. function<void(const string &square)> on_piece_move_missing;
  132. function<void()> on_start;
  133. function<void(const vector<vector<unique_ptr<Piece>>> &squares)> after_piece_move;
  134.  
  135. /// Move a chess piece if it is a valid move
  136. bool move_piece(const std::string &from, const std::string &to) {
  137. if (!started) {
  138. on_start();
  139. started = true;
  140. }
  141. int from_x = from[0] - 'a';
  142. int from_y = stoi(string() + from[1]) - 1;
  143. int to_x = to[0] - 'a';
  144. int to_y = stoi(string() + to[1]) - 1;
  145.  
  146. auto &piece_from = squares[from_x][from_y];
  147. if (piece_from) {
  148. if (piece_from->valid_move(from_x, from_y, to_x, to_y)) {
  149. if (on_piece_move)
  150. on_piece_move(*piece_from, from, to);
  151. auto &piece_to = squares[to_x][to_y];
  152. if (piece_to) {
  153. if (piece_from->color != piece_to->color) {
  154. if (on_piece_removed)
  155. on_piece_removed(*piece_to, to);
  156. if (auto king = dynamic_cast<King *>(piece_to.get())) {
  157. if (on_lost_game)
  158. on_lost_game(king->color);
  159. }
  160. } else {
  161. if (on_piece_move_invalid)
  162. on_piece_move_invalid(*piece_from, from, to);
  163. return false;
  164. }
  165. }
  166. piece_to = move(piece_from);
  167. after_piece_move(squares);
  168. return true;
  169. } else {
  170. if (on_piece_move_invalid)
  171. on_piece_move_invalid(*piece_from, from, to);
  172. return false;
  173. }
  174. } else {
  175. if (on_piece_move_missing)
  176. on_piece_move_missing(from);
  177. return false;
  178. }
  179. }
  180. };
  181.  
  182. class ChessBoardPrint {
  183. public:
  184. ChessBoardPrint(ChessBoard &board) {
  185. board.on_piece_move = [](const ChessBoard::Piece &piece, const string &from, const string &to) {
  186. cout << piece.type() << " is moving from " << from << " to " << to << endl;
  187. };
  188. board.on_piece_removed = [](const ChessBoard::Piece &piece, const string &square) {
  189. cout << piece.type() << " is being removed from " << square << endl;
  190. };
  191. board.on_lost_game = [](ChessBoard::Color color) {
  192. if (color == ChessBoard::Color::WHITE)
  193. cout << "Black";
  194. else
  195. cout << "White";
  196. cout << " lost the game" << endl;
  197. };
  198. board.on_piece_move_invalid = [](const ChessBoard::Piece &piece, const string &from, const string &to) {
  199. cout << "can not move " << piece.type() << " from " << from << " to " << to << endl;
  200. };
  201. board.on_piece_move_missing = [](const string &square) {
  202. cout << "no piece at " << square << endl;
  203. };
  204. board.on_start = []() {
  205. cout << "~ Chess ~\nPieces are indicated with color, w = white and b = black\nKing = K and Knight = j\nEmpty squares are either white [ ] or black [##]\nA simulated game:\n"
  206. << endl;
  207. };
  208. board.after_piece_move = [](const vector<vector<unique_ptr<ChessBoard::Piece>>> &squares) {
  209. cout << " A B C D E F G H" << endl;
  210. cout << " -------------------------" << endl;
  211. for (int c = 7; c >= 0; --c) {
  212. cout << c + 1 << " |";
  213. for (int d = 0; d < 8; ++d) {
  214. auto &piece_from = squares[d][c];
  215. if (piece_from) {
  216. string col = piece_from->color_abbrev();
  217. string abb = piece_from->abbrev();
  218. cout << col << abb;
  219. } else if (d % 2 == 0 && c % 2 == 0) {
  220. string emptyTile = "##";
  221. cout << emptyTile;
  222. } else if (d % 2 != 0 && c % 2 != 0) {
  223. string emptyTile = "##";
  224. cout << emptyTile;
  225. } else {
  226. string emptyTile = " ";
  227. cout << emptyTile;
  228. }
  229. cout << "|";
  230. }
  231. cout << endl;
  232. }
  233. cout << " -------------------------" << endl;
  234. };
  235. }
  236. };
  237.  
  238. int main() {
  239. ChessBoard board;
  240. ChessBoardPrint print(board);
  241.  
  242. board.squares[4][0] = make_unique<ChessBoard::King>(ChessBoard::Color::WHITE);
  243. board.squares[1][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
  244. board.squares[6][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
  245.  
  246. board.squares[4][7] = make_unique<ChessBoard::King>(ChessBoard::Color::BLACK);
  247. board.squares[1][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
  248. board.squares[6][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
  249.  
  250. board.move_piece("e3", "e2");
  251. board.move_piece("e1", "e3");
  252. board.move_piece("b1", "b2");
  253.  
  254. board.move_piece("e1", "e2");
  255. board.move_piece("g8", "h6");
  256. board.move_piece("b1", "c3");
  257. board.move_piece("h6", "g8");
  258. board.move_piece("c3", "d5");
  259. board.move_piece("g8", "h6");
  260. board.move_piece("d5", "f6");
  261. board.move_piece("h6", "g8");
  262. board.move_piece("f6", "e8");
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement