Skip to content

Shm 2 #106

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

Open
wants to merge 25 commits into
base: shm
Choose a base branch
from
Open

Shm 2 #106

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b358851
fix #30
ziobron Jun 11, 2021
c5340c8
fix #84
ziobron Jun 11, 2021
220bf13
fix #88, module3 almost removed
ziobron Jun 11, 2021
44b8e81
Update 09-operators.md
BarTes8 Jun 14, 2021
2f41754
module2
ziobron Jun 23, 2021
d3ca5df
module2 further work
ziobron Jun 23, 2021
43b51d7
module2 finished
ziobron Jun 23, 2021
0b7e6a3
md rename
ziobron Jun 23, 2021
0c2d7b8
Fix number -> amount
ziobron Jun 24, 2021
fb9d392
added getUserInput() to the Laptop class
ziobron Jun 29, 2021
4163401
Update README.md (#89)
BarTes8 Jul 3, 2021
90ee55a
Script return values changed
ziobron Jul 5, 2021
09391a3
completed SHM-Vol.1
KacperKaletaScytheStudio Jul 7, 2021
06bb9cc
8 moved map implementation into source file, simplified map class (#12)
Ptysiek Jul 23, 2021
d91324f
6 moved definitions from Fortune.hpp file into Fortune.cpp source fil…
Ptysiek Jul 26, 2021
242ff66
9 moved player definitions into source file (#24)
Ptysiek Jul 26, 2021
e737f34
5 moved Cargo definitions into source file (#23)
Ptysiek Jul 26, 2021
d8b792e
16 created new class: Game (#22)
Ptysiek Jul 26, 2021
97acf13
10 moved ship definitions into source file (#25)
Ptysiek Jul 26, 2021
dd89d0a
removed maxAvailableSpace field from Player class
KacperKaletaScytheStudio Jul 26, 2021
f96b5fa
20 create derived classes from class cargo (#26)
Ptysiek Jul 26, 2021
a32c398
14 cs task4 (#27)
Ptysiek Jul 26, 2021
3192262
13 cs task3 (#29)
Ptysiek Jul 26, 2021
77cb21d
21 created new class DryFruit (#30)
Ptysiek Aug 21, 2021
d5c4ee3
7 moved Island class definitions into source file (#31)
Ptysiek Aug 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
completed SHM-Vol.1
  • Loading branch information
KacperKaletaScytheStudio committed Jul 7, 2021
commit 09391a3c510382daad942f848de5267943f27fb5
42 changes: 42 additions & 0 deletions shm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.10)

set(CMAKE_C_COMPILER "/usr/bin/gcc")
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# set the project name
project(SHM)

# set executables
set(THIS_PROJECT_SRC_DIRECTORIES
)
set(THIS_PROJECT_TESTS_DIRECTORIES
)

# set flags
set(THIS_PROJECT_FLAGS
-Wall
-Werror
-Weffc++
-Wextra
-pedantic
-Wconversion
-O3
)

# add all executables
add_executable(${PROJECT_NAME}
main.cpp
${THIS_PROJECT_SRC_DIRECTORIES}
)

# specify compile options for target
target_compile_options(${PROJECT_NAME} PRIVATE ${THIS_PROJECT_FLAGS})

# enable standard cpp17
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

# link ncurses static lib
target_link_libraries(${PROJECT_NAME} ncurses)

20 changes: 20 additions & 0 deletions shm/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>

#include "source/Cargo.hpp"
#include "source/Map.hpp"
#include "source/Player.hpp"

int main() {
Map();
Player a;
Player b;
Player c;

c.giveAwayShip();

std::cout << "\n" << a.takeOverShip(b.giveAwayShip())
<< "\n" << b.takeOverShip(a.giveAwayShip());

std::cout << "\n\n\n";
return 0;
}
42 changes: 42 additions & 0 deletions shm/source/Cargo.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <string>

class Cargo {
public:
Cargo(const std::string& name, const size_t amount, const size_t basePrice):
name_(name),
amount_(amount),
basePrice_(basePrice)
{}

bool operator==(const Cargo& cargo) const {
if (name_ != cargo.getName()) {
return false;
}
if (amount_ != cargo.getAmount()) {
return false;
}
if (basePrice_ != cargo.getBasePrice()) {
return false;
}
return true;
}

Cargo& operator+=(size_t amount) {
amount_ += amount;
return *this;
}
Cargo& operator-=(size_t amount) {
amount_ -= amount;
return *this;
}

std::string getName() const { return name_; }
size_t getAmount() const { return amount_; }
size_t getBasePrice() const { return basePrice_; }

protected:
std::string name_;
size_t amount_;
size_t basePrice_;
};
42 changes: 42 additions & 0 deletions shm/source/Fortune.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once
#include <exception>
#include <random>
#include <stdexcept>
#include <vector>

struct Fortune {
static int getNumber(int first, int last) {
const int result = makeRandomNumber(first, last);
if (result < first || result > last) {
throw std::logic_error("Fortune::getNumber(int,int): Result value out of scope");
}
return result;
}

static std::vector<int> getNumbersEvenlyDistributed(int first, int last, const int n) {
if (n < 1) {
return {};
}
std::vector<int> result(n);
for (int i = 0; i < n; ++i) {
int localFirst = (last / n * i) + first;
int localLast = (last / n * (i + 1)) + first;
result.at(i) = (getNumber(localFirst, localLast));
}
return result;
}

static void shuffle(std::vector<int>& vector) {
std::random_device random_device;
std::mt19937 random_engine(random_device());
std::shuffle(vector.begin(), vector.end(), random_engine);
}

private:
static int makeRandomNumber(const int first, const int last) {
std::random_device random_device;
std::mt19937 random_engine(random_device());
std::uniform_int_distribution<int> distribution(first, last);
return distribution(random_engine);
}
};
27 changes: 27 additions & 0 deletions shm/source/Island.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

struct Island {
struct Coordinates {
int positionX;
int positionY;

Coordinates(const int x, const int y):
positionX(x),
positionY(y)
{}

bool operator==(const Coordinates& coordinates) {
return (positionX == coordinates.positionX && positionY == coordinates.positionY);
}
};

public:
Island(const Coordinates& coordinates):
position_(coordinates)
{}

Coordinates getPosition() const { return position_; }

private:
Coordinates position_;
};
63 changes: 63 additions & 0 deletions shm/source/Map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once
#include <algorithm>
#include <vector>

#include "Fortune.hpp"
#include "Island.hpp"

class Map {
size_t horizonLimit_;
std::vector<Island> islands_;
Island* currentPosition_;

public:
Map():
horizonLimit_(100),
islands_(generateIslands(10)),
currentPosition_()
{}

Map(const Map& map):
horizonLimit_(map.horizonLimit_),
islands_(map.islands_),
currentPosition_(map.currentPosition_)
{}

Map& operator=(const Map& map) {
horizonLimit_ = map.horizonLimit_;
islands_ = map.islands_;
currentPosition_ = currentPosition_;
return *this;
}

Island* getIsland(const Island::Coordinates& coordinate) {
for (auto& island : islands_) {
if (island.getPosition() == coordinate) {
return &island;
}
}
return nullptr;
}

private:
std::vector<Island> generateIslands(const size_t numberOfIslands) const {
if (numberOfIslands < 1) {
return {};
}
auto xyPositions = generateRandomPositions(numberOfIslands);
std::vector<Island> islands;
islands.reserve(xyPositions.size() / 2);
for (size_t i = 1; i < xyPositions.size(); i += 2) {
islands.emplace_back(Island::Coordinates(xyPositions.at(i - 1), xyPositions.at(i)));
}
return islands;
}

std::vector<int> generateRandomPositions(const size_t n) const {
auto positions = Fortune::getNumbersEvenlyDistributed(0, static_cast<int>(horizonLimit_), static_cast<int>(n));
auto yPositions = Fortune::getNumbersEvenlyDistributed(0, static_cast<int>(horizonLimit_), static_cast<int>(n));
positions.insert(positions.end(), yPositions.begin(), yPositions.end());
Fortune::shuffle(positions);
return positions;
}
};
61 changes: 61 additions & 0 deletions shm/source/Player.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once
#include <memory>

#include "Ship.hpp"

class Player {
std::unique_ptr<Ship> ship_;
int money_;
size_t availableSpace_;

public:
Player():
ship_(std::make_unique<Ship>()),
money_(0),
availableSpace_()
{}

int getMoney() const { return money_; }

size_t getAvailableSpace() const { return availableSpace_; }

size_t getSpeed() const {
if (ship_) {
return ship_->getSpeed();
}
return 0;
}

Cargo* getCargo(size_t index) const {
if (ship_) {
return ship_->getCargo(index);
}
return nullptr;
}

std::string takeOverShip(std::unique_ptr<Ship> someoneElsesShip) {
if (!someoneElsesShip) {
return "there is no ship";
}
if (ship_) {
return "already have one";
}
ship_ = std::move(someoneElsesShip);
return "yarr";
}

std::unique_ptr<Ship> giveAwayShip() {
return std::move(ship_);
}

int calculateAvailableSpace() const {
size_t index = 0;
size_t count = 0;
auto cargo = getCargo(index);
while (cargo) {
count += cargo->getAmount();
cargo = getCargo(++index);
}
return static_cast<int>(getAvailableSpace() - count);
}
};
64 changes: 64 additions & 0 deletions shm/source/Ship.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#pragma once
#include <string>

#include "Cargo.hpp"

class Ship {
public:
Ship()
: capacity_()
, maxCrew_()
, crew_()
, speed_()
, name_()
, cargo_()
, id_(-1)
{}

Ship(int capacity, int maxCrew, int speed, const std::string& name, size_t id)
: capacity_(capacity)
, maxCrew_(maxCrew)
, crew_(0)
, speed_(speed)
, name_(name)
, cargo_()
, id_(id)
{}

Ship(int maxCrew, int speed, size_t id)
: Ship(0, maxCrew, speed, "", id)
{}

void setName(const std::string& name) { name_ = name; }

Ship& operator-=(size_t num) {
crew_ -= num;
return *this;
}
Ship& operator+=(size_t num) {
crew_ += num;
return *this;
}

size_t getCapacity() const { return capacity_; }
size_t getMaxCrew() const { return maxCrew_; }
size_t getSpeed() const { return speed_; }
std::string getName() const { return name_; }
size_t getId() const { return id_; }

Cargo* getCargo(const size_t index) {
if (index < cargo_.size()) {
return &cargo_.at(index);
}
return nullptr;
}

private:
size_t capacity_;
size_t maxCrew_;
size_t crew_;
size_t speed_;
std::string name_;
std::vector<Cargo> cargo_;
const size_t id_;
};