Advertisement
Guest User

Marko's Text Adventure

a guest
Feb 10th, 2012
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define PRINT_OPTIONS puts("What would you like to do?\n"\
  6.         "(F)ight\n(S)leep\n(Q)uit");
  7. #define PRINT_COMBAT_OPTIONS puts("What would you like to do?\n"\
  8.         "(A)ttack\n(R)un");
  9. #define PRINT_TITLE puts("**************************\n" \
  10.         "* Marko's Text Adventure *\n**************************")
  11.  
  12. #define YOUR_SWING 30
  13. #define THEIR_SWING 15
  14. #define RUN_CHANCE .20
  15. #define DRAND ((double) rand())/((double) RAND_MAX)
  16. #define CHECK_VALID_CHOICE if(choice[1] != '\n' && choice[1] != '\0'){\
  17.         while( (c = fgetc( stdin )) != EOF && c != '\n' );\
  18.         continue;\
  19. }
  20.  
  21. int main(){
  22.  
  23.     double  hp = 100.0,
  24.             enemy_hp,
  25.             damage;
  26.     int monsters_killed = 0;
  27.     char c;
  28.     char    name[256],
  29.     choice[3];
  30.  
  31.     srand(time(NULL));
  32.     PRINT_TITLE;
  33.     puts("What is your name?");
  34.  
  35.     fgets(name,254,stdin);
  36.     int i = 0;
  37.     for(;i <255;i++){
  38.         if(name[i] == '\n' || name[i] == '\0')
  39.             break;
  40.     }
  41.     name[i] = '\0';
  42.     printf("Hello, %s.\n",name);
  43.  
  44.     while(1){
  45.         /* Labels and goto's necessary with lack of multiple level breaks */
  46.         main_loop:
  47.         PRINT_OPTIONS;
  48.         fgets(choice,3,stdin);
  49.         switch(choice[0]){
  50.         /* User chose fight */
  51.         case 'F':
  52.         case 'f':
  53.             CHECK_VALID_CHOICE;
  54.             /* Begin fight loop */
  55.             puts("A monster appears!");
  56.             enemy_hp = DRAND*100.0;
  57.             while(1){
  58.                 printf("Your hp: %.2f\n",hp);
  59.                 printf("Monster hp: %.2f\n",enemy_hp);
  60.                 PRINT_COMBAT_OPTIONS;
  61.                 fgets(choice,3,stdin);
  62.                 switch(choice[0]){
  63.                 case 'A':
  64.                 case 'a':
  65.                     CHECK_VALID_CHOICE;
  66.                     puts("You swing wildly at the monster.");
  67.                     damage = DRAND*YOUR_SWING;
  68.                     printf("You deal %.2f damage to the monster.\n",damage);
  69.                     enemy_hp -= damage;
  70.                     if(enemy_hp < 0){
  71.                         puts("You killed the monster!");
  72.                         monsters_killed++;
  73.                         goto main_loop;
  74.                     }
  75.                     break;
  76.                 case 'R':
  77.                 case 'r':
  78.                     CHECK_VALID_CHOICE;
  79.                     puts("You attempt to run away!");
  80.                     if(DRAND < RUN_CHANCE){
  81.                         puts("You ran away successfully! Pussy.");
  82.                         goto main_loop;
  83.                     }
  84.                     puts("You failed to run away!");
  85.                     break;
  86.                 default:
  87.                     continue;
  88.                 }
  89.                 damage = DRAND * THEIR_SWING;
  90.                 printf("The monster distractions you with pictures from /r/aww"\
  91.                         " and strikes, dealing %.2f damage\n",damage);
  92.                 hp -= damage;
  93.                 if(hp <0){
  94.                     goto died;
  95.                 }
  96.  
  97.             }
  98.             break;
  99.         case 'S':
  100.         case 's':
  101.             CHECK_VALID_CHOICE;
  102.             puts("You decide to sleep for a couple hours. Not very exciting.");
  103.             break;
  104.         case 'Q':
  105.         case 'q':
  106.             CHECK_VALID_CHOICE;
  107.             puts("You grow tired of killing monsters and decide to take your"\
  108.                     " own life.");
  109.             goto died;
  110.             break;
  111.         default:
  112.             break;
  113.         }
  114.     }
  115.     died:
  116.     printf("You have died. However, you were able to take "\
  117.             "%d monsters with you before you went.\n", monsters_killed);
  118.     return 0;
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement