-
Notifications
You must be signed in to change notification settings - Fork 88
SHM part 2 #105
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 part 2 #105
Changes from all commits
b358851
c5340c8
220bf13
44b8e81
124ad57
5904834
1e0942d
65a6e33
f94483f
814c82e
4d803d1
80f9fc0
57d4b21
cff4651
140899b
7392348
187f30e
c62d0cc
453dea1
7eafa61
193c1b2
a099870
3c0dd7d
8aee177
47df11f
8d987b8
5070b12
5c09851
7fec7d3
f1965b3
ec03282
17d1ad6
40d9448
711f112
65e4ea0
b0d2e51
696a99a
6df4544
2099da7
44ca58b
373c6bd
3c96853
fe4f5ac
5593b20
0d154d2
a0ce807
aa91d5a
d321d3a
90f6010
5257ba6
08564da
0a43340
c1a310c
f890a34
a441404
7a05ac4
ddac1bc
a5ce88e
5c0af0d
2b9b0c8
5da6d6e
a782114
ca3e0cf
ba49fa6
fed4d2c
71371d2
57107ed
5ab23a3
46fecab
0a11444
33f8086
2afbb7b
f1b55f3
9676a03
0ccb59e
fee2dae
b88fa98
f8975d3
7eb2b2d
b31c912
ff179b4
fd93d48
393628a
7080968
ca2ae4d
fd60baf
cb7cacd
31395e8
e50459f
8aa5cd3
b6746f0
fa948fd
b39c214
4072f66
734a84b
7d5d411
32d054b
62debca
4e8ded2
02fb5e4
970c55f
a341acd
d2ec501
8faed3d
4c0368c
835dedf
848f532
1d4dfe9
8f6e059
a0d9078
0f64976
b969857
cf5bc6b
910edc5
7f6c90e
7aec38b
772c9ef
0217ca4
c552906
5b8a807
1a8d780
8b966e4
f96f5e0
a4a5594
4f4828c
caf391c
d246d74
975fe21
c26d6fa
164021a
a3f0a77
a674ee9
5ece995
cf7e88a
c2f2b9f
2f8f119
489e2a0
3916c4d
1459625
613c8d7
68ed808
3b18f75
4ba8bcd
3c1e86e
340f996
a1a4f0a
cf8801e
6fddf4f
fcbd1c3
cc6e5d3
d429b2e
a780c93
6f60192
c3d1db1
1901176
939c494
9e6b2d4
c08090c
113b96d
68939f4
6e5d003
e150a0e
cf1bed7
d4fc6e0
681f9f0
08f0381
f027a90
d49c0f7
3d6969e
f084cac
3e5ebad
adf5dad
4e32374
6b97b91
80b756a
9e22538
005163c
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,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) | ||
|
|
||
|
|
||
|
|
| 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) | ||
| { | ||
| 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; | ||
|
Collaborator
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. What when someone provides the wrong input? Like "Ala ma kota" 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. 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
Collaborator
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 repeated few times, maybe create function |
||
| 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; | ||
|
Collaborator
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. validate input 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. 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; | ||
|
Collaborator
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 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. 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'; | ||
| } | ||
| 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_; | ||
| }; |
| 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++) | ||
|
Collaborator
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. 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
Collaborator
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. 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
Collaborator
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. 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() | ||
|
Collaborator
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. 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_) | ||
|
Collaborator
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. 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)); | ||
| } | ||
| 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; | ||
|
Collaborator
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. ? |
||
| class Coordinates; | ||
|
|
||
| //Class responsible for representing map in game | ||
| class Map { | ||
| public: | ||
| Map(Time * time); | ||
| void DebugPrintIsland(); | ||
| void PrintCurrentPosition(); | ||
| friend class Game; | ||
|
Collaborator
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. don't mix function with friend declaration |
||
| Island* getCurrentPos() {return current_pos_;} | ||
|
Collaborator
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. 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 *); | ||
| }; | ||
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.
else if
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.
PR e150a0e