Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdlib>
  4. #include <string>
  5. #include <sstream>
  6. #include <iterator>
  7. #include <algorithm>
  8. #include <locale>
  9. /*
  10. Name: Josh Marzic
  11. Date: 4/16/2017
  12. Project: Final Project, RPG text based game
  13.  
  14. */
  15. using namespace std;
  16.  
  17. /*
  18. Player class that has HP/ARMOR/GOLD/Positions/etc.
  19. */
  20. class player
  21. {
  22. public:
  23.     player();             //default constructor
  24.     player(int);        //sets race
  25.     void setGold(int);  //mutator function
  26.     void setHP(int);
  27.     void setArmor(int);
  28.     void setSTR(int);
  29.     void setPositionX(int);
  30.     void setPositionY(int);
  31.     void setPositionZ(int);
  32.     int getGold();      //accessor functions
  33.     int getHP();        //gets players hitpoints
  34.     int getArmor();     //gets players armor value
  35.     int getSTR();       //gets players strength(to calculate dmg)
  36.     int getPositionX();
  37.     int getPositionY();
  38.     int getPositionZ();
  39.     void calcHP(int d); //to calc the players hp (calculating armor / etc.)
  40.  
  41.  
  42. private:
  43.     int gold;
  44.     int hp;
  45.     int armor;
  46.     int str;
  47.     int positionX;
  48.     int positionY;
  49.     int positionZ;
  50.     int race;   //impliment elf/dwarf/etc someday?
  51. };
  52.  
  53. player::player()  //make player object(constructor)
  54. {
  55.     //players starting gold / hp values
  56.     gold = 0;
  57.     hp = 100;
  58.     armor = 0;
  59.     str = 0;
  60.     race = 1;
  61.  
  62.     //set player at the start of game(middle of first level)
  63.     positionX = 2;
  64.     positionY = 0;
  65.     positionZ = 2;
  66. }
  67.  
  68. player::player(int a)  //start player with more gold, if needed
  69. {
  70.     //players starting gold / hp values
  71.     gold = a;
  72.     hp = 100;
  73.     str = 0;
  74.     armor = 0;
  75.     race = 1;
  76.  
  77.     //set player at the start of game(middle of first level)
  78.     positionX = 2;
  79.     positionY = 0;
  80.     positionZ = 2;
  81. }
  82.  
  83.  
  84.  
  85. void player::setGold(int g)
  86. {
  87.     gold = gold + g;
  88. }
  89.  
  90. void player::setHP(int h)
  91. {
  92.     hp = h;
  93. }
  94.  
  95. void player::setArmor(int a)
  96. {
  97.     armor = a;
  98. }
  99. void player::setSTR(int s)
  100. {
  101.     s = str;
  102. }
  103. void player::setPositionX(int x)
  104. {
  105.     positionX = x + positionX;
  106. }
  107.  
  108. void player::setPositionY(int x)
  109. {
  110.     positionY = x + positionY;
  111. }
  112. void player::setPositionZ(int x)
  113. {
  114.     positionZ = x + positionZ;
  115. }
  116.  
  117. int player::getGold()
  118. {
  119.     return gold;
  120. }
  121.  
  122. int player::getHP()
  123. {
  124.     return hp;
  125. }
  126.  
  127. int player::getArmor()
  128. {
  129.     return armor;
  130. }
  131.  
  132. int player::getSTR()
  133. {
  134.     return str;
  135. }
  136.  
  137. int player::getPositionX()
  138. {
  139.     return positionX;
  140. }
  141.  
  142. int player::getPositionY()
  143. {
  144.     return positionY;
  145. }
  146.  
  147. int player::getPositionZ()
  148. {
  149.     return positionZ;
  150. }
  151.  
  152. void player::calcHP(int d)
  153. {
  154.     hp = hp - (d - armor);
  155. }
  156.  
  157.  
  158. //holds information on each room within the dungeon(public)
  159. /*struct level {
  160.  
  161.     int id;
  162.  
  163.     //booleans to hold if there is a doorway(so they can pass through that direction)
  164.     bool north;
  165.     bool east;
  166.     bool south;
  167.     bool west;
  168.     bool up; //bool to hold if there is a ladder(so they can go up )
  169.     bool down; //bool to hold if there is stairs / ladder to go back down
  170.  
  171.     //bool for random events(monster/gold)
  172.     bool monster;
  173.     bool gold;
  174.  
  175.  
  176. }; */
  177.  
  178. //public player object, and level
  179. //void buildFloor(struct level main[5][3][5]);
  180. bool checkValidMove(player &player1, char d);
  181. void combat(player &player1);
  182. void roomEvent(player &player1);
  183. void playerInput(player &player1);
  184.    
  185. int main()
  186. {
  187.     player player1;
  188.     int position = 0;
  189.     int position = player1.getPositionY;
  190.    
  191.  
  192.     while (position != 2) {
  193.         playerInput(player1);
  194.         int position = player1.getPositionY;
  195.     }
  196.    
  197.     system("pause");
  198.     return 0;
  199. }
  200.  
  201.  
  202. void playerInput(player &player1) {
  203.     string input = "";
  204.     string first, second, third, fourth, fifth, sixth; //allows up to 6 word input from user
  205.  
  206.     //get input
  207.     cout << "What would you like to do now?" << endl;
  208.     getline(cin, input);
  209.  
  210.     //break intput down
  211.     stringstream input_stringstream(input);     //moves input into a string stream
  212.     getline(input_stringstream, first, ' ');    //moves each word of input into its own string, using ' ' as a delimetor
  213.     getline(input_stringstream, second, ' ');
  214.     getline(input_stringstream, third, ' ');
  215.     getline(input_stringstream, fourth, ' ');
  216.     getline(input_stringstream, fifth, ' ');
  217.     getline(input_stringstream, sixth, ' ');
  218.  
  219.     locale loc;
  220.     //change all to lower case (source: http://www.cplusplus.com/reference/locale/tolower/ )
  221.     for (std::string::size_type i = 0; i<first.length(); ++i)
  222.         first[i] = tolower(first[i], loc);
  223.     for (std::string::size_type i = 0; i<second.length(); ++i)
  224.         second[i] = tolower(second[i], loc);
  225.     for (std::string::size_type i = 0; i<third.length(); ++i)
  226.         third[i] = tolower(third[i], loc);
  227.     for (std::string::size_type i = 0; i<fourth.length(); ++i)
  228.         fourth[i] = tolower(fourth[i], loc);
  229.     for (std::string::size_type i = 0; i<fifth.length(); ++i)
  230.         fifth[i] = tolower(fifth[i], loc);
  231.     for (std::string::size_type i = 0; i<sixth.length(); ++i)
  232.         sixth[i] = tolower(sixth[i], loc);
  233.  
  234.     //analyze input for keywords, then do w/e the word should be doing
  235.  
  236.     if (first == "move" || second == "move" || third == "move" || fourth == "move" || fifth == "move" || sixth == "move") {
  237.         //MOVE
  238.         //
  239.         //MOVE EAST
  240.         if (first == "east" || second == "east" || third == "east" || fourth == "east" || fifth == "east" || sixth == "east") {
  241.             if (checkValidMove(player1, 'e')) { //validate they can move there
  242.                 player1.setPositionX(1);    //if they can move there change their position
  243.                 cout << "You moved east one position, you're now in a new room"; //let them know they're now in a new room
  244.                 roomEvent(player1); //checks to see if that is a position that has a monster / gold
  245.             }
  246.             else {
  247.                 cout << "You cant move that way!"; //if the move isn't valid display that
  248.                 playerInput(player1); //restart this function to get a new input
  249.             }
  250.         }
  251.  
  252.         //MOVE WEST
  253.         if (first == "west" || second == "west" || third == "west" || fourth == "west" || fifth == "west" || sixth == "west") {
  254.             if (checkValidMove(player1, 'w')) { //validate they can move there
  255.                 player1.setPositionX(-1);   //if they can move there change their position
  256.                 cout << "You moved west one position, you're now in a new room"; //let them know they're now in a new room
  257.             }
  258.             else {
  259.                 cout << "You cant move that way!"; //if the move isn't valid display that
  260.                 playerInput(player1); //restart this function to get a new input
  261.             }
  262.         }
  263.  
  264.         //MOVE NORTH
  265.         if (first == "north" || second == "north" || third == "north" || fourth == "north" || fifth == "north" || sixth == "north") {
  266.             if (checkValidMove(player1, 'n')) { //validate they can move there
  267.                 player1.setPositionZ(1);    //if they can move there change their position
  268.                 cout << "You moved North one position, you're now in a new room"; //let them know they're now in a new room
  269.             }
  270.             else {
  271.                 cout << "You cant move that way!"; //if the move isn't valid display that
  272.                 playerInput(player1); //restart this function to get a new input
  273.             }
  274.         }
  275.  
  276.         //MOVE SOUTH
  277.         if (first == "south" || second == "south" || third == "south" || fourth == "south" || fifth == "south" || sixth == "south") {
  278.             if (checkValidMove(player1, 's')) { //validate they can move there
  279.                 player1.setPositionZ(-1);   //if they can move there change their position
  280.                 cout << "You moved South one position, you're now in a new room"; //let them know they're now in a new room
  281.             }
  282.             else {
  283.                 cout << "You cant move that way!"; //if the move isn't valid display that
  284.                 playerInput(player1); //restart this function to get a new input
  285.             }
  286.         }
  287.         //MOVE UP
  288.         if (first == "up" || second == "up" || third == "up" || fourth == "up" || fifth == "up" || sixth == "up") {
  289.             if (checkValidMove(player1, 'u')) { //validate they can move there
  290.                 player1.setPositionY(1);    //if they can move there change their position
  291.                 cout << "You moved up one position, you're now in a new room(next level!)"; //let them know they're now in a new room
  292.             }
  293.             else {
  294.                 cout << "You cant move that way!"; //if the move isn't valid display that
  295.                 playerInput(player1); //restart this function to get a new input
  296.             }
  297.         }
  298.  
  299.     }
  300.     if (first == "look" || second == "look" || third == "look" || fourth == "look" || fifth == "look" || sixth == "look") {
  301.         //LOOK
  302.         //next - direction?
  303.     }
  304.     if (first == "run" || second == "run" || third == "run" || fourth == "run" || fifth == "run" || sixth == "run") {
  305.         //RUN
  306.         //returns player back to previous level?
  307.     }
  308.     if (first == "attack" || second == "attack" || third == "attack" || fourth == "attack" || fifth == "attack" || sixth == "attack") {
  309.         //attack
  310.         //attack monster -> make combat function?
  311.         //attack random object -> damage self ?
  312.     }
  313.     if (first == "dig" || second == "dig" || third == "dig" || fourth == "dig" || fifth == "dig" || sixth == "dig") {
  314.         //dig
  315.         //certain levels have treasure maybe? Random chance at treasure?
  316.     }
  317.     if (first == "inspect" || second == "inspect" || third == "inspect" || fourth == "inspect" || fifth == "inspect" || sixth == "inspect") {
  318.         //inspect
  319.         //displays information an object in sentence?
  320.     }
  321.     if (first == "help" || second == "help" || third == "help" || fourth == "help" || fifth == "help" || sixth == "help") {
  322.         //help menu
  323.         cout << "The following are words you may find helpful;" << endl;
  324.         cout << "Move(north/west/south/east/up/down), look, run, attack, dig, inspect, etc.";
  325.     }
  326.  
  327. }
  328. bool checkValidMove(player &player1, char d) {
  329.     //map - array
  330.     //player 1 - current position
  331.     //char d - direction they want to move
  332.  
  333.     if (d == 'e' || d == 'E') {
  334.         if (player1.getPositionX < 5) {
  335.             //checks to see if current player's position is within bounds, if so allows them to move
  336.             return true;
  337.         }
  338.     }
  339.     if (d == 's' || d == 'S') {
  340.         if (player1.getPositionZ > 0) {
  341.             //checks to see if current player's position is within bounds, if so allows them to move
  342.             return true;
  343.         }
  344.     }
  345.     if (d == 'w' || d == 'W') {
  346.         if (player1.getPositionX > 0) {
  347.             //checks to see if current player's position is within bounds, if so allows them to move
  348.             return true;
  349.         }
  350.     }
  351.     if (d == 'n' || d == 'N') {
  352.         if (player1.getPositionZ < 5) {
  353.             //checks to see if current player's position is within bounds, if so allows them to move
  354.             return true;
  355.         }
  356.     }
  357.     if (d == 'd' || d == 'D') {
  358.         if (player1.getPositionY >= 1) {
  359.             //checks to see if current player's position is within bounds, if so allows them to move
  360.             return true;
  361.         }
  362.     }
  363.     if (d == 'u' || d == 'U') {
  364.         if (player1.getPositionY == 0 && (player1.getPositionX == 0 && player1.getPositionZ == 0)) {
  365.             //checks to see if current player's position is within bounds, if so allows them to move
  366.             return true;
  367.         }
  368.         if (player1.getPositionY == 1 && (player1.getPositionX == 4 && player1.getPositionZ == 3)) {
  369.             return true;
  370.         }
  371.     }
  372.     return false;
  373.  
  374. }
  375. void roomEvent(player &player1) {
  376.    
  377.     //Allows the setup of gold / monster events, set position in this function
  378.  
  379.     //initialize combat / monster
  380.     if (player1.getPositionX == 1 && player1.getPositionZ == 2) {
  381.         //call combat function
  382.         combat( player1);
  383.     }
  384.     //grant gold
  385.     if (player1.getPositionX == 3 && (player1.getPositionZ == 2 || player1.getPositionZ == 0)) {
  386.         cout << "Congrats! You found some gold within this room! +100 gold! " << endl;
  387.         player1.setGold(100);
  388.     }
  389.  
  390.     playerInput(player1);
  391. }
  392.  
  393. void combat(player &player1) {
  394.     cout << "OH no! There is a monster in this room! It has already seen you and started to charge towards you. ";
  395.     if (player1.getPositionY == 0) {
  396.         //first floor monster combat
  397.         cout << "Luckily it is only a slime, you are fairly safe." << endl;
  398.         cout << "You take 3 points of damage." << endl;
  399.         player1.calcHP(3); //player takes 3 points of damage(could be mitagated by armor)
  400.     }
  401.     if (player1.getPositionY == 1) {
  402.         //second floor monster combat
  403.         cout << "Unfortunately its a minotaur you may wish to pray to a god at this point." << endl;
  404.         cout << "You take 10 points of damage." << endl;
  405.         player1.calcHP(10);
  406.     }
  407.     if (player1.getPositionY == 2) {
  408.         //third floor monster combat
  409.         cout << "Yay you made it to safety! ...Until a boss appeared out of nowhere! " << endl;
  410.         cout << "You take 50 points of damage." << endl;
  411.         player1.calcHP(50);
  412.     }
  413. }
  414. /*void buildFloor(struct level mainLevel[5][3][5]) {
  415.    
  416.     FUNCTION NOT WORKING, saving for later (struct + 3 dimensional array + function = bad mixture)
  417.     see - http://stackoverflow.com/questions/3613302/passing-array-of-structures-to-function-c for more info(to fix someday)
  418.  
  419.     //set all to false
  420.     for (int i = 0; i < 5; i++) {
  421.         for (int b = 0; b < 5; b++) {
  422.             for (int h = 0; h < 3; h++) {
  423.                 mainLevel[i][h][b].down = false;
  424.                 mainLevel[i][h][b].up = false;
  425.                 mainLevel[i][h][b].west = false;
  426.                 mainLevel[i][h][b].south = false;
  427.                 mainLevel[i][h][b].east = false;
  428.                 mainLevel[i][h][b].north = false;
  429.             }  
  430.         }
  431.     }
  432.     //create proper path which picks a corner and puts the way up / down there, then fills the next 2 levels above.
  433.     //-------------------
  434.    
  435.         mainLevel[0][0][0].up = true;
  436.         mainLevel[0][0][0].north = true;
  437.         mainLevel[0][0][1].south = true;
  438.         mainLevel[0][0][1].monster = true;
  439.         mainLevel[0][0][1].east = true;
  440.         mainLevel[1][0][1].west = true;
  441.         mainLevel[1][0][1].gold = true;
  442.         mainLevel[1][0][1].north = true;
  443.         mainLevel[1][0][2].south = true;
  444.         mainLevel[1][0][2].east = true;
  445.         mainLevel[2][0][2].west = true;
  446.         //next level up
  447.         mainLevel[0][1][0].down = true;
  448.         mainLevel[0][1][0].north = true;
  449.         mainLevel[0][1][1].south = true;
  450.         mainLevel[0][1][1].north = true;
  451.         mainLevel[0][1][2].south = true;
  452.         mainLevel[0][1][2].east = true;
  453.         mainLevel[1][1][2].west = true;
  454.         mainLevel[1][1][2].north = true;
  455.         mainLevel[1][1][3].south = true;
  456.         mainLevel[1][1][3].east = true;
  457.         mainLevel[2][1][3].west = true;
  458.         mainLevel[2][1][3].east = true;
  459.         mainLevel[3][1][3].west = true;
  460.         mainLevel[3][1][3].east = true;
  461.         mainLevel[4][1][3].west = true;
  462.         mainLevel[4][1][3].south = true;
  463.         mainLevel[4][1][2].north = true;
  464.         mainLevel[4][1][2].up = true;
  465.         //next level up
  466.         mainLevel[4][2][2].down = true; //ending map at this position for now
  467.         mainLevel[4][2][2].monster = true;
  468.  
  469.  
  470.  
  471.     //_________Possible randomization in the future? ___________
  472.     //int xRan;
  473.     //srand(time(0));
  474.     //xRan = rand() % 4 + 1; //creating a random number between 1 - 4
  475.     //if (xRan == 1) {
  476.         //fill level here
  477.     //}
  478.  
  479.     //add some extra turns(TO DO)
  480.     //--------------------
  481.  
  482. }
  483. */
  484.  
  485.  
  486.  
  487. /*
  488. TO DO LIST
  489. ===========
  490. A) Make player class -done
  491. B) Make 3 dimensional array to hold level values(possibly an array of a new data structure called level? ) - done, however it didnd't work with functions, scrapped
  492. C) Make a function to build the level(1 floor / build) - done, however it didn't work with structs, scrapped
  493. D) Take player input - done(needs more input options)
  494. E) Look at player input, decide their action(change position, fight mob, inspect room, etc. ) - done
  495. F) Make a save function that writes to a file
  496.  
  497.  
  498.  
  499.  
  500.  
  501. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement