Advertisement
JCLC

Untitled

Jun 8th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. //estrutura para salvar as informações dos personagens u.u
  5. struct personagens
  6. {
  7.     char nome[11];
  8.     int ataque;
  9.     int defesa;
  10.     int hp;
  11.     int x, y;
  12.     int status;
  13. };
  14.  
  15. typedef struct personagens per;
  16.  
  17. int main(){
  18.     int n = 0, m = 0, x = 0, y = 0, z = 0;
  19.     char **matriz, enter;
  20.     per *heroi = (per *) malloc(sizeof(per));
  21.     per *monstro = (per *) malloc(sizeof(per)*4);
  22.  
  23.     FILE *rpg;
  24.  
  25.     //abrindo o arquivo txt.
  26.     rpg = fopen("mapa.txt", "w");
  27.  
  28.     if(rpg == NULL){
  29.         printf("Pare de jogar e va estudar algebra, calculo, e discreta.(e programar, claro :d)");
  30.     }
  31.  
  32.     printf("Digite n para as dimensoes do mapa segundo a expressao (5+4n)x(5+4n):\n");
  33.     scanf("%d", &n);
  34.  
  35.     //o +1 é pra fazer a borda
  36.     m = (5+4*n);
  37. a    //alocando dinamicamente a nossa matrizinha
  38.     matriz = (char **) malloc((m)*sizeof(char*));
  39.         if(matriz == NULL){
  40.             printf("Tem memoria nao. Pare de jogar e a estudar, pois o fim do periodo ta ai.");
  41.             exit(1);
  42.         }
  43.         //alocando memória para as colunas
  44.         for(x = 0; x < m; x++){
  45.             matriz[x] = (char *) malloc((m+1)*sizeof(char));
  46.             if(matriz[x] == NULL){
  47.                 printf("Tem memoria nao. Pare de jogar e va estudar, pois o fim do periodo ta ai.");
  48.                 exit(1);
  49.             }
  50.         }
  51.     printf("Digite o mapa:\n");
  52.     //o ser humaninho digita aqui os astericos e tal
  53.     for(x = 0; x < m; x++){
  54.        scanf(" %[^\n]", matriz[x]);
  55.     }
  56.  
  57.  
  58.      //////////////////////////////
  59.  
  60.     //colocando dentro do arquivo .txt a nossa matriz que o ser humaninho digitou
  61.     fprintf(rpg, "%d\n", n);
  62.     for(x = 0; x < m; x++)
  63.         fprintf(rpg, "%s\n", matriz[x]);
  64.  
  65.     //liberando memória alocada
  66.     for(x = 0; x < m; x++){
  67.         free(matriz[x]);
  68.     }
  69.  
  70.     free(matriz);
  71.  
  72.     fclose(rpg);
  73.  
  74.     //abrindo agora o arquivo .bin para colocar nele as informações fornecidas sobre os personagens
  75.     rpg = fopen("personagens.bin", "ab");
  76.  
  77.     //colocando monstros nos quadrantes
  78.     heroi[0].status=1;
  79.     monstro[0].status=1;
  80.     monstro[1].status=1;
  81.     monstro[2].status=1;
  82.     monstro[3].status=1;
  83.     monstro[1].y = (2+n-1);
  84.     monstro[1].x = (2+n-1);
  85.     monstro[0].y = (2+n-1);
  86.     monstro[0].x = (4+3*n-1);
  87.     monstro[2].y = (4+3*n-1);
  88.     monstro[2].x = (2+n-1);
  89.     monstro[3].x = (4+3*n-1);
  90.     monstro[3].y = (4+3*n-1);
  91.     heroi[0].x = (3+2*n-1);
  92.     heroi[0].y = (3+2*n-1);
  93.  
  94.     for(x = 0; x < 1; x++){
  95.         printf("Digite o nome do heroi\n");
  96.         scanf(" %s", heroi[x].nome);
  97.         printf("Digite o atk, def e hp respectivamente\n");
  98.         scanf("%d %d %d", &heroi[x].ataque, &heroi[x].defesa, &heroi[x].hp);
  99.     }
  100.  
  101.     //fornecendo nome e atributos aos monstros.
  102.     for(x = 0; x < 4; x++){
  103.         printf("Digite o nome do monstro %d\n", x+1);
  104.         scanf(" %10s", monstro[x].nome);
  105.         printf("Digite o atk , def e hp respectivamente\n");
  106.         scanf("%d %d %d", &monstro[x].ataque, &monstro[x].defesa, &monstro[x].hp);
  107.     }
  108.  
  109.     //escrevendo as informações fornecidas para o herói dentro do arquivo pensonagens.bin
  110.     fwrite(heroi, sizeof(*heroi), 1, rpg);
  111.     for(x=0; x<4; x++){
  112.     fwrite(&monstro[x], sizeof(monstro[x]), 1, rpg);
  113.     }
  114.     free(heroi);
  115.     free(monstro);
  116.     free(rpg);
  117.  
  118.     fclose(rpg);
  119.  
  120.     return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement