Labirinto com desafios iluminacao e enigma

1. Labirinto com desafios iluminacao e enigma

Douglas Batista da Silva
kurokawabr

(usa Ubuntu)

Enviado em 23/06/2019 - 20:18h

galera estou tendo dificuldades para desenvolver a programação de um labirinto alguém pode me ajudar?

eu preciso fazer uma programação de iluminação 5x7, controle de entrada, salas com enigmas, desafios e vida (que serem perdidas caso os desafios não sejam compridos.

consegui desenvolver apenas isto e não está compilando quando boto -Wall. Alguém pode me ajudar?

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

#define CHAO 0
#define PAREDE 1
#define VOCE 4
#define BORDA 5
#define PORTA 2
#define ENIGMA 3

#define OK 1
#define ERRO 2

#define LIMPARTELA “clear”

int coluna_atual = 4, linha_atual = 4;

int mapa[30][60] = {
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,
5,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,
5,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,
5,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,
5,1,0,0,1,1,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0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
printf("@");
continue;
}
if (mapa[linha][coluna] == CHAO)
printf(" “);
if (mapa[linha][coluna] == PAREDE)
printf(”#");
if (mapa[linha][coluna] == BORDA)
printf("#");
if (mapa[linha][coluna] == PORTA)
printf("!");
if (mapa[linha][coluna] == ENIGMA)
printf("?");
}
printf("\n");
}
return OK;
}

int main(){
char movimento;
while ((movimento != ‘x’) && (movimento != ‘X’)) {
system(LIMPARTELA);
desenhar_mapa();
printf("\n\n (W)\n(A)+(D)\n (S)\n\n");
if ((scanf("%c",&movimento))==0) {
printf(“mensagem de erro…\n”);
getchar();
return ERRO;
}
if ((movimento == ‘w’) || (movimento == ‘W’)) {
linha_atual = linha_atual - 1;

if (mapa[linha_atual][coluna_atual] != CHAO) {
linha_atual = linha_atual + 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 's') || (movimento == 'S')) {
linha_atual = linha_atual + 1;

if (mapa[linha_atual][coluna_atual] != CHAO) {
linha_atual = linha_atual - 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 'd') || (movimento == 'D')) {
coluna_atual = coluna_atual + 1;

if (mapa[linha_atual][coluna_atual] != CHAO) {
coluna_atual = coluna_atual - 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 'a') || (movimento == 'A')) {
coluna_atual = coluna_atual - 1;

if (mapa[linha_atual][coluna_atual] != CHAO) {
coluna_atual = coluna_atual + 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
}

}



  


2. Re: Labirinto com desafios iluminacao e enigma

Paulo
paulo1205

(usa Ubuntu)

Enviado em 25/06/2019 - 23:14h

Uma rápida mexida, para compilar sem erros, com algumas das mexidas marcadas em negrito. Comento o programa e faço sugestões na próxima mensagem.
#include <stdio.h>
#include <stdlib.h>

#define CHAO 0
#define PAREDE 1
#define VOCE 4
#define BORDA 5
#define PORTA 2
#define ENIGMA 3

#define OK 1
#define ERRO 2

#define LIMPARTELA "clear" /* Note o uso de aspas comuns do ASCII; você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,0,0,0,1,1,2,2,1,1,1,1,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5 },
{ 5,1,0,0,0,0,0,1,0,0,0,0,0,0,1,1,1,0,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5 },
{ 5,1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,0,0,0,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5 },
{ 5,1,0,0,0,0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5 },
{ 5,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5 },
{ 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5 }
};

int desenhar_mapa(){
int linha,coluna;
for (linha=0;linha<30;linha++) {
for (coluna=0;coluna<60;coluna++) {
if ((linha == linha_atual) && (coluna == coluna_atual)) {
printf("@");
continue;
}
if (mapa[linha][coluna] == CHAO)
printf(" ");
if (mapa[linha][coluna] == PAREDE)
printf("#");
if (mapa[linha][coluna] == BORDA)
printf("#");
if (mapa[linha][coluna] == PORTA)
printf("!");
if (mapa[linha][coluna] == ENIGMA)
printf("?");
}
printf("\n");
}
return OK;
}

int main(void){
char movimento='\0';
while ((movimento != 'x') && (movimento != 'X')) {
system(LIMPARTELA);
desenhar_mapa();
printf("\n\n (W)\n(A)+(D)\n (S)\n\n");
if ((scanf("%c",&movimento))==0) {
printf("mensagem de erro…\n");
getchar();
return ERRO;
}
if ((movimento == 'w') || (movimento == 'W')) {
linha_atual = linha_atual - 1;
if (mapa[linha_atual][coluna_atual] != CHAO) {
linha_atual = linha_atual + 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 's') || (movimento == 'S')) {
linha_atual = linha_atual + 1;
if (mapa[linha_atual][coluna_atual] != CHAO) {
linha_atual = linha_atual - 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 'd') || (movimento == 'D')) {
coluna_atual = coluna_atual + 1;
if (mapa[linha_atual][coluna_atual] != CHAO) {
coluna_atual = coluna_atual - 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
if ((movimento == 'a') || (movimento == 'A')) {
coluna_atual = coluna_atual - 1;
if (mapa[linha_atual][coluna_atual] != CHAO) {
coluna_atual = coluna_atual + 1;
printf("\n\nups.. ali não =)\n");
getchar();
}
}
}
}



... “Principium sapientiae timor Domini, et scientia sanctorum prudentia.” (Proverbia 9:10)


3. Re: Labirinto com desafios iluminacao e enigma

Paulo
paulo1205

(usa Ubuntu)

Enviado em 29/06/2019 - 16:24h

Fiquei brincando com seu programa um pouquinho, fazendo-o compilar não apenas no Linux, com GCC e CLANG, mas também no DOS, com Turbo C e com DJGPP.

Mudei algumas coisas:

  • Em lugar de getchar(), usei rotinas de baixo nível para ler comandos do teclado, a fim de dar um pouco mais de agilidade ao jogo, e não ter de apertar <Enter> para os movimentos acontecerem.
  • Representei o labirinto usando caracteres, em lugar de números.
  • Imprimo de uma só vez as linhas do mapa (exceto a linha onde está o personagem).
  • Implementei uma versão ridícula de um enigma, que dá uma chave para a abertura da porta, se você acertar a pergunta (ou mata você, se você a errar).
  • Rearrumei a parte de processamento das teclas e movimentação do personagem.

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#if defined(__DOS__) || defined(__MSDOS__) || defined(_WIN32)

# include <conio.h>

# define ENTER '\r'

#elif defined(__unix__)

# include <term.h>
# include <unistd.h>

# define ENTER '\n'

#endif


#define CHAO ' '
#define PAREDE '='
#define VOCE '@'
#define BORDA '#'
#define PORTA '!'
#define ENIGMA '?'

#define OK 1
#define ERRO 2


#define LINHAS 30
#define COLUNAS 60

char mapa[LINHAS][COLUNAS+1] = {
"############################################################",
"#==========================================================#",
"#== ==== ==== ================#",
"#== =================== === === ================#",
"#== ============== === == === ================#",
"#= == ======= == ==== ====== =================#",
"#= == ====== ! ====== ==================#",
"#= == ====== ! ===============#",
"#= == ====== ! ===== ========= ==============#",
"#= ===!!========= == =====!!========== =============#",
"#= === ========== === ===== ========== =============#",
"#== = ================ ==== ========= ====== ===#",
"#=== ================ === ====== ==== ==#",
"#==== ==== ======== == ===== === =#",
"#======= === ======= === ====== === =#",
"#= === = ====== ==== ======= === =#",
"#= === = ======= ===== ======== ! =#",
"#= === == ======= =============== ?! =#",
"#== === === ====== ================ ! =#",
"#=== ======= ======================== =#",
"#==== =========== ======================= =#",
"#====== ======== ! = ======================== ==#",
"#= = ==== === ======================== ===#",
"#= = = ==== ==== ======================== =====#",
"#= ==!!==== === ==============================#",
"#= = === === ==============================#",
"#= == ==== === ==============================#",
"#= == =========== ===============================#",
"#==========================================================#",
"############################################################"
};

int coluna_atual = 4, linha_atual = 4;


#if defined(__DOS__) || defined(__MSDOS__) || defined(_WIN32)

// Cria um apelido para a função getch() que possa ser usado em qualquer sistema.
int (*const my_getch)(void)=getch;

#elif defined(__unix__)

void clrscr(void){
static int terminal_ajustado=0;

if(!terminal_ajustado){
setupterm(NULL, STDOUT_FILENO, NULL);
terminal_ajustado=1;
}
putp(clear_screen);
}

int my_getch(void){
struct termios term, noncanon_term;
unsigned char ch;
ssize_t n_read;
tcgetattr(STDIN_FILENO, &term);
noncanon_term=term;
noncanon_term.c_lflag&=~(ICANON|ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &noncanon_term);
n_read=read(STDIN_FILENO, &ch, 1);
tcsetattr(STDIN_FILENO, TCSANOW, &term);
return n_read==1? ch: EOF;
}

#else

void clrscr(void){
for(int i=LINHAS; i-->0;)
putchar('\n');
}

#endif


void desenhar_mapa(){
int linha=0;
clrscr();
while(linha<linha_atual)
puts(mapa[linha++]);
printf("%.*s%c%s\n", coluna_atual, mapa[linha], VOCE, mapa[linha]+coluna_atual+1);
while(++linha<LINHAS)
puts(mapa[linha]);
}

void ignora_ate_fim_de_linha(void){
int ch;
while((ch=my_getch())!=EOF && ch!=ENTER)
;
}


int main(void){
int tem_chave_porta=0;

srand(time(NULL));

while(1){
int proxima_linha=linha_atual, proxima_coluna=coluna_atual;

desenhar_mapa();
printf("\n\n (W)\n(A)+(D) (X) para sair\n (S)\n\n");
switch(tolower(my_getch())){
case EOF:
fprintf(stderr, "Erro de leitura. Abortando programa.\n");
exit(1);
case 'x': exit(0);
case 'w': --proxima_linha; break;
case 's': ++proxima_linha; break;
case 'a': --proxima_coluna; break;
case 'd': ++proxima_coluna; break;
}
switch(mapa[proxima_linha][proxima_coluna]){
case CHAO:
linha_atual=proxima_linha;
coluna_atual=proxima_coluna;
break;
case PORTA:
/* Exemplo de código; você pode optar por outra lógica. */
if(tem_chave_porta){
linha_atual=proxima_linha;
coluna_atual=proxima_coluna;
}
else{
printf("\n\nA porta está trancada. Tecle <Enter>.\n\n");
ignora_ate_fim_de_linha();
}
break;
case PAREDE:
case BORDA:
printf("\n\nVocê deu com a cara na parede. Tecle <Enter>.\n\n");
ignora_ate_fim_de_linha();
break;
case ENIGMA:
{
int a, b, c;
a=(int)(100.0*rand()/(double)RAND_MAX);
b=(int)(100.0*rand()/(double)RAND_MAX);
printf("\n\nQuanto vale %d×%d? ", a, b);
if(scanf("%d", &c)==1){
if(c==a*b){
tem_chave_porta=1;
printf("\nParabéns! Você encontrou a chave. Tecle <Enter>.\n\n");
ignora_ate_fim_de_linha();

// Remove esse enigma do tabuleiro.
mapa[proxima_linha][proxima_coluna]=CHAO;

linha_atual=proxima_linha;
coluna_atual=proxima_coluna;
}
else{
printf("\nVocê errou e foi eletrocutado. Tecle <Enter> para encerrar o jogo.\n\n");
ignora_ate_fim_de_linha();
exit(0);
}
}
else{
fprintf(stderr, "Erro de leitura. Abortando programa.\n");
exit(1);
}
}
}
}
}


Ainda há muito espaço para melhorias possíveis. As que me vêm à mente logo de cara são as seguintes:

  • Você falou em iluminação: entendo com isso que o personagem não deva ver o labirinto inteiro, mas apenas uma região em volta dele. Confere? Para implementar isso, a ideia de imprimir linhas inteiras teria de ser desfeita, e você só poderia desenhar o entorno do personagem.
  • Isso possivelmente teria de ser feito de qualquer maneira, pois apagar a tela e redesenhá-la a cada iteração acaba provocando desconfortáveis piscadas na tela (especialmente no DOS). Melhor seria reposicionar o cursor e alterar apenas as partes da tela que fossem relevantes (apagar a área que saiu do campo de visão/iluminação, e redesenhar a qu estivesse dentro desse campo).
  • Usar enigmas mais interessantes.


... “Principium sapientiae timor Domini, et scientia sanctorum prudentia.” (Proverbia 9:10)






Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts