Advertisement
SergioRP

Edilson's Surviving Adventure 1.1

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