Advertisement
JCLC

Untitled

Jun 7th, 2016
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 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 new_game;
  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;
  20.     char *nome;
  21.     int *ataque, *desefa, *hp;
  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 vá estudar álgebra, cálculo, 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+1);
  37.    
  38.     //alocando dinamicamente a nossa matrizinha
  39.     matriz = (char **) malloc((m-1)*sizeof(char*));
  40.         if(matriz == NULL){
  41.             printf("Tem memoria nao. Pare de jogar e a estudar, pois o fim do periodo ta ai.");
  42.             exit(1);
  43.         }
  44.         //alocando memória para as colunas
  45.         for(x = 0; x < m; x++){
  46.             matriz[x] = (char *) malloc(m*sizeof(char));
  47.             if(matriz[x] == NULL){
  48.                 printf("Tem memoria nao. Pare de jogar e va estudar, pois o fim do periodo ta ai.");
  49.                 exit(1);
  50.             }          
  51.         }
  52.  
  53.     //os 4 seguintes for's é para o preenchimento da bordas
  54.     for(y = 0; y < m-1; y++)
  55.         matriz[0][y] = '*';
  56.  
  57.     for(y = 0; y < m; y++)
  58.         matriz[y][0] = '*';
  59.  
  60.     for(y = 0; y < m; y++)
  61.         matriz[m-1][y] = '*';
  62.  
  63.     for(y = 0; y < m; y++)
  64.         matriz[y][m-1] = '*';
  65.  
  66.     //o serumaninho digita aqui os astericos e tal
  67.     for(x = 1; x < m; x++)
  68.         for(y = 1; y < m; y++)
  69.             scanf("%c", &matriz[x][y]);
  70.  
  71.     //colocando dentro da .txt nossa matriz que o serumaninho digitou
  72.     for(x = 0; x < m; x++)
  73.         for(y = 0; y < m; y++)
  74.             fprintf(rpg, "%c", matriz[x][y]);
  75.  
  76.     //liberando memória alocada
  77.     for(x = 0; x < (4+5*n+1); x++){
  78.         free(matriz[x]);
  79.     }
  80.  
  81.     free(matriz);
  82.  
  83.     fclose(rpg);
  84.  
  85.     //abrindo agora o arquivo .bin para colocar nele as informações fornecidas sobre os personagens
  86.     rpg = fopen("personagens.bin", "ab");
  87.  
  88.     //alocando memória para as estruturas
  89.     per *heroi = (per *) malloc(sizeof(per));
  90.     per *monstro = (per *) malloc(sizeof(per)*4);
  91.  
  92.     //colocando monstros nos quadrantes
  93.     monstro[0].y = (2+n-1);
  94.     monstro[0].x = (2+n-1);
  95.     monstro[1].y = (2+n-1);
  96.     monstro[1].x = (4+3*n-1);
  97.     monstro[2].y = (4+3*n-1);
  98.     monstro[2].x = (2+n-1);
  99.     monstro[3].x = (4+3*n-1);
  100.     monstro[3].y = (4+3*n-1);
  101.     heroi[0].x = (3+2*n-1);
  102.     heroi[0].y = (3+2*n-1);
  103.  
  104.     for(x = 0; x < 1; x++){
  105.         printf("Digite o nome do heroi\n");
  106.         scanf(" %s", heroi[x].nome);
  107.         printf("digite o atk , def e hp respectivamente\n");
  108.         scanf("%d %d %d", &heroi[x].ataque, &heroi[x].defesa, &heroi[x].hp);
  109.     }  
  110.  
  111.     //escrevendo as informações fornecidas para o herói dentro do arquivo pensonagens.bin
  112.     fwrite(&heroi, sizeof(char), 11, rpg);
  113.     fwrite(&heroi, sizeof(int), 24, rpg);
  114.  
  115.     //fornecendo nome e atributos aos monstros.
  116.     for(x = 0; x < 4; x++){
  117.         printf("digite o nome do monstro %d\n", x+1);
  118.         scanf(" %s", monstro[x].nome);
  119.         printf("digite o atk , def e hp respectivamente\n");
  120.         scanf("%d %d %d", &monstro[x].ataque, &monstro[x].defesa, &monstro[x].hp);
  121.     }  
  122.  
  123.     free(heroi);
  124.     free(monstro);
  125.     free(rpg);
  126.  
  127.     fclose(rpg);
  128.  
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement