Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct room{
  6. char name[ 32 ];
  7. struct room * north;
  8. struct room * south;
  9. struct room * east;
  10. struct room * west;
  11. char visited;
  12. int type;
  13. struct item * thing;
  14. };
  15.  
  16. struct item{
  17. char name[ 32 ];
  18. int type;
  19. int points;
  20. };
  21.  
  22. struct player{
  23. struct room * location;
  24. int turns;
  25. int points;
  26. };
  27.  
  28. void describe_room( struct room * );
  29. void wrap_up( struct player * );
  30. int process_room_type( struct player * );
  31.  
  32. int main( int argc, char * argv[] ){
  33.  
  34. int loop = 1;
  35. char command = 'q';
  36.  
  37. struct item * coffee_maker = ( struct item * )malloc( sizeof( struct item ) );
  38. struct item * penny_of_doom = ( struct item * )malloc( sizeof( struct item ) );
  39.  
  40. struct room * room01 = ( struct room * )malloc( sizeof( struct room ) );
  41. struct room * room02 = ( struct room * )malloc( sizeof( struct room ) );
  42. struct room * room03 = ( struct room * )malloc( sizeof( struct room ) );
  43. struct room * room04 = ( struct room * )malloc( sizeof( struct room ) );
  44. struct room * room05 = ( struct room * )malloc( sizeof( struct room ) );
  45. struct room * room06 = ( struct room * )malloc( sizeof( struct room ) );
  46. struct room * room07 = ( struct room * )malloc( sizeof( struct room ) );
  47. struct room * room08 = ( struct room * )malloc( sizeof( struct room ) );
  48. struct room * room09 = ( struct room * )malloc( sizeof( struct room ) );
  49.  
  50. struct player * p = ( struct player * )malloc( sizeof( struct player ) );
  51. p->location = room01;
  52. p->turns = 0;
  53. p->points = 0;
  54.  
  55. strcpy( coffee_maker->name, "Coffee Maker+1" );
  56. coffee_maker->type = 0;
  57. coffee_maker->points = 5;
  58.  
  59. strcpy( penny_of_doom->name, "dangerous looking penny" );
  60. penny_of_doom->type = 1;
  61. penny_of_doom->points = -5;
  62.  
  63. strcpy( room01->name, "Room One" );
  64. room01->north = NULL;
  65. room01->south = room04;
  66. room01->east = room02;
  67. room01->west = NULL;
  68. room01->visited = 'f';
  69. room01->type = 0;
  70. room01->thing = NULL;
  71.  
  72. strcpy( room02->name, "Room Two" );
  73. room02->north = NULL;
  74. room02->south = room05;
  75. room02->east = room03;
  76. room02->west = room01;
  77. room02->visited = 'f';
  78. room02->type = 0;
  79. room02->thing = NULL;
  80.  
  81. strcpy( room03->name, "Room Three" );
  82. room03->north = NULL;
  83. room03->south = NULL;
  84. room03->east = NULL;
  85. room03->west = room02;
  86. room03->visited = 'f';
  87. room03->type = 0;
  88. room03->thing = coffee_maker;
  89.  
  90. strcpy( room04->name, "Room Four" );
  91. room04->north = room01;
  92. room04->south = NULL;
  93. room04->east = room05;
  94. room04->west = NULL;
  95. room04->visited = 'f';
  96. room04->type = 0;
  97. room04->thing = NULL;
  98.  
  99. strcpy( room05->name, "Room Five" );
  100. room05->north = room02;
  101. room05->south = room08;
  102. room05->east = room06;
  103. room05->west = room04;
  104. room05->visited = 'f';
  105. room05->type = 3;
  106. room05->thing = penny_of_doom;
  107.  
  108. strcpy( room06->name, "Room Six" );
  109. room06->north = NULL;
  110. room06->south = NULL;
  111. room06->east = NULL;
  112. room06->west = room05;
  113. room06->visited = 'f';
  114. room06->type = 0;
  115. room06->thing = NULL;
  116.  
  117. strcpy( room07->name, "Room Seven" );
  118. room07->north = NULL;
  119. room07->south = NULL;
  120. room07->east = NULL;
  121. room07->west = NULL;
  122. room07->visited = 'f';
  123. room07->type = 2;
  124. room07->thing = NULL;
  125.  
  126. strcpy( room08->name, "Room Eight" );
  127. room08->north = room05;
  128. room08->south = NULL;
  129. room08->east = room09;
  130. room08->west = NULL;
  131. room08->visited = 'f';
  132. room08->type = 0;
  133. room08->thing = NULL;
  134.  
  135. strcpy( room09->name, "Room Nine" );
  136. room09->north = NULL;
  137. room09->south = NULL;
  138. room09->east = NULL;
  139. room09->west = room08;
  140. room09->visited = 'f';
  141. room09->type = 1;
  142. room09->thing = NULL;
  143.  
  144. while( loop ){
  145. loop = process_room_type( p );
  146. if( !loop ){
  147. break;
  148. }
  149. if( p->turns >= 10 ){
  150. break;
  151. }
  152. describe_room( p->location );
  153. printf( "What do you want to do? " );
  154. fflush( stdin );
  155. command = getchar();
  156. switch( command ){
  157. case 'q' :
  158. printf( "Thanks for playing!\n" );
  159. loop = 0;
  160. break;
  161. case 'd' :
  162. p->location->visited = 'f';
  163. describe_room( p->location );
  164. break;
  165. case 'n' :
  166. if( p->location->north ){
  167. p->location = p->location->north;
  168. }else{
  169. printf( "You can't go that way!\n" );
  170. }
  171. break;
  172. case 's' :
  173. if( p->location->south ){
  174. p->location = p->location->south;
  175. }else{
  176. printf( "You can't go that way!\n" );
  177. }
  178. break;
  179. case 'e' :
  180. if( p->location->east ){
  181. p->location = p->location->east;
  182. }else{
  183. printf( "You can't go that way!\n" );
  184. }
  185. break;
  186. case 'w' :
  187. if( p->location->west ){
  188. p->location = p->location->west;
  189. }else{
  190. printf( "You can't go that way!\n" );
  191. }
  192. break;
  193. case 't' :
  194. if( p->location->thing ){
  195. p->points = p->points + p->location->thing->points;
  196. if( p->location->thing->type == 1 ){
  197. printf( "You feel a little weird.\n" );
  198. p->location->thing = NULL;
  199. p->location = room07;
  200. }else{
  201. p->location->thing = NULL;
  202. }
  203.  
  204. }
  205. break;
  206. default:
  207. printf( "Please enter a valid command (n, s, e, w, t, d, or q to quit).\n" );
  208. break;
  209. }
  210. p->turns++;
  211. }
  212.  
  213. wrap_up( p );
  214. return 0;
  215. }
  216.  
  217. int process_room_type( struct player * p ){
  218. int rv = 1; // continue
  219. struct room * temp = NULL;
  220. if( p->location->type == 1 ){
  221. printf( "You have won!\n" );
  222. p->points = p->points + 10;
  223. rv = 0;
  224. }else if( p->location->type == 2 ){
  225. printf( "You have died!\n" );
  226. p->points = p->points - 5;
  227. rv = 0;
  228. }else if( p->location->type == 3 ){
  229. // The spinny room
  230. printf( "You feel a little dizzy!\n" );
  231. temp = p->location->north;
  232. p->location->north = p->location->east;
  233. p->location->east = p->location->south;
  234. p->location->south = p->location->west;
  235. p->location->west = temp;
  236. }
  237. return rv;
  238. }
  239.  
  240. void wrap_up( struct player * p ){
  241. printf( "You played for %d turns\n", p->turns );
  242. if( p->turns > 9 ){
  243. p->points = p->points - 5;
  244. printf( "You lost 5 points for being too slow!\n" );
  245. }
  246. printf( "You scored: %d points!\n", p->points );
  247. }
  248.  
  249. void describe_room( struct room * r ){
  250. printf( "You are in %s\n", r->name );
  251. if( r->visited == 'f' ){
  252. if( r->north ){
  253. printf( "There is a passage to the north\n" );
  254. }
  255. if( r->south ){
  256. printf( "There is a passage to the south\n" );
  257. }
  258. if( r->east ){
  259. printf( "There is a passage to the east\n" );
  260. }
  261. if( r->west ){
  262. printf( "There is a passage to the west\n" );
  263. }
  264. r->visited = 't';
  265. }
  266. if( r->thing ){
  267. printf( "There is a %s here.\n", r->thing->name );
  268. }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement