Solucionado (ver solução)
Solucionado
(ver solução)
1
resposta

Partilho o meu código exmplo

Main file:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mapa.h"

//char** mapa;
//int linhas = 0, colunas = 0;

MAPA m;
POSICAO heroi;



int gameFinished() {return 0;}
void move(char command);

int main() {
    // Loap Map
    loadMap(&m);
    encontramap(&m,&heroi,'@');
    // Main Loop
    do {
        printMap(&m);
        char command;
        scanf(" %c",&command);
        move(command);
    } while(!gameFinished());

    // Realse memory
    freeMap(&m);

    return 0;
}

void move(char command) {

    // 1. Localizar a nossa personagem com dois ciclos fors:
    
    m.matriz[heroi.x][heroi.y] = '.';  // é para ficar vazio

     // 2. Definir a direçcao da nossa personagem usando as letras a -  esquerda, s - baixo, d - direita, w - cima;

     switch(command) {
         case 'a':
         m.matriz[heroi.x][heroi.y-1]  = '@'; 
         heroi.y--;
         break;
         case 'w': 
         m.matriz[heroi.x-1][heroi.y] = '@';
         heroi.x--;
         break;
         case 's': 
         m.matriz[heroi.x+1][heroi.y] = '@'; 
         heroi.x++;
         break;
         case 'd': 
         m.matriz[heroi.x][heroi.y+1] = '@';
         heroi.y++;
         break;
     }
}

mapa.h file:


struct mapa {
    char** matriz;
    int linhas;
    int colunas;
};

typedef struct mapa MAPA;

struct posicao {
    int x;
    int y;
};

typedef struct posicao POSICAO;


void encontramap(MAPA * m, POSICAO * p, char c);
void loadMap(MAPA * m);
void printMap(MAPA * m);
void freeMap(MAPA * m);

Implementation file of mapa.c:


#include <stdio.h>
#include <stdlib.h>
#include "mapa.h"




void encontramap(MAPA * m, POSICAO * p, char c){
     
    for(int i = 0; i<m->linhas; i++) {
       for(int j = 0; j<m->colunas; j++) {
           if(m->matriz[i][j] == c) { 
              p->x = i;
              p->y = j;
              break;
           }
        }
     }
}


void printMap(MAPA * m) {
    int i;
    for(i = 0; i < m->linhas; i++) {
        printf("%s\n",m->matriz[i]);
    }
}

void loadMap(MAPA * m) {
    FILE * f;
    f = fopen("mapa.txt","r");
    if(f == 0) {
        printf("Erro na leitura do mapa\n");
        exit(1);
    }
    fscanf(f,"%d %d", &(m->linhas), &(m->colunas));
    printf("Linhas: %d, Colunas: %d;\n",m->linhas, m->colunas);

    // Allocation memory using malloc function:
    m->matriz = malloc(sizeof(char) * m->linhas);
    int i;
    for(i = 0;i<m->linhas; i++) {
         m->matriz = (char**)malloc(sizeof(char*) * m->linhas);
         //Comment: char ** mapa = (char**) malloc(sizeof(char*) *linhas); -> (type cast -  explicity)
    }


    for (int i = 0; i < m->linhas; i++) {
        m->matriz[i] = (char*)malloc(sizeof(char) * (m->colunas + 1));  // Allocation memory for a line
        if (m->matriz[i] == NULL) {
            printf("Erro na alocação de memória para linha %d\n", i);
            exit(1);
        }
        if (fscanf(f, "%s", m->matriz[i]) != 1) {
            printf("Erro na leitura da linha %d\n", i);
            exit(1);
        }
    }
    fclose(f);
}


void freeMap(MAPA* m) {
    int i;
    for(i = 0; i < m->linhas; i++) {
        free(m->matriz[i]);
    }
    free(m->matriz);
}

Estou a gostar dos tutoriais :) obrigado!

1 resposta
solução!

Oi Licínio, tudo bem?

Muito obrigada por compartilhar com a gente o seu código! Parabéns por praticar enquanto estuda.

E que bom que você está gostando as aulas!

Um abraço e bons estudos.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software