Advertisement
Guest User

game.exe_v0.02

a guest
Feb 25th, 2014
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.38 KB | None | 0 0
  1. updates.txt
  2.  
  3. the beginnings of an adventure game/rpg type of thing
  4.  
  5. update 1: now with error management for input
  6. update 2: fixed error management
  7. update 3: added option to exit take item menu without taking anything
  8. update 4: many changes to make code more easily modifiable
  9. update 5:added ability to use items in inventory on items in inventory
  10. or environment and have something happen as a result
  11.  
  12. game_main.cpp
  13.  
  14. #include <iostream>
  15. #include <cstdlib>
  16. #include <string>
  17. #include <vector>
  18. using namespace std;
  19. #include "Item.h"
  20. #include "Room.h"
  21. #include "other_functions.h"
  22. #include "add_data.h"
  23.  
  24. int main()
  25. {
  26. vector<Item> inventory;
  27. bool no_error=true;
  28. Room rooms;
  29. int input=0;
  30. bool game=true;
  31. rooms=add_data(rooms);
  32. cout<<"Welcome to Work In Progress v0.02\n\n";
  33. while(game)
  34. {
  35. no_error=true;
  36. cout<<"Choose an option: \n1) Describe Room \n2) Check Inventory \n3) Take Item \n4) Use Item\n5) Quit\n";
  37. cout<<">";
  38. cin>>input;
  39. clear_screen();
  40. no_error=error_manage();
  41. if(no_error)
  42. {
  43. if(input==1)
  44. {
  45. rooms.describe();
  46. }
  47. else if(input==2)
  48. {
  49. list_items(inventory,"Inventory is empty","Inventory: ",0);
  50. }
  51. else if(input==3)
  52. {
  53. rooms.move_item(inventory);
  54. }
  55. else if(input==4)
  56. {
  57. rooms.use_item(inventory);
  58. }
  59. else if(input==5)
  60. {
  61. cout<<"Thanks I guess";
  62. game=false;
  63. }
  64. else if(no_error==false)
  65. {
  66. cerr << "Invalid Input. Try Again." << endl;
  67. }
  68. }
  69. }
  70. return 0;
  71. }
  72.  
  73. Room.h
  74.  
  75. #include <iostream>
  76. #include <cstdlib>
  77. #include <string>
  78. #include <vector>
  79. using namespace std;
  80. #include "Item.h"
  81.  
  82. #ifndef ROOM_H
  83. #define ROOM_H
  84.  
  85. class Room
  86. {
  87. string basic_description;
  88. int r_input;
  89. public:
  90. Room();
  91. vector<Item> list_static;
  92. vector<Item> list_take;
  93. int take_item(vector<Item>& this_list);
  94. void set_basic_desc(string basic_desc);
  95. void set_items(string item_name,string in_room, string in_inv, bool takeable, int low, int high, int loc);
  96. void move_item(vector<Item>& inventory);
  97. void describe();
  98. void use_item(vector<Item>& inventory);
  99. void set_all_items(vector<Item>& all_items, vector<Item>& inventory, int item1);
  100. void occurence(vector<Item>& inventory, int item1, int item2_loc, int item2);
  101. };
  102.  
  103. #endif /* ROOM_H */
  104.  
  105.  
  106. Room.cpp
  107.  
  108. #include <iostream>
  109. #include <cstdlib>
  110. #include <string>
  111. #include <vector>
  112. using namespace std;
  113. #include "Item.h"
  114. #include "Room.h"
  115. #include "other_functions.h"
  116. #include "add_data.h"
  117.  
  118. Room::Room()
  119. {
  120.  
  121. }
  122.  
  123. void Room::occurence(vector<Item>& inventory, int item1, int item2_loc, int item2)
  124. {
  125. //loc 0 is inventory, 1 is list_take, 2 is list_static
  126. Item blank;
  127. bool not_found=true;
  128. int result=0;
  129. for(int a=0;a<100 && not_found;a++)
  130. {
  131. if(item2_loc==0)
  132. {
  133. if(inventory[item1].to_check(a) && inventory[item2].to_check(a))
  134. {
  135. not_found=false;
  136. result=a;
  137. }
  138. }
  139. else if(item2_loc==1)
  140. {
  141. if(inventory[item1].to_check(a) && list_take[item2].to_check(a))
  142. {
  143. not_found=false;
  144. result=a;
  145. }
  146. }
  147. else if(item2_loc==2)
  148. {
  149. if(inventory[item1].to_check(a) && list_static[item2].to_check(a))
  150. {
  151. not_found=false;
  152. result=a;
  153. }
  154. }
  155. }
  156. if(result==0)
  157. {
  158. cout<<"You can't use those together\n";
  159. }
  160. else if(result==1)
  161. {
  162. cout<<"You hack apart that chair with your sword\n";
  163. list_static.erase(list_static.begin() + (item2));
  164. list_static.push_back(blank);
  165. list_static[list_static.size()-1].set_values("Kindling","There are the splintered remains of a chair. \n","blank",false,0,0,2);
  166. list_take.push_back(blank);
  167. 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);
  168. }
  169. else if(result==2)
  170. {
  171. cout<<"You absolutely trash that table with your sword\n";
  172. list_static.erase(list_static.begin() + (item2));
  173. list_static.push_back(blank);
  174. 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);
  175. }
  176. else if(result==3)
  177. {
  178. cout<<"You break your sword trying to batter down the door\n";
  179. inventory.erase(inventory.begin() + (item1));
  180. inventory.push_back(blank);
  181. inventory[inventory.size()-1].set_values("Broken Sword","A sword with a broken blade\n","Good luck repairing that", true,0,0,0);
  182. }
  183. }
  184.  
  185. void Room::use_item(vector<Item>& inventory)
  186. {
  187. int value=0;
  188. bool choice1=true;
  189. bool choice2=false;
  190. int item1;
  191. int item2_loc;
  192. int item2;
  193. vector<Item> all_items;
  194. while(choice1)
  195. {
  196. list_items(inventory, "Nothing to use", "Choose item to use(by number): ", 2);
  197. value=take_item(inventory);
  198. if(value==1)
  199. {
  200. choice1=false;
  201. }
  202. else
  203. {
  204. choice2=true;
  205. item1=(r_input-1);
  206. while(choice2)
  207. {
  208. set_all_items(all_items, inventory, item1);
  209. list_items(all_items, "Nothing to use item on", "Choose item to use first item on (by number): ", 2);
  210. value=take_item(all_items);
  211. if(value==2)
  212. {
  213. choice1=false;
  214. choice2=false;
  215. item2=r_input-1;
  216. item2_loc=all_items[item2].get_loc();
  217. item2=all_items[item2].get_id();
  218. occurence(inventory, item1, item2_loc, item2);
  219. }
  220. else
  221. {
  222. choice2=false;
  223. }
  224. }
  225. }
  226. }
  227. }
  228.  
  229. void Room::move_item(vector<Item>& inventory)
  230. {
  231. int value=0;
  232. list_items(list_take, "Nothing to take", "Choose Item(by number): ", 2);
  233. value=take_item(list_take);
  234. if(value==2)
  235. {
  236. inventory.push_back(list_take[r_input-1]);
  237. inventory[inventory.size()-1].change_loc(0);
  238. inventory[inventory.size()-1].change_id(inventory.size()-1);
  239. list_take.erase(list_take.begin()+(r_input-1));
  240. }
  241. }
  242.  
  243. void Room::set_basic_desc(string basic_desc)
  244. {
  245. basic_description=basic_desc;
  246. }
  247.  
  248. void Room::set_items(string item_name,string in_room, string in_inv,bool takeable, int low, int high, int loc)
  249. {
  250. Item blank;
  251. int id;
  252. if(takeable)
  253. {
  254. list_take.push_back(blank);
  255. id=list_take.size()-1;
  256. list_take[list_take.size()-1].set_values(item_name, in_room, in_inv, low, high, loc, id);
  257. }
  258. else
  259. {
  260. list_static.push_back(blank);
  261. id=list_static.size()-1;
  262. list_static[list_static.size()-1].set_values(item_name, in_room, in_inv, low, high, loc, id);
  263. }
  264. }
  265.  
  266. int Room::take_item(vector<Item>& this_list)
  267. {
  268. int value=0;
  269. bool no_error=true;
  270. while(value==0)
  271. {
  272. if(this_list.size()==0)
  273. {
  274. value=1;
  275. }
  276. else
  277. {
  278. no_error=true;
  279. cout<<this_list.size()+1<<") Exit Menu\n";
  280. cout<<">";
  281. cin>>r_input;
  282. clear_screen();
  283. no_error=error_manage();
  284. if(r_input>0 && r_input <= this_list.size() && no_error)
  285. {
  286. value=2;
  287. }
  288. else if(r_input==(this_list.size()+1))
  289. {
  290. value=1;
  291. }
  292. else if(no_error==false)
  293. {
  294. cerr << "Invalid Input. Try Again." << endl;
  295. }
  296. }
  297. }
  298. return value;
  299. }
  300.  
  301. void Room::describe()
  302. {
  303. cout<<basic_description;
  304. for(int a=0;a<list_static.size();a++)
  305. {
  306. list_static[a].describe(1);
  307. }
  308. for(int a=0;a<list_take.size();a++)
  309. {
  310. list_take[a].describe(1);
  311. }
  312. }
  313.  
  314. void Room::set_all_items(vector<Item>& all_items, vector<Item>& inventory, int item1)
  315. {
  316. all_items.clear();
  317. for(int a=0;a<inventory.size();a++)
  318. {
  319. if(a!=item1)
  320. {
  321. all_items.push_back(inventory[a]);
  322. }
  323. }
  324. for(int a=0;a<list_take.size();a++)
  325. {
  326. all_items.push_back(list_take[a]);
  327. }
  328. for(int a=0;a<list_static.size();a++)
  329. {
  330. all_items.push_back(list_static[a]);
  331. }
  332. }
  333.  
  334.  
  335. Item.h
  336.  
  337. #include <iostream>
  338. #include <cstdlib>
  339. #include <string>
  340. #include <vector>
  341. using namespace std;
  342.  
  343. #ifndef ITEM_H
  344. #define ITEM_H
  345.  
  346. class Item
  347. {
  348. int item_id;
  349. int current_loc;
  350. int use_low;
  351. int use_high;
  352. string room_desc;
  353. string inv_desc;
  354. string thing_name;
  355. public:
  356. Item();
  357. void describe(int location);
  358. void change_loc(int loc);
  359. int get_loc();
  360. void change_id(int id);
  361. int get_id();
  362. void set_values(string item_name, string in_room, string in_inv, int low, int high, int loc,int id);
  363. bool to_check(int a);
  364. };
  365.  
  366. #endif /* ITEM_H */
  367.  
  368.  
  369. Item.cpp
  370.  
  371. #include <iostream>
  372. #include <cstdlib>
  373. #include <string>
  374. #include <vector>
  375. using namespace std;
  376. #include "Item.h"
  377.  
  378. Item::Item()
  379. {
  380.  
  381. }
  382.  
  383. void Item::change_id(int id)
  384. {
  385. item_id=id;
  386. }
  387.  
  388. int Item::get_id()
  389. {
  390. return item_id;
  391. }
  392.  
  393. int Item::get_loc()
  394. {
  395. return current_loc;
  396. }
  397.  
  398. void Item::change_loc(int loc)
  399. {
  400. current_loc=loc;
  401. }
  402.  
  403. bool Item::to_check(int a)
  404. {
  405. bool value=false;
  406. if(a>=use_low && a<=use_high)
  407. {
  408. value=true;
  409. }
  410. return value;
  411. }
  412.  
  413. void Item::set_values(string item_name, string in_room, string in_inv, int low, int high, int loc, int id)
  414. {
  415. thing_name=item_name;
  416. inv_desc=in_inv;
  417. room_desc=in_room;
  418. use_low=low;
  419. use_high=high;
  420. current_loc=loc;
  421. item_id=id;
  422. }
  423.  
  424. void Item::describe(int location)
  425. {
  426. if(location==0)
  427. {
  428. cout<<thing_name;
  429. cout<<": "<<inv_desc<<"\n";
  430. }
  431. else if(location==1)
  432. {
  433. cout<<room_desc;
  434. }
  435. else
  436. {
  437. cout<<thing_name<<"\n";
  438. }
  439. }
  440.  
  441.  
  442. other_functions.h
  443.  
  444. #include <iostream>
  445. #include <cstdlib>
  446. #include <string>
  447. #include <vector>
  448. using namespace std;
  449.  
  450. #ifndef OTHER_FUNCTIONS_H
  451. #define OTHER_FUNCTIONS_H
  452.  
  453. void clear_screen();
  454.  
  455. void check_inventory(vector<Item>& inventory);
  456.  
  457. bool error_manage();
  458.  
  459. void list_items(vector<Item>& this_list, string empty_desc, string take_desc, int location);
  460.  
  461.  
  462.  
  463. #endif /* OTHER_FUNCTIONS_H */
  464.  
  465.  
  466. other_functions.cpp
  467.  
  468. #include <iostream>
  469. #include <cstdlib>
  470. #include <string>
  471. #include <vector>
  472. using namespace std;
  473. #include "Item.h"
  474. #include "Room.h"
  475. #include "other_functions.h"
  476.  
  477. bool error_manage()
  478. {
  479. bool no_error=true;
  480. if(cin.fail())
  481. {
  482. cin.ignore(2000, '\n');
  483. no_error=false;
  484. cin.clear();
  485. }
  486. if (cin.get() != '\n')
  487. {
  488. cin.ignore(2000, '\n');
  489. no_error-false;
  490. }
  491. if(no_error==false)
  492. {
  493. cerr << "Invalid Input. Try Again." << endl;
  494. }
  495. return no_error;
  496. }
  497.  
  498. void clear_screen()
  499. {
  500. system("cls");
  501. }
  502.  
  503. void check_inventory(vector<Item>& inventory)
  504. {
  505. if(inventory.size()==0)
  506. {
  507. cout<<"Inventory is empty\n";
  508. }
  509. else
  510. {
  511. for(int a=0;a<inventory.size();a++)
  512. {
  513. inventory[a].describe(0);
  514. }
  515. }
  516. }
  517.  
  518. void list_items(vector<Item>& this_list, string empty_desc, string take_desc, int location)
  519. {
  520. if(this_list.size()==0)
  521. {
  522. cout<<empty_desc<<endl;
  523. }
  524. else
  525. {
  526. cout<<take_desc<<endl;
  527. for(int a=0;a<this_list.size();a++)
  528. {
  529. cout<<a+1<<") ";
  530. this_list[a].describe(location);
  531. }
  532. }
  533. }
  534.  
  535.  
  536. add_data.h
  537.  
  538. #include <iostream>
  539. #include <cstdlib>
  540. #include <string>
  541. #include <vector>
  542. using namespace std;
  543. #include "Item.h"
  544. #include "Room.h"
  545. #include "other_functions.h"
  546.  
  547. #ifndef ADD_DATA_H
  548. #define ADD_DATA_H
  549.  
  550. Room add_data(Room rooms);
  551.  
  552. #endif /* ADD_DATA_H */
  553.  
  554.  
  555. add_data.cpp
  556.  
  557. #include <iostream>
  558. #include <cstdlib>
  559. #include <string>
  560. #include <vector>
  561. using namespace std;
  562. #include "Item.h"
  563. #include "Room.h"
  564. #include "other_functions.h"
  565. #include "add_data.h"
  566.  
  567. Room add_data(Room rooms)
  568. {
  569. rooms.set_basic_desc("You are in a small room with stone walls. \n");
  570. rooms.set_items("Sword", "In the corner there is a sword. \n","A nasty looking sword",true,1,3,1);
  571. rooms.set_items("Shield","On the table there is a shield. \n","A sturdy looking shield",true,0,0,1);
  572. rooms.set_items("Backpack","There is a backpack. \n","An empty backpack",true,0,0,1);
  573. 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);
  574. rooms.set_items("Table","There is an oaken table in the middle of the room. \n","blank",false,2,2,2);
  575. rooms.set_items("Chair","There is a chair. \n","blank",false,1,1,2);
  576. return rooms;
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement