Advertisement
JCLC

Untitled

Jun 7th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 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. };
  13.  
  14. typedef struct personagens per;
  15.  
  16. int main(){
  17.     int n = 0, m = 0, x = 0, y = 0, z = 0;
  18.     char **matriz;
  19.     per *heroi = (per *) malloc(sizeof(per));
  20.     per *monstro = (per *) malloc(sizeof(per)*4);
  21.  
  22.     FILE *rpg; 
  23.      
  24.     //abrindo o arquivo txt.
  25.     rpg = fopen("mapa.txt", "w");
  26.  
  27.     if(rpg == NULL){
  28.         printf("Pare de jogar e vá estudar álgebra, cálculo, e discreta.(e programar, é claro :d)");
  29.     }
  30.  
  31.     printf("Digite n para as dimensoes do mapa segundo a expressao (5+4n)x(5+4n):\n");
  32.     scanf("%d", &n);
  33.  
  34.     //o +1 é pra fazer a borda
  35.     m = (5+4*n+1);
  36.    
  37.     //alocando dinamicamente a nossa matrizinha
  38.     matriz = (char **) malloc((m-1)*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*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.  
  52.     //os 4 seguintes for's é para o preenchimento da bordas
  53.     for(y = 0; y < m-1; y++)
  54.         matriz[0][y] = '*';
  55.  
  56.     for(y = 0; y < m; y++)
  57.         matriz[y][0] = '*';
  58.  
  59.     for(y = 0; y < m; y++)
  60.         matriz[m-1][y] = '*';
  61.  
  62.     for(y = 0; y < m; y++)
  63.         matriz[y][m-1] = '*';
  64.  
  65.     //o serumaninho digita aqui os astericos e tal
  66.     for(x = 1; x < m; x++)
  67.         for(y = 1; y < m; y++)
  68.             scanf("%c", &matriz[x][y]);
  69.  
  70.     //colocando dentro da .txt nossa matriz que o serumaninho digitou
  71.     for(x = 0; x < m; x++)
  72.         for(y = 0; y < m; y++)
  73.             fprintf(rpg, "%c", matriz[x][y]);
  74.  
  75.     //liberando memória alocada
  76.     for(x = 0; x < (4+5*n+1); x++){
  77.         free(matriz[x]);
  78.     }
  79.  
  80.     free(matriz);
  81.  
  82.     fclose(rpg);
  83.  
  84.     //abrindo agora o arquivo .bin para colocar nele as informações fornecidas sobre os personagens
  85.     rpg = fopen("personagens.bin", "ab");
  86.    
  87.     //colocando monstros nos quadrantes
  88.     monstro[0].y = (2+n-1);
  89.     monstro[0].x = (2+n-1);
  90.     monstro[1].y = (2+n-1);
  91.     monstro[1].x = (4+3*n-1);
  92.     monstro[2].y = (4+3*n-1);
  93.     monstro[2].x = (2+n-1);
  94.     monstro[3].x = (4+3*n-1);
  95.     monstro[3].y = (4+3*n-1);
  96.     heroi[0].x = (3+2*n-1);
  97.     heroi[0].y = (3+2*n-1);
  98.  
  99.     for(x = 0; x < 1; x++){
  100.         printf("Digite o nome do heroi\n");
  101.         scanf(" %s", heroi[x].nome);
  102.         printf("digite o atk , def e hp respectivamente\n");
  103.         scanf("%d %d %d", &heroi[x].ataque, &heroi[x].defesa, &heroi[x].hp);
  104.     }  
  105.    
  106.     //fornecendo nome e atributos aos monstros.
  107.     for(x = 0; x < 4; x++){
  108.         printf("digite o nome do monstro %d\n", x+1);
  109.         scanf(" %s", monstro[x].nome);
  110.         printf("digite o atk , def e hp respectivamente\n");
  111.         scanf("%d %d %d", &monstro[x].ataque, &monstro[x].defesa, &monstro[x].hp);
  112.     }  
  113.  
  114.     //escrevendo as informações fornecidas para o herói dentro do arquivo pensonagens.bin
  115.     fwrite(&heroi, sizeof(char), 11, rpg);
  116.     fwrite(&heroi, sizeof(int), 24, rpg);
  117.     fwrite(&monstro, sizeof(char), 11, rpg);
  118.     fwrite(&monstro, sizeof(int), 24, rpg);
  119.  
  120.     free(heroi);
  121.     free(monstro);
  122.     free(rpg);
  123.  
  124.     fclose(rpg);
  125.  
  126.     return 0;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement