i would like to have the ability to tell the player they cannot go that way if the exit does not exist the rooms are in a text file that is read in, i know i should use a if function but dont know where to put it, any help would be appreciated, if you need more or all of the code just say

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;

    }
}

thanks in advance

Recommended Answers

All 5 Replies

I am struggling to make sense of what you have written. To clarify, are you asking how you would prevent the player from going north, for example, if the current room has no north exit?
If this is the case then you could do something like:

if((choice == 'n') && (testRooms[playerPos].getNorth() != -1) && 
(testRooms[playerPos].hasNorthExit()))
{
    playerPos = testRooms[playerPos].getNorth();
}

This code obviously relies upon the existence of a 'hasNorthExit' method which would return a bool value that is true when a room has a north exit. Hope this helped!

yeah the path is already planned out and that works ok, but if you go a way that isnt defined lets say if i should go south but i instead try to go north a message pops up saying something like you cant go that way, instead of it just repeating the current room i am in.

thanks,
hope that clears it up

Ah ok so there is already a planned route that you should be taking. So I presume then that returning -1 from your getNorth(), getSouth() etc. functions, signifies that the player is trying to go somewhere that is not part of the pre-defined route?
If my assumption is correct, then you could simply change your exising 'if' statements to something like:

if(choice == 'n')
{
    if(testRooms[playerPos].getNorth() == -1)
    {
        cout << "You cannot go north!" << endln;
    }
    else
    {
        playerPos = testRooms[playerPos].getNorth();
    }
}

-1 is just used to signify that the room is not in order, if that makes sense, i suck at explaining, i have 10 rooms 0-9 so if i can only go north in the first room it would be

testRooms[0].setEast(1);
    testRooms[0].setDescription("room 0");

where set east (1) would move player position to the 2nd room and all the other directions would in effect be -1 because they would not lead to another room

thanks

Hmm, well my previous code suggestion would still work then would it not?
If -1 effectively means that a particular direction does not lead to another room, then we can assume that when -1 is returned from getNorth(), getEast() etc, the player has attempted to go in a direction that does not lead to another room. Therefore, when -1 is returned, we should display a message that tells the player that they cannot go that way.

My previous code snippet does the following:
* First, the user input is checked so that we know which direction the player wants to travel in.
* Next, we check whether the chosen direction of travel leads to another room.
* If the choice does lead to another room (not -1) then the player's position is updated.
* If the choice does not lead to another room (-1), then a message is displayed to inform the user of this.

Let me know if I've still not answered your question :)

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.