From e82491b4c5331dcd854a446df01a9c76c0e50369 Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Sat, 21 Sep 2019 12:08:32 +0300 Subject: Add files via upload --- DiamondProblemAndPolymorphism/Chess/Bishop.hpp | 33 +++++++ DiamondProblemAndPolymorphism/Chess/Board.hpp | 124 +++++++++++++++++++++++++ DiamondProblemAndPolymorphism/Chess/Figure.hpp | 34 +++++++ DiamondProblemAndPolymorphism/Chess/Knight.hpp | 37 ++++++++ DiamondProblemAndPolymorphism/Chess/Queen.hpp | 22 +++++ DiamondProblemAndPolymorphism/Chess/Rook.hpp | 30 ++++++ 6 files changed, 280 insertions(+) create mode 100644 DiamondProblemAndPolymorphism/Chess/Bishop.hpp create mode 100644 DiamondProblemAndPolymorphism/Chess/Board.hpp create mode 100644 DiamondProblemAndPolymorphism/Chess/Figure.hpp create mode 100644 DiamondProblemAndPolymorphism/Chess/Knight.hpp create mode 100644 DiamondProblemAndPolymorphism/Chess/Queen.hpp create mode 100644 DiamondProblemAndPolymorphism/Chess/Rook.hpp (limited to 'DiamondProblemAndPolymorphism') diff --git a/DiamondProblemAndPolymorphism/Chess/Bishop.hpp b/DiamondProblemAndPolymorphism/Chess/Bishop.hpp new file mode 100644 index 0000000..b8a6b4c --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Bishop.hpp @@ -0,0 +1,33 @@ +#include "Figure.hpp" + +class Bishop : virtual public Figure +{ +public: + Bishop(int xCoordinate, int yCoordinate, bool isWhite) :Figure(xCoordinate, yCoordinate, isWhite) + {} + + bool Move(int xNew, int yNew); + + void Print(); +}; +bool Bishop::Move(int xNew, int yNew) +{ + if (!isValidCoord(xNew, yNew)) + return false; + int dx = abs(xCoord - xNew); + int dy = abs(yCoord - yNew); + if (dx == dy) + { + xCoord = xNew; + yCoord = yNew; + return true; + } + return false; + + + +} +void Bishop::Print() +{ + cout << 'B'; +} \ No newline at end of file diff --git a/DiamondProblemAndPolymorphism/Chess/Board.hpp b/DiamondProblemAndPolymorphism/Chess/Board.hpp new file mode 100644 index 0000000..2602ced --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Board.hpp @@ -0,0 +1,124 @@ +#include "Figure.hpp" + +class Board +{ + Figure*** board; + +public: + Board(); + Board(const Board& other) = delete; + Board& operator=(const Board& other) = delete; + ~Board(); + bool isEmptySquare(int x, int y) const; + bool AddKnight(int x, int y, bool isWhite); + bool AddQueen(int x, int y, bool isWhite); + bool AddBishop(int x, int y, bool isWhite); + bool AddRook(int x, int y, bool isWhite); + + void Print(); + + bool MoveFigure(int xOld, int yOld, int xNew, int yNew); + +}; +Board::Board() +{ + board = new Figure**[8]; + for (int i = 0; i < 8; ++i) + board[i] = new Figure*[8]; + + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; j++) + board[i][j] = nullptr; + } + +} + +Board::~Board() +{ + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + delete board[i][j]; + } + delete[] board[i]; + } + delete[] board; + +} +bool Board::isEmptySquare(int x, int y) const +{ + return board[x][y] == nullptr; +} + +bool Board::AddKnight(int x, int y, bool isWhite) +{ + if (!isValidCoord(x, y) || !isEmptySquare(x, y)) + return false; + board[x][y] = new Knight(x, y, isWhite); + return true; +} +bool Board::AddQueen(int x, int y, bool isWhite) +{ + if (!isValidCoord(x, y) || !isEmptySquare(x, y)) + return false; + board[x][y] = new Queen(x, y, isWhite); + return true; +} +bool Board::AddBishop(int x, int y, bool isWhite) +{ + if (!isValidCoord(x, y) || !isEmptySquare(x, y)) + return false; + board[x][y] = new Bishop(x, y, isWhite); + return true; +} +bool Board::AddRook(int x, int y, bool isWhite) +{ + if (!isValidCoord(x, y) || !isEmptySquare(x, y)) + return false; + board[x][y] = new Rook(x, y, isWhite); + return true; +} + +void Board::Print() +{ + for (int i = 0; i < 8; ++i) + { + for (int j = 0; j < 8; ++j) + { + if (!board[i][j]) + cout << '_'; + else + board[i][j]->Print(); + + } + cout << endl; + } + cout << endl; + cout << endl; +} + +bool Board::MoveFigure(int xOld, int yOld, int xNew, int yNew) +{ + if (isEmptySquare(xOld, yOld)) //Ако няма фигура, там от където искаме да преместим + return false; + + bool res = board[xOld][yOld]->Move(xNew, yNew); //провери дали фигурата може да се премести на съответната прозиция. + if (!res)// ако не -> лъжа(ходът е невъзможен) + return false; + + if (!isEmptySquare(xNew, yNew)) + { + if (board[xOld][yOld]->isWhite ^ board[xNew][yNew]->isWhite)// ако там седи фигура от различен цвят + delete board[xNew][yNew]; //взимаме я(трием обекта) + else //ако фигурата която седи на новата позиция е от СЪЩИЯ цвят + { + board[xOld][yOld]->Move(xOld, yOld); //връщаме обратно кординатите новата фигура. Тук би възникнал проблем, ако имаме фигурата пешка. + //Това е така, понеже тя няма да може да се върне назад, ако вече е преместена. + return false; //За това примерно решение е, да се направи функция goBackwards. + } + } + board[xNew][yNew] = board[xOld][yOld]; + board[xOld][yOld] = nullptr; +} \ No newline at end of file diff --git a/DiamondProblemAndPolymorphism/Chess/Figure.hpp b/DiamondProblemAndPolymorphism/Chess/Figure.hpp new file mode 100644 index 0000000..157659b --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Figure.hpp @@ -0,0 +1,34 @@ +#include + +using namespace std; + +bool isValidCoord(int xCoord, int yCoord) +{ + return xCoord >= 0 && xCoord <= 7 + && yCoord >= 0 && yCoord <= 7; +} + +class Figure +{ +public: + Figure(int xCoord, int yCoord, bool isWhite); + + int xCoord; + int yCoord; + bool isWhite; + virtual bool Move(int xCoord, int yCoord) = 0; + virtual void Print() = 0; + virtual ~Figure(){}; +}; + +Figure::Figure(int xCoord, int yCoord, bool isWhite); +{ + if (isValidCoord(xCoord, yCoord)) + { + this->xCoord = xCoord; + this->yCoord = yCoord; + this->isWhite = isWhite; + } + else + throw "Ivalid coordinates"; +} \ No newline at end of file diff --git a/DiamondProblemAndPolymorphism/Chess/Knight.hpp b/DiamondProblemAndPolymorphism/Chess/Knight.hpp new file mode 100644 index 0000000..eb2ee9f --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Knight.hpp @@ -0,0 +1,37 @@ +#include "Figure.hpp" + +class Knight : public Figure +{ +public: + Knight(int xCoordinate, int yCoordinate, bool isWhite) :Figure(xCoordinate, yCoordinate, isWhite) + {} + + bool Move(int xNew, int yNew); + + void Print(); +}; +bool Knight::Move(int xNew, int yNew) +{ + if (!isValidCoord(xNew, yNew)) + return false; + int possibleX[] = { 1, 1, -1, -1, 2, 2, -2, -2 }; + int possibleY[] = { 2, -2, 2, -2, 1, -1, 1, -1 }; + for (int i = 0; i < 8; ++i) + { + if (xNew == xCoord + possibleX[i] + && yNew == yCoord + possibleY[i]) + { + xCoord = xNew; + yCoord = yNew; + return true; + } + + } + return false; + +} + +void Knight::Print() +{ + cout << 'K'; +} \ No newline at end of file diff --git a/DiamondProblemAndPolymorphism/Chess/Queen.hpp b/DiamondProblemAndPolymorphism/Chess/Queen.hpp new file mode 100644 index 0000000..84ce90e --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Queen.hpp @@ -0,0 +1,22 @@ +#include "Figure.hpp" + +class Queen : public Bishop, public Rook +{ +public: + Queen(int xCoordinate, int yCoordinate, bool isWhite) :Figure(xCoordinate, yCoordinate, isWhite), Bishop(xCoordinate, yCoordinate, isWhite), Rook(xCoordinate, yCoordinate, isWhite) + {} + bool Move(int xNew, int yNew); + void Print(); +}; + +void Queen::Print() +{ + cout << 'Q'; +} +bool Queen::Move(int xNew, int yNew) // Тук сме наследили 2 функционалности. Имаме конфликт, трябва да дефинираме наша(използвайки предишните, ако искаме) +{ + bool res = Rook::Move(xNew, yNew);//Пробвай да се движиш като топ(по права линия) + if (res) //Ако е успял - вс е ок + return true; + return Bishop::Move(xNew, yNew); //Ако не си успял като топ, пробвай като офицер. +} \ No newline at end of file diff --git a/DiamondProblemAndPolymorphism/Chess/Rook.hpp b/DiamondProblemAndPolymorphism/Chess/Rook.hpp new file mode 100644 index 0000000..576920a --- /dev/null +++ b/DiamondProblemAndPolymorphism/Chess/Rook.hpp @@ -0,0 +1,30 @@ +#include "Figure.hpp" + +class Rook : virtual public Figure +{ + +public: + Rook(int xCoordinate, int yCoordinate, bool isWhite) :Figure(xCoordinate, yCoordinate, isWhite) + {} + + bool Move(int xNew, int yNew); + + void Print(); + +}; + +bool Rook::Move(int xNew, int yNew); +{ + if (!isValidCoord(xNew, yNew)) + return false; + if (!(xCoord == xNew ^ yCoord == yNew)) + return false; + xCoord = xNew; + yCoord = yNew; + return true; +} + +void Rook::Print() +{ + cout << 'R'; +} \ No newline at end of file -- cgit v1.3.1-23-gd82d