View Single Post
Old Mar 17th, 2008, 7:41 AM   #10
commodore
Programmer
 
Join Date: Nov 2005
Location: Estonia
Posts: 97
Rep Power: 0 commodore is an unknown quantity at this point
Re: Newb interactive fiction

I tried to take it further but it's now even more f*d up. I have trouble understanding this pointer stuff.
#include <iostream>
#include <string>
#include <map>
#include <cstdlib>

class Room
{
    public:
        std::string description;
        std::map<std::string, Room*> exits;
};

Room handle_input(std::string input, Room currentRoom)
{
    if (input=="quit" || input=="exit")
    {
        std::cout<<"Goodbye";
        std::exit(0);
    }
    
    else if (input=="n")
    {
        if (currentRoom.exits.find("n")!=currentRoom.exits.end())
        {
            currentRoom=currentRoom.exits["n"];
            std::cout<<currentRoom.description<<std::endl;
            return &currentRoom;
        }
        else {std::cout<<"There's no exit to north"<<std::endl;}
    } 

    else if (input=="look"){std::cout<<currentRoom.description<<std::endl;}
    
    else {std::cout<<"I don't understand what you're saying"<<std::endl;}
}

int main()
{
    Room firstRoom;
    firstRoom.description="Description of first room";
    Room* currentRoom=&firstRoom;
    
    Room secondRoom;
    secondRoom.description="You're in the second room";
    firstRoom.exits["n"]=&secondRoom;
    
    std::cout<<"Hi"<<"\n";
    std::string input;

    while (true)
    {
        std::cout<<">>> ";
        std::cin>>input;
        currentRoom=handle_input(input, *currentRoom);
    }
    return 0;
}
commodore is offline   Reply With Quote