From e1e7a92a157b5a639ecddf6728954461b64285a5 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Thu, 18 Jun 2020 13:27:21 +0200 Subject: [PATCH 1/6] Class Map with std::vector and Island* currentPosition_ --- shm/Map.cpp | 1 + shm/Map.hpp | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 shm/Map.cpp create mode 100644 shm/Map.hpp diff --git a/shm/Map.cpp b/shm/Map.cpp new file mode 100644 index 000000000..a509a64a3 --- /dev/null +++ b/shm/Map.cpp @@ -0,0 +1 @@ +#include "Map.hpp" diff --git a/shm/Map.hpp b/shm/Map.hpp new file mode 100644 index 000000000..7d8cbedc1 --- /dev/null +++ b/shm/Map.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +#include "Island.hpp" + +class Map { +private: + std::vector vecOfIslands_; + Island* currentPosition_; +}; From 2124aadfe15cc2d3688f4fa7333505fadb315fc8 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Thu, 18 Jun 2020 15:57:18 +0200 Subject: [PATCH 2/6] Class Map with 10 islands in std::vector --- shm/Map.cpp | 22 ++++++++++++++++++++++ shm/Map.hpp | 7 +++++++ 2 files changed, 29 insertions(+) diff --git a/shm/Map.cpp b/shm/Map.cpp index a509a64a3..d351f24b1 100644 --- a/shm/Map.cpp +++ b/shm/Map.cpp @@ -1 +1,23 @@ #include "Map.hpp" + +#include +#include +#include + +Map::Map() { + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> distrib(randomNumberStart, maxPositionXY); + vecOfIslands_.reserve(numberOfIslands); + int notRepeatedPositions = 0; + while (notRepeatedPositions != 10) { + int positionX = distrib(gen); + int positionY = distrib(gen); + if (std::none_of(vecOfIslands_.begin(), vecOfIslands_.end(), [positionX, positionY](auto& i) { + return i.getPosition() == Coordinates(positionX, positionY); + })) { + vecOfIslands_.emplace_back(Island(positionX, positionY)); + notRepeatedPositions++; + } + } +} diff --git a/shm/Map.hpp b/shm/Map.hpp index 7d8cbedc1..fa0dccd15 100644 --- a/shm/Map.hpp +++ b/shm/Map.hpp @@ -4,7 +4,14 @@ #include "Island.hpp" +constexpr int randomNumberStart = 1; +constexpr int maxPositionXY = 10; +constexpr int numberOfIslands = 10; + class Map { +public: + Map(); + private: std::vector vecOfIslands_; Island* currentPosition_; From e41afe33da46a942771ec2509e86da958d7e9b24 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Thu, 18 Jun 2020 16:01:48 +0200 Subject: [PATCH 3/6] Class Map with Island* getIsland(const Coordinates& coordinate) --- shm/Map.cpp | 5 +++++ shm/Map.hpp | 1 + 2 files changed, 6 insertions(+) diff --git a/shm/Map.cpp b/shm/Map.cpp index d351f24b1..0a61de87b 100644 --- a/shm/Map.cpp +++ b/shm/Map.cpp @@ -21,3 +21,8 @@ Map::Map() { } } } + +Island* Map::getIsland(const Coordinates& coordinate) { + auto itr = std::find_if(vecOfIslands_.begin(), vecOfIslands_.end(), [&coordinate](auto& i) { return i.getPosition() == coordinate; }); + return std::addressof(*itr); +} diff --git a/shm/Map.hpp b/shm/Map.hpp index fa0dccd15..261065fdf 100644 --- a/shm/Map.hpp +++ b/shm/Map.hpp @@ -11,6 +11,7 @@ constexpr int numberOfIslands = 10; class Map { public: Map(); + Island* getIsland(const Coordinates& coordinate); private: std::vector vecOfIslands_; From 8d602807fe87fd549568c3e5417bec424f0f0089 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Fri, 19 Jun 2020 11:02:37 +0200 Subject: [PATCH 4/6] Task 4; 5; 6 small fixes --- shm/Makefile | 2 +- shm/Map.cpp | 8 ++++---- shm/Map.hpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shm/Makefile b/shm/Makefile index f95ef2e4c..1fd2e9292 100644 --- a/shm/Makefile +++ b/shm/Makefile @@ -6,7 +6,7 @@ CC = g++ CFLAGS = -std=c++17 -Wall -Wextra -Wpedantic -Wconversion -Werror -O2 -OBJECTS = Ship.o Cargo.o SHM.o +OBJECTS = Ship.o Cargo.o Map.o SHM.o .cpp.o: $(CC) -c $(CFLAGS) $< -o $@ diff --git a/shm/Map.cpp b/shm/Map.cpp index 0a61de87b..537bd6651 100644 --- a/shm/Map.cpp +++ b/shm/Map.cpp @@ -8,21 +8,21 @@ Map::Map() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> distrib(randomNumberStart, maxPositionXY); - vecOfIslands_.reserve(numberOfIslands); + _vecOfIslands.reserve(numberOfIslands); int notRepeatedPositions = 0; while (notRepeatedPositions != 10) { int positionX = distrib(gen); int positionY = distrib(gen); - if (std::none_of(vecOfIslands_.begin(), vecOfIslands_.end(), [positionX, positionY](auto& i) { + if (std::none_of(_vecOfIslands.begin(), _vecOfIslands.end(), [positionX, positionY](auto& i) { return i.getPosition() == Coordinates(positionX, positionY); })) { - vecOfIslands_.emplace_back(Island(positionX, positionY)); + _vecOfIslands.emplace_back(Island(positionX, positionY)); notRepeatedPositions++; } } } Island* Map::getIsland(const Coordinates& coordinate) { - auto itr = std::find_if(vecOfIslands_.begin(), vecOfIslands_.end(), [&coordinate](auto& i) { return i.getPosition() == coordinate; }); + auto itr = std::find_if(_vecOfIslands.begin(), _vecOfIslands.end(), [&coordinate](auto& i) { return i.getPosition() == coordinate; }); return std::addressof(*itr); } diff --git a/shm/Map.hpp b/shm/Map.hpp index 261065fdf..569e4c614 100644 --- a/shm/Map.hpp +++ b/shm/Map.hpp @@ -14,6 +14,6 @@ class Map { Island* getIsland(const Coordinates& coordinate); private: - std::vector vecOfIslands_; - Island* currentPosition_; + std::vector _vecOfIslands; + Island* _currentPosition; }; From 0ca90fece88191bb37ff3d02e252bc79893e2fc8 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Sat, 20 Jun 2020 21:37:30 +0200 Subject: [PATCH 5/6] Task 4; 5; 6 another small fixes requested by team --- shm/Map.cpp | 4 ++-- shm/Map.hpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/shm/Map.cpp b/shm/Map.cpp index 537bd6651..87cf141b5 100644 --- a/shm/Map.cpp +++ b/shm/Map.cpp @@ -7,10 +7,10 @@ Map::Map() { std::random_device rd; std::mt19937 gen(rd()); - std::uniform_int_distribution<> distrib(randomNumberStart, maxPositionXY); + std::uniform_int_distribution<> distrib(minPositionXY, maxPositionXY); _vecOfIslands.reserve(numberOfIslands); int notRepeatedPositions = 0; - while (notRepeatedPositions != 10) { + while (notRepeatedPositions != numberOfIslands) { int positionX = distrib(gen); int positionY = distrib(gen); if (std::none_of(_vecOfIslands.begin(), _vecOfIslands.end(), [positionX, positionY](auto& i) { diff --git a/shm/Map.hpp b/shm/Map.hpp index 569e4c614..9ec36cbe9 100644 --- a/shm/Map.hpp +++ b/shm/Map.hpp @@ -4,7 +4,7 @@ #include "Island.hpp" -constexpr int randomNumberStart = 1; +constexpr int minPositionXY = 1; constexpr int maxPositionXY = 10; constexpr int numberOfIslands = 10; From fceae8cadb302125f9813667bd8ab935ae19b652 Mon Sep 17 00:00:00 2001 From: KrzysztofJ85 Date: Sat, 20 Jun 2020 22:04:59 +0200 Subject: [PATCH 6/6] compilation fix --- shm/Coordinates.cpp | 6 +++++- shm/Coordinates.hpp | 4 +--- shm/Island.cpp | 3 +++ shm/Island.hpp | 2 +- shm/Map.cpp | 2 +- 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/shm/Coordinates.cpp b/shm/Coordinates.cpp index bb5ce3551..966d5a415 100644 --- a/shm/Coordinates.cpp +++ b/shm/Coordinates.cpp @@ -1,5 +1,9 @@ #include "Coordinates.hpp" +Coordinates::Coordinates(const int positionX, + const int positionY) + : _positionX(positionX), _positionY(positionY) {} + bool Coordinates::operator==(const Coordinates& coordinatesToCheck) const { return _positionX == coordinatesToCheck._positionX && _positionY == coordinatesToCheck._positionY; @@ -7,4 +11,4 @@ bool Coordinates::operator==(const Coordinates& coordinatesToCheck) const { bool Coordinates::operator!=(const Coordinates& coordinatesToCheck) const { return !(*this == coordinatesToCheck); -} \ No newline at end of file +} diff --git a/shm/Coordinates.hpp b/shm/Coordinates.hpp index fccbc6a2e..63f1bf842 100644 --- a/shm/Coordinates.hpp +++ b/shm/Coordinates.hpp @@ -2,9 +2,7 @@ class Coordinates { public: - Coordinates(const int positionX, - const int positionY) - : _positionX(positionX), _positionY(positionY) {} + Coordinates(const int positionX, const int positionY); bool operator==(const Coordinates& coordinatesToCheck) const; bool operator!=(const Coordinates& coordinatesToCheck) const; diff --git a/shm/Island.cpp b/shm/Island.cpp index 08d1202ff..70dbbb731 100644 --- a/shm/Island.cpp +++ b/shm/Island.cpp @@ -1 +1,4 @@ #include "Island.hpp" + +Island::Island(int posX, int posY) + : _position(posX, posY) {} diff --git a/shm/Island.hpp b/shm/Island.hpp index 5a0190d7d..3bb9cfe1a 100644 --- a/shm/Island.hpp +++ b/shm/Island.hpp @@ -4,9 +4,9 @@ class Island { public: + Island(int posX, int posY); Coordinates getPosition() const { return _position; } private: Coordinates _position; }; - diff --git a/shm/Map.cpp b/shm/Map.cpp index 87cf141b5..65b0b5f2a 100644 --- a/shm/Map.cpp +++ b/shm/Map.cpp @@ -16,7 +16,7 @@ Map::Map() { if (std::none_of(_vecOfIslands.begin(), _vecOfIslands.end(), [positionX, positionY](auto& i) { return i.getPosition() == Coordinates(positionX, positionY); })) { - _vecOfIslands.emplace_back(Island(positionX, positionY)); + _vecOfIslands.push_back(Island(positionX, positionY)); notRepeatedPositions++; } }