Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctype.h>
  3.  
  4. #define MAX_HEALTH 5
  5. #define HEALTH 0
  6. #define STICK 1
  7.  
  8. void init(void);
  9. void print_status(void);
  10. void print_past(char);
  11. void player_move(void);
  12. void ai_move(void);
  13. void reset(void);
  14.  
  15. int player[2] = {MAX_HEALTH, 2};
  16. int ai[2] = {MAX_HEALTH, 2};
  17. char youpast[255];
  18. char hepast[255];
  19. int level = -1;
  20.  
  21. int main(void) {
  22. init();
  23.  
  24. while(player[HEALTH] > 0 && ai[HEALTH] > 0) {
  25. player_move();
  26. ai_move();
  27. reset();
  28. print_status();
  29. }
  30. if(player[HEALTH] == 0)
  31. printf("\nYOU LOSE!!!!\n");
  32. else
  33. printf("\nYOU WIN!!!!\n");
  34.  
  35. return 0;
  36. }
  37.  
  38. void init(void) {
  39. printf("You:\t\tHim:\n");
  40. print_status();
  41. }
  42.  
  43. void print_status(void) {
  44. int x;
  45.  
  46. for(x = 0; x < player[HEALTH]; x++)
  47. printf("=");
  48.  
  49. printf(" \t");
  50.  
  51. for(x = 0; x < ai[HEALTH]; x++)
  52. printf("=");
  53.  
  54. printf(" \n");
  55. printf("H:%d/%d\t\tH:%d/%d\n", player[HEALTH], MAX_HEALTH, ai[HEALTH], MAX_HEALTH);
  56.  
  57. printf("Stick: %d\tStick: %d\n", player[STICK], ai[STICK]);
  58.  
  59. if(level >= 0) {
  60. print_past(youpast[level]);
  61. printf(" \t");
  62. print_past(hepast[level]);
  63. }
  64. printf("\n");
  65.  
  66. if(player[HEALTH] != 0 && ai[HEALTH] != 0) {
  67. printf("What will you do [S, B");
  68. if(player[STICK] != 0)
  69. printf(", P");
  70. printf("]: ");
  71. }
  72.  
  73. printf("\033[8D");
  74.  
  75. }
  76.  
  77. void print_past(char c) {
  78. switch(c) {
  79. case 'S':
  80. printf("Sharpened!");
  81. break;
  82. case 'B':
  83. printf("Blocked! ");
  84. break;
  85. case 'P':
  86. printf("Poked! ");
  87. break;
  88. }
  89. }
  90.  
  91. void player_move(void) {
  92. char c[10];
  93. int ok = 0;
  94. char choice;
  95.  
  96. level++;
  97.  
  98. if(level >= 255)
  99. level = 0;
  100.  
  101. do {
  102. fgets(c, 10, stdin);
  103. choice = toupper(c[0]);
  104.  
  105. if(choice != 'S' && choice != 'P' && choice != 'B') {
  106. ok = 1;
  107. printf("\033[1A");
  108. if(player[STICK] == 0)
  109. printf("\033[25C");
  110. else
  111. printf("\033[28C");
  112. } else
  113. ok = 0;
  114.  
  115. if(choice == 'P' && player[STICK] == 0) {
  116. ok = 1;
  117. printf("\033[1A");
  118. printf("\033[25C");
  119. }
  120.  
  121. } while(ok != 0);
  122.  
  123. switch(c[0]) {
  124. case 'S':
  125. case 's':
  126. youpast[level] = 'S';
  127. break;
  128. case 'B':
  129. case 'b':
  130. youpast[level] = 'B';
  131. break;
  132. case 'P':
  133. case 'p':
  134. youpast[level] = 'P';
  135. break;
  136. }
  137.  
  138. }
  139.  
  140. void ai_move(void) {
  141. if(ai[STICK] <= 0)
  142. hepast[level] = 'S';
  143. else
  144. hepast[level] = 'P';
  145. }
  146.  
  147. void reset(void) {
  148. if(hepast[level] == 'P') {
  149. ai[STICK]--;
  150.  
  151. if(youpast[level] == 'S')
  152. player[HEALTH]--;
  153. }
  154.  
  155. if(youpast[level] == 'P') {
  156. player[STICK]--;
  157.  
  158. if(hepast[level] == 'S')
  159. ai[HEALTH]--;
  160. }
  161.  
  162. if(hepast[level] == 'S')
  163. ai[STICK]++;
  164. if(youpast[level] == 'S')
  165. player[STICK]++;
  166.  
  167. printf("\033[5A");
  168. printf("\033[30D");
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement