Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. #include "projekti.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. void get_line(char line[MAX_LINE_LENGTH], char* command) {
  7. printf("// ");
  8. if (fgets(line, MAX_LINE_LENGTH, stdin) && sscanf(line, "%c", command))
  9. return;
  10. else
  11. *command = '?';
  12. }
  13.  
  14. fighter* add_fighter(fighter* fighters, char line[MAX_LINE_LENGTH]) {
  15. char name[MAX_NAME_LENGTH] = { 0 };
  16. char weapon[MAX_NAME_LENGTH] = { 0 };
  17. int hp, dmg;
  18.  
  19. if (sscanf(line, "A %s %d %s %d", (char*)& name, &hp, (char*)& weapon, &dmg) != 4) {
  20. printf("Use valid command specifers!\n");
  21. return fighters;
  22. }
  23. // Checking For Invalid Input
  24. for (int i = 0; fighters[i].name[0] != 0; i++) {
  25. if (!strcmp(fighters[i].name, name)) {
  26. printf("Fighter %s already exists!\n", name);
  27. return fighters;
  28. }
  29. }
  30. if (hp <= 0 || dmg <= 0) {
  31. if (hp <= 0)
  32. printf("HP must be greater than 0!\n");
  33. if (dmg <= 0)
  34. printf("Damage must be greater than 0!\n");
  35. return fighters;
  36. }
  37. // Adding The Fighter
  38. unsigned int num_fighters = 1;
  39. for (unsigned int i = 0; 1; i++) {
  40. if (fighters[i].name[0] == 0) {
  41. strcpy(fighters[i].name, name);
  42. strcpy(fighters[i].weapon, weapon);
  43. fighters[i].hp = hp;
  44. fighters[i].dmg = dmg;
  45. num_fighters = i + 1;
  46. break;
  47. }
  48. }
  49. fighter* fighters_new = realloc(fighters, (num_fighters + 2) * sizeof(fighter));
  50. if (!fighters_new) {
  51. fprintf(stderr, "Memory Allocation failed!\n");
  52. return fighters;
  53. }
  54. char null_str[MAX_NAME_LENGTH] = { 0 };
  55. strcpy(fighters_new[num_fighters].name, null_str);
  56. strcpy(fighters_new[num_fighters].weapon, null_str);
  57. fighters_new[num_fighters].hp = 0;
  58. fighters_new[num_fighters].exp = 0;
  59. fighters_new[num_fighters].dmg = 0;
  60. printf("Successfully added fighter %s\n", name);
  61. return fighters_new;
  62. }
  63.  
  64. void attack(fighter* fighters, char line[MAX_LINE_LENGTH]) {
  65. char attacker[MAX_NAME_LENGTH] = { 0 };
  66. char target[MAX_NAME_LENGTH] = { 0 };
  67. int attacker_i = -1;
  68. int target_i = -1;
  69.  
  70. if (sscanf(line, "H %s %s", attacker, target) != 2) {
  71. printf("Use valid command specifers!\n");
  72. return;
  73. }
  74. // Checking For Invalid Input
  75. for (int i = 0; fighters[i].name[0] != 0; i++) {
  76. if (!strcmp(fighters[i].name, attacker))
  77. attacker_i = i;
  78. if (!strcmp(fighters[i].name, target))
  79. target_i = i;
  80. }
  81. if (attacker_i < 0 || target_i < 0) {
  82. if (attacker_i < 0)
  83. printf("Attacker not found!\n");
  84. if (target_i < 0)
  85. printf("Target not found!\n");
  86. return;
  87. }
  88. if (!strcmp(attacker, target)) {
  89. printf("Attacker can not be the target!\n");
  90. return;
  91. }
  92. if (fighters[attacker_i].hp == 0 || fighters[target_i].hp == 0) {
  93. if (fighters[attacker_i].hp == 0)
  94. printf("%s is already dead!\n", attacker);
  95. if (fighters[target_i].hp == 0)
  96. printf("%s is already dead!\n", target);
  97. return;
  98. }
  99. //Doing The Attacking
  100. fighters[target_i].hp -= fighters[attacker_i].dmg;
  101. if (fighters[target_i].hp <= 0) {
  102. fighters[target_i].hp = 0;
  103. fighters[attacker_i].exp += EXP_GAIN;
  104. printf("%s killed %s with %s!\n", attacker, target, fighters[attacker_i].weapon);
  105. printf("%s gained %d exp!\n", attacker, EXP_GAIN);
  106. return;
  107. }
  108. else {
  109. printf("%s attacked %s with %s by %d damage!\n",
  110. attacker, target, fighters[attacker_i].weapon, fighters[attacker_i].dmg);
  111. printf("%s has %d hitpoints remaining!\n", target, fighters[target_i].hp);
  112. }
  113. }
  114.  
  115. void print_fighters(fighter* fighters) {
  116. //FIX THIS ** FIX THIS ** FIX THIS ** FIX THIS !!!!!!!!!!!!!!!!!!!!!!!!!
  117. if (fighters[0].name[0] == 0)
  118. printf("There are no fighters!\n");
  119. for (int i = 0; fighters[i].name[0] != 0; i++) {
  120. printf("%s: EXP=%d, HP=%d, WPN=%s, DMG=%d\n",
  121. fighters[i].name, fighters[i].exp,
  122. fighters[i].hp, fighters[i].weapon,
  123. fighters[i].dmg);
  124. }
  125. }
  126.  
  127. void write_game(fighter* fighters, char line[MAX_LINE_LENGTH]) {
  128. char filename[MAX_NAME_LENGTH] = { 0 };
  129. if (sscanf(line, "W %s", (char*)& filename) != 1) {
  130. printf("Use valid command specifiers!\n");
  131. return;
  132. }
  133. FILE* file = fopen(filename, "w");
  134. if (!file) {
  135. printf("Could not open file %s!\n", filename);
  136. return;
  137. }
  138. int num_fighters = 0;
  139. for (int i = 0; fighters[i].name[0] != 0; i++) {
  140. num_fighters = i + 1;
  141. }
  142. if (fwrite(fighters, sizeof(fighter), num_fighters, file) != num_fighters) {
  143. printf("Writing failed!\n");
  144. fclose(file);
  145. return;
  146. }
  147. printf("Writing was successful!\n");
  148. fclose(file);
  149. }
  150.  
  151. fighter* read_game(fighter* fighters, char line[MAX_LINE_LENGTH]) {
  152. char filename[MAX_NAME_LENGTH] = { 0 };
  153. int num_fighters = 0;
  154.  
  155. if (sscanf(line, "O %s", (char*)& filename) != 1) {
  156. printf("Use valid command specifiers!\n");
  157. return fighters;
  158. }
  159. FILE* file = fopen(filename, "r");
  160. if (!file) {
  161. printf("Could not open file %s!\n", filename);
  162. return fighters;
  163. }
  164. while (fgetc(file) != EOF) {
  165. fseek(file, sizeof(fighter) - 1, SEEK_CUR);
  166. num_fighters++;
  167. }
  168. fseek(file, 0, SEEK_SET);
  169. fighter* fighters_new = malloc((num_fighters + 1) * sizeof(fighter));
  170. if (!fighters_new) {
  171. printf("Memory allocation failed!\n");
  172. fclose(file);
  173. return fighters;
  174. }
  175. if (fread(fighters_new, sizeof(fighter), num_fighters, file) != num_fighters) {
  176. printf("Reading failed!\n");
  177. fclose(file);
  178. return fighters;
  179. }
  180. char null_str[MAX_NAME_LENGTH] = { 0 };
  181. strcpy(fighters_new[num_fighters].name, null_str);
  182. strcpy(fighters_new[num_fighters].weapon, null_str);
  183. fighters_new[num_fighters].hp = 0;
  184. fighters_new[num_fighters].exp = 0;
  185. fighters_new[num_fighters].dmg = 0;
  186. printf("Reading was successful!\n");
  187. fclose(file);
  188. return fighters_new;
  189. }
  190.  
  191. int main(void) {
  192. printf("%d", sizeof(fighter) + sizeof(char));
  193. fighter* fighters = malloc(sizeof(fighter));
  194. if (!fighters) {
  195. fprintf(stderr, "Memory Allocation Failed!");
  196. return -1;
  197. }
  198. fighter null_fighter = { {0}, {0}, 0, 0, 0 };
  199. fighters[0] = null_fighter;
  200.  
  201. char line[MAX_LINE_LENGTH], command;
  202. int game_on = 1;
  203.  
  204. while (game_on) {
  205. get_line(line, &command);
  206. switch (command) {
  207. case ('A'):
  208. fighters = add_fighter(fighters, line);
  209. break;
  210. case ('H'):
  211. attack(fighters, line);
  212. break;
  213. case ('L'):
  214. print_fighters(fighters);
  215. break;
  216. case ('W'):
  217. write_game(fighters, line);
  218. break;
  219. case ('O'):
  220. fighters = read_game(fighters, line);
  221. break;
  222. case ('Q'):
  223. free(fighters);
  224. game_on = 0;
  225. break;
  226. default:
  227. printf("Use a valid command!\n");
  228. break;
  229. };
  230. }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement