Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
177 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
124ad57
task 4
pwiatr3 Jun 23, 2021
5904834
added island and coordinates classes
Crazyayane07 Jun 23, 2021
1e0942d
Adding ship class
Kasia9311 Jun 23, 2021
65a6e33
Adding cargo class
Kasia9311 Jun 23, 2021
f94483f
Adding comparison operator to class Cargo and little fixes
Kasia9311 Jun 23, 2021
814c82e
Added cargo getters and constructor
NikczemnyOkularnik Jun 23, 2021
4d803d1
Player class + main update
kondorski Jun 23, 2021
80f9fc0
player refactor
kondorski Jun 25, 2021
57d4b21
ship class, added vector for cargo and addCargo function
kondorski Jun 25, 2021
cff4651
Fixes
Kasia9311 Jun 26, 2021
140899b
Merging
Kasia9311 Jun 26, 2021
7392348
Merge pull request #9 from NikczemnyOkularnik/NikczemnyOkularnik
NikczemnyOkularnik Jun 26, 2021
187f30e
task 9
kondorski Jun 26, 2021
c62d0cc
Mergin task 4
Kasia9311 Jun 26, 2021
453dea1
Merge branch 'shm' of https://github.com/Kasia9311/object-oriented-pr…
kondorski Jun 26, 2021
7eafa61
Merge v2
Kasia9311 Jun 26, 2021
193c1b2
Checks
Kasia9311 Jun 26, 2021
a099870
Adding task 5
Kasia9311 Jun 26, 2021
3c0dd7d
Adding function contains
Kasia9311 Jun 26, 2021
8aee177
Islands tryouts
kondorski Jun 26, 2021
47df11f
task 5
pwiatr3 Jun 27, 2021
8d987b8
tasks 5,6,7
kondorski Jun 27, 2021
5070b12
zepsuty vector
kondorski Jun 27, 2021
5c09851
all done!
kondorski Jun 27, 2021
7fec7d3
Fix pt 1
kondorski Jul 3, 2021
f1965b3
fixes part 2
kondorski Jul 3, 2021
ec03282
fixy part 3
kondorski Jul 3, 2021
17d1ad6
getIsland find if working
kondorski Jul 4, 2021
40d9448
formated main.cpp
NikczemnyOkularnik Jul 5, 2021
711f112
Added std::accumulate, changed cargo bananas to cotton beacuse of an …
NikczemnyOkularnik Jul 6, 2021
65e4ea0
Changes in getCargo. getSpeed
NikczemnyOkularnik Jul 7, 2021
b0d2e51
progres? shm2
kondorski Jul 17, 2021
696a99a
fixed pointers, changed raw to shared
NikczemnyOkularnik Jul 17, 2021
6df4544
Added test store, Sell function in store
NikczemnyOkularnik Jul 17, 2021
2099da7
Added print store cargo
NikczemnyOkularnik Jul 17, 2021
44ca58b
Store cargo add
NikczemnyOkularnik Jul 17, 2021
373c6bd
Adding excerscises: 5, 7
Kasia9311 Jul 18, 2021
3c96853
TimeEffectable added
Crazyayane07 Jul 18, 2021
fe4f5ac
Adding operators -= and +=
Kasia9311 Jul 18, 2021
5593b20
Adding operator == to cargo classes
Kasia9311 Jul 18, 2021
0d154d2
Merge pull request #15 from Crazyayane07/SHM2
Kasia9311 Jul 18, 2021
a0ce807
Changed cargo to Fruit&Alcohol
NikczemnyOkularnik Jul 18, 2021
aa91d5a
Problem with everything
NikczemnyOkularnik Jul 18, 2021
d321d3a
Observer patter in Time class working with Ship class
kondorski Jul 20, 2021
90f6010
Fixes in cargos
Kasia9311 Jul 20, 2021
5257ba6
Fixes merged
Kasia9311 Jul 20, 2021
08564da
Added virtual destructors
NikczemnyOkularnik Jul 21, 2021
0a43340
Testing Store cargo
NikczemnyOkularnik Jul 21, 2021
c1a310c
Adding virtual constructors
Kasia9311 Jul 21, 2021
f890a34
TimeEffectable interface
kondorski Jul 21, 2021
a441404
Merge branch 'SHM2' of https://github.com/Kasia9311/object-oriented-p…
kondorski Jul 21, 2021
7a05ac4
Delete binary, delete player and ship from Cargo::operator+=
kondorski Jul 21, 2021
ddac1bc
testing access to store
NikczemnyOkularnik Jul 22, 2021
a5ce88e
Time working in Ship, Cargo and Store
kondorski Jul 22, 2021
5c0af0d
rest of zad3 and fin zad5
Crazyayane07 Jul 23, 2021
2b9b0c8
TODO ship cargo, store money
NikczemnyOkularnik Jul 23, 2021
5da6d6e
Merge pull request #19 from kondorski/SHM2
Kasia9311 Jul 24, 2021
a782114
Merge branch 'SHM2' into SHM2
Kasia9311 Jul 24, 2021
ca3e0cf
Merge pull request #20 from Crazyayane07/SHM2
Kasia9311 Jul 24, 2021
ba49fa6
main working
kondorski Jul 24, 2021
fed4d2c
Remove print from Alcohol::nextDay() and Item::nextDay()
kondorski Jul 24, 2021
71371d2
Added amount increasing, unique ptr ship fixes
NikczemnyOkularnik Jul 24, 2021
57107ed
Merging SHM2 + Problems
Kasia9311 Jul 24, 2021
5ab23a3
Test & fixes
Kasia9311 Jul 24, 2021
46fecab
Reduce amount changed
NikczemnyOkularnik Jul 25, 2021
0a11444
Changed add cargo, added removeCargo(TODO)
NikczemnyOkularnik Jul 25, 2021
33f8086
Dead end
NikczemnyOkularnik Jul 25, 2021
2afbb7b
Changed to shared pointers, changed store sell
NikczemnyOkularnik Jul 25, 2021
f1b55f3
Fixes in sell
NikczemnyOkularnik Jul 25, 2021
9676a03
problems with shipCargo
NikczemnyOkularnik Jul 25, 2021
0ccb59e
experimental changes
NikczemnyOkularnik Jul 26, 2021
fee2dae
Store raw pointer tests
pwiatr3 Jul 27, 2021
b88fa98
After changing everything to raw pointers
pwiatr3 Jul 28, 2021
f8975d3
Everything on raw pointers
pwiatr3 Jul 28, 2021
7eb2b2d
added stores to mai.cpp
Crazyayane07 Jul 28, 2021
b31c912
Added Game class
NikczemnyOkularnik Jul 29, 2021
ff179b4
Adding class Game - set player
NikczemnyOkularnik Jul 29, 2021
fd93d48
Added player with ship
NikczemnyOkularnik Jul 29, 2021
393628a
changes after pushing branch to gihub
pwiatr3 Jul 31, 2021
7080968
Merge branch 'shm2_pwiatr3' of https://github.com/Kasia9311/object-or…
pwiatr3 Jul 31, 2021
ca2ae4d
working on remove Cargo in Ship class
pwiatr3 Aug 1, 2021
fd60baf
Adding map generating to the menu
Kasia9311 Aug 1, 2021
cb7cacd
Some fixes
Kasia9311 Aug 1, 2021
31395e8
Menu proto working
kondorski Aug 1, 2021
e50459f
Merge branch 'game_okular' of https://github.com/Kasia9311/object-ori…
kondorski Aug 1, 2021
8aa5cd3
add return to empty Menu method
kondorski Aug 1, 2021
b6746f0
Adding stuff...
Kasia9311 Aug 2, 2021
fa948fd
Adding current position
Kasia9311 Aug 2, 2021
b39c214
Adding travel to menu
Kasia9311 Aug 3, 2021
4072f66
Adding counting travel distance
Kasia9311 Aug 3, 2021
734a84b
Calculate distance issues
Kasia9311 Aug 4, 2021
7d5d411
removeCargo in Ship class works & storing random Cargo in Store class…
pwiatr3 Aug 7, 2021
32d054b
Fixed travel
NikczemnyOkularnik Aug 7, 2021
62debca
menu changes
kondorski Aug 5, 2021
4e8ded2
menu contstructor not working by passing Game as a unique ptr
kondorski Aug 7, 2021
02fb5e4
travel working and menu almost working
kondorski Aug 7, 2021
970c55f
delete binaries
kondorski Aug 7, 2021
a341acd
working menu
kondorski Aug 9, 2021
d2ec501
Menu class - delete unused private members
kondorski Aug 9, 2021
8faed3d
Menu formatting
kondorski Aug 9, 2021
4c0368c
Menu - remove unused method
kondorski Aug 9, 2021
835dedf
menuChoice change default message
kondorski Aug 9, 2021
848f532
Game - add method for displaying gold and position
kondorski Aug 9, 2021
1d4dfe9
Add print money in menu
NikczemnyOkularnik Aug 9, 2021
8f6e059
merging display_player_stats
NikczemnyOkularnik Aug 9, 2021
a0d9078
change return type in calculateDistance for consistency
kondorski Aug 9, 2021
0f64976
changeTime now takes argument to pass amount of days passed
kondorski Aug 9, 2021
b969857
display number of days passed
kondorski Aug 9, 2021
cf5bc6b
added print_store_cargo in menu
NikczemnyOkularnik Aug 9, 2021
910edc5
Merge branch 'game_okular' of https://github.com/Kasia9311/object-ori…
NikczemnyOkularnik Aug 9, 2021
7f6c90e
cleared main.cpp
NikczemnyOkularnik Aug 9, 2021
7aec38b
cleaning files from unnecessary comments
NikczemnyOkularnik Aug 9, 2021
772c9ef
deleted unnecessary printShipCargo
NikczemnyOkularnik Aug 9, 2021
0217ca4
testing buyCargo
NikczemnyOkularnik Aug 10, 2021
c552906
tested sell, buy cargo does not increase amount
NikczemnyOkularnik Aug 10, 2021
5b8a807
added responses in store
NikczemnyOkularnik Aug 10, 2021
1a8d780
nextDay passes elapsedDays to Observers
kondorski Aug 10, 2021
8b966e4
Merge branch 'game_okular' of https://github.com/Kasia9311/object-ori…
NikczemnyOkularnik Aug 10, 2021
f96f5e0
added random rarity&lcohol perctenge
NikczemnyOkularnik Aug 11, 2021
a4a5594
merging random islands to game
NikczemnyOkularnik Aug 11, 2021
4f4828c
Issue with buy/sell cargo
NikczemnyOkularnik Aug 11, 2021
caf391c
changed constructor order to fit member list
kondorski Aug 10, 2021
d246d74
constructor order in Fruit
kondorski Aug 10, 2021
975fe21
chagned float to size_t in getPrice
kondorski Aug 10, 2021
c26d6fa
fixed segmentation fault?
kondorski Aug 11, 2021
164021a
changed condition
NikczemnyOkularnik Aug 12, 2021
a3f0a77
Fixed alcohol price
NikczemnyOkularnik Aug 14, 2021
a674ee9
Merge pull request #25 from NikczemnyOkularnik/game_okular
Kasia9311 Aug 14, 2021
5ece995
added crew to ship
NikczemnyOkularnik Aug 14, 2021
cf7e88a
Merge pull request #27 from NikczemnyOkularnik/game_okular
Kasia9311 Aug 14, 2021
c2f2b9f
Alcohol/Item - nextDay uses elapsedTime - compiler fixes
kondorski Aug 13, 2021
2f8f119
GetPositionX/Y const methods
kondorski Aug 13, 2021
489e2a0
Ship nextDay uses elapsed time
kondorski Aug 13, 2021
3916c4d
Alcohol price = elapsedTime * 2
kondorski Aug 14, 2021
1459625
currentStore changes after travelling to a new island
kondorski Aug 14, 2021
613c8d7
Crew get salary each day
NikczemnyOkularnik Aug 14, 2021
68ed808
Merged fixes
NikczemnyOkularnik Aug 14, 2021
3b18f75
Deleted comments
NikczemnyOkularnik Aug 14, 2021
4ba8bcd
Fixed out of range in buy/sell cargo
NikczemnyOkularnik Aug 14, 2021
3c1e86e
Formated hpp files
NikczemnyOkularnik Aug 14, 2021
340f996
currentStore_ set in setPlayer()
kondorski Aug 15, 2021
a1a4f0a
Game - removed buyAllCargo & sellAllCargo
kondorski Aug 15, 2021
cf8801e
fixed issue with spending money
NikczemnyOkularnik Aug 15, 2021
6fddf4f
fixed sell issues
NikczemnyOkularnik Aug 15, 2021
fcbd1c3
Adding ending of the game
Kasia9311 Aug 15, 2021
cc6e5d3
Merging
Kasia9311 Aug 15, 2021
d429b2e
Fixes and adding ending of the game
Kasia9311 Aug 15, 2021
a780c93
end game works
NikczemnyOkularnik Aug 15, 2021
6f60192
Organizing the menu
NikczemnyOkularnik Aug 15, 2021
c3d1db1
Menu methods isPlayerChoiceValid, menuChoice, printMenu are const
kondorski Sep 5, 2021
1901176
Storable - constructor definition moved to cpp file
kondorski Sep 5, 2021
939c494
Removed observer attachement from one of the constructors
kondorski Sep 5, 2021
9e6b2d4
removed empty method
kondorski Sep 5, 2021
c08090c
Time::notify - value passed by reference
kondorski Sep 5, 2021
113b96d
Time::notify added reference to elements in loop
kondorski Sep 5, 2021
68939f4
Ship::addCargo - vector appended with push_back instead of emplace_back
kondorski Sep 6, 2021
6e5d003
Ship::removeCargo - const added to shipCargoAmount and iterator
kondorski Sep 6, 2021
e150a0e
Adding input validation
Kasia9311 Sep 7, 2021
cf1bed7
Changing if's for else if's
Kasia9311 Sep 7, 2021
d4fc6e0
Changing do while to while loop
Kasia9311 Sep 7, 2021
681f9f0
Adding checking if input is digit
Kasia9311 Sep 7, 2021
08f0381
Adding const to calculating distance
Kasia9311 Sep 7, 2021
f027a90
Removed ugly dynamic cast, created Clone()
NikczemnyOkularnik Oct 28, 2021
d49c0f7
warning fixes
Crazyayane07 Oct 30, 2021
3d6969e
Explosion of format fixed
NikczemnyOkularnik Nov 3, 2021
f084cac
gtest added
NikczemnyOkularnik Nov 13, 2021
3e5ebad
Testing
NikczemnyOkularnik Nov 13, 2021
adf5dad
Memory leak in storeCargo and shipCargo fixed
NikczemnyOkularnik Jan 15, 2022
4e32374
Deleted comments in destructors
NikczemnyOkularnik Jan 15, 2022
6b97b91
Merging after memory leak fix
NikczemnyOkularnik Jan 15, 2022
80b756a
delete gtest
NikczemnyOkularnik Jan 15, 2022
9e22538
Tests in catch2
NikczemnyOkularnik Jan 15, 2022
005163c
added tests
NikczemnyOkularnik Jan 15, 2022
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
41 changes: 41 additions & 0 deletions shm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.16)
project(shm)
set(testFile ../test/test.cpp ../test/catch_amalgamated.cpp)

set(SRC
alcohol.cpp
cargo.cpp
coordinates.cpp
fruit.cpp
Game.cpp
island.cpp
item.cpp
Map.cpp
Menu.cpp
player.cpp
ship.cpp
Storable.cpp
Store.cpp
Time.cpp)

set(FLAGS -Wall -Wextra -Werror -Wconversion -O3)

add_library(${PROJECT_NAME}-lib STATIC ${SRC})
add_executable(${PROJECT_NAME} main.cpp ${SRC})
add_executable(${PROJECT_NAME}-ut ${testFile} ${SRC})

target_link_libraries(${PROJECT_NAME} ${PROJECT_NAME}-lib)
target_link_libraries(${PROJECT_NAME}-ut ${PROJECT_NAME}-lib)

set_target_properties(${PROJECT_NAME} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)
set_target_properties(${PROJECT_NAME}-lib PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)
set_target_properties(${PROJECT_NAME}-ut PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON)



190 changes: 190 additions & 0 deletions shm/Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#include "Game.hpp"
#include <limits>

class Map;

Game::Game(size_t money, size_t gameDays, size_t finalGoal)
: time_(new Time()),
money_(money),
gameDays_(gameDays),
finalGoal_(finalGoal),
menu_(std::make_unique<Menu>(this)),
map_(new Map(time_)),
playerOne_(new Player(std::make_unique<Ship>(20, 30, 10, "Dar Pomorza", 3, time_), 1000, 1000))
{}

Game::Game(){}

Game::~Game()
{
//std::for_each(currentStore_.storeCargo.begin(), currentStore_.storeCargo.end(),[](Cargo* n){delete n;});
delete time_;
}

void Game::startGame()
{
printTitle();
setPlayer();

while (true)
{
if (playerOne_->getMoney() >= 2000)
{
endGameWin();
quitRequested();
}
else if (playerOne_->getMoney() < 0 || time_->getCurrentTime() >= gameDays_)
{
endGameLose();
quitRequested();
}
if (quitRequest)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR e150a0e

{
delete map_;
delete playerOne_;
break;
}
menu_->printMenu();
menu_->playerChoice();
}
}

void Game::printTitle()
{
std::cout << "Project SHM" << '\n';
}

void Game::setPlayer()
{
std::cout << "Set your name captain!:" << '\n';
std::string playerName;
std::cin >> playerName;
playerOne_->setName(playerName);
setStartingCargo();
std::cout << "Welcome on board captain " << playerOne_->getName() << '\n';
map_->changeCurrentPosition(&map_->islands_.at(0));
currentStore_ = map_->islands_.at(0).returnIslandStore();
std::cout << "Your's ship " << playerOne_->getShip()->getName() << " is waiting! Good Luck!" << '\n';
std::cout << "You are in start point. ";
map_->PrintCurrentPosition();
std::cout << "You have " << playerOne_->getMoney() << " gold in Your treasure chest!" << '\n';
std::cout << "Choose Your next move!" << '\n';
}

void Game::printMap(Map &map)

{
map.PrintCurrentPosition();
map.DebugPrintIsland();
}

void Game::travel()

{
auto i = 0;
printMap(*map_);
std::cout << "Choose Your destination captain!" << '\n';
std::cin >> i;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What when someone provides the wrong input? Like "Ala ma kota"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 681f9f0

if (i < (int)map_->islands_.size() && i >= 0)
{
auto travelTime = map_->calculateDistance(map_->islands_.at(i)) / playerOne_->getSpeed();
std::cout << "Your travel will take " << travelTime << " days." << '\n';
map_->changeCurrentPosition(&map_->islands_.at(i));
map_->PrintCurrentPosition();
time_->changeTime(travelTime);
currentStore_ = map_->islands_.at(i).returnIslandStore();
}
else
{
std::cout << "There is no such island on the map! Choose another destination!" << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
Comment on lines +100 to +101
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is repeated few times, maybe create function clearInputStream()?
DRY rule -> don't repeat yourself :)

Game::travel();
}
}

void Game::printPlayerCargo()
{
playerOne_->getShip()->printShipCargo();
}

void Game::setStartingCargo()
{
playerOne_->getShip()->shipCargo.push_back(new Fruit("Banany", 1, 40, time_, 5, 0));
playerOne_->getShip()->shipCargo.push_back(new Fruit("Apple", 1, 14, time_, 20, 0));
playerOne_->getShip()->shipCargo.push_back(new Alcohol("Rum", 1, 60, time_, 70));
playerOne_->getShip()->shipCargo.push_back(new Item("Hook", 1, 100, time_, Rarity::common));
}

void Game::quitRequested()
{
quitRequest = true;
}

void Game::displayPlayerStats()
{
std::cout << "(Little reminder)" << '\n';
std::cout << "Available gold: " << playerOne_->getMoney() << '\n';
std::cout << "Your position on the map: \n";
map_->PrintCurrentPosition();
std::cout << "Day " << time_->getCurrentTime() << "/" << gameDays_ << '\n';
std::cout << "==========================\n";
}

void Game::printStoreCargo()
{
std::cout << "This store contains: " << '\n';
currentStore_->printStoreCargo();
}

void Game::buyCargo()
{
int cargoElement = 0;
size_t amount = 0;
std::cout << "Choose cargo: ";
std::cin >> cargoElement;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validate input

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 6e5d003

if (cargoElement >= 0 && cargoElement < (int)currentStore_->storeCargo.size())
{
std::cout << "Choose amount: ";
std::cin >> amount;
currentStore_->buy(currentStore_->storeCargo.at(cargoElement), amount, playerOne_);
}
else
{
std::cout << "There is no such cargo in this store." << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
buyCargo();
}
}

void Game::sellCargo()
{
int cargoElement = 0;
size_t amount = 0;
std::cout << "Choose cargo: ";
std::cin >> cargoElement;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR 6e5d003

if (cargoElement >= 0 && cargoElement < (int)playerOne_->getShip()->shipCargo.size())
{
std::cout << "Choose amount: ";
std::cin >> amount;
currentStore_->sell(playerOne_->getCargo(cargoElement), amount, playerOne_);
}
else
{
std::cout << "There is no such cargo in this ship." << '\n';
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
sellCargo();
}
}

void Game::endGameLose()
{
std::cout << "You're time has come Captain! This is sad end!" << '\n';
}
void Game::endGameWin()

{
std::cout << "Arrr you won" << '\n';
}
41 changes: 41 additions & 0 deletions shm/Game.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include <iostream>
#include <memory>
#include "Map.hpp"
#include "Menu.hpp"
#include "player.hpp"
#include "Time.hpp"
#include "coordinates.hpp"
class Game
{
public:
Game(size_t money, size_t gameDays, size_t finalGoal);
Game();
~Game();

void displayPlayerStats();
void setPlayer();
void startGame();
void printTitle();
void printMap(Map &);
void travel();
void buyCargo();
void sellCargo();
void setStartingCargo();
void printPlayerCargo();
void quitRequested();
void printStoreCargo();
void endGameWin();
void endGameLose();

private:
Time* time_;
size_t money_;
size_t gameDays_;
size_t finalGoal_;
bool quitRequest = false;
std::unique_ptr<Menu> menu_;
Map* map_;
Store* currentStore_;
Player* playerOne_;
};
86 changes: 86 additions & 0 deletions shm/Map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include <algorithm>
#include <iostream>
#include <random>
#include <utility>
#include <iterator>
#include <math.h>
#include "coordinates.hpp"
#include "island.hpp"
#include "Map.hpp"

namespace constVariables
{

constexpr size_t COORDINATE_MIN = 0;
constexpr size_t COORDINATE_MAX = 99;
constexpr size_t ISLANDS_COUNT = 10;

} // namespace constVariables

Map::Map(Time *time)
{
SetUpRandomIsland(time);
}

void Map::SetUpRandomIsland(Time *time)
{
islands_.reserve(constVariables::ISLANDS_COUNT);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> distrib(constVariables::COORDINATE_MIN, constVariables::COORDINATE_MAX);
std::vector<Coordinates> cords;
cords.reserve(constVariables::ISLANDS_COUNT);
for (int i = 0; i < (int)constVariables::ISLANDS_COUNT; i++)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better size_t i = 0 and don't cast to int

{
int x = (int)distrib(gen);
int y = (int)distrib(gen);
Comment on lines +35 to +36
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid old (C) style casting. Because it unsafe

Coordinates c(x, y);
while (contains(cords, c))
{
x = (int)distrib(gen);
y = (int)distrib(gen);
Comment on lines +40 to +41
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid old (C) style casting

c.SetPositionX(x);
c.SetPositionY(y);
}

islands_.emplace_back(Island(c, time));
cords.push_back(c);
}
}

void Map::DebugPrintIsland()
{
int j = 0;
for (auto &el : islands_)
{
std::cout << j << " | " << std::to_string(el.getPosition().GetPositionX()) << " | " << std::to_string(el.getPosition().GetPositionY()) << '\n';

j++;
}
}

void Map::PrintCurrentPosition()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cont method

{
std::cout << "Current coordinates: " << std::to_string(current_pos_->getPosition().GetPositionX()) << " | " << std::to_string(current_pos_->getPosition().GetPositionY()) << '\n';
}

bool Map::contains(const std::vector<Coordinates> &vec, const Coordinates &c)
{
return std::find(vec.begin(), vec.end(), c) != vec.end();
}

size_t Map::calculateDistance(Island island_pos_)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like const method

{
const auto distanceX = current_pos_->getPosition().GetPositionX();
const auto distanceY = current_pos_->getPosition().GetPositionY();
const auto disX = island_pos_.getPosition().GetPositionX();
const auto disY = island_pos_.getPosition().GetPositionY();
const auto distance = sqrt(pow((disX - distanceX), 2) + pow((disY - distanceY), 2));
std::cout << "Distance: " << distance << '\n';
return (size_t)distance;
}

void Map::addIsland(Coordinates &coordinate, Time *time)
{
islands_.push_back(Island(coordinate, time));
}
27 changes: 27 additions & 0 deletions shm/Map.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include "island.hpp"
#include <vector>
#include "Time.hpp"

//class Island;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

class Coordinates;

//Class responsible for representing map in game
class Map {
public:
Map(Time * time);
void DebugPrintIsland();
void PrintCurrentPosition();
friend class Game;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't mix function with friend declaration

Island* getCurrentPos() {return current_pos_;}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cont method

void changeCurrentPosition(Island* position) {current_pos_ = position;}
size_t calculateDistance(Island);
Island *getIsland(const Coordinates &);
void addIsland(Coordinates &, Time *);

private:
Island *current_pos_;
std::vector<Island> islands_;
bool contains(const std::vector<Coordinates>&, const Coordinates&);
void SetUpRandomIsland(Time *);
};
Loading