Jump to content

  • Log In with Google      Sign In   
  • Create Account


First "game" in C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
12 replies to this topic

#1 markrodgers11   Members   -  Reputation: 149

Like
3Likes
Like

Posted 07 October 2012 - 07:25 PM

I've been reading a book on C++ and I decided it'd be good practice to code something from scratch instead of outside of the book. I decided it wouldn't be too hard to code a knock off of ZORK...I was wrong Posted Image

It's not that I find it HARD to code a knock off of ZORK but one thing after another keeps adding up and my main.cpp is getting so huge im started to get lost! ...I know, i know, its not THAT big, but for someone like me who is used to coding little things like calculators and other basic things, its pretty long ;p

I know I've probably coded this in a stupid way so If anyone has some suggestions as to how I can split it into other files or reuse parts of code so I don't have to manually re-write things, etc. Maybe some new methods I could check into because idk if this is normal but I feel like I used if, if else, and else statements for practically everything! xD

any advice / critic is appreciated. Thanks!


#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
//strings
string name;
string input;
string output;
string confirm;
string currentArea;
string area;
bool confused;
bool dark;
//commands
string about;
string help;
string n;
string e;
string s;
string w;
string look;
string open;
string close;
//objects
string door1 = "closed";
bool checkArea(string area)
{
if (area == "area1")
{
  if (input == "n")
  {
   look = "There is a mountain range to the North.";
   cout << look;
   confused = false;
  }
  else if (input == "e")
  {
   look = "The door seems to be unlocked.";
   cout << look;
   confused = false;
  }
  else if (input == "s")
  {
   look = "*I watch the waves run up the shore.*";
   cout << look;
   confused = false;
  }
  else if (input == "w")
  {
   look = "There seems to be what appears as a chain-link fence running from the coast to the mountain line. The fence has a gate but it looks as if it is locked.";
   cout << look;
   confused = false;
  }
  else
  {
   confused = true;
  }
}
if (area == "area2")
{
  if (input == "n")
  {
   look = "There is a painting of a whealthy looking man with a mustache on this wall.";
   cout << look;
   confused = false;
  }
  else if (input == "e")
  {
   look = "It is dark inside but I see a desk...";
   cout << look;
   confused = false;
  }
  else if (input == "s")
  {
   look = "Nothing here.";
   cout << look;
   confused = false;
  }
  else if (input == "w")
  {
  
  }
  else
  {
   confused = true;
  }
}
return false;
}
void checkInput()
{
cout << endl << "> ";
cin >> input;

if (input == "about")
{
  output = "You are playing a text based RPG coded by Mark Rodgers.";
  cout << output << endl;
  checkInput();
}
else if (input == "help")
{
  output = "COMMAND LIST:\n help - welcome!\n about - info about the game\n look - describe surroundings\n name - reminds you of your name\n open_OBJECTNAME - opens specified object\n close_OBJECTNAME - closes specified object\n exit - close game (alternative: quit)\n\nMOVEMENT:\n n - Move North\n e - Move East\n s - Move South\n w - Move West";
  cout << output << endl;
  checkInput();
}
else if (input == "look")
{
  cout << look << endl;
  checkInput();
}
else if (input == "name")
{
  output = name;
  cout << "My name is " << name << "." << endl;
  checkInput();
}
else if (input == "open_door")
{
  if (look == "The door seems to be unlocked.")
  {
   if (door1 == "closed")
   {
	output = "*I open the door*";
	cout << output << endl;
	door1 = "opened";
  
	checkInput();
   }
   else if (door1 == "opened")
   {
	output = "The door is already open.";
	cout << output << endl;
	checkInput();
   }
  }
  else
  {
   output = "There is nothing here to open.";
   cout << output << endl;
   checkInput();
  }
}
else if (input == "close_door")
{
  if (look == "The door seems to be unlocked.")
  {
   if (door1 == "opened")
   {
	output = "*I close the door*";
	cout << output << endl;
	door1 = "closed";
	checkInput();
   }
   else if (door1 == "closed")
   {
	output = "The door is already closed.";
	cout << output << endl;
	checkInput();
   }
  }
  else
  {
   output = "There is nothing here to open.";
   cout << output << endl;
   checkInput();
  }
}
else if (input == "exit" || input == "quit")
{
  cout << "Are you sure?(y/n)" << endl << endl << "> ";
  cin >> confirm;
  if (confirm == "yes" || confirm == "YES" || confirm == "Yes" || confirm == "y" || confirm == "Y")
  {
   return;
  }
  else if (confirm == "no" || confirm == "NO" || confirm == "No" || confirm == "n" || confirm == "N")
  {
   checkInput();
  }
  else
  {
   output = "Invalid response.";
   cout << output << endl;
   checkInput();
  }
}
else
{
  checkArea(currentArea);
  if (confused = true)
  {
   output = "Huh?";
   cout << output << endl;
  }
  checkInput();
}
}
void area2()
{
currentArea = "area2";
dark = true;
look = "It is dark inside but I see a desk...";
cout << look << endl;
checkInput();
}
void area1()
{
currentArea = "area1";
look = "I am standing on the beach.\nThere is a small shack to the east.";
cout << look << endl;
checkInput();
}
void main()
{
SetConsoleTitle( "ZORK" );
cout << "What is my name?" << endl << endl << "> ";
cin >> name;
area1();
}


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 16714

Like
8Likes
Like

Posted 07 October 2012 - 07:56 PM

I've been there before! I think I might even still have my first text adventure game somewhere. Mine was worse. Posted Image

Two things that are bloating your project:
1) Use a std::vector of separate commands, not a variable for each command. I posted a few other ideas in this thread not that long ago.
As much as possible, make your code do the work of connecting things (commands, rooms, items, etc...) together for you.

2) Your rooms need to be loaded from text files, not hard-coded into the game, otherwise you're in for a world of pain. Posted Image
If this is your first attempt, that's fine that you're hard coding it, but for the sake of your own sanity, give yourself a hard-limit (say: 10 rooms), then scrap the project and learn how to load the rooms and connect the rooms together using external files.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#3 Wilfrost   Members   -  Reputation: 142

Like
2Likes
Like

Posted 07 October 2012 - 08:10 PM

I'm currently working on my first game - a clone of pong written in Java. After coding for several hours I had "hit the wall" so to speak, so I went and found a "Write pong in Java" tutorial online. I looked through it, to see how they had tackled the problems I was having trouble with. After understanding their code I went back and continued working on my code with the knowledge I had gained. This way I'm not just doing a copy / paste. I can call the code my own because I have written every line of it myself. All I "took" from the tutorial was the basic concept of how to attack the problem. You might try finding a similar Zork tutorial and scan through it for optimizations that will make your code easier to handel. Happy coding!

#4 Black-Rook   Members   -  Reputation: 1563

Like
4Likes
Like

Posted 07 October 2012 - 10:52 PM

Servant of the Lord brings up some great points!

Early on in game development I got heavily into tool development, and believe me it saves so much time and hassle from hard coding. I still remember my first text game in C++, I had a ton of classes: Weapon, Enemy, Player, Tool, Battle System, ect...

Keep up the great work!
GameDev Journal: http://www.gamedev.n...-rooks-journal/

OpenChess - 1.0 done!

Classic RPG #1 - Task 9 -> January 1st 2013

#5 BeerNutts   Crossbones+   -  Reputation: 2520

Like
5Likes
Like

Posted 08 October 2012 - 07:32 AM

One suggestion you should look into is creating a class to handle each "area" (typically called rooms in Text Adventure games). Something like this:
// In TRoom.h, you could declare the class like this

class TRoom
{
public:
  TRoom(std::string description);
  ~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(std::string itemName, std::string itemDescription);

  // SetExit set a room to move to when going that direction
  void SetExit(std::string exitDirection, TRoom& roomConnection);

  // Update handles the command given to it, like "look" will print the Description
  // it returns the new room to move to
  TRoom& Update(std::string command);

private:
  std::string Description;
  // You may want to use a std::map here, but I'll use two vectors to store the directions
  // and the rooms to move
  std::vector<std::string> Directions;
  std::vector<TRoom&> RoomsToMove;
};

That's only the header file. You should probably look at some tutorials about classes if you don't understand this, but moving to that kind of coding will help you a lot. Then, when writing the actual code, your main loop would be this:


// load all the rooms, this function could read from a file that lays out your world.
std::vector<TRoom> Rooms = LoadRooms();

// The 1st room in the list is the room we start with
TRoom& CurrentRoom = Rooms.front();

while(!bQuit) {
  cout << endl << "> ";
  cin >> input;

  // CheckStandardInput checks for "help" "about" "quit", etc.
  if (!CheckStandardInput(input, bQuit)) {
    CurrentRoom = CurrentRoom.Update(input);
  }
}

That's just an idead
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#6 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 08 October 2012 - 10:00 AM

Wow thanks guys. Some really great responses. Looks like I've got lots of new things to look into Posted Image
I don't know anything about classes atm or loading rooms via text files, etc.

Thanks for all the great responses!

EDIT: Today i scraped the old one and did it again from scratch (changed the dialogue alittle ;p) but it still seems as I am hard-coding it, which is what i was trying to avoid doing again xD I haven't yet looked into classes/vecters/arrays/etc, so I will take a look at those tonight and hopefully that will make my games less "hard-coded" ;p

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;
string charName, charRace, charClass;
string input;
string look;
string activeRoom;
string pos, north, east, south, west;
string open_door, close_door;
bool doorOpened;
bool doorInRange;
void checkInput()
{
cout << "> ";
cin >> input;
if (input == "help")
{
  cout << endl << "COMMAND LIST:\n\n help - Welcome to ZORK ;]\n look - describe your surroundings\n exit - closes ZORK (alternative: quit)\n\nMOVEMENT:\n\n n - moves North\n e - moves East\n s - moves South\n w - moves West\n\nACTIONS:\n\n open_OBJECTNAME - opens object\n close_OBJECTNAME - closes object\n\n";
}
else if (input == "exit" || input == "quit")
{
  return;
}
else if (input == "look")
{
  cout << endl << look;
}
else if (input == "n")
{
  pos = north;
  look = pos;
  cout << endl << look;
}
else if (input == "e")
{
  pos = east;
  look = pos;
  cout << endl << look;
}
else if (input == "s")
{
  pos = south;
  look = pos;
  cout << endl << look;
}
else if (input == "w")
{
  pos = west;
  look = pos;
  cout << endl << look;
}
else if (input == "open_door")
{
  if (activeRoom == "room1")
  {
   if (look == north)
   {
    doorInRange = true;
    if (doorOpened = false)
    {
	 cout << "Door opened.\n\n";
	 doorOpened = true;
    }
    else if (doorOpened = true)
    {
	 cout << "Door is already open.\n\n";
    }
   }
   else
   {
    doorInRange = false;
    cout << "No door is in range.\n\n";
   }
  }
}
else if (input == "close_door")
{
  if (doorOpened = true)
  {
   cout << "Door closed.\n\n";
   doorOpened = false;
  }
  else if (doorOpened = false)
  {
   cout << "Door is already closed.\n\n";
  }
}
else
{
  cout << "huh?\n\n";
}
checkInput();
}

void room1()
{
activeRoom = "room1";
doorOpened = false;
pos = north;
north = "There is a small shack here. It appears as if it is unlocked.\n\n";
east = "A chain-linked fence blocks moving this direction.\n\n";
south = "There is an ocean here.\n\n";
west = "A mountain range blocks me from going here.\n\n";
cout << endl << north;
look = north;
checkInput();
}
string pickName()
{
//name selection
cout << "What is your name?\n\n" << "> ";
cin >> charName;
return charName;
}
string pickRace()
{
//race selection
cout << endl << "What race do you want to be?\n Human\n Orc\n Elf\n\n" << "> ";
cin >> charRace;
if (charRace == "human" || charRace == "HUMAN" || charRace == "Human")
{
  charRace = "human";
}
else if (charRace == "orc" || charRace == "ORC" || charRace == "Orc")
{
  charRace = "orc";
}
else if (charRace == "elf" || charRace == "ELF" || charRace == "Elf")
{
  charRace = "elf";
}
else
{
  cout << "Invalid Entry.\n Valid inputs are 'human', 'orc', or 'elf'\n\n";
  pickRace();
}
return charRace;
}
string pickClass()
{
//class selection
cout << endl << "What race do you want to be?\n Assassin\n Hunter\n Wizard\n\n" << "> ";
cin >> charClass;
if (charClass == "assassin" || charClass == "ASSASSIN" || charClass == "Assassin")
{
  charClass = "assassin";
}
else if (charClass == "hunter" || charClass == "HUNTER" || charClass == "Hunter")
{
  charClass = "hunter";
}
else if (charClass == "wizard" || charClass == "WIZARD" || charClass == "Wizard")
{
  charClass = "wizard";
}
else
{
  cout << "Invalid Entry.\n Valid inputs are 'assassin', 'hunter', or 'wizard'\n\n";
  pickClass();
}
return charClass;
}
void charVerify()
{
cout << "Welcome to ZORK " << charName << "! You are a " << charRace << " playing as a\\an " << charClass << ".\nAny last changes you want to make?\n\n1) Change Name\n2) Change Race\n3) Change Class\n4) Continue\n\n" << endl << "> ";
cin >> input;
if (input == "1")
{
  pickName();
  charVerify();
}
else if (input == "2")
{
  pickRace();
  charVerify();
}
else if (input == "3")
{
  pickClass();
  charVerify();
}
else if (input == "4")
{
  room1();
}
else
{
  cout << "Invalid Entry.\n Valid inputs are '1', '2', '3', or '4'\n\n";
  charVerify();
}
}
void main()
{
SetConsoleTitle("Text-Based RPG");
pickName();
pickRace();
pickClass();
charVerify();
system("pause");
return;
}

Edited by markrodgers11, 08 October 2012 - 11:25 AM.


#7 Lightness1024   Members   -  Reputation: 677

Like
0Likes
Like

Posted 09 October 2012 - 03:25 PM

At least, you are doing something.
Making a data driver text-adventure game engine at this point of skill would be really amazing. Just keep it up, you'll figure it out someday. no rush.

PS: If ever you want to try linux some day, first thing to install : valgrind. This tool is the savior for every C and C++ developer, particularly beginners. Because it tells where are the runtime mistakes, when they happen, and not 3 days later.
PS2: also you could have a lot of fun with ncurses to extend your game.

#8 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 09 October 2012 - 05:37 PM

yes yes i'm quite familiar with Linux actually ;) was even reading a book on linux shell scripting and crap so i got pretty familiar with the basics of how things work in linux and knowing my way around the terminal ;)

#9 SuperVGA   Members   -  Reputation: 1118

Like
0Likes
Like

Posted 10 October 2012 - 02:09 AM

I've been reading a book on C++ and I decided it'd be good practice to code something from scratch instead of outside of the book. I decided it wouldn't be too hard to code a knock off of ZORK...I was wrong Posted Image

It's not that I find it HARD to code a knock off of ZORK but one thing after another keeps adding up and my main.cpp is getting so huge im started to get lost! ...I know, i know, its not THAT big, but for someone like me who is used to coding little things like calculators and other basic things, its pretty long ;p

I know I've probably coded this in a stupid way so If anyone has some suggestions as to how I can split it into other files or reuse parts of code so I don't have to manually re-write things, etc. Maybe some new methods I could check into because idk if this is normal but I feel like I used if, if else, and else statements for practically everything! xD

any advice / critic is appreciated. Thanks!


(lots of code on single line)... if (confused = true) ...(lots of code on single line)


Perhaps here you meant to say if(confused == true) the assignment will (in this case) always evaluate true...

Aside from that: Good job on your first game! I remember my first text adventure; I wanted to make it so advanced, and thus I never finished it.
Keep it up and write lot's of small applications!

Edited by SuperVGA, 10 October 2012 - 02:13 AM.


#10 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 10 October 2012 - 09:57 AM

hmm thank you, that might be the solution to one of the bugs i am having xP thanks for point that out! :)

#11 BeerNutts   Crossbones+   -  Reputation: 2520

Like
2Likes
Like

Posted 11 October 2012 - 02:06 PM

I posted this in another thread, but I thought it'd be helpful here too.

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;
}

Edited by BeerNutts, 11 October 2012 - 02:06 PM.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#12 BeerNutts   Crossbones+   -  Reputation: 2520

Like
0Likes
Like

Posted 11 October 2012 - 03:28 PM

Oops, there's a little bug in my code. I was assigning a separate TRoom CurrentRoom from the Rooms vector, but that wasn't holding the changes to the actual rooms themselves. Here's the updated main() function:


int main(int argc, char *argv[])
{
  LoadRooms();
  TPlayer Player("Mark");
  std::cout << "Hello " << Player.GetName() << " Welcome to My test Adventure Game.";
  bool bRunning = true;
  int currentRoom = 0;
  // Print the first room's description
  std::string temp;
  Rooms[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, Rooms[currentRoom], bRunning)) {
	    std::string roomName = Rooms[currentRoom].GetName();
	    Rooms[currentRoom].Update(input, roomName, Player);
	    // Change rooms
	    if (roomName != Rooms[currentRoom].GetName()) {
		  for (int i = 0; i < Rooms.size(); i++) {
		    if (Rooms[i].GetName() == roomName) {
			  currentRoom = i;
		   
			  // Print new rooms description
			  Rooms[currentRoom].Update("l", roomName, Player);
			  break;
		    }
		  }
	    }
	  }
    }
  }
  std::cout << "Thanks for Playing!";
}

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#13 markrodgers11   Members   -  Reputation: 149

Like
0Likes
Like

Posted 12 October 2012 - 07:00 PM

wow thanks for the release in the source, once I find the time I will definitely setup a project and paste all your code it, study it, modify it to learn new things, etc!
Love looking at other people's source code, I find it a good way to study it, unless you have no idea what any of it is then its just like trying to read Chinese ;p




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS