-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Shm #110
base: shm
Are you sure you want to change the base?
Shm #110
Changes from 105 commits
b358851
c5340c8
220bf13
44b8e81
5fc1b6f
ea70a73
5f82ef7
268dd71
1f17478
3f5d8f7
cd73046
92d4e90
ae6a443
99cdf86
daa404b
8637398
ac8dc4b
52bf2f5
b1fe926
75cb5a2
f0bd5b5
714a9ba
f7233b5
9967da7
c4a1c7b
5a4cf38
18f68ee
b212c45
3c8a7d7
4c184e8
54b9ed6
c63de9d
f12be8a
ff3c21b
d946046
680db84
c5f98ae
1b9e862
c1b96cc
1ab9d3d
5b7353f
8de5823
9ed1f3c
cd918ff
9fbb5a8
44eb3f5
0fa6324
c837a38
0e7d86e
bb12618
aae412a
188c3b7
b28eb70
fec6fd5
763a42d
9bad12c
e7d5614
3817b97
e1ed853
88acddd
2a02c33
03c6ce7
0516f0e
eb4149c
96044fd
f8ed192
d3dafd9
65e8c68
a34c2d6
2885a07
79e0485
3912043
a1fb936
5c3f05a
3d7c55c
a52b949
c2e251d
b1a3aca
0984f02
d320f4e
85d5b75
7990ddb
5d99fbc
8a129e5
9e4155f
26c4daf
4a3ed88
2a51252
eae7cb9
755efc9
6dee2fd
fda7c7b
4dca07a
5c2c99e
897ac40
f89d725
407dc63
f376156
482f093
5fc35a9
81bc456
83216df
f78999f
3b60c1c
a3dff7b
4a94837
f31e2cd
6310af7
4d5803a
2a8230f
b058834
337b091
e1399df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(SHM_2077) | ||
|
||
set(FILES | ||
island.cpp | ||
map.cpp | ||
player.cpp | ||
ship.cpp | ||
coordinates.cpp | ||
cargo.cpp | ||
store.cpp | ||
alcohol.cpp | ||
item.cpp | ||
fruit.cpp | ||
time.cpp | ||
game.cpp | ||
) | ||
|
||
set(FLAGS | ||
-Wall | ||
#-Wextra | ||
-Wpedantic | ||
-Wconversion | ||
-O0) | ||
|
||
add_executable(${PROJECT_NAME} main.cpp ${FILES}) | ||
target_compile_options(${PROJECT_NAME} PRIVATE ${FLAGS}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include "alcohol.hpp" | ||
#include <iostream> | ||
|
||
Alcohol::Alcohol(const std::string& name, size_t amount, size_t basePrice, size_t percentage) | ||
: Cargo(name, amount, basePrice), percentage_(percentage) {} ; | ||
|
||
size_t Alcohol::getPrice() const { | ||
return static_cast<size_t>(basePrice_ * static_cast<float>(percentage_)/static_cast<float>(MaxPercentage)); | ||
} | ||
|
||
Cargo& Alcohol::operator+=(size_t amount) { | ||
amount_ += amount; | ||
return *this; | ||
} | ||
|
||
Cargo& Alcohol::operator-=(size_t amount) { | ||
if (amount <= amount_) { | ||
amount_ -= amount; | ||
} else if(amount_ < 0){ | ||
|
||
} | ||
return *this; | ||
} | ||
|
||
bool Alcohol::operator==(const Cargo& cargo) const { | ||
return cargo.getName() == getName(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
#include "cargo.hpp" | ||
|
||
constexpr size_t MaxPercentage{96}; | ||
class Alcohol : public Cargo { | ||
public: | ||
Alcohol(const std::string& name, size_t amount, size_t basePrice, size_t percentage); | ||
Alcohol() {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = default |
||
Alcohol(const std::string& name, size_t amount) : Cargo(name, amount) {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to cpp |
||
~Alcohol() override{}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. = default |
||
|
||
Cargo& operator+=(size_t amount) override; | ||
Cargo& operator-=(size_t amount) override; | ||
bool operator==(const Cargo&) const override; | ||
|
||
size_t getPercentage() const { return percentage_; }; | ||
void setPercentage(size_t percentage) { percentage_ = percentage; }; | ||
|
||
//Methods override from Cargo class. | ||
size_t getPrice() const override; | ||
std::string getName() const override { return name_; }; | ||
size_t getAmount() const override { return amount_; }; | ||
size_t getBasePrice() const override { return basePrice_; }; | ||
void setAmount(const size_t& amount) override { amount_ = amount;}; | ||
void nextDay() override {}; | ||
private: | ||
size_t percentage_; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "cargo.hpp" | ||
#include <iostream> | ||
|
||
|
||
Cargo::Cargo(const std::string& name, size_t amount, size_t basePrice, std::shared_ptr<Time> time) | ||
: name_(name) | ||
, amount_(amount) | ||
, basePrice_(basePrice) | ||
, time_(time) | ||
{ | ||
if (time_) { | ||
time_->attachObserver(this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You never detach observer. This will cause crash when this object will be deleted |
||
} | ||
} | ||
|
||
Cargo& Cargo::operator+=(size_t amount) { | ||
amount_ += amount; | ||
return *this; | ||
} | ||
|
||
Cargo& Cargo::operator-=(size_t amount) { | ||
if (amount <= amount_) { | ||
amount_ -= amount; | ||
} else { | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant else |
||
return *this; | ||
} | ||
|
||
bool Cargo::operator==(const Cargo& cargo) const { | ||
return cargo.getName() == getName(); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
#pragma once | ||
#include <string> | ||
#include "time.hpp" | ||
#include "iObserver.hpp" | ||
|
||
class Cargo : public IObserver { | ||
public: | ||
Cargo() {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. default |
||
Cargo(const std::string& name, size_t amount) : name_(name), amount_(amount) {}; | ||
Cargo(const std::string& name, size_t amount, size_t basePrice, std::shared_ptr<Time> time); | ||
Cargo(const std::string& name, size_t amount, size_t basePrice) | ||
: name_(name), amount_(amount), basePrice_(basePrice) {}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to cpp |
||
virtual ~Cargo() = default; | ||
|
||
virtual Cargo& operator+=(size_t amount); | ||
virtual Cargo& operator-=(size_t amount); | ||
virtual bool operator==(const Cargo&) const; | ||
|
||
virtual size_t getPrice() const = 0; | ||
virtual std::string getName() const = 0; | ||
virtual size_t getAmount() const = 0; | ||
virtual size_t getBasePrice() const = 0; | ||
|
||
virtual void setAmount(const size_t& amount) = 0; | ||
|
||
protected: | ||
std::string name_; | ||
size_t amount_; | ||
size_t basePrice_; | ||
std::shared_ptr<Time> time_ {nullptr}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "coordinates.hpp" | ||
|
||
void Coordinates::setPositionX(size_t positionX) { | ||
positionX_ = positionX; | ||
} | ||
size_t Coordinates::getPositionX() const { | ||
return positionX_; | ||
} | ||
|
||
void Coordinates::setPositionY(size_t positionY) { | ||
positionY_ = positionY; | ||
} | ||
size_t Coordinates::getPositionY() const { | ||
return positionY_; | ||
} | ||
|
||
bool Coordinates::operator== (const Coordinates& n_pos) const { | ||
return positionX_ == n_pos.getPositionX() && positionY_ == n_pos.getPositionY(); | ||
} | ||
|
||
size_t Coordinates::distance(const Coordinates& rhs) { | ||
return std::round(std::sqrt(std::pow((static_cast<int>(positionX_) - static_cast<int>(rhs.getPositionX())),2) + std::pow((static_cast<int>(positionY_) - static_cast<int>(rhs.getPositionY())),2))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. split it to 2 lines |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
#include <cmath> | ||
|
||
class Coordinates { | ||
public: | ||
Coordinates() | ||
: positionX_(0) | ||
, positionY_(0) | ||
{} | ||
|
||
Coordinates(size_t positionX, size_t positionY) | ||
: positionX_(positionX) | ||
, positionY_(positionY) | ||
{} | ||
|
||
void setPositionX(size_t ); | ||
size_t getPositionX() const; | ||
|
||
void setPositionY(size_t ); | ||
size_t getPositionY() const; | ||
|
||
bool operator== (const Coordinates& ) const; | ||
|
||
size_t distance(const Coordinates&); | ||
|
||
private: | ||
size_t positionX_; | ||
size_t positionY_; | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is ideall candidate to constexpr class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#include "fruit.hpp" | ||
#include <iostream> | ||
|
||
Fruit::Fruit(const std::string &name, size_t amount, size_t basePrice, size_t expirationDate, std::shared_ptr<Time> time) : Cargo(name, amount, basePrice, time), expirationDate_(expirationDate){ | ||
}; | ||
|
||
size_t Fruit::getPrice() const | ||
{ | ||
return static_cast<size_t>(basePrice_ * static_cast<float>(purchaseDate_) / expirationDate_); | ||
} | ||
|
||
Cargo &Fruit::operator+=(size_t amount) | ||
{ | ||
amount_ += amount; | ||
return *this; | ||
} | ||
|
||
Cargo &Fruit::operator-=(size_t amount) | ||
{ | ||
if (amount <= amount_) | ||
{ | ||
amount_ -= amount; | ||
} | ||
else | ||
{ | ||
} | ||
return *this; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Please have logic with add/ substract in one place. |
||
|
||
bool Fruit::operator==(const Cargo &cargo) const | ||
{ | ||
return cargo.getName() == getName(); | ||
} | ||
|
||
Fruit &Fruit::operator--() | ||
{ | ||
if (purchaseDate_ <= 0) | ||
{ | ||
purchaseDate_ = 0; | ||
} | ||
purchaseDate_--; | ||
return *this; | ||
} | ||
|
||
Fruit &Fruit::operator--(int) | ||
{ | ||
if (purchaseDate_ <= 0) | ||
{ | ||
purchaseDate_ = 0; | ||
} | ||
purchaseDate_--; | ||
return *this; | ||
} | ||
|
||
void Fruit::nextDay() | ||
{ | ||
if (purchaseDate_) | ||
{ | ||
operator--(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#pragma once | ||
|
||
#include "cargo.hpp" | ||
//Class responsible for managing Fruit in the game. | ||
class Fruit : public Cargo { | ||
public: | ||
Fruit(const std::string& name, size_t amount, size_t basePrice, size_t expirationDate, std::shared_ptr<Time> time); | ||
|
||
~Fruit() override {}; | ||
|
||
Cargo& operator+=(size_t amount) override; | ||
Cargo& operator-=(size_t amount) override; | ||
bool operator==(const Cargo&) const override; | ||
Fruit& operator--(); | ||
Fruit& operator--(int); | ||
|
||
//Methods override from Cargo class. | ||
virtual size_t getPrice() const override; | ||
virtual std::string getName() const override { return name_; }; | ||
size_t getAmount() const override { return amount_; }; | ||
size_t getBasePrice() const override { return basePrice_; }; | ||
|
||
size_t getExpirationDate() const { return expirationDate_; }; | ||
size_t getPurchaseData() const { return purchaseDate_; }; | ||
void setAmount(const size_t& amount) override { amount_ = amount;}; | ||
|
||
void nextDay() override; | ||
|
||
protected: | ||
size_t expirationDate_; | ||
size_t purchaseDate_ = expirationDate_; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happend when you add to much cargo (more then capacity?) Do you handle this somewhere? If yes please move also below logic to this function (with substract) because now you have logic for substract in Cargo and for add in other class this is not god :)