Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <functional>
- #include <iostream>
- #include <memory>
- #include <string>
- #include <vector>
- using namespace std;
- class ChessBoard {
- private:
- bool started = false;
- public:
- enum class Color { WHITE,
- BLACK };
- class Piece {
- public:
- Piece(Color color) : color(color) {}
- virtual ~Piece() {}
- Color color;
- string color_string() const {
- if (color == Color::WHITE)
- return "white";
- else
- return "black";
- }
- string color_abbrev() const {
- if (color == Color::WHITE)
- return "w";
- else
- return "b";
- }
- /// Return color and type of the chess piece
- virtual string type() const = 0;
- /// Returns true if the given chess piece move is valid
- virtual bool valid_move(int from_x, int from_y, int to_x, int to_y) const = 0;
- /// Return abbreviation of the chess piece
- virtual string abbrev() const = 0;
- };
- //Oppgave 1:
- class King : public Piece {
- public:
- const string name = "king";
- const string abbreviation = "K";
- Color color;
- King(const Color color_) : Piece(color_) {}
- string type() const override {
- string type = color_string() + " " + name;
- return type;
- }
- bool valid_move(int from_x, int from_y, int to_x, int to_y) const override {
- int diffY = from_y - to_y;
- int diffX = from_x - to_x;
- //Checks if the difference in either direction is smaller than or equal to 1
- if ((diffY <= 1 && diffY >= -1) && (diffX <= 1 && diffX >= -1)) {
- return true;
- }
- return false;
- }
- string abbrev() const override {
- return abbreviation;
- }
- };
- class Knight : public Piece {
- public:
- const string name = "knight";
- const string abbreviation = "N";
- Color color;
- Knight(const Color color_) : Piece(color_) {}
- string type() const override {
- string type = color_string() + " " + name;
- return type;
- }
- bool valid_move(int from_x, int from_y, int to_x, int to_y) const override {
- int diffY = from_y - to_y;
- int diffX = from_x - to_x;
- //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
- if ((diffY == 2) && (diffX == 1)) {
- return true;
- } else if ((diffY == -2) && (diffX == 1)) {
- return true;
- } else if ((diffY == -2) && (diffX == -1)) {
- return true;
- } else if ((diffY == 2) && (diffX == -1)) {
- return true;
- } else if ((diffY == 1) && (diffX == 2)) {
- return true;
- } else if ((diffY == -1) && (diffX == 2)) {
- return true;
- } else if ((diffY == -1) && (diffX == -2)) {
- return true;
- } else if ((diffY == 1) && (diffX == -2)) {
- return true;
- }
- return false;
- }
- string abbrev() const override {
- return abbreviation;
- }
- };
- ChessBoard() {
- // Initialize the squares stored in 8 columns and 8 rows:
- squares.resize(8);
- for (auto &square_column : squares)
- square_column.resize(8);
- }
- /// 8x8 squares occupied by 1 or 0 chess pieces
- vector<vector<unique_ptr<Piece>>> squares;
- //Funksjonsobjekter
- function<void(const Piece &piece, const string &from, const string &to)> on_piece_move;
- function<void(const Piece &piece, const string &square)> on_piece_removed;
- function<void(Color color)> on_lost_game;
- function<void(const Piece &piece, const string &from, const string &to)> on_piece_move_invalid;
- function<void(const string &square)> on_piece_move_missing;
- function<void()> on_start;
- function<void(const vector<vector<unique_ptr<Piece>>> &squares)> after_piece_move;
- /// Move a chess piece if it is a valid move
- bool move_piece(const std::string &from, const std::string &to) {
- if (!started) {
- on_start();
- started = true;
- }
- int from_x = from[0] - 'a';
- int from_y = stoi(string() + from[1]) - 1;
- int to_x = to[0] - 'a';
- int to_y = stoi(string() + to[1]) - 1;
- auto &piece_from = squares[from_x][from_y];
- if (piece_from) {
- if (piece_from->valid_move(from_x, from_y, to_x, to_y)) {
- if (on_piece_move)
- on_piece_move(*piece_from, from, to);
- auto &piece_to = squares[to_x][to_y];
- if (piece_to) {
- if (piece_from->color != piece_to->color) {
- if (on_piece_removed)
- on_piece_removed(*piece_to, to);
- if (auto king = dynamic_cast<King *>(piece_to.get())) {
- if (on_lost_game)
- on_lost_game(king->color);
- }
- } else {
- if (on_piece_move_invalid)
- on_piece_move_invalid(*piece_from, from, to);
- return false;
- }
- }
- piece_to = move(piece_from);
- after_piece_move(squares);
- return true;
- } else {
- if (on_piece_move_invalid)
- on_piece_move_invalid(*piece_from, from, to);
- return false;
- }
- } else {
- if (on_piece_move_missing)
- on_piece_move_missing(from);
- return false;
- }
- }
- };
- class ChessBoardPrint {
- public:
- ChessBoardPrint(ChessBoard &board) {
- board.on_piece_move = [](const ChessBoard::Piece &piece, const string &from, const string &to) {
- cout << piece.type() << " is moving from " << from << " to " << to << endl;
- };
- board.on_piece_removed = [](const ChessBoard::Piece &piece, const string &square) {
- cout << piece.type() << " is being removed from " << square << endl;
- };
- board.on_lost_game = [](ChessBoard::Color color) {
- if (color == ChessBoard::Color::WHITE)
- cout << "Black";
- else
- cout << "White";
- cout << " lost the game" << endl;
- };
- board.on_piece_move_invalid = [](const ChessBoard::Piece &piece, const string &from, const string &to) {
- cout << "can not move " << piece.type() << " from " << from << " to " << to << endl;
- };
- board.on_piece_move_missing = [](const string &square) {
- cout << "no piece at " << square << endl;
- };
- board.on_start = []() {
- 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"
- << endl;
- };
- board.after_piece_move = [](const vector<vector<unique_ptr<ChessBoard::Piece>>> &squares) {
- cout << " A B C D E F G H" << endl;
- cout << " -------------------------" << endl;
- for (int c = 7; c >= 0; --c) {
- cout << c + 1 << " |";
- for (int d = 0; d < 8; ++d) {
- auto &piece_from = squares[d][c];
- if (piece_from) {
- string col = piece_from->color_abbrev();
- string abb = piece_from->abbrev();
- cout << col << abb;
- } else if (d % 2 == 0 && c % 2 == 0) {
- string emptyTile = "##";
- cout << emptyTile;
- } else if (d % 2 != 0 && c % 2 != 0) {
- string emptyTile = "##";
- cout << emptyTile;
- } else {
- string emptyTile = " ";
- cout << emptyTile;
- }
- cout << "|";
- }
- cout << endl;
- }
- cout << " -------------------------" << endl;
- };
- }
- };
- int main() {
- ChessBoard board;
- ChessBoardPrint print(board);
- board.squares[4][0] = make_unique<ChessBoard::King>(ChessBoard::Color::WHITE);
- board.squares[1][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
- board.squares[6][0] = make_unique<ChessBoard::Knight>(ChessBoard::Color::WHITE);
- board.squares[4][7] = make_unique<ChessBoard::King>(ChessBoard::Color::BLACK);
- board.squares[1][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
- board.squares[6][7] = make_unique<ChessBoard::Knight>(ChessBoard::Color::BLACK);
- board.move_piece("e3", "e2");
- board.move_piece("e1", "e3");
- board.move_piece("b1", "b2");
- board.move_piece("e1", "e2");
- board.move_piece("g8", "h6");
- board.move_piece("b1", "c3");
- board.move_piece("h6", "g8");
- board.move_piece("c3", "d5");
- board.move_piece("g8", "h6");
- board.move_piece("d5", "f6");
- board.move_piece("h6", "g8");
- board.move_piece("f6", "e8");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement