diff options
20 files changed, 353 insertions, 0 deletions
| -rw-r--r-- | Sem. 15/Chess/ChessBoard/ChessBoard.cpp | 46 | ||||
| -rw-r--r-- | Sem. 15/Chess/ChessBoard/ChessBoard.h | 40 | ||||
| -rw-r--r-- | Sem. 15/Chess/FigureFactory/FigureFactory.cpp | 36 | ||||
| -rw-r--r-- | Sem. 15/Chess/FigureFactory/FigureFactory.h | 13 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Bishop/Bishop.cpp | 16 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Bishop/Bishop.h | 13 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/FigureBase/Figure.cpp | 0 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/FigureBase/Figure.h | 16 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/King/King.cpp | 16 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/King/King.h | 11 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Knight/Knight.cpp | 26 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Knight/Knight.h | 11 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Pawn/Pawn.cpp | 31 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Pawn/Pawn.h | 11 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Queen/Queen.cpp | 18 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Queen/Queen.h | 11 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Rook/Rook.cpp | 15 | ||||
| -rw-r--r-- | Sem. 15/Chess/Figures/Rook/Rook.h | 11 | ||||
| -rw-r--r-- | Sem. 15/Chess/main.cpp | 12 | ||||
| -rw-r--r-- | Sem. 15/README.md | 0 |
diff --git a/Sem. 15/Chess/ChessBoard/ChessBoard.cpp b/Sem. 15/Chess/ChessBoard/ChessBoard.cpp new file mode 100644 index 0000000..dc809cc --- /dev/null +++ b/Sem. 15/Chess/ChessBoard/ChessBoard.cpp @@ -0,0 +1,46 @@ +#include "ChessBoard.h" +#include "../FigureFactory/FigureFactory.h" + +ChessBoard::ChessBoard() +{ + FigureFactory factory; + for (int i = 0; i < BOARD_SIZE; i++) + { + board[1][i].f = factory.createPawn(false); + board[6][i].f = factory.createPawn(true); + } + + board[0][0].f = factory.createRook(false); + board[0][7].f = factory.createRook(false); + + board[7][0].f = factory.createRook(true); + board[7][7].f = factory.createRook(true); + + + board[0][1].f = factory.createKnight(false); + board[0][6].f = factory.createKnight(false); + board[7][1].f = factory.createKnight(true); + board[7][6].f = factory.createKnight(true); + + board[0][2].f = factory.createBishop(false); + board[0][5].f = factory.createBishop(false); + board[7][2].f = factory.createBishop(true); + board[7][5].f = factory.createBishop(true); + + board[0][3].f = factory.createKnig(false); + board[0][4].f = factory.createQueen(false); + board[7][4].f = factory.createQueen(true); + board[7][3].f = factory.createKnig(true); +} + +void ChessBoard::print() const +{ + for (int i = 0; i < BOARD_SIZE; i++) + { + for (int j = 0; j < BOARD_SIZE; j++) + { + board[i][j].print(); + } + std::cout << std::endl; + } +}
\ No newline at end of file diff --git a/Sem. 15/Chess/ChessBoard/ChessBoard.h b/Sem. 15/Chess/ChessBoard/ChessBoard.h new file mode 100644 index 0000000..1825119 --- /dev/null +++ b/Sem. 15/Chess/ChessBoard/ChessBoard.h @@ -0,0 +1,40 @@ +#pragma once +#include "../Figures/FigureBase/Figure.h" + +const size_t BOARD_SIZE = 8; +class ChessBoard +{ + struct Cell + { + Figure* f = nullptr; + Cell() = default; + + Cell(const Cell& other) = delete; + Cell& operator=(const Cell& other) = delete; + bool isEmpty() + { + return f == nullptr; + } + + void print() const + { + if (f != nullptr) + f->print(); + else + std::cout << ' '; + } + + ~Cell() + { + delete f; + } + }; + Cell board[BOARD_SIZE][BOARD_SIZE]; + +public: + ChessBoard(); + ChessBoard(const ChessBoard& other) = delete; + ChessBoard& operator=(const ChessBoard&) = delete; + + void print() const; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/FigureFactory/FigureFactory.cpp b/Sem. 15/Chess/FigureFactory/FigureFactory.cpp new file mode 100644 index 0000000..db70d1e --- /dev/null +++ b/Sem. 15/Chess/FigureFactory/FigureFactory.cpp @@ -0,0 +1,36 @@ +#include "FigureFactory.h" +#include "../Figures/Bishop/Bishop.h" +#include "../Figures/King/King.h" +#include "../Figures/Knight/Knight.h" +#include "../Figures/Rook/Rook.h" +#include "../Figures/Queen/Queen.h" +#include "../Figures/Pawn/Pawn.h" + +Figure* FigureFactory::createPawn(bool isWhite) +{ + return new Pawn(isWhite); +} +Figure* FigureFactory::createBishop(bool isWhite) +{ + return new Bishop(isWhite); +} +Figure* FigureFactory::createRook(bool isWhite) +{ + return new Rook(isWhite); +} +Figure* createQueen(bool isWhite) +{ + return new Queen(isWhite); +} +Figure* FigureFactory::createKnight(bool isWhite) +{ + return new Knight(isWhite); +} +Figure* FigureFactory::createKnig(bool isWhite) +{ + return new King(isWhite); +} +Figure* FigureFactory::createQueen(bool isWhite) +{ + return new Queen(isWhite); +}
\ No newline at end of file diff --git a/Sem. 15/Chess/FigureFactory/FigureFactory.h b/Sem. 15/Chess/FigureFactory/FigureFactory.h new file mode 100644 index 0000000..da5b4db --- /dev/null +++ b/Sem. 15/Chess/FigureFactory/FigureFactory.h @@ -0,0 +1,13 @@ +#pragma once +#include "../Figures/FigureBase/Figure.h" + +class FigureFactory +{ +public: + Figure* createPawn(bool isWhite); + Figure* createBishop(bool isWhite); + Figure* createRook(bool isWhite); + Figure* createQueen(bool isWhite); + Figure* createKnight(bool isWhite); + Figure* createKnig(bool isWhite); +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Bishop/Bishop.cpp b/Sem. 15/Chess/Figures/Bishop/Bishop.cpp new file mode 100644 index 0000000..80121e2 --- /dev/null +++ b/Sem. 15/Chess/Figures/Bishop/Bishop.cpp @@ -0,0 +1,16 @@ +#include "Bishop.h" +#include <cmath> + +Bishop::Bishop(bool isWhite) : Figure(isWhite) +{} +bool Bishop::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + return abs((int)currentX - (int)destX) == abs((int)currentY - (int)destY); +} +void Bishop::print() const +{ + if (getIsWhite()) + std::cout << 'B'; + else + std::cout << 'b'; +} diff --git a/Sem. 15/Chess/Figures/Bishop/Bishop.h b/Sem. 15/Chess/Figures/Bishop/Bishop.h new file mode 100644 index 0000000..86949a7 --- /dev/null +++ b/Sem. 15/Chess/Figures/Bishop/Bishop.h @@ -0,0 +1,13 @@ +#pragma once +#include "../FigureBase/Figure.h" + +class Bishop : virtual public Figure +{ + +public: + Bishop(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; + + void print() const override; + +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/FigureBase/Figure.cpp b/Sem. 15/Chess/Figures/FigureBase/Figure.cpp new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Sem. 15/Chess/Figures/FigureBase/Figure.cpp diff --git a/Sem. 15/Chess/Figures/FigureBase/Figure.h b/Sem. 15/Chess/Figures/FigureBase/Figure.h new file mode 100644 index 0000000..c0a84a5 --- /dev/null +++ b/Sem. 15/Chess/Figures/FigureBase/Figure.h @@ -0,0 +1,16 @@ +#pragma once +#include <iostream> + +class Figure +{ + bool isWhite; +public: + Figure(bool isWhite) :isWhite(isWhite) + {} + virtual bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const = 0; + virtual void print() const = 0; + + bool getIsWhite() const { return isWhite; }; + + virtual ~Figure(){}; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/King/King.cpp b/Sem. 15/Chess/Figures/King/King.cpp new file mode 100644 index 0000000..9d323f3 --- /dev/null +++ b/Sem. 15/Chess/Figures/King/King.cpp @@ -0,0 +1,16 @@ +#include "King.h" +#include <cmath> + +King::King(bool isWhite) : Figure(isWhite) +{} +bool King::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + return abs((int)currentX - (int)destX) <= 1 && abs((int)currentY - (int)destY) <= 1; +} +void King::print() const +{ + if (getIsWhite()) + std::cout << '%'; + else + std::cout << '$'; +}
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/King/King.h b/Sem. 15/Chess/Figures/King/King.h new file mode 100644 index 0000000..85a8e02 --- /dev/null +++ b/Sem. 15/Chess/Figures/King/King.h @@ -0,0 +1,11 @@ +#pragma once +#include "../FigureBase/Figure.h" + +class King : public Figure +{ + +public: + King(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; + void print() const override; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Knight/Knight.cpp b/Sem. 15/Chess/Figures/Knight/Knight.cpp new file mode 100644 index 0000000..e6db119 --- /dev/null +++ b/Sem. 15/Chess/Figures/Knight/Knight.cpp @@ -0,0 +1,26 @@ +#include "Knight.h" +#include <cmath> + +Knight::Knight(bool isWhite) : Figure(isWhite) +{} +bool Knight::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + int xMovеment[] = { 1, 1, -1, -1, 2, -2, 2, -2 }; + int yMovеment[] = { 2, -2, 2, -2, 1, 1, -1, -1 }; + + for (int i = 0; i < 8; i++) + { + if (currentX + xMovеment[i] == destX && + currentY + yMovеment[i] == destY) + return true; + } + return false; +} + +void Knight::print() const +{ + if (getIsWhite()) + std::cout << 'K'; + else + std::cout << 'k'; +}
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Knight/Knight.h b/Sem. 15/Chess/Figures/Knight/Knight.h new file mode 100644 index 0000000..72acfea --- /dev/null +++ b/Sem. 15/Chess/Figures/Knight/Knight.h @@ -0,0 +1,11 @@ +#pragma once +#include "../FigureBase/Figure.h" + +class Knight : public Figure +{ + +public: + Knight(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; + void print() const override; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Pawn/Pawn.cpp b/Sem. 15/Chess/Figures/Pawn/Pawn.cpp new file mode 100644 index 0000000..fd04dad --- /dev/null +++ b/Sem. 15/Chess/Figures/Pawn/Pawn.cpp @@ -0,0 +1,31 @@ +#include "Pawn.h" +#include <cmath> +Pawn::Pawn(bool isWhite) : Figure(isWhite), isFirstMove(true) +{} +bool Pawn::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + /*if (currentX != destX) + return false; + + int diff = destY - currentY; + + if (abs(diff) > 2) + return false; + + if (diff < 0 && getIsWhite() || diff > 0 && !getIsWhite()) + return false; + + if (!abs(diff) == 2 || isFirstMove) + { + return true; + } + return false;*/ + return false; +} +void Pawn::print() const +{ + if (getIsWhite()) + std::cout << 'P'; + else + std::cout << 'p'; +}
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Pawn/Pawn.h b/Sem. 15/Chess/Figures/Pawn/Pawn.h new file mode 100644 index 0000000..675bf0b --- /dev/null +++ b/Sem. 15/Chess/Figures/Pawn/Pawn.h @@ -0,0 +1,11 @@ +#pragma once +#include "../FigureBase/Figure.h" + +class Pawn : virtual public Figure +{ + bool isFirstMove; +public: + Pawn(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; + void print() const override; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Queen/Queen.cpp b/Sem. 15/Chess/Figures/Queen/Queen.cpp new file mode 100644 index 0000000..da49c1c --- /dev/null +++ b/Sem. 15/Chess/Figures/Queen/Queen.cpp @@ -0,0 +1,18 @@ +#include "Queen.h" + +Queen::Queen(bool isWhite) : Bishop(isWhite), Rook(isWhite), Figure(isWhite) +{} + +bool Queen::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + return Bishop::canBeMoved(currentX, currentY, destX, destY) + || Rook::canBeMoved(currentX, currentY, destX, destY); +} + +void Queen::print() const +{ + if (getIsWhite()) + std::cout << 'Q'; + else + std::cout << 'q'; +}
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Queen/Queen.h b/Sem. 15/Chess/Figures/Queen/Queen.h new file mode 100644 index 0000000..c4fb1da --- /dev/null +++ b/Sem. 15/Chess/Figures/Queen/Queen.h @@ -0,0 +1,11 @@ +#pragma once +#include "../Bishop/Bishop.h" +#include "../Rook/Rook.h" + +class Queen : public Bishop, public Rook +{ +public: + Queen(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const; + void print() const; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/Figures/Rook/Rook.cpp b/Sem. 15/Chess/Figures/Rook/Rook.cpp new file mode 100644 index 0000000..19e84a6 --- /dev/null +++ b/Sem. 15/Chess/Figures/Rook/Rook.cpp @@ -0,0 +1,15 @@ +#include "Rook.h" + +Rook::Rook(bool isWhite) : Figure(isWhite) +{} +bool Rook::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + return currentX == destX || currentY == destY; +} +void Rook::print() const +{ + if (getIsWhite()) + std::cout << 'R'; + else + std::cout << 'r'; +} diff --git a/Sem. 15/Chess/Figures/Rook/Rook.h b/Sem. 15/Chess/Figures/Rook/Rook.h new file mode 100644 index 0000000..f6190a8 --- /dev/null +++ b/Sem. 15/Chess/Figures/Rook/Rook.h @@ -0,0 +1,11 @@ +#pragma once +#include "../FigureBase/Figure.h" + +class Rook : virtual public Figure +{ + +public: + Rook(bool isWhite); + bool canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const override; + void print() const override; +};
\ No newline at end of file diff --git a/Sem. 15/Chess/main.cpp b/Sem. 15/Chess/main.cpp new file mode 100644 index 0000000..15db61d --- /dev/null +++ b/Sem. 15/Chess/main.cpp @@ -0,0 +1,12 @@ +#include <iostream> +#include "ChessBoard/ChessBoard.h" +using namespace std; + +//ШАХ + +int main() +{ + ChessBoard c; + + c.print(); +}
\ No newline at end of file diff --git a/Sem. 15/README.md b/Sem. 15/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Sem. 15/README.md |
