Advertisement
Guest User

chess

a guest
Apr 18th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <iostream>
  2. #include <memory>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. class ChessBoard {
  9. public:
  10. enum class Color { WHITE,
  11. BLACK };
  12.  
  13. class Piece {
  14. public:
  15. Piece(Color color) : color(color) {}
  16. virtual ~Piece() {}
  17.  
  18. Color color;
  19. std::string color_string() const {
  20. if (color == Color::WHITE)
  21. return "white";
  22. else
  23. return "black";
  24. }
  25.  
  26. /// Return color and type of the chess piece
  27. virtual std::string type() const = 0;
  28.  
  29. /// Returns true if the given chess piece move is valid
  30. virtual bool valid_move(int from_x, int from_y, int to_x, int to_y) const = 0;
  31. };
  32.  
  33. class King : public Piece {
  34. // missing implementations
  35. };
  36.  
  37. class Knight : public Piece {
  38. // missing implementations
  39. };
  40.  
  41. ChessBoard() {
  42. // Initialize the squares stored in 8 columns and 8 rows:
  43. squares.resize(8);
  44. for (auto &square_column : squares)
  45. square_column.resize(8);
  46. }
  47.  
  48. /// 8x8 squares occupied by 1 or 0 chess pieces
  49. vector<vector<unique_ptr<Piece>>> squares;
  50.  
  51. /// Move a chess piece if it is a valid move.
  52. /// Does not test for check or checkmate.
  53. bool move_piece(const std::string &from, const std::string &to) {
  54. int from_x = from[0] - 'a';
  55. int from_y = stoi(string() + from[1]) - 1;
  56. int to_x = to[0] - 'a';
  57. int to_y = stoi(string() + to[1]) - 1;
  58.  
  59. auto &piece_from = squares[from_x][from_y];
  60. if (piece_from) {
  61. if (piece_from->valid_move(from_x, from_y, to_x, to_y)) {
  62. cout << piece_from->type() << " is moving from " << from << " to " << to << endl;
  63. auto &piece_to = squares[to_x][to_y];
  64. if (piece_to) {
  65. if (piece_from->color != piece_to->color) {
  66. cout << piece_to->type() << " is being removed from " << to << endl;
  67. if (auto king = dynamic_cast<King *>(piece_to.get()))
  68. cout << king->color_string() << " lost the game" << endl;
  69. } else {
  70. // piece in the from square has the same color as the piece in the to square
  71. cout << "can not move " << piece_from->type() << " from " << from << " to " << to << endl;
  72. return false;
  73. }
  74. }
  75. piece_to = move(piece_from);
  76. return true;
  77. } else {
  78. cout << "can not move " << piece_from->type() << " from " << from << " to " << to << endl;
  79. return false;
  80. }
  81. } else {
  82. cout << "no piece at " << from << endl;
  83. return false;
  84. }
  85. }
  86. };
  87.  
  88. int main() {
  89. ChessBoard board;
  90.  
  91. board.squares[4][0] = make_unique<ChessBoard::King>(ChessBoard::Color::WHITE);
  92. board.squares[1][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
  93. board.squares[6][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
  94.  
  95. board.squares[4][7] = make_unique<ChessBoard::King>(ChessBoard::Color::BLACK);
  96. board.squares[1][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
  97. board.squares[6][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
  98.  
  99. cout << "Invalid moves:" << endl;
  100. board.move_piece("e3", "e2");
  101. board.move_piece("e1", "e3");
  102. board.move_piece("b1", "b2");
  103. cout << endl;
  104.  
  105. cout << "A simulated game:" << endl;
  106. board.move_piece("e1", "e2");
  107. board.move_piece("g8", "h6");
  108. board.move_piece("b1", "c3");
  109. board.move_piece("h6", "g8");
  110. board.move_piece("c3", "d5");
  111. board.move_piece("g8", "h6");
  112. board.move_piece("d5", "f6");
  113. board.move_piece("h6", "g8");
  114. board.move_piece("f6", "e8");
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement