Advertisement
SergioRP

Edilson's Surviving Adventure

Aug 17th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6.  
  7. struct player {
  8.     char name[20];
  9.     float health = 100;
  10.     float maxHealth = 100;
  11.     float attack = 10;
  12.     int level = 1;
  13.     float media = 10;
  14.     int age;
  15.     bool defeated = false;
  16. };
  17.  
  18. void remove_all_chars(char* str, char c) { //http://stackoverflow.com/questions/9895216/how-to-remove-all-occurrences-of-a-given-character-from-string-in-c
  19.     char *pr = str, *pw = str;
  20.     while (*pr) {
  21.         *pw = *pr++;
  22.         pw += (*pw != c);
  23.     }
  24.     *pw = '\0';
  25. }
  26.  
  27. struct player player;
  28. struct player otavio;
  29. struct player toninho;
  30. int i, room, levelToninho = 1;
  31. time_t timerStart = time(0);
  32. char topico[3];
  33. bool gameStarted = false, gameWon = false;
  34.  
  35. void printTitle(bool clearScreen = true) {
  36.     if (clearScreen)
  37.         system("cls");
  38.    
  39.     printf("************************************"); if(gameStarted) printf("  Nome: %s", player.name); printf("\n");
  40.     printf("**                                **"); if(gameStarted) printf("  Idade: %i", player.age); printf("\n");
  41.     printf("**         WELCOME TO THE         **"); if(gameStarted) printf("  Level: %i", player.level); printf("\n");
  42.     printf("**       EDILSON'S SURVIVING      **"); if(gameStarted) printf("  HP: %.0f", player.health); printf("\n");
  43.     printf("**            ADVENTURE           **"); if(gameStarted) printf("  Media: %.1f", player.media); printf("\n");
  44.     printf("**                                **"); if(gameStarted) printf("  Ataque: %.1f", player.attack); printf("\n");
  45.     printf("************************************"); printf("\n");
  46. }
  47.  
  48. void printRooms() {
  49.     printf("\nEscolha uma sala para entrar:\n\n");
  50.     printf("**************     **************  \n");
  51.     printf("**************     **************  \n");
  52.     printf("*** OTAVIO ***     *** TONINHO **  \n");
  53.     printf("*** SALA 1 ***     *** SALA 2 ***  \n");
  54.     printf("**************     **************  \n");
  55.     printf("**********( )*     **********( )*  \n");
  56.     printf("**************     **************  \n");
  57.     printf("**************     **************  \n");
  58.    
  59.     roomChoose:
  60.     printf("Sala: ");
  61.     scanf("%i", &room);
  62.     if (room < 1 || room > 2)
  63.         goto roomChoose;
  64.        
  65.     printTitle();
  66. }
  67.  
  68. float passedTime() {
  69.     return time(0) - timerStart;
  70. }
  71. void resetTimer() {
  72.     timerStart = time(0);
  73. }
  74. void levelUp() {
  75.     player.level++;
  76.     player.attack *= 1.25;
  77. }
  78.  
  79. void otavioCreate(int topicos, int timer) {
  80.    
  81.     if (otavio.health > 0) {
  82.    
  83.         printf("Um Otavio (HP: %.0f) selvagem acaba de entrar na sala!\nRapido, vc tem %i segundos para digitar %i topicos\n\n", otavio.health, timer, topicos);
  84.         resetTimer();
  85.         for (i = 0; i < topicos; i++) {
  86.             getTopic:
  87.             printf("Digite o topico %i: ", i + 1);
  88.             fflush(stdin);
  89.             gets(topico);
  90.             if (strlen(topico) < 3) {
  91.                 printf("Seu topico eh muito curto!\n");
  92.                 goto getTopic;
  93.             }
  94.         }
  95.        
  96.         if (passedTime() > timer) {
  97.             printf("\nVc nao digitou a tempo. Vc perdeu 5 hp e meio ponto na media\n");
  98.             player.media -= 0.5;
  99.             player.health -= 5;
  100.         } else {
  101.             otavio.health -= player.attack;
  102.             printf("\nVc venceu o Otavio (HP: %.1f) nesta batalha! Prepare-se para a proxima.\n", otavio.health);
  103.             levelUp();
  104.         }
  105.     } else {
  106.         printf("Voce ja venceu o otavio!");
  107.         otavio.defeated = true;
  108.         system("pause");
  109.     }
  110. }
  111.  
  112. bool toninhoFunction(char derivada[30]) {
  113.     char resposta[30];
  114.     printf("\nDigite sua resposta: ");
  115.     fflush(stdin);
  116.     gets(resposta);
  117.     for(i = 0; resposta[i]; i++){
  118.         resposta[i] = tolower(resposta[i]);
  119.     }
  120.    
  121.     remove_all_chars(resposta, '\(');
  122.     remove_all_chars(resposta, '\)');
  123.     remove_all_chars(resposta, ' ');
  124.    
  125.     if (strcmp(resposta, derivada) == 0)
  126.         return true;
  127.     else
  128.         return false;
  129. }
  130.  
  131. void toninhoCreate(int timer) {
  132.     if (toninho.health == 0) {
  133.         printf("Voce ja venceu o jovem Toninho!");
  134.         toninho.defeated = true;
  135.     } else {
  136.        
  137.         printf("Um jovem Toninho acaba de entrar na sala com oculos escuros!\nRapido, vc tem %i segundos para derivar a funcao: \n\n", timer);
  138.         resetTimer();
  139.         bool beatToninho = false;
  140.         switch(levelToninho) {
  141.             case 1:
  142.                 printf("x^2 + 5x^5");
  143.                 beatToninho = toninhoFunction("2x+25x^4");
  144.                 break;
  145.             case 2:
  146.                 printf("sen(2x)");
  147.                 beatToninho = toninhoFunction("2cos2x");
  148.                 break;
  149.             case 3:
  150.                 printf("ln(27x)");
  151.                 beatToninho = toninhoFunction("27/27x");
  152.                 break;
  153.             case 4:
  154.                 printf("e^(4x^2)");
  155.                 beatToninho = toninhoFunction("8x*e^4x^2");
  156.                 break;
  157.             default:
  158.                 toninho.health = 0;
  159.                 printf("Voce ja venceu o jovem Toninho!");
  160.                 break;
  161.         }
  162.    
  163.         if (passedTime() > timer)
  164.             beatToninho = false;
  165.        
  166.         if (beatToninho) {
  167.             printf("Voce venceu o toninho nesta batalha!");
  168.             levelToninho++;
  169.             levelUp();
  170.         } else {
  171.             printf("Voce nao conseguiu vencer o toninho desta vez! -10hp e -0.5 na media.");
  172.             player.health -= 10;
  173.             player.media -= 0.5;
  174.         }
  175.         printf("\n");
  176.     }
  177. }
  178.  
  179. int main() {
  180.     printTitle();
  181.    
  182.     getName:
  183.    
  184.     printf("Digite seu nome: ");
  185.     fflush(stdin);
  186.     gets(player.name);
  187.    
  188.     printTitle();
  189.    
  190.     if (strlen(player.name) > 15) {
  191.         printf("Vsf nome grande da porra, digita isso direito\n");
  192.         goto getName;
  193.     }
  194.     if (strlen(player.name) < 4) {
  195.         printf("Eh pra digitar seu nome, nao as siglas dele\nfdp\n");
  196.         goto getName;
  197.     }
  198.     printTitle();
  199.    
  200.     printf("Ola, %s. Digite sua idade: ", player.name);
  201.     getAge:
  202.     scanf("%i", &player.age);
  203.     printTitle();
  204.    
  205.     bool ageProblem = true;
  206.    
  207.     if (player.age < 17) {
  208.         if (player.age < 6)
  209.             printf("Parabens, vc aprendeu a ler cedo, hein?\n");
  210.         else
  211.             printf("Parabens, vc entrou na faculdade cedo, hein?\n");
  212.         printf("fdp digite sua idade: ");
  213.     }
  214.     else if (player.age > 100) {
  215.         printf("PARABENS VC EH UM ANCIAO E TA ESTUDANDO NA UNAERP\nVC VENCEU NA VIDA\nVai digitar sua idade de verdade cusao: ");
  216.     }
  217.     else if (player.age > 70)
  218.         printf("Ta velho hein cusao. Digita isso direito: ");
  219.     else
  220.         ageProblem = false;
  221.        
  222.     if (ageProblem)
  223.         goto getAge;
  224.    
  225.     gameStarted = true;
  226.    
  227.     do {
  228.         printTitle();
  229.         printRooms();
  230.        
  231.         if (room == 1) {
  232.             otavioCreate(4 + player.level, 20 - player.level * 2);
  233.         }
  234.         if (room == 2) {
  235.             toninhoCreate(30);
  236.         }
  237.        
  238.         system("PAUSE");
  239.        
  240.         if (toninho.defeated && otavio.defeated)
  241.             gameWon = true;
  242.        
  243.     } while (player.health > 0 && player.media >= 5 && !gameWon);
  244.    
  245.     printTitle();
  246.     if (gameWon) {
  247.         printf("Parabens, %s! Voce ganhou! Considere fazer alguma coisa da vida agora.", player.name);
  248.     } else {
  249.         printf("Voce perdeu o jogo do edilson. Que triste, cara");
  250.     }
  251.    
  252.     return 0;  
  253. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement