Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- #include <string>
- #include <sstream>
- #include <iterator>
- #include <algorithm>
- #include <locale>
- /*
- Name: Josh Marzic
- Date: 4/16/2017
- Project: Final Project, RPG text based game
- */
- using namespace std;
- /*
- Player class that has HP/ARMOR/GOLD/Positions/etc.
- */
- class player
- {
- public:
- player(); //default constructor
- player(int); //sets race
- void setGold(int); //mutator function
- void setHP(int);
- void setArmor(int);
- void setSTR(int);
- void setPositionX(int);
- void setPositionY(int);
- void setPositionZ(int);
- int getGold(); //accessor functions
- int getHP(); //gets players hitpoints
- int getArmor(); //gets players armor value
- int getSTR(); //gets players strength(to calculate dmg)
- int getPositionX();
- int getPositionY();
- int getPositionZ();
- void calcHP(int d); //to calc the players hp (calculating armor / etc.)
- private:
- int gold;
- int hp;
- int armor;
- int str;
- int positionX;
- int positionY;
- int positionZ;
- int race; //impliment elf/dwarf/etc someday?
- };
- player::player() //make player object(constructor)
- {
- //players starting gold / hp values
- gold = 0;
- hp = 100;
- armor = 0;
- str = 0;
- race = 1;
- //set player at the start of game(middle of first level)
- positionX = 2;
- positionY = 0;
- positionZ = 2;
- }
- player::player(int a) //start player with more gold, if needed
- {
- //players starting gold / hp values
- gold = a;
- hp = 100;
- str = 0;
- armor = 0;
- race = 1;
- //set player at the start of game(middle of first level)
- positionX = 2;
- positionY = 0;
- positionZ = 2;
- }
- void player::setGold(int g)
- {
- gold = gold + g;
- }
- void player::setHP(int h)
- {
- hp = h;
- }
- void player::setArmor(int a)
- {
- armor = a;
- }
- void player::setSTR(int s)
- {
- s = str;
- }
- void player::setPositionX(int x)
- {
- positionX = x + positionX;
- }
- void player::setPositionY(int x)
- {
- positionY = x + positionY;
- }
- void player::setPositionZ(int x)
- {
- positionZ = x + positionZ;
- }
- int player::getGold()
- {
- return gold;
- }
- int player::getHP()
- {
- return hp;
- }
- int player::getArmor()
- {
- return armor;
- }
- int player::getSTR()
- {
- return str;
- }
- int player::getPositionX()
- {
- return positionX;
- }
- int player::getPositionY()
- {
- return positionY;
- }
- int player::getPositionZ()
- {
- return positionZ;
- }
- void player::calcHP(int d)
- {
- hp = hp - (d - armor);
- }
- //holds information on each room within the dungeon(public)
- /*struct level {
- int id;
- //booleans to hold if there is a doorway(so they can pass through that direction)
- bool north;
- bool east;
- bool south;
- bool west;
- bool up; //bool to hold if there is a ladder(so they can go up )
- bool down; //bool to hold if there is stairs / ladder to go back down
- //bool for random events(monster/gold)
- bool monster;
- bool gold;
- }; */
- //public player object, and level
- //void buildFloor(struct level main[5][3][5]);
- bool checkValidMove(player &player1, char d);
- void combat(player &player1);
- void roomEvent(player &player1);
- void playerInput(player &player1);
- int main()
- {
- player player1;
- int position = 0;
- int position = player1.getPositionY;
- while (position != 2) {
- playerInput(player1);
- int position = player1.getPositionY;
- }
- system("pause");
- return 0;
- }
- void playerInput(player &player1) {
- string input = "";
- string first, second, third, fourth, fifth, sixth; //allows up to 6 word input from user
- //get input
- cout << "What would you like to do now?" << endl;
- getline(cin, input);
- //break intput down
- stringstream input_stringstream(input); //moves input into a string stream
- getline(input_stringstream, first, ' '); //moves each word of input into its own string, using ' ' as a delimetor
- getline(input_stringstream, second, ' ');
- getline(input_stringstream, third, ' ');
- getline(input_stringstream, fourth, ' ');
- getline(input_stringstream, fifth, ' ');
- getline(input_stringstream, sixth, ' ');
- locale loc;
- //change all to lower case (source: http://www.cplusplus.com/reference/locale/tolower/ )
- for (std::string::size_type i = 0; i<first.length(); ++i)
- first[i] = tolower(first[i], loc);
- for (std::string::size_type i = 0; i<second.length(); ++i)
- second[i] = tolower(second[i], loc);
- for (std::string::size_type i = 0; i<third.length(); ++i)
- third[i] = tolower(third[i], loc);
- for (std::string::size_type i = 0; i<fourth.length(); ++i)
- fourth[i] = tolower(fourth[i], loc);
- for (std::string::size_type i = 0; i<fifth.length(); ++i)
- fifth[i] = tolower(fifth[i], loc);
- for (std::string::size_type i = 0; i<sixth.length(); ++i)
- sixth[i] = tolower(sixth[i], loc);
- //analyze input for keywords, then do w/e the word should be doing
- if (first == "move" || second == "move" || third == "move" || fourth == "move" || fifth == "move" || sixth == "move") {
- //MOVE
- //
- //MOVE EAST
- if (first == "east" || second == "east" || third == "east" || fourth == "east" || fifth == "east" || sixth == "east") {
- if (checkValidMove(player1, 'e')) { //validate they can move there
- player1.setPositionX(1); //if they can move there change their position
- cout << "You moved east one position, you're now in a new room"; //let them know they're now in a new room
- roomEvent(player1); //checks to see if that is a position that has a monster / gold
- }
- else {
- cout << "You cant move that way!"; //if the move isn't valid display that
- playerInput(player1); //restart this function to get a new input
- }
- }
- //MOVE WEST
- if (first == "west" || second == "west" || third == "west" || fourth == "west" || fifth == "west" || sixth == "west") {
- if (checkValidMove(player1, 'w')) { //validate they can move there
- player1.setPositionX(-1); //if they can move there change their position
- cout << "You moved west one position, you're now in a new room"; //let them know they're now in a new room
- }
- else {
- cout << "You cant move that way!"; //if the move isn't valid display that
- playerInput(player1); //restart this function to get a new input
- }
- }
- //MOVE NORTH
- if (first == "north" || second == "north" || third == "north" || fourth == "north" || fifth == "north" || sixth == "north") {
- if (checkValidMove(player1, 'n')) { //validate they can move there
- player1.setPositionZ(1); //if they can move there change their position
- cout << "You moved North one position, you're now in a new room"; //let them know they're now in a new room
- }
- else {
- cout << "You cant move that way!"; //if the move isn't valid display that
- playerInput(player1); //restart this function to get a new input
- }
- }
- //MOVE SOUTH
- if (first == "south" || second == "south" || third == "south" || fourth == "south" || fifth == "south" || sixth == "south") {
- if (checkValidMove(player1, 's')) { //validate they can move there
- player1.setPositionZ(-1); //if they can move there change their position
- cout << "You moved South one position, you're now in a new room"; //let them know they're now in a new room
- }
- else {
- cout << "You cant move that way!"; //if the move isn't valid display that
- playerInput(player1); //restart this function to get a new input
- }
- }
- //MOVE UP
- if (first == "up" || second == "up" || third == "up" || fourth == "up" || fifth == "up" || sixth == "up") {
- if (checkValidMove(player1, 'u')) { //validate they can move there
- player1.setPositionY(1); //if they can move there change their position
- 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
- }
- else {
- cout << "You cant move that way!"; //if the move isn't valid display that
- playerInput(player1); //restart this function to get a new input
- }
- }
- }
- if (first == "look" || second == "look" || third == "look" || fourth == "look" || fifth == "look" || sixth == "look") {
- //LOOK
- //next - direction?
- }
- if (first == "run" || second == "run" || third == "run" || fourth == "run" || fifth == "run" || sixth == "run") {
- //RUN
- //returns player back to previous level?
- }
- if (first == "attack" || second == "attack" || third == "attack" || fourth == "attack" || fifth == "attack" || sixth == "attack") {
- //attack
- //attack monster -> make combat function?
- //attack random object -> damage self ?
- }
- if (first == "dig" || second == "dig" || third == "dig" || fourth == "dig" || fifth == "dig" || sixth == "dig") {
- //dig
- //certain levels have treasure maybe? Random chance at treasure?
- }
- if (first == "inspect" || second == "inspect" || third == "inspect" || fourth == "inspect" || fifth == "inspect" || sixth == "inspect") {
- //inspect
- //displays information an object in sentence?
- }
- if (first == "help" || second == "help" || third == "help" || fourth == "help" || fifth == "help" || sixth == "help") {
- //help menu
- cout << "The following are words you may find helpful;" << endl;
- cout << "Move(north/west/south/east/up/down), look, run, attack, dig, inspect, etc.";
- }
- }
- bool checkValidMove(player &player1, char d) {
- //map - array
- //player 1 - current position
- //char d - direction they want to move
- if (d == 'e' || d == 'E') {
- if (player1.getPositionX < 5) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- }
- if (d == 's' || d == 'S') {
- if (player1.getPositionZ > 0) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- }
- if (d == 'w' || d == 'W') {
- if (player1.getPositionX > 0) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- }
- if (d == 'n' || d == 'N') {
- if (player1.getPositionZ < 5) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- }
- if (d == 'd' || d == 'D') {
- if (player1.getPositionY >= 1) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- }
- if (d == 'u' || d == 'U') {
- if (player1.getPositionY == 0 && (player1.getPositionX == 0 && player1.getPositionZ == 0)) {
- //checks to see if current player's position is within bounds, if so allows them to move
- return true;
- }
- if (player1.getPositionY == 1 && (player1.getPositionX == 4 && player1.getPositionZ == 3)) {
- return true;
- }
- }
- return false;
- }
- void roomEvent(player &player1) {
- //Allows the setup of gold / monster events, set position in this function
- //initialize combat / monster
- if (player1.getPositionX == 1 && player1.getPositionZ == 2) {
- //call combat function
- combat( player1);
- }
- //grant gold
- if (player1.getPositionX == 3 && (player1.getPositionZ == 2 || player1.getPositionZ == 0)) {
- cout << "Congrats! You found some gold within this room! +100 gold! " << endl;
- player1.setGold(100);
- }
- playerInput(player1);
- }
- void combat(player &player1) {
- cout << "OH no! There is a monster in this room! It has already seen you and started to charge towards you. ";
- if (player1.getPositionY == 0) {
- //first floor monster combat
- cout << "Luckily it is only a slime, you are fairly safe." << endl;
- cout << "You take 3 points of damage." << endl;
- player1.calcHP(3); //player takes 3 points of damage(could be mitagated by armor)
- }
- if (player1.getPositionY == 1) {
- //second floor monster combat
- cout << "Unfortunately its a minotaur you may wish to pray to a god at this point." << endl;
- cout << "You take 10 points of damage." << endl;
- player1.calcHP(10);
- }
- if (player1.getPositionY == 2) {
- //third floor monster combat
- cout << "Yay you made it to safety! ...Until a boss appeared out of nowhere! " << endl;
- cout << "You take 50 points of damage." << endl;
- player1.calcHP(50);
- }
- }
- /*void buildFloor(struct level mainLevel[5][3][5]) {
- FUNCTION NOT WORKING, saving for later (struct + 3 dimensional array + function = bad mixture)
- see - http://stackoverflow.com/questions/3613302/passing-array-of-structures-to-function-c for more info(to fix someday)
- //set all to false
- for (int i = 0; i < 5; i++) {
- for (int b = 0; b < 5; b++) {
- for (int h = 0; h < 3; h++) {
- mainLevel[i][h][b].down = false;
- mainLevel[i][h][b].up = false;
- mainLevel[i][h][b].west = false;
- mainLevel[i][h][b].south = false;
- mainLevel[i][h][b].east = false;
- mainLevel[i][h][b].north = false;
- }
- }
- }
- //create proper path which picks a corner and puts the way up / down there, then fills the next 2 levels above.
- //-------------------
- mainLevel[0][0][0].up = true;
- mainLevel[0][0][0].north = true;
- mainLevel[0][0][1].south = true;
- mainLevel[0][0][1].monster = true;
- mainLevel[0][0][1].east = true;
- mainLevel[1][0][1].west = true;
- mainLevel[1][0][1].gold = true;
- mainLevel[1][0][1].north = true;
- mainLevel[1][0][2].south = true;
- mainLevel[1][0][2].east = true;
- mainLevel[2][0][2].west = true;
- //next level up
- mainLevel[0][1][0].down = true;
- mainLevel[0][1][0].north = true;
- mainLevel[0][1][1].south = true;
- mainLevel[0][1][1].north = true;
- mainLevel[0][1][2].south = true;
- mainLevel[0][1][2].east = true;
- mainLevel[1][1][2].west = true;
- mainLevel[1][1][2].north = true;
- mainLevel[1][1][3].south = true;
- mainLevel[1][1][3].east = true;
- mainLevel[2][1][3].west = true;
- mainLevel[2][1][3].east = true;
- mainLevel[3][1][3].west = true;
- mainLevel[3][1][3].east = true;
- mainLevel[4][1][3].west = true;
- mainLevel[4][1][3].south = true;
- mainLevel[4][1][2].north = true;
- mainLevel[4][1][2].up = true;
- //next level up
- mainLevel[4][2][2].down = true; //ending map at this position for now
- mainLevel[4][2][2].monster = true;
- //_________Possible randomization in the future? ___________
- //int xRan;
- //srand(time(0));
- //xRan = rand() % 4 + 1; //creating a random number between 1 - 4
- //if (xRan == 1) {
- //fill level here
- //}
- //add some extra turns(TO DO)
- //--------------------
- }
- */
- /*
- TO DO LIST
- ===========
- A) Make player class -done
- 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
- C) Make a function to build the level(1 floor / build) - done, however it didn't work with structs, scrapped
- D) Take player input - done(needs more input options)
- E) Look at player input, decide their action(change position, fight mob, inspect room, etc. ) - done
- F) Make a save function that writes to a file
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement