forked from coders-school/object-oriented-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request coders-school#18 from KrzysztofJ85/KrzysztofJ85
Task 4: Map (new class) coders-school#4; Task 5: Map (constructor) coders-school#5; Task 6: Map (getIsland) coders-school#9
- Loading branch information
Showing
7 changed files
with
58 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
#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; | ||
} | ||
|
||
bool Coordinates::operator!=(const Coordinates& coordinatesToCheck) const { | ||
return !(*this == coordinatesToCheck); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
#include "Island.hpp" | ||
|
||
Island::Island(int posX, int posY) | ||
: _position(posX, posY) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "Map.hpp" | ||
|
||
#include <algorithm> | ||
#include <memory> | ||
#include <random> | ||
|
||
Map::Map() { | ||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::uniform_int_distribution<> distrib(minPositionXY, maxPositionXY); | ||
_vecOfIslands.reserve(numberOfIslands); | ||
int notRepeatedPositions = 0; | ||
while (notRepeatedPositions != numberOfIslands) { | ||
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.push_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; }); | ||
return std::addressof(*itr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
#include "Island.hpp" | ||
|
||
constexpr int minPositionXY = 1; | ||
constexpr int maxPositionXY = 10; | ||
constexpr int numberOfIslands = 10; | ||
|
||
class Map { | ||
public: | ||
Map(); | ||
Island* getIsland(const Coordinates& coordinate); | ||
|
||
private: | ||
std::vector<Island> _vecOfIslands; | ||
Island* _currentPosition; | ||
}; |