so i have a mini text adventure game where i can go through the rooms and have made a start trying to implement a inventory, i can type things in and store them in a text file, but i want to be able to find an object in a room for instance a key and ask the player if they want to pick up the key, to use on a door later, which i also need help on locking a room BTW.

thanks in advanced !!!!!
here is the game code below:

#include <iostream>
#include <string>
#include <locale>
#include <fstream>

const int NUM_OF_ITEMS = 5;

using namespace std;

void RoomsGame();
void HelpMenu();
void About();
void initDungeon();
void inventory();
void displayInv();


class Room
{
    private : 

        int N, E, S, W;
        string description;

    public:

        Room(); 
        void setNorth(int);
        void setEast(int);
        void setSouth(int);
        void setWest(int);
        void setDescription(string);

        int getNorth();
        int getEast();
        int getSouth();
        int getWest();

        string getDescription();
};

Room::Room()

{
    N = -1;
    E = -1;
    W = -1;
    S = -1;
    description = "";
}

void Room::setNorth(int n)
{
    N = n;
}

void Room::setEast(int e)
{
    E = e;
}

void Room::setSouth(int s)
{
    S = s;
}

void Room::setWest(int w)
{
    W = w;
}

void Room::setDescription(string desc)
{
    description = desc;
}

int Room::getNorth()
{
    return N;
}

int Room::getEast()
{
    return E;
}

int Room::getSouth()
{
    return S;
}

int Room::getWest()
{
    return W;
}

string Room::getDescription()
{
    return description;
}

Room testRooms[10];


void RoomsGame()
{
    char choice;

    int playerPos = 0;


    initDungeon();

    cout << "Choice : ";
    cin >> choice;
    choice = tolower(choice);

    while (choice != 'q')
    {

        if ((choice == 'n') && (testRooms[playerPos].getNorth() != -1))
        {
            playerPos = testRooms[playerPos].getNorth();
        }
        else if ((choice == 'e') && (testRooms[playerPos].getEast() != -1)) 
        {
            playerPos = testRooms[playerPos].getEast();
        }
        else if ((choice == 's') && (testRooms[playerPos].getSouth() != -1))
        {
            playerPos = testRooms[playerPos].getSouth();
        }
        else if ((choice == 'w') && (testRooms[playerPos].getWest() != -1))
        {
            playerPos = testRooms[playerPos].getWest();
        }
        else if (choice == 'i')
        {
            inventory();
        }

        cout << "You Are Currently Standing in = " ;
        cout << testRooms[playerPos].getDescription(); 
        cout << endl;

        cout<< "Where Do You Dare To Go Now ? : ";
        cin >> choice;
        choice = tolower(choice);
        cout<<endl;

    }
}

void displayMenu()
{
    cout << endl;
    cout<< "  (=)_____________________________________________________________(=) "<<endl;
    cout<< "   | Welcome To By Haunted Mansion, Care to take a look around ?/n/n |"
        << "   |  1 - Take A look Around                                       |" <<endl
        << "   |  2 - Help Me Work Out What To Do                              |" <<endl
        << "   |  3 - About This Game                                          |"<< endl
        << "   |  4 -  Quit                                                    |" <<endl<<endl
        << "  (=)_____________________________________________________________(=)"
        <<endl
        <<endl
        << "So, What's it To Be : ";

}

class Help
{
    private : 

        int N, B, O;
        string description;

    public:

        Help(); // constructor
        void setOpen(int);
        void setNext(int);
        void setBack(int);
        void setDescription(string);

        int getOpen();
        int getNext();
        int getBack();

        string getDescription();
};

Help::Help()
{
    N = -1;
    O = 0;
    B = -1;
}

void Help::setNext(int n)
{
    N = n;
}

void Help::setBack(int b)
{
    B = b;
}

void Help::setOpen(int o)
{
    O = o;
}
void Help::setDescription(string desc)
{
    description = desc;
}

int Help::getNext()
{
    return N;
}

int Help::getBack()
{
    return B;
}

int Help::getOpen()
{
    return O;
}

string Help::getDescription()
{
    return description;
}

Help testPage[2];

void HelpMenu()
{
    char choice;
    int MenuPos = 0;

    testPage[0].setNext(1);
    testPage[0].setDescription("Page 1\n\nThis Page tells you about the bacic Rules of the game\nthe aim of the game is simple! get to the last room\nyou will be given a set of options such as the\ndirection (n, e, s, w) you would like to go in...\n\npress n to view next page or q to quit to menu.\n");

    testPage[1].setBack(0);
    testPage[1].setDescription("Page 2\n\nThere will be some Surprises along the way\nwith some interesting collectables as well.........\n\n       so Enjoy!!\n\npress b to view previous page or q to quit to menu.\n");

    cout<<endl;
    cout<<"Choose either o to look at help on offer\nor\nq to quit to main menu" << endl;
    cout << endl;
    cout<<endl;
    cout<< "Choice : ";
    cin >> choice;
    choice = tolower(choice);
    cout<<endl;

    while (choice != 'q')
    {
        if ((choice == 'o') && (testPage[MenuPos].getOpen() != -1))
        {
            MenuPos = testPage[MenuPos].getOpen();
        }
        else if ((choice == 'n') && (testPage[MenuPos].getNext() != -1))
        {
            MenuPos = testPage[MenuPos].getNext();
        }
        else if ((choice == 'b') && (testPage[MenuPos].getBack() != -1)) 
        {
            MenuPos = testPage[MenuPos].getBack();
        }

        cout << "Current Page = " ;
        cout << testPage[MenuPos].getDescription(); 
        cout << endl;

        cout<< "Choice : ";
        cin >> choice;
        choice = tolower(choice);
        cout<<endl;

    }
}


int main()
{
    char choice1;
    int playerPos = 0;

    initDungeon();

    do
    {
        displayMenu();
        cin>>choice1;

        if (choice1 == '1')
        {
            RoomsGame();
        }
        else if (choice1 == '2')
        {
            HelpMenu();
        }
        else if (choice1 == '3')
        {
            About();
        }


    }

    while (choice1 != '4');

    cout << endl;

    return 0;
}


void About()
{
    cout << endl;
    cout << "Basic Text Adventure Game" <<endl;
    cout << "Build 0.1" <<endl;
    cout << "Built For Glyndwr University Course" <<endl;
    cout << "Computer Game Development" <<endl;
    cout << "Masterfully Created By Liam Marney" <<endl;
    cout << "Created with C++" << endl;
    cout << "Copyright 2013... i think, i will, definitely copyrighted" <<endl;
    cout << "If You Liked It, Feel Free To Throw Money At Me in The Street" <<endl;
    cout << endl;

    system("PAUSE");
    cout<< endl;

}

void initDungeon()
{
    fstream InputFile;
    string line;
    int pos = 0;

    InputFile.open("initDungeon.txt");

    while ( !InputFile.eof() )

    {   
        //north
        getline (InputFile, line);
        testRooms[pos].setNorth(stoi(line));

        //east
        getline (InputFile, line);
        testRooms[pos].setEast(stoi(line));

        //south
        getline (InputFile, line);
        testRooms[pos].setSouth(stoi(line));

        //west
        getline (InputFile, line);
        testRooms[pos].setWest(stoi(line));

        //description
        getline (InputFile, line);
        testRooms[pos].setDescription(line);

        pos++;
    }

    InputFile.close(); 
}
void displayInv();
void add();
void remove();

string inv[NUM_OF_ITEMS];

void inventory()
{
    char choice;

    // init inventory
    for (int a = 0; a < NUM_OF_ITEMS; a++)
    {
        inv[a] = "empty";
    }



    displayInv();

    cout << " 1 = list, 2 = add, 3 = remove" << endl;
    cout << "choice : ";
    cin >> choice;

    while (choice != 'q')
    {
        if ( choice == '1')
        {
            displayInv();
        }
        else if (choice == '2')
        {
            add();
        }
        else if (choice == '3')
        {
            remove();
        }

        cout << " 1 = list, 2 = add, 3 = remove" << endl;
        cout << "choice : ";
        cin >> choice;
    }

    return;
}


void displayInv()
{
    // display inventory
    for (int a = 0; a < NUM_OF_ITEMS; a++)
    {
        cout << inv[a] << endl;
    }

    cout << endl;
}

void add()
{
    int pos = 0;
    string tempstr;

    while ((inv[pos] != "empty") && (pos < NUM_OF_ITEMS))
    {
        pos++;
    }

    if (pos < NUM_OF_ITEMS)
    {
    cout << "pos = " << pos << endl;

    cout << "enter string : ";
    cin >> tempstr;

    inv[pos] = tempstr;
    }
    else
    {
        cout << "Inventory is Full" << endl;
    }
}

void remove()
{
    string item;
    int pos = 0;

    cout << "what to remove :";
    cin >> item;

    while ((inv[pos] != item) && (pos != -1))
    {
        if ( pos < NUM_OF_ITEMS)
        {
            pos++;
        }
        else
        {
            pos = -1;
        }
        }
        if ( pos == -1)
    {
        cout << "Not Found" << endl;
    }
    else
    {
        inv[pos] = "empty";
    }
}

Recommended Answers

All 6 Replies

Storing them in a text file seems a bit of a pain. Why not create an objects class, and store them in , for example, a vector<objects>?

needs to be a input output text file, its part of my requirments

needs to be a input output text file, its part of my requirments

That doesn't preclude you from also storing the data you need in memory. Load the file into memory, use it in memory, and save it back periodically or upon request. This is how most software that persists data in files works.

ok i get there are other ways to do this, but can i get help for the method i am actually trying to do, without trying to seem like an idiot, it needs to be done like this otherwise i would have done it a wealth of different ways.

As I understand, you're trying to use the file as a random access collection, is that correct?

If that's so, I'd recommend writing a completely separate utility class for handling the file access through a more convenient interface. Something like this (written in haste, so there's probably plenty of useful member functions missing):

class RandomAccessFile {
public:
    RandomAccessFile(string filename);

    // Accessors
    int LineCount();
    string GetLine(int index);

    // Modifiers
    bool AddLineBefore(const string& line, int index);
    bool AddLineAfter(const string& line, int index);
};

The reason I suggest that is the file handling code is verbose and tedious. You'll also notice that it's slow as molasses because sequential files aren't meant to be used that way and file handling is one of the slower tasks one can perform in general.

Indeed. It's just crazy. Are you sure you read the instructions properly? Does it clearly state "make an inventory system to be used during playing the game that doesn't store anything in memory, but writes it to disk and reads it back from there" or does it say something you've misinterpreted like "have the ability to save the inventory to disk" or even just "read and write something to disk"?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.