Jump to content

Recommended Posts

Posted

pessoal eu estou a fazer um jogo (mapshot)

que conssiste em boneco que enda em mapas, econtra pontos, derrota os enimigos com tiros e tem de chegar a um determinado sitio...

eu estou a tratar da parte dos mapas onde eu ponho no notepad muito mais que isto:

2 0 0 2 1 0

0 0 0 1 0 0

0 0 1 0 0 2

0 1 0 0 0 2

1 0 0 0 0 2

queria que me ajudassem um bocado na parte da leitura:

mover estes números todos para uma matriz(em uma só função!)

int matriz[6][5];

(a biblioterca que estou a trabalhar com leitura e escrita é a stdio.h)

obrigado,seuqram 😉

Posted

maneira simplista/hardcoded/não aconselhada para problemas de tamanho variável

FILE * fd = NULL;
char buffer[256];
int matriz[6][5];
int line = 0;

...

for (line = 0; line < 6; line++) {
  fgets(buffer, 256, fd);
  sscanf("%d%d%d%d%d%d", &matrix[line][0], &matrix[line][1], &matrix[line][2], &matrix[line][3], &matrix[line][4], &matrix[line][5]);
}
IRC : sim, é algo que ainda existe >> #p@p
Posted

em vez disso tudo posso fazer 2 variáveis linex e liney e depois ele vai assumindo tudo não?

o mapa é:

100

por 30

mais uma pergunta:

nesse jogo terá um editor de mapas onde o jogador guarda esse mapa no notepad com o nome que desejar e depois vai, quando quiser busca-lo para jogar...

o problema é que... estão a ver isto?:

printf("%d",variavel);

ele diz a variavel "variavel"

e o que eu queria que acontecece era que isso desse para abrir/criar uma pasta notepad com o nome de uma variavel (que seria o nome do mapa que o jogador escolhece)!:

editor=fopen(variavel,"%s","r+");

estão a perceber a teoria?

obrigado,seuqram...

Posted

o melhor seria :

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

/// ficheiro :

/// 5 6
/// 2 0 0 2 1 0
/// 0 0 0 1 0 0
/// 0 0 1 0 0 2
/// 0 1 0 0 0 2
/// 1 0 0 0 0 2

typedef struct {
int ** matrix;
int n_linhas;
int n_colunas;
} Matrix;

int matrixAlloc(Matrix * matrix) {
int i = 0;

if ((matrix->matrix = malloc(sizeof(int) * matrix->n_linhas)) == NULL)
	return -1;

for(i = 0; i < matrix->n_linhas; i++) {
	if ((matrix->matrix[i] = malloc(sizeof(int) * matrix->n_colunas)) == NULL)
		return -1;
}

return 0;
}

void matrixFree(Matrix * matrix) {
int i = 0;

for(i = 0; i < matrix->n_linhas; i++)
	free(matrix->matrix[i]);
free(matrix->matrix);
}

int main(void) {
FILE * fd = NULL;
Matrix matrix;
char buffer[256];
char * aux = NULL;
int i = 0, j = 0;

// abrir o ficheiro
if ((fd = fopen("matrix.txt", "r")) == NULL)
	return -1;

// ler a primeira linha de informacao do ficheiro
if (fgets(buffer, 256, fd) != buffer) {
	fclose(fd);
	return -1;
}
// parsing da linha
if (sscanf(buffer, "%d%d", &matrix.n_linhas, &matrix.n_colunas) != 2) {
	fclose(fd);
	return -1;
}

// allocar memória para a matrix
if (matrixAlloc(&matrix) != 0) {
	fclose(fd);
	return -1;
}

// ler info do ficheiro
for (i = 0; i < matrix.n_linhas; i++) {
	// ler linha do ficheiro
	if (fgets(buffer, 256, fd) != buffer) {
		matrixFree(&matrix);
		fclose(fd);
		return -1;
	}

	// ler os varios numeros da linha
	j = 0;
	aux = strtok(buffer, " ");
	while (aux != NULL && j < matrix.n_colunas) {
		sscanf(aux, "%d", &(matrix.matrix[i][j]));
		aux = strtok(NULL, " ");
		j++;
	}

	// verificar se leu todos
	if (j != matrix.n_colunas) {
		matrixFree(&matrix);
		fclose(fd);
		return -1;
	}
}

// testing
for (i = 0; i < matrix.n_linhas; i++) {
	for (j = 0; j < matrix.n_colunas; j++) {
		printf("%d", matrix.matrix[i][j]);
	}
	printf("\n");
}

// limpesa
matrixFree(&matrix);
fclose(fd);

return 0;
}
IRC : sim, é algo que ainda existe >> #p@p

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.