From 112a0d344da9e66d20ee9ab5d488e59c5d52e6ad Mon Sep 17 00:00:00 2001 From: Angeld55 Date: Thu, 9 Apr 2020 18:09:42 +0300 Subject: added new examples --- DiamondProblemAndPolymorphism/Chess/Bishop.hpp | 34 ------- DiamondProblemAndPolymorphism/Chess/Board.hpp | 127 ------------------------- DiamondProblemAndPolymorphism/Chess/Figure.hpp | 35 ------- DiamondProblemAndPolymorphism/Chess/Knight.hpp | 38 -------- DiamondProblemAndPolymorphism/Chess/Queen.hpp | 25 ----- DiamondProblemAndPolymorphism/Chess/README.md | 4 - DiamondProblemAndPolymorphism/Chess/Rook.hpp | 31 ------ DiamondProblemAndPolymorphism/Chess/main.cpp | 16 ---- DiamondProblemAndPolymorphism/sd | 1 - 9 files changed, 311 deletions(-) delete mode 100644 DiamondProblemAndPolymorphism/Chess/Bishop.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/Board.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/Figure.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/Knight.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/Queen.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/README.md delete mode 100644 DiamondProblemAndPolymorphism/Chess/Rook.hpp delete mode 100644 DiamondProblemAndPolymorphism/Chess/main.cpp delete mode 100644 DiamondProblemAndPolymorphism/sd (limited to 'DiamondProblemAndPolymorphism') diff --git a/DiamondProblemAndPolymorphism/Chess/Bishop.hpp b/DiamondProblemAndPolymorphism/Chess/Bishop.hpp deleted file mode 100644 index b46e315..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Bishop.hpp +++ /dev/null @@ -1,34 +0,0 @@ -#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/DiamondProblemAndPolymorphism/Chess/Board.hpp b/DiamondProblemAndPolymorphism/Chess/Board.hpp deleted file mode 100644 index 9ea5b93..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Board.hpp +++ /dev/null @@ -1,127 +0,0 @@ -#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/DiamondProblemAndPolymorphism/Chess/Figure.hpp b/DiamondProblemAndPolymorphism/Chess/Figure.hpp deleted file mode 100644 index 2d3e73e..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Figure.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#pragma once -#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 deleted file mode 100644 index 90f93e9..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Knight.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#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/DiamondProblemAndPolymorphism/Chess/Queen.hpp b/DiamondProblemAndPolymorphism/Chess/Queen.hpp deleted file mode 100644 index ad1add4..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Queen.hpp +++ /dev/null @@ -1,25 +0,0 @@ -#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/DiamondProblemAndPolymorphism/Chess/README.md b/DiamondProblemAndPolymorphism/Chess/README.md deleted file mode 100644 index 97ee907..0000000 --- a/DiamondProblemAndPolymorphism/Chess/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# Chess - -Not a full chess project! Just a prototype. - diff --git a/DiamondProblemAndPolymorphism/Chess/Rook.hpp b/DiamondProblemAndPolymorphism/Chess/Rook.hpp deleted file mode 100644 index d973870..0000000 --- a/DiamondProblemAndPolymorphism/Chess/Rook.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#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/DiamondProblemAndPolymorphism/Chess/main.cpp b/DiamondProblemAndPolymorphism/Chess/main.cpp deleted file mode 100644 index 2caaee8..0000000 --- a/DiamondProblemAndPolymorphism/Chess/main.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#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/DiamondProblemAndPolymorphism/sd b/DiamondProblemAndPolymorphism/sd deleted file mode 100644 index 6e9f0da..0000000 --- a/DiamondProblemAndPolymorphism/sd +++ /dev/null @@ -1 +0,0 @@ -h -- cgit v1.3.1-23-gd82d