Cpp Learning Discussion

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 -

  1. If "char *" is replaced by "string " in the definitions, what exactly is the difference in the structure?
  1. Are there alternatives to struct in C++ which achieve a similar overall structure?
  1. What kinds of structure, if any, are NOT readily accomplishable in C++, even though they are in other languages?
  1. What is the point of putting a space after an opening brace and before a closing brace?
  2. The "->" syntax looks clumsy (two characters for one symbol). Any comments on it?
  1. Certain minor aspects of coding style are visible in the above. Can someone with the SAMS book comment on whether it uses the same conventions?
  1. The statements are given in a sensible order, but are there any arcane rules about order or positioning of statements, especially definitions?

Thanks. Hmm...as to rephrasing...hmm....


CeePlusPlusRoadMap CategoryCpp