Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct{
- char name[50];
- char symbol;
- int x;
- int y;
- int moves;
- } hero;
- typedef struct{
- char organ[50];
- int numVil;
- char name[50];
- int x[50];
- int y[50];
- } villains;
- void read_hero ( char *file, hero *h )
- { FILE *heroF;
- int save=1;
- heroF = fopen("saveName.hero","r");
- if(heroF== NULL){
- heroF = fopen("default.hero","r");
- save = 0;
- }
- //gets hero info
- fgets(buff, 256, fp);
- sscanf(buff, "%s", h->name);
- fgets(buff, 256, fp);
- sscanf(buff, "%c", &h->symbol);
- fgets(buff, 256, fp);
- sscanf(buff, "%d,%d", &h->x, &h->y);
- fgets(buff, 256, fp);
- sscanf(buff, "%d", &h->moves);
- fclose(heroF);
- FILE *heroNewF;
- //writes hero data to newSave.hero
- heroNewF = fopen("saveName.hero","w+");
- fprintf(heroNewF,"%s\n",h.name);
- fprintf(heroNewF,"%c\n",&h.symbol);
- fprintf(heroNewF,"%d,%d\n",&h.x,&h.y);
- fprintf(heroNewF,"%d\n",&h.moves);
- fclose(heroNewF);
- }
- void print_hero ( hero *h )
- {//maybe this should be in the read location.
- printf("Name: %s\nSym: %c\nLoc: (%d,%d)\nMoves: %d\n", h->name, h->symbol, h->x, h->y, h->moves);
- }
- void read_villain(char *file, villains *v){
- FILE *vilF;
- vilF = fopen("saveName.villains","r");
- if(vilF==NULL){
- vilF = fopen("default.villains","r");
- }
- //gets villain info
- fgets(buff, 256, fp);
- sscanf(buff, "%s\n", v->organ);
- fgets(buff, 256, fp);
- sscanf(buff, "%d\n", &v->numVil);
- int c = 0;
- for(c=0; c<&v.numVil; c++){
- fscanf(vilF, "%c\n",v->name[c]);
- fscanf(vilF, "%d,%d\n",v->x[c],v->y[c]);
- }
- c=0;
- fclose(vilF);
- FILE *vilFSave;
- //writes villains data to newSave.villains
- vilFSave = fopen("saveName.villains","w+");
- fprintf(vilFSave,"%s\n",v->organization);
- fprintf(vilFSave,"%d\n",&v.numVillains);
- for(c=0; c<&v.numVil; c++) {
- fprintf(vilFSave,"%c\n",v->name[c]);
- fprintf(vilFSave,"%d,%d\n",v->x[c],v->y[c]);
- counter++;
- }
- fclose(vilFSave);
- }
- void print_villain (villains *v )
- {
- printf("Name: %s\nSym: %c\nLoc: (%d,%d)\nMoves: %d\n", v->name, v->x, v->y);
- }
- int main(void) {
- //print info about hero
- hero my_hero;
- read_hero("saveName.hero", &my_hero);
- print_hero(&my_hero);
- //print stuff about villains
- villains my_vil;
- read_villain("saveName.villains", &my_vil);
- print_villain(&my_vil);
- //now map stuff
- FILE *mapF;
- //loading *.map file
- mapF = fopen("saveName.map","r");
- if(mapF==NULL) {mapF = fopen("default.map","r");}
- int mapX;
- int mapY;
- fscanf(mapF,"%d,%d\n",&mapY,&mapX);
- char map[mapY][mapX];
- //counters
- int yC = 0;
- int xC = 0;
- int temp;
- //extracting map data
- while(yC!=mapY) {
- //if still on X line
- if(xC < mapX){
- fscanf(mapF, "%c",&map[yC][xC]);
- xC++;
- }
- //up a row
- if(xC == mapX)
- {
- yC++;
- xC = 0;
- fscanf(mapF, "\n",&temp);
- }
- }
- fclose(mapF);
- //create new map
- FILE *mapFNew;
- //writes map to newSave.map
- mapFNew = fopen("saveName.map","w+");
- xC = 0;
- yC = 0;
- fprintf(mapFNew,"%d,%d\n",mapY,mapX);
- while(yC != mapY){
- while(xC != mapX){
- fprintf(mapFNew,"%c",map[yC][xC]);
- xC++;
- }
- yC++;
- xC = 0;
- fprintf(mapFNew,"\n");
- }
- fclose(mapFNew);
- //sets everyone's position on the map.
- map[my_hero.y][my_hero.x] = my_hero.symbol;
- int counter = 0;
- while(counter != my_vil.numVillains) {
- map[my_vil.y[counter]][my_vil.x[counter]] = my_vil.name[counter];
- counter++;
- }
- //prints the map with everybody's position on it
- xC = 0;
- yC = 0;
- while(yC != mapY){
- while(xC != mapX){
- printf("%c",map[yC][xC]);
- xC++;
- }
- yC++;
- xC = 0;
- printf("\n");
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment