diff --git a/.vscode/settings.json b/.vscode/settings.json index bae48cd..25df19d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,38 +1,38 @@ -{ - "files.associations": { - "cmath": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "cwchar": "cpp", - "exception": "cpp", - "initializer_list": "cpp", - "ios": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "ostream": "cpp", - "sstream": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "string": "cpp", - "system_error": "cpp", - "type_traits": "cpp", - "typeinfo": "cpp", - "utility": "cpp", - "xfacet": "cpp", - "xiosbase": "cpp", - "xlocale": "cpp", - "xlocinfo": "cpp", - "xlocnum": "cpp", - "xmemory0": "cpp", - "xstddef": "cpp", - "xstring": "cpp", - "xtr1common": "cpp", - "xutility": "cpp" - } +{ + "files.associations": { + "cmath": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "cwchar": "cpp", + "exception": "cpp", + "initializer_list": "cpp", + "ios": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "ostream": "cpp", + "sstream": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "string": "cpp", + "system_error": "cpp", + "type_traits": "cpp", + "typeinfo": "cpp", + "utility": "cpp", + "xfacet": "cpp", + "xiosbase": "cpp", + "xlocale": "cpp", + "xlocinfo": "cpp", + "xlocnum": "cpp", + "xmemory0": "cpp", + "xstddef": "cpp", + "xstring": "cpp", + "xtr1common": "cpp", + "xutility": "cpp" + } } \ No newline at end of file diff --git a/Carrier.hh b/Carrier.hh index d35b4c3..a263bf6 100644 --- a/Carrier.hh +++ b/Carrier.hh @@ -6,7 +6,7 @@ class Carrier : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/CellProperty.cpp b/CellProperty.cpp index b2ecf87..57c10d1 100644 --- a/CellProperty.cpp +++ b/CellProperty.cpp @@ -1,146 +1,146 @@ -#include "CellProperty.hh" - -CellProperty::CellProperty(const bool walkable, const bool swimmable, const bool flyable, const bool event, const bool monster) noexcept - : _walkable(walkable), _swimmable(swimmable), _flyable(flyable), _event(event), _monster(monster) -{ -} - -CellProperty::CellProperty(const CellType cell) noexcept -{ - switch (cell) { - case CellType::GrassCell: - CellProperty(true, false, true, false, false); - break; - case CellType::WaterCell: - CellProperty(false, true, true, false, false); - break; - case CellType::RockCell: - CellProperty(false, false, true, false, false); - break; - case CellType::MountainCell: - default: - CellProperty(false, false, false, false, false); - break; - } -} - -bool CellProperty::operator==(CellProperty property) const noexcept -{ - return this->isEqual(property); -} - -bool CellProperty::operator!=(CellProperty property) const noexcept -{ - return !this->isEqual(property); -} - -bool CellProperty::isEqual(const CellProperty property) const noexcept -{ - return (this->isWalkable() == property.isWalkable()) && - (this->isSwimmable() == property.isSwimmable()) && - (this->isFlyable() == property.isFlyable()) && - (this->isEvent() == property.isEvent()) && - (this->isMonster() == property.isMonster()); -} - -bool CellProperty::isWalkable(void) const noexcept -{ - return _walkable; -} - -void CellProperty::setWalkable(void) noexcept -{ - this->_walkable = true; -} - -void CellProperty::setNotWalkable(void) noexcept -{ - this->_walkable = false; -} - -bool CellProperty::isSwimmable(void) const noexcept -{ - return this->_swimmable; -} - -void CellProperty::setSwimmable(void) noexcept -{ - this->_swimmable = true; -} - -void CellProperty::setNotSwimmable(void) noexcept -{ - this->_swimmable = false; -} - -bool CellProperty::isFlyable(void) const noexcept -{ - return this->_flyable; -} - -void CellProperty::setFlyable(void) noexcept -{ - this->_flyable = true; -} - -void CellProperty::setNotFlyable(void) noexcept -{ - this->_flyable = false; -} - -bool CellProperty::isEvent(void) const noexcept -{ - return this->_event; -} - -void CellProperty::setEvent(void) noexcept -{ - this->_event = true; -} - -void CellProperty::setNotEvent(void) noexcept -{ - this->_event = false; -} - -bool CellProperty::isMonster(void) const noexcept -{ - return _monster; -} - -void CellProperty::setMonster(void) noexcept -{ - this->_monster = true; -} - -void CellProperty::setNotMonster(void) noexcept -{ - this->_monster = false; -} - -CellProperty getCellFlags(const CellType cell) noexcept -{ - switch (cell) { - case CellType::GrassCell: - return CellProperty(true, false, true, false, false); - case CellType::WaterCell: - return CellProperty(false, true, true, false, false); - case CellType::RockCell: - return CellProperty(false, false, true, false, false); - case CellType::MountainCell: - return CellProperty(false, false, false, false, false); - default: - return CellProperty(false, false, false, false, false); - } -} - -bool hasFlag(CellType cell, CellProperty property) noexcept -{ - CellProperty cellProperty(getCellFlags(cell)); - - return (cellProperty.isWalkable() == property.isWalkable()) || - (cellProperty.isSwimmable() == property.isSwimmable()) || - (cellProperty.isFlyable() == property.isFlyable()) || - (cellProperty.isEvent() == property.isEvent()) || - (cellProperty.isMonster() == property.isMonster()); +#include "CellProperty.hh" + +CellProperty::CellProperty(const bool walkable, const bool swimmable, const bool flyable, const bool event, const bool monster) noexcept + : _walkable(walkable), _swimmable(swimmable), _flyable(flyable), _event(event), _monster(monster) +{ +} + +CellProperty::CellProperty(const CellType cell) noexcept +{ + switch (cell) { + case CellType::GrassCell: + CellProperty(true, false, true, false, false); + break; + case CellType::WaterCell: + CellProperty(false, true, true, false, false); + break; + case CellType::RockCell: + CellProperty(false, false, true, false, false); + break; + case CellType::MountainCell: + default: + CellProperty(false, false, false, false, false); + break; + } +} + +bool CellProperty::operator==(CellProperty property) const noexcept +{ + return this->isEqual(property); +} + +bool CellProperty::operator!=(CellProperty property) const noexcept +{ + return !this->isEqual(property); +} + +bool CellProperty::isEqual(const CellProperty property) const noexcept +{ + return (this->isWalkable() == property.isWalkable()) && + (this->isSwimmable() == property.isSwimmable()) && + (this->isFlyable() == property.isFlyable()) && + (this->isEvent() == property.isEvent()) && + (this->isMonster() == property.isMonster()); +} + +bool CellProperty::isWalkable(void) const noexcept +{ + return _walkable; +} + +void CellProperty::setWalkable(void) noexcept +{ + this->_walkable = true; +} + +void CellProperty::setNotWalkable(void) noexcept +{ + this->_walkable = false; +} + +bool CellProperty::isSwimmable(void) const noexcept +{ + return this->_swimmable; +} + +void CellProperty::setSwimmable(void) noexcept +{ + this->_swimmable = true; +} + +void CellProperty::setNotSwimmable(void) noexcept +{ + this->_swimmable = false; +} + +bool CellProperty::isFlyable(void) const noexcept +{ + return this->_flyable; +} + +void CellProperty::setFlyable(void) noexcept +{ + this->_flyable = true; +} + +void CellProperty::setNotFlyable(void) noexcept +{ + this->_flyable = false; +} + +bool CellProperty::isEvent(void) const noexcept +{ + return this->_event; +} + +void CellProperty::setEvent(void) noexcept +{ + this->_event = true; +} + +void CellProperty::setNotEvent(void) noexcept +{ + this->_event = false; +} + +bool CellProperty::isMonster(void) const noexcept +{ + return _monster; +} + +void CellProperty::setMonster(void) noexcept +{ + this->_monster = true; +} + +void CellProperty::setNotMonster(void) noexcept +{ + this->_monster = false; +} + +CellProperty getCellFlags(const CellType cell) noexcept +{ + switch (cell) { + case CellType::GrassCell: + return CellProperty(true, false, true, false, false); + case CellType::WaterCell: + return CellProperty(false, true, true, false, false); + case CellType::RockCell: + return CellProperty(false, false, true, false, false); + case CellType::MountainCell: + return CellProperty(false, false, false, false, false); + default: + return CellProperty(false, false, false, false, false); + } +} + +bool hasFlag(CellType cell, CellProperty property) noexcept +{ + CellProperty cellProperty(getCellFlags(cell)); + + return (cellProperty.isWalkable() == property.isWalkable()) || + (cellProperty.isSwimmable() == property.isSwimmable()) || + (cellProperty.isFlyable() == property.isFlyable()) || + (cellProperty.isEvent() == property.isEvent()) || + (cellProperty.isMonster() == property.isMonster()); } \ No newline at end of file diff --git a/CellType.cpp b/CellType.cpp index 676392d..5cf1421 100644 --- a/CellType.cpp +++ b/CellType.cpp @@ -1,74 +1,74 @@ -#include -#include -#include -#include "CellType.hh" - -void fillMap(CellType **map); -CellType** initMap(void) noexcept; - -CellType** initMap(void) noexcept -{ - unsigned int width; - unsigned int height; - CellType **map; - - std::cin >> width; - std::cin >> height; - - map = new CellType*[width]; - for (unsigned int i = 0; i < width; ++i) { - map[i] = new CellType[height]; - } - - return map; -} - -void fillMap(CellType **map) -{ - char character; - - for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) { - std::cin >> character; - for (unsigned int j = 0; j < (sizeof(map[i]) / sizeof(*map)); ++j) { - switch(character) { - case 'G': - map[i][j] = CellType::GrassCell; - break; - case 'W': - map[i][j] = CellType::WaterCell; - break; - case 'R': - map[i][j] = CellType::RockCell; - break; - case 'M': - map[i][j] = CellType::MountainCell; - break; - default: - throw std::runtime_error("Unvalid map type input"); - } - } - } -} - -void freeMap(CellType **map) noexcept -{ - for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) { - delete[] map[i]; - } - delete[] map; -} - -CellType** parseMap(void) noexcept -{ - CellType **map; - - try { - map = initMap(); - fillMap(map); - } catch (std::invalid_argument) { - std::cout << "invalid map data" << std::endl; - freeMap(map); - } - - return map; -} +#include +#include +#include +#include "CellType.hh" + +void fillMap(CellType **map); +CellType** initMap(void) noexcept; + +CellType** initMap(void) noexcept +{ + unsigned int width; + unsigned int height; + CellType **map; + + std::cin >> width; + std::cin >> height; + + map = new CellType*[width]; + for (unsigned int i = 0; i < width; ++i) { + map[i] = new CellType[height]; + } + + return map; +} + +void fillMap(CellType **map) +{ + char character; + + for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) { + std::cin >> character; + for (unsigned int j = 0; j < (sizeof(map[i]) / sizeof(*map)); ++j) { + switch(character) { + case 'G': + map[i][j] = CellType::GrassCell; + break; + case 'W': + map[i][j] = CellType::WaterCell; + break; + case 'R': + map[i][j] = CellType::RockCell; + break; + case 'M': + map[i][j] = CellType::MountainCell; + break; + default: + throw std::runtime_error("Unvalid map type input"); + } + } + } +} + +void freeMap(CellType **map) noexcept +{ + for (unsigned int i = 0; i < (sizeof(map) / sizeof(*map)); ++i) { + delete[] map[i]; + } + delete[] map; +} + +CellType** parseMap(void) noexcept +{ + CellType **map; + + try { + map = initMap(); + fillMap(map); + } catch (std::invalid_argument) { + std::cout << "invalid map data" << std::endl; + freeMap(map); + } + + return map; +} diff --git a/Corruptor.hh b/Corruptor.hh index 5b1069c..4c95c57 100644 --- a/Corruptor.hh +++ b/Corruptor.hh @@ -6,7 +6,7 @@ class Corruptor : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/Map.cpp b/Map.cpp index b13a7d3..6a92dc7 100644 --- a/Map.cpp +++ b/Map.cpp @@ -1,141 +1,141 @@ -#include -#include -#include "Map.hh" -#define DEBUG_BUILD 1 - -Map::Map(void) -{ - std::string buffer; - - try { - std::cin >> buffer; - this->_width = static_cast(std::stoi(buffer)); -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] Input width: " << this->_width << std::endl; -#endif - std::cin >> buffer; - this->_height = static_cast(std::stoi(buffer)); -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] Input height: " << this->_height << std::endl; -#endif - } catch (std::invalid_argument) { - std::cout << "invalid map data" << std::endl; - } - this->_cells = this->parseMap(&this->_width, &this->_height); -} - -Map::~Map(void) -{ - if (!this->_cells) - return; - freeMap(this->_cells); -} - -Map::Map(Map&& other) noexcept -{ - this->_cells = other._cells; - other._cells = nullptr; -} - -Map& Map::operator=(Map&& other) noexcept -{ - if (this == &other) { - return *this; - } - this->_cells = other._cells; - other._cells = nullptr; - return *this; -} - -CellType Map::getCell(const int x, const int y) const noexcept -{ - return this->_cells[x][y]; -} - -CellProperty Map::getCellProperties(const int x, const int y) const noexcept -{ - return getCellFlags(this->getCell(x, y)); -} - -CellType** Map::initCells(const int *width, const int *height) noexcept -{ - this->_cells = new CellType*[*height]; - for (unsigned int i = 0; i < static_cast(*width); ++i) { - this->_cells[i] = new CellType[*width]; - } -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] Map size of x:" << *width << " y:" << *height << std::endl; -#endif - return this->_cells; -} - -void Map::fillCells(void) -{ - char character; - -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] Current map size: x:" << static_cast(this->_width) << " y:" << static_cast(this->_height) << std::endl; -#endif - for (unsigned int i = 0; i < static_cast(this->_height); ++i) { - std::cin >> character; - for (unsigned int j = 0; j < static_cast(this->_width); ++j) { -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] Map element at x:" << j << " y:" << i << ": " << character << std::endl; -#endif - switch(character) { - case 'R': - this->_cells[j][i] = CellType::RockCell; - break; - case 'G': - this->_cells[j][i] = CellType::GrassCell; - break; - case 'W': - this->_cells[j][i] = CellType::WaterCell; - break; - case 'M': - this->_cells[j][i] = CellType::MountainCell; - break; - default: - throw std::runtime_error("Unvalid map type input"); - } - } - } -} - -CellType** Map::parseMap(const int *width, const int *height) noexcept -{ - this->_cells = this->initCells(width, height); - try { - this->fillCells(); -#ifdef DEBUG_BUILD - std::cout << "[DEBUG] final map: " << std::endl; - for (unsigned int i = 0; i < static_cast(this->_height); ++i) { - for (unsigned int j = 0; j < static_cast(this->_width); ++j) { - std::cout << this->_cells[j][i]; - } - std::cout << std::endl; - } -#endif - } catch (std::invalid_argument) { - std::cout << "invalid map data" << std::endl; - freeMap(this->_cells); - } - return this->_cells; - -} - -bool Map::canGo(int x, int y, Unit const& unit) const noexcept -{ - CellProperty cellProperty = this->getCellProperties(x, y); - - switch (unit.getField()) { - case UnitField::Ground: - return cellProperty.isFlyable() || cellProperty.isWalkable(); - case UnitField::Sky: - return cellProperty.isFlyable(); - case UnitField::Water: - return cellProperty.isSwimmable(); - default: - return false; - } -} +#include +#include +#include "Map.hh" +#define DEBUG_BUILD 1 + +Map::Map(void) +{ + std::string buffer; + + try { + std::cin >> buffer; + this->_width = static_cast(std::stoi(buffer)); +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] Input width: " << this->_width << std::endl; +#endif + std::cin >> buffer; + this->_height = static_cast(std::stoi(buffer)); +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] Input height: " << this->_height << std::endl; +#endif + } catch (std::invalid_argument) { + std::cout << "invalid map data" << std::endl; + } + this->_cells = this->parseMap(&this->_width, &this->_height); +} + +Map::~Map(void) +{ + if (!this->_cells) + return; + freeMap(this->_cells); +} + +Map::Map(Map&& other) noexcept +{ + this->_cells = other._cells; + other._cells = nullptr; +} + +Map& Map::operator=(Map&& other) noexcept +{ + if (this == &other) { + return *this; + } + this->_cells = other._cells; + other._cells = nullptr; + return *this; +} + +CellType Map::getCell(const int x, const int y) const noexcept +{ + return this->_cells[x][y]; +} + +CellProperty Map::getCellProperties(const int x, const int y) const noexcept +{ + return getCellFlags(this->getCell(x, y)); +} + +CellType** Map::initCells(const int *width, const int *height) noexcept +{ + this->_cells = new CellType*[*height]; + for (unsigned int i = 0; i < static_cast(*width); ++i) { + this->_cells[i] = new CellType[*width]; + } +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] Map size of x:" << *width << " y:" << *height << std::endl; +#endif + return this->_cells; +} + +void Map::fillCells(void) +{ + char character; + +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] Current map size: x:" << static_cast(this->_width) << " y:" << static_cast(this->_height) << std::endl; +#endif + for (unsigned int i = 0; i < static_cast(this->_height); ++i) { + std::cin >> character; + for (unsigned int j = 0; j < static_cast(this->_width); ++j) { +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] Map element at x:" << j << " y:" << i << ": " << character << std::endl; +#endif + switch(character) { + case 'R': + this->_cells[j][i] = CellType::RockCell; + break; + case 'G': + this->_cells[j][i] = CellType::GrassCell; + break; + case 'W': + this->_cells[j][i] = CellType::WaterCell; + break; + case 'M': + this->_cells[j][i] = CellType::MountainCell; + break; + default: + throw std::runtime_error("Unvalid map type input"); + } + } + } +} + +CellType** Map::parseMap(const int *width, const int *height) noexcept +{ + this->_cells = this->initCells(width, height); + try { + this->fillCells(); +#ifdef DEBUG_BUILD + std::cout << "[DEBUG] final map: " << std::endl; + for (unsigned int i = 0; i < static_cast(this->_height); ++i) { + for (unsigned int j = 0; j < static_cast(this->_width); ++j) { + std::cout << this->_cells[j][i]; + } + std::cout << std::endl; + } +#endif + } catch (std::invalid_argument) { + std::cout << "invalid map data" << std::endl; + freeMap(this->_cells); + } + return this->_cells; + +} + +bool Map::canGo(int x, int y, Unit const& unit) const noexcept +{ + CellProperty cellProperty = this->getCellProperties(x, y); + + switch (unit.getField()) { + case UnitField::Ground: + return cellProperty.isFlyable() || cellProperty.isWalkable(); + case UnitField::Sky: + return cellProperty.isFlyable(); + case UnitField::Water: + return cellProperty.isSwimmable(); + default: + return false; + } +} diff --git a/Marine.hh b/Marine.hh index 28e6a3d..0b1c174 100644 --- a/Marine.hh +++ b/Marine.hh @@ -6,7 +6,7 @@ class Marine : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/Roach.hh b/Roach.hh index 399e75b..8b5bc2a 100644 --- a/Roach.hh +++ b/Roach.hh @@ -6,7 +6,7 @@ class Roach : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/Stalker.hh b/Stalker.hh index 462b288..fed3dda 100644 --- a/Stalker.hh +++ b/Stalker.hh @@ -6,7 +6,7 @@ class Stalker : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/Tank.hh b/Tank.hh index 77eda88..b5d4101 100644 --- a/Tank.hh +++ b/Tank.hh @@ -6,7 +6,7 @@ class Tank : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif diff --git a/Unit.hh b/Unit.hh index 2a1356f..ea66add 100644 --- a/Unit.hh +++ b/Unit.hh @@ -10,6 +10,7 @@ private: int _y; public: Unit(const int x, const int y) noexcept; + virtual ~Unit() noexcept; int getPositionX(void) const noexcept; int getPositionY(void) const noexcept; diff --git a/UnitField.hh b/UnitField.hh index 947c656..a7fca8d 100644 --- a/UnitField.hh +++ b/UnitField.hh @@ -1,11 +1,11 @@ -#ifndef __UNITFIELD_HH__ -#define __UNITFIELD_HH__ - -enum UnitField -{ - Sky, - Ground, - Water -}; - +#ifndef __UNITFIELD_HH__ +#define __UNITFIELD_HH__ + +enum UnitField +{ + Sky, + Ground, + Water +}; + #endif \ No newline at end of file diff --git a/Ursadon.hh b/Ursadon.hh index d20d01d..d533867 100644 --- a/Ursadon.hh +++ b/Ursadon.hh @@ -6,7 +6,7 @@ class Ursadon : public Unit { public: - UnitField getField(void) const noexcept; + UnitField getField(void) override const noexcept; }; #endif