diff options
Sem 14
33 files changed, 646 insertions, 0 deletions
diff --git a/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp new file mode 100644 index 0000000..ed9823d --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.cpp @@ -0,0 +1,69 @@ +#include "ChessBoard.h" +#include "../FigureFactory/FigureFactory.h" + +ChessBoard::ChessBoard() +{ + + for (int i = 0; i < BOARD_SIZE; i++) + { + board[1][i].f = FigureFactory::createFigure(false, PawnFigure); + board[6][i].f = FigureFactory::createFigure(true, PawnFigure); + } + + board[0][0].f = FigureFactory::createFigure(false, RookFigure); + board[0][7].f = FigureFactory::createFigure(false, RookFigure); + + board[7][0].f = FigureFactory::createFigure(true, RookFigure); + board[7][7].f = FigureFactory::createFigure(true, RookFigure); + + + board[0][1].f = FigureFactory::createFigure(false, KnightFigure); + board[0][6].f = FigureFactory::createFigure(false, KnightFigure); + board[7][1].f = FigureFactory::createFigure(true, KnightFigure);; + board[7][6].f = FigureFactory::createFigure(true, KnightFigure); + + board[0][2].f = FigureFactory::createFigure(false, BishopFigure); + board[0][5].f = FigureFactory::createFigure(false, BishopFigure);; + board[7][2].f = FigureFactory::createFigure(true, BishopFigure); + board[7][5].f = FigureFactory::createFigure(true, BishopFigure); + + board[0][3].f = FigureFactory::createFigure(false, KingFigure); + board[0][4].f = FigureFactory::createFigure(false, QueenFigure); + board[7][4].f = FigureFactory::createFigure(true, QueenFigure); + board[7][3].f = FigureFactory::createFigure(true, KingFigure); +} + +void ChessBoard::print() const +{ + std::cout << "--------" << std::endl; + for (int i = 0; i < BOARD_SIZE; i++) + { + for (int j = 0; j < BOARD_SIZE; j++) + { + board[i][j].print(); + } + std::cout << std::endl; + } + std::cout << "--------" << std::endl << std::endl << std::endl; +} + +void ChessBoard::moveFigure(size_t x, size_t y, size_t destX, size_t destY) +{ + + Figure* current = board[x][y].f; + + if (current->getType() == PawnFigure) + { + //Some other logic..... El'passant and other + } + else + { + if (board[x][y].f && board[x][y].f->canBeMoved(x, y, destX, destY)) + { + delete board[destX][destY].f; + board[destX][destY].f = board[x][y].f; + board[x][y].f = nullptr; + } + } + +}
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.h b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.h new file mode 100644 index 0000000..315e492 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/ChessBoard/ChessBoard.h @@ -0,0 +1,43 @@ +#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; + + void moveFigure(size_t x, size_t y, size_t destX, size_t destY); + +}; diff --git a/Sem. 14/Chess_basic_prototype/ConfigEnums/ConfigEnums.h b/Sem. 14/Chess_basic_prototype/ConfigEnums/ConfigEnums.h new file mode 100644 index 0000000..c3448e8 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/ConfigEnums/ConfigEnums.h @@ -0,0 +1,11 @@ +#pragma once + +enum FigureType +{ + KingFigure, + QueenFigure, + KnightFigure, + BishopFigure, + PawnFigure, + RookFigure +};
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.cpp b/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.cpp new file mode 100644 index 0000000..e9db605 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.cpp @@ -0,0 +1,27 @@ +#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::createFigure(bool isWhite, FigureType type) +{ + + switch (type) + { + case KingFigure: + return new King(isWhite); + case QueenFigure: + return new Queen(isWhite); + case KnightFigure: + return new Knight(isWhite); + case BishopFigure: + return new Bishop(isWhite); + case PawnFigure: + return new Pawn(isWhite); + case RookFigure: + return new Rook(isWhite); + } +}
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.h b/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.h new file mode 100644 index 0000000..012a60c --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/FigureFactory/FigureFactory.h @@ -0,0 +1,9 @@ +#pragma once +#include "../Figures/FigureBase/Figure.h" +#include "../ConfigEnums/ConfigEnums.h" + +class FigureFactory +{ +public: + static Figure* createFigure(bool isWhite, FigureType); //enum +};
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/Figures/Bishop/Bishop.cpp b/Sem. 14/Chess_basic_prototype/Figures/Bishop/Bishop.cpp new file mode 100644 index 0000000..786c9eb --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/Bishop/Bishop.cpp @@ -0,0 +1,16 @@ +#include "Bishop.h" +#include <cmath> + +Bishop::Bishop(bool isWhite) : Figure(isWhite, RookFigure) +{} +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. 14/Chess_basic_prototype/Figures/Bishop/Bishop.h b/Sem. 14/Chess_basic_prototype/Figures/Bishop/Bishop.h new file mode 100644 index 0000000..86949a7 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp new file mode 100644 index 0000000..4d94406 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.cpp @@ -0,0 +1 @@ +todo
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h new file mode 100644 index 0000000..9e2300a --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/FigureBase/Figure.h @@ -0,0 +1,21 @@ +#pragma once +#include <iostream> +#include "../../ConfigEnums/ConfigEnums.h" + +class Figure +{ + bool isWhite; + FigureType type; + +public: + Figure(bool isWhite, FigureType type) :isWhite(isWhite), type(type) + {} + + 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; }; + FigureType getType() const { return type; } + + virtual ~Figure(){}; +};
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/Figures/King/King.cpp b/Sem. 14/Chess_basic_prototype/Figures/King/King.cpp new file mode 100644 index 0000000..8ca67e8 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/King/King.cpp @@ -0,0 +1,16 @@ +#include "King.h" +#include <cmath> + +King::King(bool isWhite) : Figure(isWhite, KingFigure) +{} +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. 14/Chess_basic_prototype/Figures/King/King.h b/Sem. 14/Chess_basic_prototype/Figures/King/King.h new file mode 100644 index 0000000..85a8e02 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/Figures/Knight/Knight.cpp b/Sem. 14/Chess_basic_prototype/Figures/Knight/Knight.cpp new file mode 100644 index 0000000..5713d7c --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/Knight/Knight.cpp @@ -0,0 +1,26 @@ +#include "Knight.h" +#include <cmath> + +Knight::Knight(bool isWhite) : Figure(isWhite, KnightFigure) +{} +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. 14/Chess_basic_prototype/Figures/Knight/Knight.h b/Sem. 14/Chess_basic_prototype/Figures/Knight/Knight.h new file mode 100644 index 0000000..72acfea --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/Figures/Pawn/Pawn.cpp b/Sem. 14/Chess_basic_prototype/Figures/Pawn/Pawn.cpp new file mode 100644 index 0000000..347032b --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/Pawn/Pawn.cpp @@ -0,0 +1,20 @@ +#include "Pawn.h" +#include <cmath> +Pawn::Pawn(bool isWhite) : Figure(isWhite, PawnFigure), isFirstMove(true) +{} + +bool Pawn::canBeMoved(size_t currentX, size_t currentY, size_t destX, size_t destY) const +{ + if (getIsWhite()) + return currentY - 1 == destY && abs((int)currentX - (int)destX) <= 1; + else + return currentY + 1 == destY && abs((int)currentX - (int)destX) <= 1; +} + +void Pawn::print() const +{ + if (getIsWhite()) + std::cout << 'P'; + else + std::cout << 'p'; +}
\ No newline at end of file diff --git a/Sem. 14/Chess_basic_prototype/Figures/Pawn/Pawn.h b/Sem. 14/Chess_basic_prototype/Figures/Pawn/Pawn.h new file mode 100644 index 0000000..675bf0b --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/Figures/Queen/Queen.cpp b/Sem. 14/Chess_basic_prototype/Figures/Queen/Queen.cpp new file mode 100644 index 0000000..0e33198 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/Queen/Queen.cpp @@ -0,0 +1,18 @@ +#include "Queen.h" + +Queen::Queen(bool isWhite) : Bishop(isWhite), Rook(isWhite), Figure(isWhite, QueenFigure) +{} + +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. 14/Chess_basic_prototype/Figures/Queen/Queen.h b/Sem. 14/Chess_basic_prototype/Figures/Queen/Queen.h new file mode 100644 index 0000000..c4fb1da --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/Figures/Rook/Rook.cpp b/Sem. 14/Chess_basic_prototype/Figures/Rook/Rook.cpp new file mode 100644 index 0000000..2cb96a3 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/Figures/Rook/Rook.cpp @@ -0,0 +1,15 @@ +#include "Rook.h" + +Rook::Rook(bool isWhite) : Figure(isWhite, RookFigure) +{} +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. 14/Chess_basic_prototype/Figures/Rook/Rook.h b/Sem. 14/Chess_basic_prototype/Figures/Rook/Rook.h new file mode 100644 index 0000000..f6190a8 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/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. 14/Chess_basic_prototype/main.cpp b/Sem. 14/Chess_basic_prototype/main.cpp new file mode 100644 index 0000000..6673ec8 --- /dev/null +++ b/Sem. 14/Chess_basic_prototype/main.cpp @@ -0,0 +1,16 @@ +#include <iostream> +#include "ChessBoard/ChessBoard.h" +using namespace std; + +//ШАХ + +int main() +{ + ChessBoard c; + + c.print(); + + c.moveFigure(0, 1, 2, 0); + + c.print(); +}
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Animal.h b/Sem. 14/Farm/Animals/Animal.h new file mode 100644 index 0000000..8038843 --- /dev/null +++ b/Sem. 14/Farm/Animals/Animal.h @@ -0,0 +1,27 @@ +#pragma once +#include <iostream> +class Animal +{ + +public: + + enum AnimalType + { + Dog = 0, + Cat = 1, + Mouse = 2 + }; + virtual void sayHello() const = 0; + + virtual Animal* clone() const = 0;//създава копие на текущия обект. + + virtual ~Animal() {} //!!! + + AnimalType getType() const + { + return myType; + } + +protected: + AnimalType myType; +};
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Cat.cpp b/Sem. 14/Farm/Animals/Cat.cpp new file mode 100644 index 0000000..8716d4b --- /dev/null +++ b/Sem. 14/Farm/Animals/Cat.cpp @@ -0,0 +1,12 @@ +#include "Cat.h" + +void Cat::sayHello() const +{ + std::cout << "Hello, I am a Cat!" << std::endl; +} + +Animal* Cat::clone() const +{ + Animal* newObj = new Cat(*this); //copy constr of Mouse + return newObj; +}
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Cat.h b/Sem. 14/Farm/Animals/Cat.h new file mode 100644 index 0000000..57aa627 --- /dev/null +++ b/Sem. 14/Farm/Animals/Cat.h @@ -0,0 +1,12 @@ +#pragma once +#include "Animal.h" +class Cat : public Animal +{ +public: + Cat() + { + myType = Animal::AnimalType::Cat; + } + void sayHello() const override; + Animal* clone() const override; +};
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Dog.cpp b/Sem. 14/Farm/Animals/Dog.cpp new file mode 100644 index 0000000..879957f --- /dev/null +++ b/Sem. 14/Farm/Animals/Dog.cpp @@ -0,0 +1,12 @@ +#include "Dog.h" + +void Dog::sayHello() const +{ + std::cout << "Hello, I am a Dog!" << std::endl; +} + +Animal* Dog::clone() const +{ + Animal* newObj = new Dog(*this); //copy constr of Mouse + return newObj; +}
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Dog.h b/Sem. 14/Farm/Animals/Dog.h new file mode 100644 index 0000000..3bf27b9 --- /dev/null +++ b/Sem. 14/Farm/Animals/Dog.h @@ -0,0 +1,12 @@ +#pragma once +#include "Animal.h" +class Dog : public Animal +{ +public: + Dog() + { + myType = Animal::AnimalType::Dog; + } + void sayHello() const override; + Animal* clone() const override; +};
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Mouse.cpp b/Sem. 14/Farm/Animals/Mouse.cpp new file mode 100644 index 0000000..e3789ce --- /dev/null +++ b/Sem. 14/Farm/Animals/Mouse.cpp @@ -0,0 +1,12 @@ +#include "Mouse.h" + +void Mouse::sayHello() const +{ + std::cout << "Hello, I am a Mouse!" << std::endl; +} + +Animal* Mouse::clone() const +{ + Animal* newObj = new Mouse(*this); //copy constr of Mouse + return newObj; +}
\ No newline at end of file diff --git a/Sem. 14/Farm/Animals/Mouse.h b/Sem. 14/Farm/Animals/Mouse.h new file mode 100644 index 0000000..b979fa3 --- /dev/null +++ b/Sem. 14/Farm/Animals/Mouse.h @@ -0,0 +1,12 @@ +#pragma once +#include "Animal.h" +class Mouse : public Animal +{ +public: + Mouse() + { + myType = Animal::AnimalType::Mouse; + } + void sayHello() const override; + Animal* clone() const override; +};
\ No newline at end of file diff --git a/Sem. 14/Farm/Collection/Farm.cpp b/Sem. 14/Farm/Collection/Farm.cpp new file mode 100644 index 0000000..b604a2e --- /dev/null +++ b/Sem. 14/Farm/Collection/Farm.cpp @@ -0,0 +1,97 @@ +#include "Farm.h" +#include "../Animals/Dog.h" + +void Farm::free() +{ + for (size_t i = 0; i < animalsCount; i++) + delete animals[i]; //не се инт. какъв обект е. (вирт дестр) + delete[] animals; +} +void Farm::copyFrom(const Farm& other) +{ + animals = new Animal * [other.capacity]; + animalsCount = other.animalsCount; + capacity = other.capacity; + + for (size_t i = 0; i < animalsCount; i++) + { + animals[i] = other.animals[i]->clone(); + } +} +void Farm::resize() +{ + Animal** newCollection = new Animal * [capacity *= 2]; + for (size_t i = 0; i < animalsCount; i++) + newCollection[i] = animals[i]; // !!не правим клониране + delete[] animals; + animals = newCollection; +} +void Farm::addAnimal(const Animal& currentAnimal) +{ + if (animalsCount == capacity) + resize(); + animals[animalsCount++] = currentAnimal.clone(); +} + +Farm::Farm() +{ + capacity = 8; + animalsCount = 0; + animals = new Animal * [capacity]; +} +Farm::Farm(const Farm& other) +{ + copyFrom(other); +} +Farm& Farm::operator=(const Farm& other) +{ + if (this != &other) + { + free(); + copyFrom(other); + } + return *this; +} +Farm::~Farm() +{ + free(); +} + +void Farm::allSayHello() const +{ + for (size_t i = 0; i < animalsCount; i++) + animals[i]->sayHello(); +} + +Animal::AnimalType Farm::getTypeByIndex(size_t index) const +{ + if (index >= animalsCount) + throw std::out_of_range("No such index!"); + + return animals[index]->getType(); + +} + +bool Farm::addAnimal(Animal::AnimalType animalType) +{ + + Animal* ptr = factory.createAnimal(animalType); + + if (ptr != nullptr) + { + if (animalsCount == capacity) + resize(); + animals[animalsCount++] = ptr; + } + +} + +size_t Farm::getCount() const +{ + return animalsCount; +} + +bool Farm::isEmpty() const +{ + return animalsCount == 0; +}
\ No newline at end of file diff --git a/Sem. 14/Farm/Collection/Farm.h b/Sem. 14/Farm/Collection/Farm.h new file mode 100644 index 0000000..7742973 --- /dev/null +++ b/Sem. 14/Farm/Collection/Farm.h @@ -0,0 +1,32 @@ +#pragma once +#include "../Animals/Animal.h" +#include "../Factory/Factory.h" + +class Farm +{ + Animal** animals; + size_t animalsCount; + size_t capacity; + + void free(); + void copyFrom(const Farm& other); + void resize(); + + AnimalFactory factory; //better singelton + +public: + Farm(); + Farm(const Farm& other); + Farm& operator=(const Farm& other); + ~Farm(); + + void addAnimal(const Animal& animal); + bool addAnimal(Animal::AnimalType animalType); + + void allSayHello() const; + + size_t getCount() const; + bool isEmpty() const; + + Animal::AnimalType getTypeByIndex(size_t index) const; +}; diff --git a/Sem. 14/Farm/Factory/Factory.cpp b/Sem. 14/Farm/Factory/Factory.cpp new file mode 100644 index 0000000..d7d6ec5 --- /dev/null +++ b/Sem. 14/Farm/Factory/Factory.cpp @@ -0,0 +1,13 @@ +#include "Factory.h" + + +Animal* AnimalFactory::createAnimal(Animal::AnimalType type) +{ + switch (type) + { + case Animal::AnimalType::Cat: return new Cat(); + case Animal::AnimalType::Dog: return new Dog(); + case Animal::AnimalType::Mouse: return new Mouse(); + } + return nullptr; +} diff --git a/Sem. 14/Farm/Factory/Factory.h b/Sem. 14/Farm/Factory/Factory.h new file mode 100644 index 0000000..5532f4f --- /dev/null +++ b/Sem. 14/Farm/Factory/Factory.h @@ -0,0 +1,10 @@ +#include "../Animals/Animal.h" +#include "../Animals/Dog.h" +#include "../Animals/Cat.h" +#include "../Animals/Mouse.h" + +class AnimalFactory +{ +public: + virtual Animal* createAnimal(Animal::AnimalType type); +};
\ No newline at end of file diff --git a/Sem. 14/Farm/source.cpp b/Sem. 14/Farm/source.cpp new file mode 100644 index 0000000..e72e7ca --- /dev/null +++ b/Sem. 14/Farm/source.cpp @@ -0,0 +1,19 @@ +#include <iostream> +#include "../Farm/Collection/Farm.h" + +int main() +{ + Farm myFarm; + + while (1) + { + size_t animalId; + std::cin >> animalId; + bool result = myFarm.addAnimal((Animal::AnimalType)animalId); + + if (result) + std::cout << "Added animal of type: " << myFarm.getTypeByIndex(myFarm.getCount() - 1) << std::endl; + else + std::cout << "Invalid animal type" << std::endl; + } +}
\ No newline at end of file diff --git a/Sem. 14/README.md b/Sem. 14/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/Sem. 14/README.md |
