Skip to content

Commit

Permalink
Merge pull request coders-school#18 from KrzysztofJ85/KrzysztofJ85
Browse files Browse the repository at this point in the history
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
kszytko authored Jun 20, 2020
2 parents af4b71e + fceae8c commit 1a2335d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 6 deletions.
6 changes: 5 additions & 1 deletion shm/Coordinates.cpp
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);
}
}
4 changes: 1 addition & 3 deletions shm/Coordinates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions shm/Island.cpp
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) {}
2 changes: 1 addition & 1 deletion shm/Island.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class Island {
public:
Island(int posX, int posY);
Coordinates getPosition() const { return _position; }

private:
Coordinates _position;
};

2 changes: 1 addition & 1 deletion shm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CC = g++

CFLAGS = -std=c++17 -Wall -Wextra -Wpedantic -Wconversion -Werror -O2

OBJECTS = Ship.o Cargo.o Coordinates.o Island.o SHM.o
OBJECTS = Cargo.o Ship.o Coordinates.o Island.o Map.o SHM.o

.cpp.o:
$(CC) -c $(CFLAGS) $< -o $@
Expand Down
28 changes: 28 additions & 0 deletions shm/Map.cpp
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);
}
19 changes: 19 additions & 0 deletions shm/Map.hpp
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;
};

0 comments on commit 1a2335d

Please sign in to comment.