Guest User

Untitled

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