The game so far.. (moved from CeeProgramsForBeginnersDiscussion)
#include
struct RoomDefinition {
char *description;
RoomDefinition *nextRoom[2];
};
// Declare that these rooms exist, prior to defining what they are:
extern RoomDefinition footpath, forest, stream1, clearing, fairycircle;
extern RoomDefinition finishgame, bridge, fort, bed;
RoomDefinition footpath = {
"You are on a footpath... To go to the forest enter 1, to go to the stream enter 2\n",
{ &forest, &stream1 }
};
RoomDefinition forest = {
"You are in the forest. Enter 1 to go to the clearing, Enter 2 to return to the footpath\n",
{ &clearing, &footpath }
};
RoomDefinition clearing = {
"You are in the clearing. Enter 1 to go to the fairy circle or 2 to return to the forest.\n",
{ &fairycircle, &forest }
};
RoomDefinition fairycircle = {
"You are in the fairy circle. Enter 1 to leave the world or 2 to return to the clearing.\n",
{ &finishgame, &clearing }
};
RoomDefinition finishgame = {
"Thanks for playing. I hope you enjoyed exploring, see you soon.\n",
{ 0, 0 } // 0 means exit
};
RoomDefinition stream1 = {
"You are standing next to a stream. Enter 2 to find a bridge or 1 to go back to the footpath.\n",
{ &footpath, &bridge }
};
RoomDefinition bridge = {
"You have found a bridge. To cross the bridge to the fort enter 2 or 1 to go back to the stream.\n",
{ &stream1, &fort }
};
RoomDefinition fort = {
"You have reached the fort. Enter 2 to go to bed or 1 to go back to the bridge.\n",
{ &bridge, &bed }
};
RoomDefinition bed = {
"Night Night sleepy-head. Thank-you for playing.\n",
{ 0, &bridge } // 0 means exit
};
RoomDefinition *
move_somewhere(RoomDefinition *currentRoom)
{
RoomDefinition *nextRoom;
int userChoice;
std::cin >> userChoice;
if (userChoice == 1)
nextRoom = currentRoom->nextRoom[0];
else
nextRoom = currentRoom->nextRoom[1];
if (!nextRoom) { // exit if room pointer is 0
system("PAUSE");
exit(0);
}
return nextRoom;
}
main() {
RoomDefinition *room = &footpath;
while (1) {
std::cout << room->description;
room = move_somewhere(room);
}
}
// End of Susannah's code recast into the data-driven
// form that Jonathan recommended.
// Note that forest and clearing have paths leading to each other, which
// was what you wanted. But this is quite different from having functions
// forest() and clearing() call each other, which is what someone was talking
// about above.
// This is a natural confusion; the reason it doesn't work as well for them
// to call each other is that a function call must return to where
// it started. But that's not true of moving around in a forest. So the
// problem is correctly modeled as a change to data, and less correctly
// as a function call. -- dm
Here are a few obvious questions which beginners might ask -
Thanks. Hmm...as to rephrasing...hmm....