Hey guys,
I am working on a text game and I want to use a linked list to hold the room info.
I just wrote a little test program to get the fell for this.
I know this works but would this be a proper method for this?Code:#include <iostream> #include <string> enum {North,South,East,West}; struct room { std::string name; room *next[4]; }; int main() { char choice; room *start; room *walker; std::cout<<"Welcome to my game"<<std::endl; start = new room; start->name = "Starting Room"; for(int i = 0; i < 4; i++) { start->next[i] = NULL; } walker = start; walker->next[North] = new room; walker->next[North]->name = "North Room"; walker->next[South] = new room; walker->next[South]->name = "South Room"; walker->next[East] = new room; walker->next[East]->name = "East Room"; walker->next[West] = new room; walker->next[West]->name = "West Room"; while(choice != 'q') { std::cout<<"You are in the "<<walker->name<<std::endl; std::cout<<"Where do you want to go?"<<std::endl; std::cout<<"[N]orth [S]outh [E]ast [W]est [q] for exiting"<<std::endl; std::cin>>choice; switch(choice) { case 'n': if(walker->next[North] != NULL) { walker->next[North]->next[South] = walker; walker = walker->next[North]; } else { std::cout<<"You can't go there"<<std::endl; } break; case 's': if(walker->next[South] != NULL) { walker->next[South]->next[North]= walker; walker = walker->next[South]; } else { std::cout<<"You can't go there"<<std::endl; } break; case 'e': if(walker->next[East] != NULL) { walker->next[East]->next[West] = walker; walker = walker->next[East]; } else { std::cout<<"You can't go there"<<std::endl; } break; case 'w': if(walker->next[West] != NULL) { walker->next[West]->next[East] = walker; walker = walker->next[West]; } else { std::cout<<"You Can't go there"<<std::endl; } break; } } return 0; }
If not who wants to make fun of me and tell me how you would do it. Basically what I want to do is have a series of predefined rooms.



LinkBack URL
About LinkBacks



