Created
June 23, 2018 18:39
-
-
Save rauldeavila/1252e93176f67ea867af211def0f5a4a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <stdio.h> | |
#define MAX_INIMIGOS 20 | |
typedef struct tipo_posicao | |
{ | |
int linha, coluna; | |
} TIPO_POSICAO; | |
int le_arquivo(int* nVelocidade, int* nMovimento, TIPO_POSICAO* vetorElementos, FILE* file){ | |
fscanf(file, "%d\n", nVelocidade); | |
fscanf(file, "%d\n\n", nMovimento); | |
/* LOOP PARA VARRER TODOS OS INIMIGOS POR LINHAS E COLUNAS */ | |
int line, col, quantInimigos, posicao, count; | |
line = 1; | |
col = 0; | |
count = 0; | |
quantInimigos = 0; | |
char lido; | |
while (!feof(file)) { | |
lido = getc(file); | |
col++; | |
if(lido == '\n'){ | |
line++; | |
col = 0; | |
} | |
if(lido == 'X' || lido == 'x') { | |
quantInimigos = quantInimigos + 1; // incremento no contador | |
posicao = ftell(file); | |
//printf("INIMIGO: linha %d\t coluna %d\n", line, col); | |
if (count <= MAX_INIMIGOS){ | |
vetorElementos[count].coluna = col; | |
vetorElementos[count].linha = line; | |
count++; | |
} | |
} | |
} | |
return quantInimigos; | |
} | |
// Escreva uma função void preenche_matriz(int, TIPO_POSICAO*, char[][80]) que receba | |
//um vetor com as posições dos inimigos e preencha uma matriz de caracteres com os elementos dos | |
//inimigos, completando os demais caracteres com espaços em branco (' '). | |
void preenche_matriz(int totalInimigos, TIPO_POSICAO* vetorElementos, char matrizInimigos[][80]){ | |
int counter, i, j, blankSpaces; | |
counter = 0; | |
i = 1; | |
j = 1; | |
int ultimaLinha = 1; | |
/* PEQUENO LOOP PARA ENCONTRAR ULTIMA LINHA DE INIMIGOS */ | |
while (counter < totalInimigos){ | |
ultimaLinha = vetorElementos[counter].linha; | |
counter++; | |
} | |
for ( i=1 ; i <= ultimaLinha; i++ ){ | |
for ( j=1 ; j<=80 ; j++ ){ | |
matrizInimigos[i][j] = ' '; | |
} | |
} | |
i = 1; | |
j = 1; | |
counter = 0; | |
while (counter < totalInimigos){ | |
// printf("linha = %d\t coluna = %d\n", vetorElementos[counter].linha, vetorElementos[counter].coluna); | |
// counter++; | |
i = vetorElementos[counter].linha; | |
j = vetorElementos[counter].coluna; | |
matrizInimigos[i][j] = 'o'; | |
matrizInimigos[i][j+1] = 'o'; | |
matrizInimigos[i][j+2] = 'o'; | |
matrizInimigos[i][j+3] = 'o'; | |
matrizInimigos[i][j+4] = 'o'; | |
counter++; | |
} | |
i = 1; | |
j = 1; | |
for ( i=1 ; i <= ultimaLinha; i++ ){ | |
for ( j=1 ; j<=80 ; j++ ){ | |
printf(" %c", matrizInimigos[i][j]); | |
} | |
printf("\n"); | |
} | |
} | |
int main(){ | |
FILE *map; | |
char fileToRead[15]; | |
do{ | |
printf("Insert file name: "); | |
gets(fileToRead); | |
map = fopen(fileToRead, "r"); | |
if (map == NULL) | |
printf("Error, can't open file:\t%s \n\n", fileToRead); | |
} while (map == NULL); | |
/* VARIAVEIS */ | |
int velocidade, tipoMovimento, totalInimigos; | |
velocidade = 0; | |
tipoMovimento = 0; | |
totalInimigos = 0; | |
char matrizInimigos [24][80]; | |
TIPO_POSICAO vetorElementos[MAX_INIMIGOS]; | |
/* CHAMANDO PRIMEIRA FUNCAO */ | |
totalInimigos = le_arquivo(&velocidade, &tipoMovimento, vetorElementos, map); | |
/* CHAMANDO SEGUNDA FUNCAO */ | |
preenche_matriz(totalInimigos, vetorElementos, matrizInimigos); | |
/* PEQUENO LOOP PARA ENCONTRAR ULTIMA LINHA DE INIMIGOS */ | |
int counter = 0; | |
int ultimaLinha = 1; | |
while (counter < totalInimigos){ | |
ultimaLinha = vetorElementos[counter].linha; | |
counter++; | |
} | |
int totalUtilizado = ultimaLinha + 3; // total de linhas utilizadas pelo programa, com velocidade, direcao e inimigos | |
int quantNewLines = 24 - totalUtilizado; | |
counter = 0; | |
while (counter < quantNewLines){ | |
printf("\n"); | |
counter++; | |
} | |
/* PRINTANDO VELOCIDADE E TIPO MOVIMENTO COMO INTEIROS */ | |
printf("\n\nVELOCIDADE DO JOGO: %d\n", velocidade); | |
printf("DIRECAO DOS INIMIGOS: %d\n", tipoMovimento); | |
printf("TOTAL DE INIMIGOS LIDOS: %d\n\n", totalInimigos); | |
/* TESTANDO O TIPO_POSICAO PASSADO POR REFERENCIA */ | |
// int mainCounter = 0; | |
// while (mainCounter < totalInimigos){ | |
// printf("linha = %d\t coluna = %d\n", vetorElementos[mainCounter].linha, vetorElementos[mainCounter].coluna); | |
// mainCounter++; | |
// } | |
fclose(map); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment