fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. typedef struct TRoom
  5. {
  6. char desc[256];
  7. struct TRoom *north;
  8. struct TRoom *south;
  9. } Room;
  10.  
  11. int main()
  12. {
  13. Room northRoom, southRoom;
  14. Room *currentRoom;
  15. int c;
  16.  
  17. strcpy(northRoom.desc, "This is the north room");
  18. northRoom.north = NULL;
  19. northRoom.south = &southRoom;
  20. strcpy(southRoom.desc, "This is the south room");
  21. southRoom.north = &northRoom;
  22. southRoom.south = NULL;
  23.  
  24. currentRoom = &northRoom;
  25.  
  26. printf("n: north, s: south, q: quit\n");
  27.  
  28. do
  29. {
  30. Room *nextRoom = currentRoom;
  31. printf("%s\n", currentRoom->desc);
  32.  
  33. c = getchar();
  34. switch (c)
  35. {
  36. case 'n':
  37. nextRoom = currentRoom->north;
  38. break;
  39. case 's':
  40. nextRoom = currentRoom->south;
  41. break;
  42. }
  43.  
  44. if (nextRoom == NULL)
  45. {
  46. printf("Can't go there\n");
  47. }
  48. else
  49. {
  50. currentRoom = nextRoom;
  51. }
  52. } while (c != 'q');
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0s 1792KB
stdin
nssnq
stdout
n: north, s: south, q: quit
This is the north room
Can't go there
This is the north room
This is the south room
Can't go there
This is the south room
This is the north room