Home
C Sci 40 - Adventure Island - Navigation
Classes

/*
This an example program that shows how navigation can be done, and
proposes data structures that may be useful.
*/
#include <iostream>
using namespace std;
enum roomName {BANANA_TREE,ISLAND,GANGPLANK,NO_ROOM};
//If you declare an array to be of size NO_ROOM, it will be of size 3, and
//BANANA_TREE, ISLAND, and GANGPLANK become the valid subscripts.  When you add
// more rooms, the array will automatically grow to match.
struct itemList
    {
    bool bananas;
    bool keys;
    bool knife;
    bool treasure;
    };
struct roomType
    {
    string shortDescription;
    string longDescription;
    roomName exitUp;// These are the rooms that you enter when you leave
    roomName exitDown;// in these directions.
    roomName exitNorth;
    roomName exitEast;
    roomName exitSouth;
    roomName exitWest;
    itemList items;//items in room
    bool visited;//true if the player has been in the room before.
                // used to choose long or short description
    };
struct puzzleType
    {
    bool gorillaSolved;//Initially false, true when puzzle is solved
    bool prisonerSolved;
    bool parrotSolved;
    };
struct playerType
    {
    string name;
    roomName currentRoom;
    itemList items;//what player is carrying
    };
   
void readCommand(string&);
void execute(string, playerType&,roomType[]);
void initialize(playerType&,roomType[]);

int main()
    {
    string command;
    playerType player;
    roomType rooms[NO_ROOM];
    initialize(player,rooms);
    readCommand(command);
    while (command!="quit")
        {
        execute(command,player,rooms);
        readCommand(command);       
        }
    cout<<"Thanks for playing!\n";
    return 0;
    }

//This function reads the command.  For now, it's a simple cin of one word.
void readCommand(string& command)
    {
    cout<<"Command? ";
    cin>>command;
    }
   
//Right now, all this does is recognize the one letter abbreviations for the
// directional commands.  Whole words should be recognized.  It should not matter
//if the commands are in upper or lower case.
void execute(string command, playerType& player,roomType rooms[NO_ROOM])
    {
    roomName oldRoom;
    oldRoom=player.currentRoom;
    if(command=="n")//Should work with whole words too.  Upper or lower case
        player.currentRoom=rooms[player.currentRoom].exitNorth;//should not matter.
    else if(command=="s")
        player.currentRoom=rooms[player.currentRoom].exitSouth;
    else if(command=="e")
        player.currentRoom=rooms[player.currentRoom].exitEast;
    else if(command=="w")
        player.currentRoom=rooms[player.currentRoom].exitWest;
    else if(command=="u")
        player.currentRoom=rooms[player.currentRoom].exitUp;
    else if(command=="d")
        player.currentRoom=rooms[player.currentRoom].exitDown;
    if(player.currentRoom==NO_ROOM)//If you went somewhere invalid,
        player.currentRoom=oldRoom;//go back where you were.
    cout<<rooms[player.currentRoom].shortDescription<<endl;// Use long or short
                                                    //description as needed
    }
   
//You can use a for loop to set all the descriptions to "", all the bools to
//false, and all the exits to NO_ROOM, and then only reset those values
// which are different.
void initialize(playerType& player ,roomType rooms[NO_ROOM])
    {
    player.currentRoom=ISLAND;
    player.items.bananas=false;//etc
    cout<<"What is your name? ";
    cin>>player.name; //or use getline(cin, player.name);
   
    rooms[BANANA_TREE].longDescription="There is a large banana tree here.";
    rooms[BANANA_TREE].longDescription+="  There is one bunch of ripe bananas";
    rooms[BANANA_TREE].longDescription+=" on the tree within reach.";
    rooms[BANANA_TREE].shortDescription="You are at the banana tree.";
    rooms[BANANA_TREE].exitUp=NO_ROOM;
    rooms[BANANA_TREE].exitDown=NO_ROOM;
    rooms[BANANA_TREE].exitNorth=NO_ROOM;
    rooms[BANANA_TREE].exitSouth=ISLAND;
    rooms[BANANA_TREE].exitEast=NO_ROOM;
    rooms[BANANA_TREE].exitWest=NO_ROOM;
    rooms[BANANA_TREE].items.bananas=true;//because there are bananas on the tree
    rooms[BANANA_TREE].items.keys=false;
    rooms[BANANA_TREE].items.knife=false;
    rooms[BANANA_TREE].items.treasure=false;

    rooms[ISLAND].shortDescription="You are on the island.";
    rooms[ISLAND].exitUp=NO_ROOM;
    rooms[ISLAND].exitDown=NO_ROOM;
    rooms[ISLAND].exitNorth=BANANA_TREE;
    rooms[ISLAND].exitSouth=GANGPLANK;
    rooms[ISLAND].exitEast=NO_ROOM;
    rooms[ISLAND].exitWest=NO_ROOM;
    rooms[ISLAND].items.bananas=false;
    rooms[ISLAND].items.keys=false;
    rooms[ISLAND].items.knife=false;
    rooms[ISLAND].items.treasure=false;

    rooms[GANGPLANK].shortDescription="You are on the gangplank.";
    rooms[GANGPLANK].exitUp=NO_ROOM;
    rooms[GANGPLANK].exitDown=NO_ROOM;
    rooms[GANGPLANK].exitNorth=ISLAND;
    rooms[GANGPLANK].exitSouth=NO_ROOM;// this is TOP_DECK in real game
    rooms[GANGPLANK].exitEast=NO_ROOM;
    rooms[GANGPLANK].exitWest=NO_ROOM;
    rooms[GANGPLANK].items.bananas=false;
    rooms[GANGPLANK].items.keys=false;
    rooms[GANGPLANK].items.knife=false;
    rooms[GANGPLANK].items.treasure=false;
    }