Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBeerNutts

Posted 11 October 2012 - 02:05 PM

This might be a bit more complex than what you are looking for, but here's a simple example I put together for the heck of it. There are 5 source files for this, and I'll list them here, but it's generic enough, it should handle a good, fun game. The only thing I would modify is better handling of SpecializedCheck() function. I think this should be integrated into the rooms as well.

// TItem.h Handles Items in a text Adventure Game
#ifndef TITEM_H
#define TITEM_H
#include <string>
class TItem
{
public:
  TItem(std::string name, std::string description) :
  Name(name),
  Description(description),
  HasBeenUsed(false) {}
  ~TItem() {}

  bool IsUsable() { return !HasBeenUsed; }
  void UseItem() { HasBeenUsed = true; }

  std::string GetName() { return Name; }
  std::string getDescription() { return Description; }

private:
  std::string Name;
  std::string Description;
  bool HasBeenUsed;
};
#endif

// TRoom.h Handles Rooms in a text Adventure Game
#ifndef TROOM_H
#define TROOM_H
#include <string>
#include <vector>
#include "TItem.h"
#include "TPlayer.h"
class TRoom
{
public:
  TRoom(std::string description, std::string name);
  ~TRoom() {}
  // AddItem puts an item in the room to be displayed when looking, and get be picked up
  // an "Item" (which can be carried in your inventory, used, and picked up) should
  // probably be it's own class, and you would pass it to Add Item.  it would have a name, and a description at least.
  void AddItem(TItem item);
  // SetExit set a room to move to when going that direction
  void SetExit(std::string exitDirection, std::string roomConnection, bool enabled = true);
  void EnableExit(std::string direction, bool enabled);
  // Update handles the command given to it, like "look" will print the Description
  // it returns the new room to move to
  void TRoom::Update(std::string command, std::string& currentRoom, TPlayer &player);

  std::string GetName();
  std::string GetDescription();

  // Allows you to change the description
  void SetDescription(std::string description);
private:
  // structure to store the exits
  struct ExitInfo {
	std::string Direction;
	std::string RoomToMove;
	bool IsEnabled;
  };

  std::string Name;
  std::string Description;

  std::vector<ExitInfo> RoomsToMove;

  std::vector<TItem> Items;
};

#endif

// Player.h Handles Player in a text Adventure Game
#ifndef TPLAYER_H
#define TPLAYER_H
#include <string>
#include "TItem.h"
class TPlayer
{
public:
  TPlayer(std::string name) :
  Name(name) {}
  ~TPlayer() {}

  void AddItem(TItem item)
  { Inventory.push_back(item); }

  bool UseItem(std::string itemName)
  {
	for (int i = 0; i < Inventory.size(); i++) {
	  if (Inventory[i].GetName() == itemName &&
		  Inventory[i].IsUsable()) {
		Inventory[i].UseItem();
		return true;
	  }
	}
	return false;
  }

  void GetInventory(std::vector<TItem>& items)
  { items = Inventory; }

  std::string GetName() { return Name; }

private:
  std::string Name;
  std::vector<TItem> Inventory;

};

#endif

// TRoom.cpp Handles Rooms in a text Adventure Game
#include "TRoom.h"
#include <iostream>
TRoom::TRoom(std::string description, std::string name)
{
  Description = description;
  Name = name;
}
void TRoom::AddItem(TItem item)
{
  Items.push_back(item);
}
void TRoom::SetExit(std::string exitDirection, std::string roomConnection, bool enabled)
{
  ExitInfo exitInfo;

  exitInfo.Direction = exitDirection;
  exitInfo.RoomToMove = roomConnection;
  exitInfo.IsEnabled = enabled;
  RoomsToMove.push_back(exitInfo);

}
void TRoom::EnableExit(std::string direction, bool enabled)
{
  for (int i = 0; i < RoomsToMove.size(); i++) {
	if (RoomsToMove[i].Direction == direction) {
	  RoomsToMove[i].IsEnabled = enabled;
	  break;
	}
  }
}
void TRoom::Update(std::string command, std::string& currentRoom,
				   TPlayer &player)
{
  // Check for look first
  if (command == "look" || command == "l") {
	std::cout << Description << std::endl;
	for (int i = 0; i < Items.size(); i++) {
	 std::cout << "You see a " << Items[i].GetName() << std::endl;
	}
	
	std::cout << "Valid Exits are:";
	for (int i = 0; i < RoomsToMove.size(); i++) {
	  if (RoomsToMove[i].IsEnabled) {
		std::cout << " " << RoomsToMove[i].Direction;
	  }
	}
	return;
  }
  if (command.compare(0, 4, "get ") == 0) {
	// Someone typed "get <object>", see if we have the <object>
  
	std::string object = command.substr(4);  
	for (int i = 0; i < Items.size(); i++) {
	  if (Items[i].GetName() == object) {
		// we have the item, give it to the player
		std::cout << "You pick up " << object  << " and carry it with you.";
		player.AddItem(Items[i]);
		// remove the item from our vector
		Items.erase(Items.begin() + i);
		return;
	  }
	}
	std::cout << "You cannot pickup " << object;
	return;
  }
  // Check for a direction
  for (int i = 0; i < RoomsToMove.size(); i++) {
	if (command == RoomsToMove[i].Direction && RoomsToMove[i].IsEnabled) {
	  // Move to this room
	  currentRoom = RoomsToMove[i].RoomToMove;
	  return;
	}
  }

  std::cout << "I do not understand " << command;
  return;
}
std::string TRoom::GetDescription()
{
  return Description;
}
void TRoom::SetDescription(std::string description)
{
  Description = description;
}
std::string TRoom::GetName()
{
  return Name;
}

// GameMain.cpp Handles The mian loop in a text Adventure Game
#include "TRoom.h"
#include "TPlayer.h"
#include <iostream>
void LoadRooms();
bool CheckStandardInput(std::string input, bool &bRunning);
bool SpecializedCheck(std::string input, TPlayer &player,
					  TRoom &currentRoom, bool &bRunning);
// The vector holding all the rooms
std::vector<TRoom> Rooms;
int main(int argc, char *argv[])
{
  LoadRooms();

  TPlayer Player("Mark");

  std::cout << "Hello " << Player.GetName() << " Welcome to My test Adventure Game.";

  bool bRunning = true;
  TRoom &CurrentRoom = Rooms.front();

  // Print the first room's description
  std::string temp;
  CurrentRoom.Update("l", temp, Player);

  while(bRunning) {
	std::string input;
	std::cout << std::endl << "> ";
	getline( std::cin, input, '\n' );
	//std::cin >> input;
  
	if (!CheckStandardInput(input, bRunning)) {
	  // Perform Specialized check (for specific things)
	  if (!SpecializedCheck(input, Player, CurrentRoom, bRunning)) {
		std::string roomName = CurrentRoom.GetName();
		CurrentRoom.Update(input, roomName, Player);
		// Change rooms
		if (roomName != CurrentRoom.GetName()) {
		  for (int i = 0; i < Rooms.size(); i++) {
			if (Rooms[i].GetName() == roomName) {
			  CurrentRoom = Rooms[i];
			
			  // Print new rooms description
			  CurrentRoom.Update("l", roomName, Player);
			  break;
			}
		  }
		}
	  }
	}
  }

  std::cout << "Thanks for Playing!";
}
void LoadRooms()
{
  // Typically, you would load the rooms from a file here, but I'll hard code it for now
  // We create the rooms then link them togetehr After
  TRoom Closest("You Are in a Large Closest.  There is an Exit to the North.", "Closest");

  // This room has a broom
  TItem Broom("broom", "It's an old Broom.");
  Closest.AddItem(Broom);

  // The Closest is linked to a kitchen
  TRoom Kitchen("This is an old kitchen, With a Dirty Floor.  There is an exit to the south.", "Kitchen");

  // No Item, but if you use Broom, it will show a trap door leading down
  TRoom TreasureRoom("You've Found a Treasure Room with chests filled with Gold!  You can retire rich now.  Congratulations!", "TreasureRoom");

  // now set the exits; Trap door going down not set yet, so make usable false
  Closest.SetExit("n", Kitchen.GetName());
  Kitchen.SetExit("s", Closest.GetName());
  Kitchen.SetExit("d", TreasureRoom.GetName(), false);
  TreasureRoom.SetExit("u", Kitchen.GetName());

  Rooms.push_back(Closest);
  Rooms.push_back(Kitchen);
  Rooms.push_back(TreasureRoom);
}
bool CheckStandardInput(std::string input, bool &bRunning)
{
  if (input == "q" || input == "quit") {
	bRunning = false;
	return true;
  }
  if (input == "h" || input == "help") {
	std::cout << "Commands are: get, use, n, s, e, w, u, and d";
	return true;
  }

  return false;
}
bool SpecializedCheck(std::string input, TPlayer &player,
					  TRoom &currentRoom, bool &bRunning)
{
  // only special command is "use broom" if the player has the broom and he's in the kitchen
  if (input == "use broom") {
	// UseItem() will only wirk if player has it and it hasn't been used
	if (currentRoom.GetName() == "Kitchen" && player.UseItem("broom")) {
	  std::cout << "You sweep the floor, and find a trap door leading Down!";
	
	  // Change the room's description and set exit usable
	  currentRoom.SetDescription("This is an old kitchen. There is an exit to the south.  You've uncovered a trap door leading down.");
	  currentRoom.EnableExit("d", true);
	  return true;
	}
  }  
  return false;
}

#1BeerNutts

Posted 11 October 2012 - 02:01 PM

This might be a bit more complex than what you are looking for, but here's a simple example I put together for the heck of it. There are 5 source files for this, and I'll list them here, but it's generic enough, it should handle a good, fun game. The only thing I would modify is better handling of SpecializedCheck() function. I think this should be integrated into the rooms as well.

// TItem.h Handles Items in a text Adventure Game
#ifndef TITEM_H
#define TITEM_H
#include <string>
class TItem
{
public:
  TItem(std::string name, std::string description) :
  Name(name),
  Description(description),
  HasBeenUsed(false) {}
  ~TItem() {}
 
  bool IsUsable() { return !HasBeenUsed; }
  void UseItem() { HasBeenUsed = true; }
 
  std::string GetName() { return Name; }
  std::string getDescription() { return Description; }
 
private:
  std::string Name;
  std::string Description;
  bool HasBeenUsed;
};
#endif

// TRoom.h Handles Rooms in a text Adventure Game
#ifndef TROOM_H
#define TROOM_H
#include <string>
#include <vector>
#include "TItem.h"
#include "TPlayer.h"
class TRoom
{
public:
  TRoom(std::string description, std::string name);
  ~TRoom() {}
  // AddItem puts an item in the room to be displayed when looking, and get be picked up
  // an "Item" (which can be carried in your inventory, used, and picked up) should
  // probably be it's own class, and you would pass it to Add Item.  it would have a name, and a description at least.
  void AddItem(TItem item);
  // SetExit set a room to move to when going that direction
  void SetExit(std::string exitDirection, std::string roomConnection, bool enabled = true);
  void EnableExit(std::string direction, bool enabled);
  // Update handles the command given to it, like "look" will print the Description
  // it returns the new room to move to
  void TRoom::Update(std::string command, std::string& currentRoom, TPlayer &player);
 
  std::string GetName();
  std::string GetDescription();
 
  // Allows you to change the description
  void SetDescription(std::string description);
private:
  // structure to store the exits
  struct ExitInfo {
    std::string Direction;
    std::string RoomToMove;
    bool IsEnabled;
  };
 
  std::string Name;
  std::string Description;
 
  std::vector<ExitInfo> RoomsToMove;
 
  std::vector<TItem> Items;
};

#endif

// Player.h Handles Player in a text Adventure Game
#ifndef TPLAYER_H
#define TPLAYER_H
#include <string>
#include "TItem.h"
class TPlayer
{
public:
  TPlayer(std::string name) :
  Name(name) {}
  ~TPlayer() {}
 
  void AddItem(TItem item)
  { Inventory.push_back(item); }
 
  bool UseItem(std::string itemName)
  {
    for (int i = 0; i < Inventory.size(); i++) {
	  if (Inventory[i].GetName() == itemName &&
		  Inventory[i].IsUsable()) {
	    Inventory[i].UseItem();
	    return true;
	  }
    }
    return false;
  }
 
  void GetInventory(std::vector<TItem>& items)
  { items = Inventory; }
 
  std::string GetName() { return Name; }
 
private:
  std::string Name;
  std::vector<TItem> Inventory;
 
};

#endif

// TRoom.cpp Handles Rooms in a text Adventure Game
#include "TRoom.h"
#include <iostream>
TRoom::TRoom(std::string description, std::string name)
{
  Description = description;
  Name = name;
}
void TRoom::AddItem(TItem item)
{
  Items.push_back(item);
}
void TRoom::SetExit(std::string exitDirection, std::string roomConnection, bool enabled)
{
  ExitInfo exitInfo;
 
  exitInfo.Direction = exitDirection;
  exitInfo.RoomToMove = roomConnection;
  exitInfo.IsEnabled = enabled;
  RoomsToMove.push_back(exitInfo);
 
}
void TRoom::EnableExit(std::string direction, bool enabled)
{
  for (int i = 0; i < RoomsToMove.size(); i++) {
    if (RoomsToMove[i].Direction == direction) {
	  RoomsToMove[i].IsEnabled = enabled;
	  break;
    }
  }
}
void TRoom::Update(std::string command, std::string& currentRoom,
				   TPlayer &player)
{
  // Check for look first
  if (command == "look" || command == "l") {
    std::cout << Description << std::endl;
    for (int i = 0; i < Items.size(); i++) {
	 std::cout << "You see a " << Items[i].GetName() << std::endl;
    }
	 
    std::cout << "Valid Exits are:";
    for (int i = 0; i < RoomsToMove.size(); i++) {
	  if (RoomsToMove[i].IsEnabled) {
	    std::cout << " " << RoomsToMove[i].Direction;
	  }
    }
    return;
  }
  if (command.compare(0, 4, "get ") == 0) {
    // Someone typed "get <object>", see if we have the <object>
   
    std::string object = command.substr(4);   
    for (int i = 0; i < Items.size(); i++) {
	  if (Items[i].GetName() == object) {
	    // we have the item, give it to the player
	    std::cout << "You pick up " << object  << " and carry it with you.";
	    player.AddItem(Items[i]);
	    // remove the item from our vector
	    Items.erase(Items.begin() + i);
	    return;
	  }
    }
    std::cout << "You cannot pickup " << object;
    return;
  }
  // Check for a direction
  for (int i = 0; i < RoomsToMove.size(); i++) {
    if (command == RoomsToMove[i].Direction && RoomsToMove[i].IsEnabled) {
	  // Move to this room
	  currentRoom = RoomsToMove[i].RoomToMove;
	  return;
    }
  }
 
  std::cout << "I do not understand " << command;
  return;
}
std::string TRoom::GetDescription()
{
  return Description;
}
void TRoom::SetDescription(std::string description)
{
  Description = description;
}
std::string TRoom::GetName()
{
  return Name;
}

// GameMain.cpp Handles The mian loop in a text Adventure Game
#include "TRoom.h"
#include "TPlayer.h"
#include <iostream>
void LoadRooms();
bool CheckStandardInput(std::string input, bool &bRunning);
bool SpecializedCheck(std::string input, TPlayer &player,
					  TRoom &currentRoom, bool &bRunning);
// The vector holding all the rooms
std::vector<TRoom> Rooms;
int main(int argc, char *argv[])
{
  LoadRooms();
 
  TPlayer Player("Mark");
 
  std::cout << "Hello " << Player.GetName() << " Welcome to My test Adventure Game.";
 
  bool bRunning = true;
  TRoom &CurrentRoom = Rooms.front();
 
  // Print the first room's description
  std::string temp;
  CurrentRoom.Update("l", temp, Player);
 
  while(bRunning) {
    std::string input;
    std::cout << std::endl << "> ";
    getline( std::cin, input, '\n' );
    //std::cin >> input;
   
    if (!CheckStandardInput(input, bRunning)) {
	  // Perform Specialized check (for specific things)
	  if (!SpecializedCheck(input, Player, CurrentRoom, bRunning)) {
	    std::string roomName = CurrentRoom.GetName();
	    CurrentRoom.Update(input, roomName, Player);
	    // Change rooms
	    if (roomName != CurrentRoom.GetName()) {
		  for (int i = 0; i < Rooms.size(); i++) {
		    if (Rooms[i].GetName() == roomName) {
			  CurrentRoom = Rooms[i];
			 
			  // Print new rooms description
			  CurrentRoom.Update("l", roomName, Player);
			  break;
		    }
		  }
	    }
	  }
    }
  }
 
  std::cout << "Thanks for Playing!";
}
void LoadRooms()
{
  // Typically, you would load the rooms from a file here, but I'll hard code it for now
  // We create the rooms then link them togetehr After
  TRoom Closest("You Are in a Large Closest.  There is an Exit to the North.", "Closest");
 
  // This room has a broom
  TItem Broom("broom", "It's an old Broom.");
  Closest.AddItem(Broom);
 
  // The Closest is linked to a kitchen
  TRoom Kitchen("This is an old kitchen, With a Dirty Floor.  There is an exit to the south.", "Kitchen"); 
 
  // No Item, but if you use Broom, it will show a trap door leading down
  TRoom TreasureRoom("You've Found a Treasure Room with chests filled with Gold!  You can retire rich now.  Congratulations!", "TreasureRoom");
 
  // now set the exits; Trap door going down not set yet, so make usable false
  Closest.SetExit("n", Kitchen.GetName());
  Kitchen.SetExit("s", Closest.GetName());
  Kitchen.SetExit("d", TreasureRoom.GetName(), false);
  TreasureRoom.SetExit("u", Kitchen.GetName());
 
  Rooms.push_back(Closest);
  Rooms.push_back(Kitchen);
  Rooms.push_back(TreasureRoom);
}
bool CheckStandardInput(std::string input, bool &bRunning)
{
  if (input == "q" || input == "quit") {
    bRunning = false;
    return true;
  }
  if (input == "h" || input == "help") {
    std::cout << "Commands are: get, use, n, s, e, w, u, and d";
    return true;
  }
 
  return false;
}
bool SpecializedCheck(std::string input, TPlayer &player,
					  TRoom &currentRoom, bool &bRunning)
{
  // only special command is "use broom" if the player has the broom and he's in the kitchen
  if (input == "use broom") {
    // UseItem() will only wirk if player has it and it hasn't been used
    if (currentRoom.GetName() == "Kitchen" && player.UseItem("broom")) {
	  std::cout << "You sweep the floor, and find a trap door leading Down!";
	 
	  // Change the room's description and set exit usable
	  currentRoom.SetDescription("This is an old kitchen. There is an exit to the south.  You've uncovered a trap door leading down.");
	  currentRoom.EnableExit("d", true);
	  return true;
    }
  }  
  return false;
}
[/code]

PARTNERS