diff options
9 files changed, 311 insertions, 0 deletions
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Bishop.hpp | 34 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Board.hpp | 127 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Figure.hpp | 35 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Knight.hpp | 38 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Queen.hpp | 25 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/README.md | 4 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/Rook.hpp | 31 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/Chess/main.cpp | 16 | ||||
| -rw-r--r-- | Diamond_problem_and_polymorphism/sd | 1 |
diff --git a/Diamond_problem_and_polymorphism/Chess/Bishop.hpp b/Diamond_problem_and_polymorphism/Chess/Bishop.hpp new file mode 100644 index 0000000..014eb48 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Bishop.hpp @@ -0,0 +1,34 @@ +#pragma once +#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/Diamond_problem_and_polymorphism/Chess/Board.hpp b/Diamond_problem_and_polymorphism/Chess/Board.hpp new file mode 100644 index 0000000..95c0f41 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Board.hpp @@ -0,0 +1,127 @@ +#pragma once +#include "Figure.hpp" +#include "Knight.hpp" +#include "Queen.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/Diamond_problem_and_polymorphism/Chess/Figure.hpp b/Diamond_problem_and_polymorphism/Chess/Figure.hpp new file mode 100644 index 0000000..e3fa89d --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Figure.hpp @@ -0,0 +1,35 @@ +#pragma once +#include <iostream> + +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/Diamond_problem_and_polymorphism/Chess/Knight.hpp b/Diamond_problem_and_polymorphism/Chess/Knight.hpp new file mode 100644 index 0000000..01b9c34 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Knight.hpp @@ -0,0 +1,38 @@ +#pragma once +#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/Diamond_problem_and_polymorphism/Chess/Queen.hpp b/Diamond_problem_and_polymorphism/Chess/Queen.hpp new file mode 100644 index 0000000..2f89029 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Queen.hpp @@ -0,0 +1,25 @@ +#pragma once +#include "Figure.hpp" +#include "Bishop.hpp" +#include "Rook.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); //Ако не си успял като топ, пробвай като офицер. +} diff --git a/Diamond_problem_and_polymorphism/Chess/README.md b/Diamond_problem_and_polymorphism/Chess/README.md new file mode 100644 index 0000000..97ee907 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/README.md @@ -0,0 +1,4 @@ +# Chess + +Not a full chess project! Just a prototype. + diff --git a/Diamond_problem_and_polymorphism/Chess/Rook.hpp b/Diamond_problem_and_polymorphism/Chess/Rook.hpp new file mode 100644 index 0000000..5ad81c8 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/Rook.hpp @@ -0,0 +1,31 @@ +#pragma once +#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 diff --git a/Diamond_problem_and_polymorphism/Chess/main.cpp b/Diamond_problem_and_polymorphism/Chess/main.cpp new file mode 100644 index 0000000..2caaee8 --- /dev/null +++ b/Diamond_problem_and_polymorphism/Chess/main.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include "Board.hpp" +using namespace std; + +int main() +{ + while (1) + { + Board b; + b.AddBishop(3, 4, true); + b.AddKnight(2, 3, false); + b.Print(); + b.MoveFigure(3, 4, 2, 3); + b.Print(); + } +} diff --git a/Diamond_problem_and_polymorphism/sd b/Diamond_problem_and_polymorphism/sd new file mode 100644 index 0000000..6e9f0da --- /dev/null +++ b/Diamond_problem_and_polymorphism/sd @@ -0,0 +1 @@ +h |
