Advertisement
Guest User

ExampleAdventureGame

a guest
Oct 10th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.54 KB | None | 0 0
  1. // ExampleTextAdventure.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <string>
  6. #include <vector>
  7. #include <iostream>
  8.  
  9.  
  10. using namespace std;
  11.  
  12. //This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
  13. class CRoom
  14. {
  15. public:
  16.  
  17.     string Name;
  18.  
  19.     //a list of exit name strings, and key name strings
  20.     //if key string is empty, door is unlocked
  21.     string ExitNorth, KeyNorth;
  22.     string ExitEast, KeyEast;
  23.     string ExitSouth, KeySouth;
  24.     string ExitWest, KeyWest;
  25.  
  26.     //Constrcutor, for easy room initalization!
  27.     CRoom(string name,
  28.         string exitnorth, string keynorth,
  29.         string exiteast, string keyeast,
  30.         string exitsouth, string keysouth,
  31.         string exitwest, string keywest)
  32.     {
  33.         Name = name;
  34.  
  35.         ExitNorth = exitnorth; KeyNorth = keynorth;
  36.         ExitEast = exiteast; KeyEast = keyeast;
  37.         ExitSouth = exitsouth; KeySouth = keysouth;
  38.         ExitWest = exitwest; KeyWest = keywest;
  39.        
  40.         return;
  41.     }
  42.  
  43.     ~CRoom()
  44.     {
  45.         //nothing to do!
  46.         //strings clean themselves up
  47.        
  48.         return;
  49.     }
  50.  
  51. };
  52.  
  53.  
  54. //game class, just to keep player and room data organized
  55. class CGame
  56. {
  57. public:
  58.  
  59.     //an array of keys
  60.     vector <string> Key;
  61.    
  62.     //an array of rooms
  63.     vector <CRoom> Room;
  64.  
  65.     int CurrentRoom;
  66.     string Input;
  67.  
  68.  
  69.     CGame()
  70.     {
  71.        
  72.         //add 9 rooms using the constructor to fill them out quickly
  73.         //123
  74.         //456
  75.         //789
  76.         Room.push_back(CRoom("Room 1""","",          "Room 2","",    "Room 4","",    "",""));
  77.         Room.push_back(CRoom("Room 2""","",          "Room 3","",    "Room 5","",    "Room 1",""));
  78.         Room.push_back(CRoom("Room 3""","",          "","",          "Room 6","",    "Room 5",""));
  79.         Room.push_back(CRoom("Room 4""Room 1","",    "Room 5","",    "Room 7","",    "",""));
  80.         Room.push_back(CRoom("Room 5""Room 2","",    "Room 6","",    "Room 8","",    "Room 4",""));
  81.         Room.push_back(CRoom("Room 6""Room 3","",    "","",          "Room 9","",    "Room 5",""));
  82.         Room.push_back(CRoom("Room 7""Room 4","",    "Room 8","",    "","",          "",""));
  83.         Room.push_back(CRoom("Room 8""Room 5","",    "Room 9","",    "","",          "",""));
  84.         Room.push_back(CRoom("Room 9""Room 6","",    "","",          "","",          "Room 8",""));
  85.        
  86.         //set the current room to room 5, right in the middle
  87.         CurrentRoom = FindRoom("Room 5");
  88.  
  89.         return;
  90.     }
  91.  
  92.    
  93.     //a prompt function
  94.     bool Prompt()
  95.     {
  96.         cout << "\n\n\n";
  97.         cout << "You are in " + Room[CurrentRoom]. Name << endl;
  98.         cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
  99.         cout << "-> ";
  100.        
  101.         //read player input
  102.         string Input;
  103.         cin >> Input;
  104.  
  105.         int NewRoom = 0;
  106.  
  107.         switch (Input[0])
  108.         {
  109.             //if the user entered 'q', return false
  110.         case 'Q':
  111.         case 'q':
  112.             return false;
  113.             break;
  114.  
  115.             //user wants to go north
  116.         case 'N':
  117.         case 'n':
  118.             //check to see if that room exists
  119.             //if FindRoom found a legal room, assign it as the current room
  120.             NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);           
  121.             if (NewRoom > -1) CurrentRoom = NewRoom;
  122.             else cout << "You can't travel in that direction from here" << endl;
  123.             break;
  124.         case 'E':
  125.         case 'e':
  126.             NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
  127.             if (NewRoom > -1) CurrentRoom = NewRoom;
  128.             else cout << "You can't travel in that direction from here" << endl;
  129.             break;
  130.         case 'S':
  131.         case 's':
  132.             NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
  133.             if (NewRoom > -1) CurrentRoom = NewRoom;
  134.             else cout << "You can't travel in that direction from here" << endl;
  135.             break;
  136.         case 'W':
  137.         case 'w':
  138.             NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
  139.             if (NewRoom > -1) CurrentRoom = NewRoom;
  140.             else cout << "You can't travel in that direction from here" << endl;
  141.             break;
  142.             //if the user enters an unrecognized command...
  143.         default:
  144.             cout << "Invalid command" << endl;
  145.         }
  146.  
  147.        
  148.         return true;
  149.     }
  150.    
  151.    
  152.     //Find a room index based on it's name
  153.     //returns an int that is the index of the room in the array
  154.     //or -1 if it's not found
  155.     int FindRoom(string RoomName)
  156.     {
  157.         for (int i = 0; i < Room.size(); ++i)
  158.         {
  159.             //if the room name matches 'RoomName', return the room index
  160.             if (Room[i].Name == RoomName)
  161.             {
  162.                 return i;
  163.             }
  164.         }
  165.        
  166.         //if the code gets down here, the room with 'RoomName' wasn't found
  167.         //so -1 is returned to indicate failure
  168.         return -1;
  169.     }
  170.  
  171. };
  172.  
  173.  
  174. int _tmain(int argc, _TCHAR* argv[])
  175. {
  176.    
  177.     //this will hold the input from the player
  178.     string Input;
  179.  
  180.     //intialize a new instance of the game class
  181.     CGame Game;
  182.  
  183.     while(1)
  184.     {
  185.         //run the prompt, and break if the returns false;
  186.         if (!Game.Prompt()) break;
  187.     }
  188.  
  189.  
  190.     cout << "Byeeeeeeeeee" << endl;
  191.    
  192.  
  193.     return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement