• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
King of the Boneheads

Text Adventure Help

8 posts in this topic

I'm less-then-beginner in C++ at the moment and I'm working on my first official game: A Text Adventure.
I realized it should be simple enough to do, and I want to start with a basic one. Can I have 1 .cpp file for each room? Here's some example rooms:

[u]|R1|R2|R3|[/u]
[u]|R4|R5|R6|[/u]
[u]|R7|R8|R9|[/u]

So l want it to make it so you start in the bottom room, it tells you what the room looks like, the name of the room, and gives you 3 choices.
Type in "N" for north "E" for east "W" for west.
So if you want to go north to the middle room, you type in "N" and hit enter.
Then you appear in the middle room, and the same thing happens.
However, I want to be able to have locked doors & keys.
So lets say there's a key in the top-right room, and you collect it if you walk into the room. Then there's door1 (you just collected key1) all around the middle room. Then if you try to walk in, it will act as if the door isn't there and you let you in. Otherwise it says you need the key.
Might be confusing, and I'm willing to take the time to write this, but I really want to make it happen.

Thanks in Advance.

Regards, King of the Boneheads.
0

Share this post


Link to post
Share on other sites
You really don't "need" any more files to do what you want. You could (because you did state you were a beginner) setup a while loop and some conditional statements and work through the rooms

[CODE]
if(direction == 'n' || direction == 'N')
{
cout << "Moving North through the door";
}
[/CODE]

For the locked key situation you could hard code it so certain rooms are always locked:

[CODE]
if(direction == 'e' || direction == 'E')
{
if(hasKey1)
{
cout << "You unlocked the door.";
}
else
{
cout << "The door ahead is locked.";
}
}
[/CODE]

EDIT: You "can" if you want the extra files honestly. I figured it would be easier to work with one file at a time till you become more experienced with C++ is all. :P My apologies. Edited by Inuyashakagome16
0

Share this post


Link to post
Share on other sites
[quote name='Inuyashakagome16' timestamp='1349873480' post='4988698']
You really don't "need" any more files to do what you want. You could (because you did state you were a beginner) setup a while loop and some conditional statements and work through the rooms

[CODE]
if(direction == 'n' || direction == 'N')
{
cout << "Moving North through the door";
}
[/CODE]

For the locked key situation you could hard code it so certain rooms are always locked:

[CODE]
if(direction == 'e' || direction == 'E')
{
if(hasKey1)
{
cout << "You unlocked the door.";
}
else
{
cout << "The door ahead is locked.";
}
}
[/CODE]

EDIT: You "can" if you want the extra files honestly. I figured it would be easier to work with one file at a time till you become more experienced with C++ is all. [img]http://public.gamedev.net//public/style_emoticons/default/tongue.png[/img] My apologies.
[/quote]

Thanks!
0

Share this post


Link to post
Share on other sites
1 code file for each room is not a good way to do this.

Make a class that will be your room type, and then handle all rooms in a general way. Give each room a name, and then point to which room each direction goes to, and if it's locked. You might also want a list of any items that are in the room.

Then you can handle your entire game in one easy, general purpose, loop.
2

Share this post


Link to post
Share on other sites
[quote name='Daaark' timestamp='1349895654' post='4988808']
1 code file for each room is not a good way to do this.

Make a class that will be your room type, and then handle all rooms in a general way. Give each room a name, and then point to which room each direction goes to, and if it's locked. You might also want a list of any items that are in the room.

Then you can handle your entire game in one easy, general purpose, loop.
[/quote]

That would be the way. :P I couldn't find the right way to say it without sounding dumb in some way. That's the method i would honestly go with. I wasn't sure if Boneheads had gotten that far into learning C++.
0

Share this post


Link to post
Share on other sites
Here is something I hacked together quick to show how to handle all rooms without coding each one. It's a little sloppy as I was pressed for time and haven't written C++ in a LONG time now. I added space to define keys, but I do not check for them.

edit: Looks better on PasteBin: http://pastebin.com/cYh15CWk

[code]// ExampleTextAdventure.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;
//This class will be the data type that all rooms are based on. It will contain data that is general to all rooms
class CRoom
{
public:
string Name;
//a list of exit name strings, and key name strings
//if key string is empty, door is unlocked
string ExitNorth, KeyNorth;
string ExitEast, KeyEast;
string ExitSouth, KeySouth;
string ExitWest, KeyWest;
//Constrcutor, for easy room initalization!
CRoom(string name,
string exitnorth, string keynorth,
string exiteast, string keyeast,
string exitsouth, string keysouth,
string exitwest, string keywest)
{
Name = name;
ExitNorth = exitnorth; KeyNorth = keynorth;
ExitEast = exiteast; KeyEast = keyeast;
ExitSouth = exitsouth; KeySouth = keysouth;
ExitWest = exitwest; KeyWest = keywest;

return;
}
~CRoom()
{
//nothing to do!
//strings clean themselves up

return;
}
};

//game class, just to keep player and room data organized
class CGame
{
public:
//an array of keys
vector <string> Key;

//an array of rooms
vector <CRoom> Room;
int CurrentRoom;
string Input;

CGame()
{

//add 9 rooms using the constructor to fill them out quickly
//123
//456
//789
Room.push_back(CRoom("Room 1", "","", "Room 2","", "Room 4","", "",""));
Room.push_back(CRoom("Room 2", "","", "Room 3","", "Room 5","", "Room 1",""));
Room.push_back(CRoom("Room 3", "","", "","", "Room 6","", "Room 5",""));
Room.push_back(CRoom("Room 4", "Room 1","", "Room 5","", "Room 7","", "",""));
Room.push_back(CRoom("Room 5", "Room 2","", "Room 6","", "Room 8","", "Room 4",""));
Room.push_back(CRoom("Room 6", "Room 3","", "","", "Room 9","", "Room 5",""));
Room.push_back(CRoom("Room 7", "Room 4","", "Room 8","", "","", "",""));
Room.push_back(CRoom("Room 8", "Room 5","", "Room 9","", "","", "",""));
Room.push_back(CRoom("Room 9", "Room 6","", "","", "","", "Room 8",""));

//set the current room to room 5, right in the middle
CurrentRoom = FindRoom("Room 5");
return;
}

//a prompt function
bool Prompt()
{
cout << "\n\n\n";
cout << "You are in " + Room[CurrentRoom]. Name << endl;
cout << "Enter a direction to travel (n,e,s,w), or q to quit" << endl;
cout << "-> ";

//read player input
string Input;
cin >> Input;
int NewRoom = 0;
switch (Input[0])
{
//if the user entered 'q', return false
case 'Q':
case 'q':
return false;
break;
//user wants to go north
case 'N':
case 'n':
//check to see if that room exists
//if FindRoom found a legal room, assign it as the current room
NewRoom = FindRoom(Room[CurrentRoom].ExitNorth);
if (NewRoom > -1) CurrentRoom = NewRoom;
else cout << "You can't travel in that direction from here" << endl;
break;
case 'E':
case 'e':
NewRoom = FindRoom(Room[CurrentRoom].ExitEast);
if (NewRoom > -1) CurrentRoom = NewRoom;
else cout << "You can't travel in that direction from here" << endl;
break;
case 'S':
case 's':
NewRoom = FindRoom(Room[CurrentRoom].ExitSouth);
if (NewRoom > -1) CurrentRoom = NewRoom;
else cout << "You can't travel in that direction from here" << endl;
break;
case 'W':
case 'w':
NewRoom = FindRoom(Room[CurrentRoom].ExitWest);
if (NewRoom > -1) CurrentRoom = NewRoom;
else cout << "You can't travel in that direction from here" << endl;
break;
//if the user enters an unrecognized command...
default:
cout << "Invalid command" << endl;
}

return true;
}


//Find a room index based on it's name
//returns an int that is the index of the room in the array
//or -1 if it's not found
int FindRoom(string RoomName)
{
for (int i = 0; i < Room.size(); ++i)
{
//if the room name matches 'RoomName', return the room index
if (Room[i].Name == RoomName)
{
return i;
}
}

//if the code gets down here, the room with 'RoomName' wasn't found
//so -1 is returned to indicate failure
return -1;
}
};

int _tmain(int argc, _TCHAR* argv[])
{

//this will hold the input from the player
string Input;
//intialize a new instance of the game class
CGame Game;
while(1)
{
//run the prompt, and break if the returns false;
if (!Game.Prompt()) break;
}

cout << "Byeeeeeeeeee" << endl;
return 0;
}[/code]


edit: What is up with the code tags? turned my formatting into a disaster! Edited by Daaark
0

Share this post


Link to post
Share on other sites
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.

[code]
// 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
[/code]

[code]
// 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
[/code]

[code]
// 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
[/code]

[code]
// 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;
}
[/code]

[code]
// 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] Edited by BeerNutts
1

Share this post


Link to post
Share on other sites
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:

[code]
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!";
}
[/code]
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0