diff --git a/dungeonWorld/LICENSE b/dungeonWorld/LICENSE new file mode 100644 index 0000000..af8a4aa --- /dev/null +++ b/dungeonWorld/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 PolishPigeon + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/dungeonWorld/a.out b/dungeonWorld/a.out new file mode 100755 index 0000000..cca96f2 Binary files /dev/null and b/dungeonWorld/a.out differ diff --git a/dungeonWorld/dungeonWorld.cpp b/dungeonWorld/dungeonWorld.cpp new file mode 100644 index 0000000..0e99256 --- /dev/null +++ b/dungeonWorld/dungeonWorld.cpp @@ -0,0 +1,258 @@ +#include +#include +#include +#include +const int STAT_LENGTH = 3; +const int NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD = 6; +const int NUMBER_OF_STATS_IN_DUNGEON_WORLD = 5; +const char STAT_SEPARATOR = ' '; + +const int STR_POSITION = 0; +const int DEX_POSITION = 1; +const int CON_POSITION = 2; +const int INT_POSITION = 3; +const int WIS_POSITION = 4; +const int CHA_POSITION = 5; + +const int HP_POSITION = 0; +const int ARMOR_POSITION = 1; +const int LVL_POSITION = 2; +const int XP_POSITION = 3; +const int XPNEEDED_POSITION = 4; + +const int QUIT = 0; +const int CHANGE_HP_CODE = 1; +const std::string MENU = "What do you want to do? \n 0. QUIT \n 1. Change HP"; + + + +void print(const std::string s) +{ + std::cout << s << std::endl; +} + +void printNoEndline(const std::string s) +{ + std::cout << s << " "; +} + +void printInt(const int i) +{ + std::cout << i << std::endl; +} + +void printIntInfo(const std::string s, const int i) +{ + printNoEndline(s); + printInt(i); +} + +void showHP(const int hp) +{ + printIntInfo("Current HP:", hp); +} + +std::string enterString() +{ + std::string s; + std::cin >> s; + return s; +} + +std::string printAndEnterString(const std::string s) +{ + print(s); + return enterString(); +} + +void printStringVector(const std::vector v) +{ + for(unsigned int i = 0; i < v.size(); i++) print(v[i]); +} + +bool isNumber(const char c) +{ + return (c >= '0' && c <= '9'); +} + +int changeHpLogic(const std::string changeS) +{ + char c = changeS[0]; + if(!isNumber(c)) + { + if(c == '+') return std::stoi(changeS); + else return -std::stoi(changeS.substr(1, changeS.length())); + }else return -std::stoi(changeS); + +} + +int changeHp(int hp) +{ + return hp + changeHpLogic(printAndEnterString("Enter hp change: ('+' for positive, '' or '-' for negative):")); +} + +int calculateHp(int constitution) { return constitution + 8; } + +std::vector fileToVector(std::ifstream &file) +{ + std::string line; + std::vector strings; + if(file.is_open()) + { + while(getline(file, line)) + { + strings.push_back(line); + } + file.close(); + } + return strings; +} + +void vectorToFile(const std::vector strings, std::ofstream &file) +{ + for(unsigned int i = 0; i < strings.size(); i++) + { + file << strings.at(i) << std::endl; + } +} + +std::vector > stringToAttributes(const std::vector stats) +{ + std::vector > statsAndValues(NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD); + for(int i = 0; i < NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD; i++) + { + + std::string currentString = stats[i]; + statsAndValues[i].first = currentString.substr(0, STAT_LENGTH); + std::size_t findSpace = currentString.find(STAT_SEPARATOR); + statsAndValues[i].second = std::stoi(currentString.substr(findSpace + 1)); + } + return statsAndValues; +} + +std::vector > stringToStats(const std::vector stats) +{ + std::vector > statsAndValues(NUMBER_OF_STATS_IN_DUNGEON_WORLD); + + for(int i = NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD; i < NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD + NUMBER_OF_STATS_IN_DUNGEON_WORLD; i++) + { + + std::string currentString = stats[i]; + std::size_t findSpace = currentString.find(STAT_SEPARATOR); + statsAndValues[i - NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD].first = currentString.substr(0, findSpace); + statsAndValues[i - NUMBER_OF_ATTRIBUTES_IN_DUNGEON_WORLD].second = std::stoi(currentString.substr(findSpace + 1)); + } + return statsAndValues; +} + +void printPair(const std::pair pair) +{ + printIntInfo(pair.first, pair.second); +} + +void printPairVector(const std::vector > v) +{ + for(unsigned int i = 0; i < v.size(); i++) printPair(v[i]); +} + +std::vector > attributesAndStatsTogether(const std::vector > attributesNumbers, const std::vector > statsNumbers) +{ + std::vector > v; + v.reserve( attributesNumbers.size() + statsNumbers.size() ); + v.insert( v.end(), attributesNumbers.begin(), attributesNumbers.end() ); + v.insert( v.end(), statsNumbers.begin(), statsNumbers.end() ); + return v; +} + +std::vector pairToStringVector(const std::vector > v) +{ + std::vector stringVector; + stringVector.reserve(v.size()); + for(unsigned int i = 0; i < v.size(); i++) + { + stringVector.push_back(v[i].first + ' ' + std::to_string(v[i].second)); + } + return stringVector; +} + +int charToInt(const char c) { return c - '0'; } + +bool charIsNumber(const char c) { return c >= '0' && c <= '9'; } + +void printNotValidStringLength(const std::string s, const long unsigned int desiredLength) +{ + std::cout << "String: \"" << s << "\" is too short/too long, it is: " + << s.length() << " characters long but should be: " << desiredLength + << " characters long " << std::endl; +} + +bool validStringLength(const std::string s, const long unsigned int desiredLength) +{ + if(s.length() != desiredLength) + { + printNotValidStringLength(s, desiredLength); + return 0; + } + return 1; +} + +bool checkMenu(const std::string input) +{ + if(!validStringLength(input, 1)) return 0; + if(!charIsNumber(input.at(0))) return 0; + return 1; +} + +void printAttributesAndStats(const std::vector > attributesNumbers, const std::vector > statsNumbers) +{ + printPairVector(attributesNumbers); + std::cout << std::endl; + printPairVector(statsNumbers); +} + +int inputMenu(const std::vector > attributesNumbers, const std::vector > statsNumbers) +{ + std::string choiceS; + do{ + std::system("clear"); + printAttributesAndStats(attributesNumbers, statsNumbers); + print(MENU); + getline(std::cin, choiceS); + }while(!checkMenu(choiceS)); + return charToInt(choiceS.at(0));; +} + +bool menuLoop(std::vector > attributesNumbers, std::vector > &statsNumbers) +{ + int userChoice = inputMenu(attributesNumbers, statsNumbers); + if(userChoice == QUIT) return 1; + else if(userChoice == CHANGE_HP_CODE) + { + statsNumbers[HP_POSITION].second = changeHp(statsNumbers[HP_POSITION].second); + } + return 0; +} + + +int main() +{ + std::ifstream InputstatsFile ("stats.txt"); + + std::vector stats = fileToVector(InputstatsFile); + InputstatsFile.close(); + std::vector > attributesNumbers = stringToAttributes(stats); + std::vector > statsNumbers = stringToStats(stats); + + bool end = 0; + while(!end) { end = menuLoop(attributesNumbers, statsNumbers); }; + + + std::vector > v = attributesAndStatsTogether(attributesNumbers, statsNumbers); + std::vector newStats = pairToStringVector(v); + std::ofstream OutputstatsFile ("stats.txt"); + vectorToFile(newStats, OutputstatsFile); + OutputstatsFile.close(); + + + return 0; +} diff --git a/dungeonWorld/makefile b/dungeonWorld/makefile new file mode 100644 index 0000000..10c4287 --- /dev/null +++ b/dungeonWorld/makefile @@ -0,0 +1,2 @@ +dungeonWorld: + g++ dungeonWorld.cpp -Wextra -Wall -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wwrite-strings -Wcast-qual -Wswitch-default -Wswitch-enum -Wconversion -Wunreachable-code -pedantic diff --git a/dungeonWorld/stats.txt b/dungeonWorld/stats.txt new file mode 100644 index 0000000..c2a2e24 --- /dev/null +++ b/dungeonWorld/stats.txt @@ -0,0 +1,11 @@ +STR 9 +DEX 8 +CON 16 +INT 13 +WIS 17 +CHA 12 +HP 1 +ARMOR 1 +LVL 3 +XP 2 +XPNEEDED 10