View Single Post
Old Mar 17th, 2008, 8:43 AM   #11
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 532
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Newb interactive fiction

Here's a corrected version. class Room is passed both directions as a pointer.
Room* handle_input(std::string input, Room* currentRoom)
{
    if (input=="quit" || input=="exit")
    {
        std::cout<<"Goodbye";
        std::exit(0);
    }
    
    else if (input=="n")
    {
        std::map<std::string, Room*>::iterator it;
        if ( (it = currentRoom->exits.find("n"))!=currentRoom->exits.end())
        {
            currentRoom = it->second;
            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;}
    return 0;
}

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;
}
__________________
I Like Ike. Vote for Dwight Eisenhower this November.
--This message brought to you by the the Procrastinators Club Of America.
Ancient Dragon is offline   Reply With Quote