Skip to content

Commit d99e2be

Browse files
authored
Merge pull request coders-school#23 from Marcinati/kocur1z
zadanie 4 i 5
2 parents 9003a3e + bd7fe68 commit d99e2be

File tree

9 files changed

+165
-66
lines changed

9 files changed

+165
-66
lines changed

shm/.vscode/settings.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"files.associations": {
3+
"array": "cpp",
4+
"atomic": "cpp",
5+
"bit": "cpp",
6+
"*.tcc": "cpp",
7+
"cctype": "cpp",
8+
"clocale": "cpp",
9+
"cmath": "cpp",
10+
"cstdarg": "cpp",
11+
"cstddef": "cpp",
12+
"cstdint": "cpp",
13+
"cstdio": "cpp",
14+
"cstdlib": "cpp",
15+
"cwchar": "cpp",
16+
"cwctype": "cpp",
17+
"deque": "cpp",
18+
"unordered_map": "cpp",
19+
"vector": "cpp",
20+
"exception": "cpp",
21+
"algorithm": "cpp",
22+
"functional": "cpp",
23+
"iterator": "cpp",
24+
"memory": "cpp",
25+
"memory_resource": "cpp",
26+
"numeric": "cpp",
27+
"optional": "cpp",
28+
"random": "cpp",
29+
"string": "cpp",
30+
"string_view": "cpp",
31+
"system_error": "cpp",
32+
"tuple": "cpp",
33+
"type_traits": "cpp",
34+
"utility": "cpp",
35+
"fstream": "cpp",
36+
"initializer_list": "cpp",
37+
"iosfwd": "cpp",
38+
"iostream": "cpp",
39+
"istream": "cpp",
40+
"limits": "cpp",
41+
"new": "cpp",
42+
"ostream": "cpp",
43+
"sstream": "cpp",
44+
"stdexcept": "cpp",
45+
"streambuf": "cpp",
46+
"typeinfo": "cpp"
47+
}
48+
}

shm/cargo.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
1+
#include "cargo.hpp"
2+
13
#include <iostream>
24
#include <string>
3-
#include "cargo.hpp"
45

5-
Cargo::Cargo(std::string name, size_t amount, size_t basePrice)
6-
: name_(name)
7-
, amount_(amount)
8-
, basePrice_(basePrice)
9-
{}
6+
Cargo::Cargo(std::string name, size_t amount, size_t basePrice)
7+
: name_(name), amount_(amount), basePrice_(basePrice) {}
108

11-
Cargo& Cargo::operator+=(size_t amount) {
12-
amount_ += amount;
9+
Cargo& Cargo::operator+=(size_t amount) {
10+
amount_ += amount;
11+
return *this;
12+
}
13+
Cargo& Cargo::operator-=(size_t amount) {
14+
if (amount_ < amount) {
15+
std::cerr << "Cargo is lower than 0 \n";
1316
return *this;
1417
}
15-
Cargo& Cargo::operator-=(size_t amount) {
16-
if (amount_ < amount) {
17-
std::cerr << "Cargo is lower than 0 \n";
18-
return *this;
19-
}
20-
amount_ -= amount;
21-
return *this;
22-
};
18+
amount_ -= amount;
19+
return *this;
20+
};
2321

24-
bool Cargo::operator==(std::string name) {
25-
return name_ == name;
26-
};
22+
bool Cargo::operator==(std::string name) {
23+
return name_ == name;
24+
};
2725

28-
std::string Cargo::GetName() const { return name_; }
29-
size_t Cargo::GetAmount() const { return amount_; }
30-
size_t Cargo::GetBasePrice() const { return basePrice_; }
26+
std::string Cargo::GetName() const {
27+
return name_;
28+
}
29+
size_t Cargo::GetAmount() const {
30+
return amount_;
31+
}
32+
size_t Cargo::GetBasePrice() const {
33+
return basePrice_;
34+
}
3135

36+
bool Cargo::operator==(std::string name) {
37+
return name_ == name;
38+
};

shm/cargo.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ class Cargo {
1010
size_t GetAmount() const;
1111
size_t GetBasePrice() const;
1212

13-
14-
1513
protected:
1614
std::string name_;
1715
size_t amount_;
1816
size_t basePrice_;
1917
};
20-

shm/map.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "map.hpp"
2+
3+
#include <algorithm>
4+
#include <random>
5+
6+
Map::Map() {
7+
std::vector<unsigned int> posX(maxX);
8+
std::vector<unsigned int> posY(maxY);
9+
10+
std::iota(posX.begin(), posX.end(), posX.begin());
11+
std::iota(posY.begin(), posY.end(), posY.begin());
12+
13+
std::mt19937 gen(std::random_device{}());
14+
std::shuffle(posX.begin(), posX.end(), gen);
15+
std::shuffle(posY.begin(), posY.end(), gen);
16+
17+
vecOfIslandOnMap_.reserve(initialAmountOfIsland);
18+
19+
std::transform(posX.begin(),
20+
posX.end(),
21+
posY.begin(),
22+
std::back_inserter(vecOfIslandOnMap_),
23+
[](int x, int y) {
24+
return std::make_pair(x, y);
25+
});
26+
}

shm/map.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#pragma once
2+
3+
#include <vector>
4+
5+
#include "island.hpp"
6+
7+
constexpr int initialAmountOfIsland = 10;
8+
constexpr int maxX = 10;
9+
constexpr int maxY = 10;
10+
11+
class Map {
12+
private:
13+
std::vector<Island> vecOfIslandOnMap_;
14+
Island* currentPosition_;
15+
16+
public:
17+
Map();
18+
};

shm/player.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ Player::Player(std::unique_ptr<Ship> ship, size_t money, size_t space)
88
Player::Player(std::unique_ptr<Ship> ship)
99
: Player(std::move(ship), START_MONEY, START_SPACE) {}
1010

11-
size_t Player::getSpeed() {
11+
size_t Player::getSpeed() const{
1212
if (ship_)
1313
return ship_->getSpeed();
1414

1515
return 0;
1616
}
1717

18-
Cargo Player::getCargo(size_t index) {
18+
Cargo Player::getCargo(size_t index) const{
1919
if (ship_)
2020
return ship_->getCargo(index);
2121

shm/player.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
#pragma once
22
#include <memory>
33

4+
#include "cargo.hpp"
45
#include "ship.hpp"
56

67
size_t constexpr START_MONEY = 1000;
78
size_t constexpr START_SPACE = 0;
89

910
class Player {
10-
public:
11+
public:
1112
Player(std::unique_ptr<Ship> ship, size_t money, size_t space);
1213
Player(std::unique_ptr<Ship> ship);
1314

@@ -16,7 +17,7 @@ class Player {
1617
size_t getSpeed() const;
1718
Cargo getCargo(size_t index) const;
1819

19-
private:
20+
private:
2021
std::unique_ptr<Ship> ship_;
2122
size_t money_;
2223
size_t availableSpace_;

shm/ship.cpp

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
1+
#include "ship.hpp"
2+
13
#include <iostream>
24
#include <string>
3-
#include "ship.hpp"
45

5-
Ship::Ship()
6-
: id_(-1)
7-
{}
6+
Ship::Ship()
7+
: id_(-1) {}
88

9-
Ship::Ship(int capacity, int maxCrew, int speed, const std::string& name, int id)
10-
: capacity_(capacity)
11-
, maxCrew_(maxCrew)
12-
, crew_(0)
13-
, speed_(speed)
14-
, name_(name)
15-
, id_(id)
16-
{}
17-
18-
Ship::Ship(int maxCrew, int speed, size_t id)
19-
: Ship(0, maxCrew, speed, "", id)
20-
{}
9+
Ship::Ship(int capacity, int maxCrew, int speed, const std::string& name, int id)
10+
: capacity_(capacity), maxCrew_(maxCrew), crew_(0), speed_(speed), name_(name), id_(id) {}
2111

22-
void Ship::setName(const std::string& name) { name_ = name; }
12+
Ship::Ship(int maxCrew, int speed, size_t id)
13+
: Ship(0, maxCrew, speed, "", id) {}
2314

24-
Ship& Ship::operator-=(size_t num) {
25-
if (crew_ < num) {
26-
std::cerr << "Number of papays is lower than 0 \n";
27-
return *this;
28-
}
29-
crew_ -= num;
15+
void Ship::setName(const std::string& name) {
16+
name_ = name;
17+
}
18+
19+
Ship& Ship::operator-=(size_t num) {
20+
if (crew_ < num) {
21+
std::cerr << "Number of papays is lower than 0 \n";
3022
return *this;
3123
}
32-
Ship& Ship::operator+=(size_t num) {
33-
if(num + crew_ > maxCrew_) {
34-
std::cerr << "To many papays!\n";
35-
return *this;
36-
}
37-
crew_ += num;
24+
crew_ -= num;
25+
return *this;
26+
}
27+
Ship& Ship::operator+=(size_t num) {
28+
if (num + crew_ > maxCrew_) {
29+
std::cerr << "To many papays!\n";
3830
return *this;
3931
}
32+
crew_ += num;
33+
return *this;
34+
}
4035

41-
size_t Ship::getCapacity() const { return capacity_; }
42-
size_t Ship::getMaxCrew() const { return maxCrew_; }
43-
size_t Ship::getSpeed() const { return speed_; }
44-
std::string Ship::getName() const { return name_; }
45-
int Ship::getId() const { return id_; }
46-
36+
size_t Ship::getCapacity() const {
37+
return capacity_;
38+
}
39+
size_t Ship::getMaxCrew() const {
40+
return maxCrew_;
41+
}
42+
size_t Ship::getSpeed() const {
43+
return speed_;
44+
}
45+
std::string Ship::getName() const {
46+
return name_;
47+
}
48+
int Ship::getId() {
49+
return id_;
50+
}

shm/ship.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#pragma once
22
#include <string>
33

4-
54
class Ship {
65
public:
76
Ship();
@@ -23,4 +22,3 @@ class Ship {
2322
std::string name_;
2423
const int id_;
2524
};
26-

0 commit comments

Comments
 (0)