Skip to content

9 moved player definitions into source file #24

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

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
56 changes: 56 additions & 0 deletions shm/source/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "Player.hpp"
#include "Ship.hpp"
#include "Cargo.hpp"

#include <numeric>
#include <memory>

Player::Player():
ship_(nullptr),
money_(0),
maxAvailableSpace_(100),
availableSpace_({0, false})
{}

size_t Player::getMoney() const {
return money_;
}

size_t Player::getAvailableSpace() {
if (!availableSpace_.second) {
calculateAvailableSpace();
}
return availableSpace_.first;
}

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

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

//void Player::PayCrew(size_t money) override;

//void Player::PurchaseCargo(std::unique_ptr<Cargo> cargo, size_t price);

//void Player::SellCargo(Cargo* cargo, size_t price);

//void Player::PrintCargo() const;

void Player::calculateAvailableSpace() {
if (!ship_) {
return;
}
const auto& allCargo = ship_->getCargo();
size_t count = std::accumulate(allCargo.begin(), allCargo.end(), 0, [](int sum, const auto& cargo) { return sum + cargo->getAmount(); });
availableSpace_.first = maxAvailableSpace_ - count;
availableSpace_.second = true;
}
82 changes: 28 additions & 54 deletions shm/source/Player.hpp
Original file line number Diff line number Diff line change
@@ -1,61 +1,35 @@
#pragma once

#include <memory>

#include "Ship.hpp"
class Ship;
class Cargo;

class Player {
std::unique_ptr<Ship> ship_;
int money_;
size_t availableSpace_;
Ship* ship_;
size_t money_;
size_t maxAvailableSpace_;
std::pair<size_t, bool> 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);
}
};
Player();

size_t getMoney() const;

size_t getAvailableSpace();

size_t getSpeed() const;

Cargo* getCargo(size_t index) const;

// void PayCrew(size_t money) override;

//void PurchaseCargo(std::unique_ptr<Cargo> cargo, size_t price);

//void SellCargo(Cargo* cargo, size_t price);

//void PrintCargo() const;

private:
void calculateAvailableSpace();
};