Guest User

Untitled

a guest
Jun 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct{
  6. char name[50];
  7. char symbol;
  8. int x;
  9. int y;
  10. int moves;
  11. } hero;
  12.  
  13. typedef struct{
  14. char organ[50];
  15. int numVil;
  16. char name[50];
  17. int x[50];
  18. int y[50];
  19. } villains;
  20.  
  21.  
  22. void read_hero ( char *file, hero *h )
  23. { FILE *heroF;
  24. int save=1;
  25. heroF = fopen("saveName.hero","r");
  26.  
  27. if(heroF== NULL){
  28. heroF = fopen("default.hero","r");
  29. save = 0;
  30. }
  31.  
  32. //gets hero info
  33. fgets(buff, 256, fp);
  34. sscanf(buff, "%s", h->name);
  35. fgets(buff, 256, fp);
  36. sscanf(buff, "%c", &h->symbol);
  37. fgets(buff, 256, fp);
  38. sscanf(buff, "%d,%d", &h->x, &h->y);
  39. fgets(buff, 256, fp);
  40. sscanf(buff, "%d", &h->moves);
  41. fclose(heroF);
  42.  
  43. FILE *heroNewF;
  44.  
  45. //writes hero data to newSave.hero
  46. heroNewF = fopen("saveName.hero","w+");
  47. fprintf(heroNewF,"%s\n",h.name);
  48. fprintf(heroNewF,"%c\n",&h.symbol);
  49. fprintf(heroNewF,"%d,%d\n",&h.x,&h.y);
  50. fprintf(heroNewF,"%d\n",&h.moves);
  51. fclose(heroNewF);
  52. }
  53.  
  54. void print_hero ( hero *h )
  55. {//maybe this should be in the read location.
  56. printf("Name: %s\nSym: %c\nLoc: (%d,%d)\nMoves: %d\n", h->name, h->symbol, h->x, h->y, h->moves);
  57. }
  58.  
  59. void read_villain(char *file, villains *v){
  60. FILE *vilF;
  61.  
  62. vilF = fopen("saveName.villains","r");
  63. if(vilF==NULL){
  64. vilF = fopen("default.villains","r");
  65. }
  66.  
  67. //gets villain info
  68. fgets(buff, 256, fp);
  69. sscanf(buff, "%s\n", v->organ);
  70. fgets(buff, 256, fp);
  71. sscanf(buff, "%d\n", &v->numVil);
  72.  
  73. int c = 0;
  74. for(c=0; c<&v.numVil; c++){
  75. fscanf(vilF, "%c\n",v->name[c]);
  76. fscanf(vilF, "%d,%d\n",v->x[c],v->y[c]);
  77.  
  78. }
  79. c=0;
  80.  
  81. fclose(vilF);
  82.  
  83. FILE *vilFSave;
  84.  
  85. //writes villains data to newSave.villains
  86. vilFSave = fopen("saveName.villains","w+");
  87. fprintf(vilFSave,"%s\n",v->organization);
  88. fprintf(vilFSave,"%d\n",&v.numVillains);
  89.  
  90. for(c=0; c<&v.numVil; c++) {
  91. fprintf(vilFSave,"%c\n",v->name[c]);
  92. fprintf(vilFSave,"%d,%d\n",v->x[c],v->y[c]);
  93. counter++;
  94. }
  95. fclose(vilFSave);
  96.  
  97. }
  98.  
  99.  
  100.  
  101. void print_villain (villains *v )
  102. {
  103. printf("Name: %s\nSym: %c\nLoc: (%d,%d)\nMoves: %d\n", v->name, v->x, v->y);
  104. }
  105.  
  106.  
  107.  
  108.  
  109. int main(void) {
  110.  
  111. //print info about hero
  112. hero my_hero;
  113. read_hero("saveName.hero", &my_hero);
  114. print_hero(&my_hero);
  115.  
  116. //print stuff about villains
  117. villains my_vil;
  118. read_villain("saveName.villains", &my_vil);
  119. print_villain(&my_vil);
  120.  
  121.  
  122.  
  123. //now map stuff
  124. FILE *mapF;
  125. //loading *.map file
  126. mapF = fopen("saveName.map","r");
  127. if(mapF==NULL) {mapF = fopen("default.map","r");}
  128. int mapX;
  129. int mapY;
  130. fscanf(mapF,"%d,%d\n",&mapY,&mapX);
  131. char map[mapY][mapX];
  132.  
  133. //counters
  134. int yC = 0;
  135. int xC = 0;
  136. int temp;
  137.  
  138. //extracting map data
  139. while(yC!=mapY) {
  140. //if still on X line
  141. if(xC < mapX){
  142. fscanf(mapF, "%c",&map[yC][xC]);
  143. xC++;
  144. }
  145. //up a row
  146. if(xC == mapX)
  147. {
  148. yC++;
  149. xC = 0;
  150. fscanf(mapF, "\n",&temp);
  151. }
  152. }
  153. fclose(mapF);
  154. //create new map
  155. FILE *mapFNew;
  156.  
  157. //writes map to newSave.map
  158. mapFNew = fopen("saveName.map","w+");
  159. xC = 0;
  160. yC = 0;
  161. fprintf(mapFNew,"%d,%d\n",mapY,mapX);
  162. while(yC != mapY){
  163. while(xC != mapX){
  164. fprintf(mapFNew,"%c",map[yC][xC]);
  165. xC++;
  166. }
  167. yC++;
  168. xC = 0;
  169. fprintf(mapFNew,"\n");
  170. }
  171. fclose(mapFNew);
  172.  
  173.  
  174. //sets everyone's position on the map.
  175. map[my_hero.y][my_hero.x] = my_hero.symbol;
  176. int counter = 0;
  177. while(counter != my_vil.numVillains) {
  178. map[my_vil.y[counter]][my_vil.x[counter]] = my_vil.name[counter];
  179. counter++;
  180. }
  181.  
  182. //prints the map with everybody's position on it
  183. xC = 0;
  184. yC = 0;
  185. while(yC != mapY){
  186. while(xC != mapX){
  187. printf("%c",map[yC][xC]);
  188. xC++;
  189. }
  190. yC++;
  191. xC = 0;
  192. printf("\n");
  193. }
  194.  
  195. return 0;
  196. }
Add Comment
Please, Sign In to add comment