Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define PRINT_OPTIONS puts("What would you like to do?\n"\
- "(F)ight\n(S)leep\n(Q)uit");
- #define PRINT_COMBAT_OPTIONS puts("What would you like to do?\n"\
- "(A)ttack\n(R)un");
- #define PRINT_TITLE puts("**************************\n" \
- "* Marko's Text Adventure *\n**************************")
- #define YOUR_SWING 30
- #define THEIR_SWING 15
- #define RUN_CHANCE .20
- #define DRAND ((double) rand())/((double) RAND_MAX)
- #define CHECK_VALID_CHOICE if(choice[1] != '\n' && choice[1] != '\0'){\
- while( (c = fgetc( stdin )) != EOF && c != '\n' );\
- continue;\
- }
- int main(){
- double hp = 100.0,
- enemy_hp,
- damage;
- int monsters_killed = 0;
- char c;
- char name[256],
- choice[3];
- srand(time(NULL));
- PRINT_TITLE;
- puts("What is your name?");
- fgets(name,254,stdin);
- int i = 0;
- for(;i <255;i++){
- if(name[i] == '\n' || name[i] == '\0')
- break;
- }
- name[i] = '\0';
- printf("Hello, %s.\n",name);
- while(1){
- /* Labels and goto's necessary with lack of multiple level breaks */
- main_loop:
- PRINT_OPTIONS;
- fgets(choice,3,stdin);
- switch(choice[0]){
- /* User chose fight */
- case 'F':
- case 'f':
- CHECK_VALID_CHOICE;
- /* Begin fight loop */
- puts("A monster appears!");
- enemy_hp = DRAND*100.0;
- while(1){
- printf("Your hp: %.2f\n",hp);
- printf("Monster hp: %.2f\n",enemy_hp);
- PRINT_COMBAT_OPTIONS;
- fgets(choice,3,stdin);
- switch(choice[0]){
- case 'A':
- case 'a':
- CHECK_VALID_CHOICE;
- puts("You swing wildly at the monster.");
- damage = DRAND*YOUR_SWING;
- printf("You deal %.2f damage to the monster.\n",damage);
- enemy_hp -= damage;
- if(enemy_hp < 0){
- puts("You killed the monster!");
- monsters_killed++;
- goto main_loop;
- }
- break;
- case 'R':
- case 'r':
- CHECK_VALID_CHOICE;
- puts("You attempt to run away!");
- if(DRAND < RUN_CHANCE){
- puts("You ran away successfully! Pussy.");
- goto main_loop;
- }
- puts("You failed to run away!");
- break;
- default:
- continue;
- }
- damage = DRAND * THEIR_SWING;
- printf("The monster distractions you with pictures from /r/aww"\
- " and strikes, dealing %.2f damage\n",damage);
- hp -= damage;
- if(hp <0){
- goto died;
- }
- }
- break;
- case 'S':
- case 's':
- CHECK_VALID_CHOICE;
- puts("You decide to sleep for a couple hours. Not very exciting.");
- break;
- case 'Q':
- case 'q':
- CHECK_VALID_CHOICE;
- puts("You grow tired of killing monsters and decide to take your"\
- " own life.");
- goto died;
- break;
- default:
- break;
- }
- }
- died:
- printf("You have died. However, you were able to take "\
- "%d monsters with you before you went.\n", monsters_killed);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement