Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- updates.txt
- the beginnings of an adventure game/rpg type of thing
- update 1: now with error management for input
- update 2: fixed error management
- update 3: added option to exit take item menu without taking anything
- update 4: many changes to make code more easily modifiable
- update 5:added ability to use items in inventory on items in inventory
- or environment and have something happen as a result
- game_main.cpp
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #include "Room.h"
- #include "other_functions.h"
- #include "add_data.h"
- int main()
- {
- vector<Item> inventory;
- bool no_error=true;
- Room rooms;
- int input=0;
- bool game=true;
- rooms=add_data(rooms);
- cout<<"Welcome to Work In Progress v0.02\n\n";
- while(game)
- {
- no_error=true;
- cout<<"Choose an option: \n1) Describe Room \n2) Check Inventory \n3) Take Item \n4) Use Item\n5) Quit\n";
- cout<<">";
- cin>>input;
- clear_screen();
- no_error=error_manage();
- if(no_error)
- {
- if(input==1)
- {
- rooms.describe();
- }
- else if(input==2)
- {
- list_items(inventory,"Inventory is empty","Inventory: ",0);
- }
- else if(input==3)
- {
- rooms.move_item(inventory);
- }
- else if(input==4)
- {
- rooms.use_item(inventory);
- }
- else if(input==5)
- {
- cout<<"Thanks I guess";
- game=false;
- }
- else if(no_error==false)
- {
- cerr << "Invalid Input. Try Again." << endl;
- }
- }
- }
- return 0;
- }
- Room.h
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #ifndef ROOM_H
- #define ROOM_H
- class Room
- {
- string basic_description;
- int r_input;
- public:
- Room();
- vector<Item> list_static;
- vector<Item> list_take;
- int take_item(vector<Item>& this_list);
- void set_basic_desc(string basic_desc);
- void set_items(string item_name,string in_room, string in_inv, bool takeable, int low, int high, int loc);
- void move_item(vector<Item>& inventory);
- void describe();
- void use_item(vector<Item>& inventory);
- void set_all_items(vector<Item>& all_items, vector<Item>& inventory, int item1);
- void occurence(vector<Item>& inventory, int item1, int item2_loc, int item2);
- };
- #endif /* ROOM_H */
- Room.cpp
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #include "Room.h"
- #include "other_functions.h"
- #include "add_data.h"
- Room::Room()
- {
- }
- void Room::occurence(vector<Item>& inventory, int item1, int item2_loc, int item2)
- {
- //loc 0 is inventory, 1 is list_take, 2 is list_static
- Item blank;
- bool not_found=true;
- int result=0;
- for(int a=0;a<100 && not_found;a++)
- {
- if(item2_loc==0)
- {
- if(inventory[item1].to_check(a) && inventory[item2].to_check(a))
- {
- not_found=false;
- result=a;
- }
- }
- else if(item2_loc==1)
- {
- if(inventory[item1].to_check(a) && list_take[item2].to_check(a))
- {
- not_found=false;
- result=a;
- }
- }
- else if(item2_loc==2)
- {
- if(inventory[item1].to_check(a) && list_static[item2].to_check(a))
- {
- not_found=false;
- result=a;
- }
- }
- }
- if(result==0)
- {
- cout<<"You can't use those together\n";
- }
- else if(result==1)
- {
- cout<<"You hack apart that chair with your sword\n";
- list_static.erase(list_static.begin() + (item2));
- list_static.push_back(blank);
- list_static[list_static.size()-1].set_values("Kindling","There are the splintered remains of a chair. \n","blank",false,0,0,2);
- list_take.push_back(blank);
- list_take[list_take.size()-1].set_values("Chair Leg","In the pile of kindling you see a relatively intact chair leg. \n", "Like a club, sort of",true,0,0,1);
- }
- else if(result==2)
- {
- cout<<"You absolutely trash that table with your sword\n";
- list_static.erase(list_static.begin() + (item2));
- list_static.push_back(blank);
- list_static[list_static.size()-1].set_values("Remains of Table","The table is cracked asunder, and its legs are splintered. \n","blank",false,0,0,2);
- }
- else if(result==3)
- {
- cout<<"You break your sword trying to batter down the door\n";
- inventory.erase(inventory.begin() + (item1));
- inventory.push_back(blank);
- inventory[inventory.size()-1].set_values("Broken Sword","A sword with a broken blade\n","Good luck repairing that", true,0,0,0);
- }
- }
- void Room::use_item(vector<Item>& inventory)
- {
- int value=0;
- bool choice1=true;
- bool choice2=false;
- int item1;
- int item2_loc;
- int item2;
- vector<Item> all_items;
- while(choice1)
- {
- list_items(inventory, "Nothing to use", "Choose item to use(by number): ", 2);
- value=take_item(inventory);
- if(value==1)
- {
- choice1=false;
- }
- else
- {
- choice2=true;
- item1=(r_input-1);
- while(choice2)
- {
- set_all_items(all_items, inventory, item1);
- list_items(all_items, "Nothing to use item on", "Choose item to use first item on (by number): ", 2);
- value=take_item(all_items);
- if(value==2)
- {
- choice1=false;
- choice2=false;
- item2=r_input-1;
- item2_loc=all_items[item2].get_loc();
- item2=all_items[item2].get_id();
- occurence(inventory, item1, item2_loc, item2);
- }
- else
- {
- choice2=false;
- }
- }
- }
- }
- }
- void Room::move_item(vector<Item>& inventory)
- {
- int value=0;
- list_items(list_take, "Nothing to take", "Choose Item(by number): ", 2);
- value=take_item(list_take);
- if(value==2)
- {
- inventory.push_back(list_take[r_input-1]);
- inventory[inventory.size()-1].change_loc(0);
- inventory[inventory.size()-1].change_id(inventory.size()-1);
- list_take.erase(list_take.begin()+(r_input-1));
- }
- }
- void Room::set_basic_desc(string basic_desc)
- {
- basic_description=basic_desc;
- }
- void Room::set_items(string item_name,string in_room, string in_inv,bool takeable, int low, int high, int loc)
- {
- Item blank;
- int id;
- if(takeable)
- {
- list_take.push_back(blank);
- id=list_take.size()-1;
- list_take[list_take.size()-1].set_values(item_name, in_room, in_inv, low, high, loc, id);
- }
- else
- {
- list_static.push_back(blank);
- id=list_static.size()-1;
- list_static[list_static.size()-1].set_values(item_name, in_room, in_inv, low, high, loc, id);
- }
- }
- int Room::take_item(vector<Item>& this_list)
- {
- int value=0;
- bool no_error=true;
- while(value==0)
- {
- if(this_list.size()==0)
- {
- value=1;
- }
- else
- {
- no_error=true;
- cout<<this_list.size()+1<<") Exit Menu\n";
- cout<<">";
- cin>>r_input;
- clear_screen();
- no_error=error_manage();
- if(r_input>0 && r_input <= this_list.size() && no_error)
- {
- value=2;
- }
- else if(r_input==(this_list.size()+1))
- {
- value=1;
- }
- else if(no_error==false)
- {
- cerr << "Invalid Input. Try Again." << endl;
- }
- }
- }
- return value;
- }
- void Room::describe()
- {
- cout<<basic_description;
- for(int a=0;a<list_static.size();a++)
- {
- list_static[a].describe(1);
- }
- for(int a=0;a<list_take.size();a++)
- {
- list_take[a].describe(1);
- }
- }
- void Room::set_all_items(vector<Item>& all_items, vector<Item>& inventory, int item1)
- {
- all_items.clear();
- for(int a=0;a<inventory.size();a++)
- {
- if(a!=item1)
- {
- all_items.push_back(inventory[a]);
- }
- }
- for(int a=0;a<list_take.size();a++)
- {
- all_items.push_back(list_take[a]);
- }
- for(int a=0;a<list_static.size();a++)
- {
- all_items.push_back(list_static[a]);
- }
- }
- Item.h
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #ifndef ITEM_H
- #define ITEM_H
- class Item
- {
- int item_id;
- int current_loc;
- int use_low;
- int use_high;
- string room_desc;
- string inv_desc;
- string thing_name;
- public:
- Item();
- void describe(int location);
- void change_loc(int loc);
- int get_loc();
- void change_id(int id);
- int get_id();
- void set_values(string item_name, string in_room, string in_inv, int low, int high, int loc,int id);
- bool to_check(int a);
- };
- #endif /* ITEM_H */
- Item.cpp
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- Item::Item()
- {
- }
- void Item::change_id(int id)
- {
- item_id=id;
- }
- int Item::get_id()
- {
- return item_id;
- }
- int Item::get_loc()
- {
- return current_loc;
- }
- void Item::change_loc(int loc)
- {
- current_loc=loc;
- }
- bool Item::to_check(int a)
- {
- bool value=false;
- if(a>=use_low && a<=use_high)
- {
- value=true;
- }
- return value;
- }
- void Item::set_values(string item_name, string in_room, string in_inv, int low, int high, int loc, int id)
- {
- thing_name=item_name;
- inv_desc=in_inv;
- room_desc=in_room;
- use_low=low;
- use_high=high;
- current_loc=loc;
- item_id=id;
- }
- void Item::describe(int location)
- {
- if(location==0)
- {
- cout<<thing_name;
- cout<<": "<<inv_desc<<"\n";
- }
- else if(location==1)
- {
- cout<<room_desc;
- }
- else
- {
- cout<<thing_name<<"\n";
- }
- }
- other_functions.h
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #ifndef OTHER_FUNCTIONS_H
- #define OTHER_FUNCTIONS_H
- void clear_screen();
- void check_inventory(vector<Item>& inventory);
- bool error_manage();
- void list_items(vector<Item>& this_list, string empty_desc, string take_desc, int location);
- #endif /* OTHER_FUNCTIONS_H */
- other_functions.cpp
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #include "Room.h"
- #include "other_functions.h"
- bool error_manage()
- {
- bool no_error=true;
- if(cin.fail())
- {
- cin.ignore(2000, '\n');
- no_error=false;
- cin.clear();
- }
- if (cin.get() != '\n')
- {
- cin.ignore(2000, '\n');
- no_error-false;
- }
- if(no_error==false)
- {
- cerr << "Invalid Input. Try Again." << endl;
- }
- return no_error;
- }
- void clear_screen()
- {
- system("cls");
- }
- void check_inventory(vector<Item>& inventory)
- {
- if(inventory.size()==0)
- {
- cout<<"Inventory is empty\n";
- }
- else
- {
- for(int a=0;a<inventory.size();a++)
- {
- inventory[a].describe(0);
- }
- }
- }
- void list_items(vector<Item>& this_list, string empty_desc, string take_desc, int location)
- {
- if(this_list.size()==0)
- {
- cout<<empty_desc<<endl;
- }
- else
- {
- cout<<take_desc<<endl;
- for(int a=0;a<this_list.size();a++)
- {
- cout<<a+1<<") ";
- this_list[a].describe(location);
- }
- }
- }
- add_data.h
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #include "Room.h"
- #include "other_functions.h"
- #ifndef ADD_DATA_H
- #define ADD_DATA_H
- Room add_data(Room rooms);
- #endif /* ADD_DATA_H */
- add_data.cpp
- #include <iostream>
- #include <cstdlib>
- #include <string>
- #include <vector>
- using namespace std;
- #include "Item.h"
- #include "Room.h"
- #include "other_functions.h"
- #include "add_data.h"
- Room add_data(Room rooms)
- {
- rooms.set_basic_desc("You are in a small room with stone walls. \n");
- rooms.set_items("Sword", "In the corner there is a sword. \n","A nasty looking sword",true,1,3,1);
- rooms.set_items("Shield","On the table there is a shield. \n","A sturdy looking shield",true,0,0,1);
- rooms.set_items("Backpack","There is a backpack. \n","An empty backpack",true,0,0,1);
- rooms.set_items("Door","On the far side of the room is a sturdy looking door,\n reinforced with iron plates. \n","blank",false,3,3,2);
- rooms.set_items("Table","There is an oaken table in the middle of the room. \n","blank",false,2,2,2);
- rooms.set_items("Chair","There is a chair. \n","blank",false,1,1,2);
- return rooms;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement