Problem using the getch () function in C

2

I am writing a simple game that is based on traversing a number in the first element of the array to the last element. However, I want to use the getch() function so that the element moves through the array immediately after a key is entered, but the element only moves after I press the "b" character.

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

void matriz(int v[][30], int i, int j, int bomb);

int main()
{
srand(time(NULL));
int maze[30][30], i, j, bomb = rand()%70;
puts("Ola, Benvindo ao Maze game.\nOs comandos sao simples\nW para cima\nA para a esquerda\nS para baixo\nD para direita\nVoce comeca na primeira posicao e devera chegar ate a ultima\nsem passar por cima de nenhum 1 ou 2 mas, voce EXPLODIR o 2 apertando 'b'\n(Sendo que vc so pode usar uma determinada qtd de bombas!)\nMAXIMIZE a tela, tecle enter e divirta-se");
getchar();
fflush(stdin);
system("cls");
for (i = 0; i < 30; i++)
{
    for (j = 0; j < 30; j++)
    {
        if (i == 29 && j == 29)
        {
            maze[i][j] = 0;
        } else if(i == 0 && j == 0)
        {
            maze[i][j] = 5;
        }else
        {
            maze[i][j] = rand()%3;
        }
        printf("%d ", maze[i][j]);
    }
    puts("");
}
printf("\nvoce tem %d bombas\n", bomb);
i = j = 0;
matriz(maze, i, j, bomb);
return 0;
}

void matriz(int v[][30], int i, int j, int bomb)
{
int a, b;
char direcao;
do
{
    direcao = getch(); //AQUI!
    switch(direcao)
    {
        case 'w':
            v[i][j] = 0;
            i--;
            break;
        case 'a':
            v[i][j] = 0;
            j--;
            break;
        case 's':
            v[i][j] = 0;
            i++;
            break;
        case 'd':
            v[i][j] = 0;
            j++;
            break;
        case 'b':
            if (bomb == 0)
            {
                direcao = 0;
            } else {
                printf("");
            bomb--;
            if (v[i + 1][j] == 2)
            {
                v[i + 1][j] = 0;
            }
            if (v[i - 1][j] == 2)
            {
                v[i - 1][j] = 0;
            }
            if (v[i][j + 1] == 2)
            {
                v[i][j + 1] = 0;
            }
            if (v[i][j - 1] == 2)
            {
                v[i][j - 1] = 0;
            }
            }
        default:
            direcao = 0;
    }
} while(direcao);
if (i < 0 || j < 0 || i > 29 || j > 29 || v[i][j] == 1 || v[i][j] == 2)
{
    puts("GAME OVER!!!");
    exit(1);
} else if (i == 29 && j == 29)
{
    system("cls");
    v[i][j] = 5;
    for (a = 0; a < 30; a++)
    {
        for (b = 0; b < 30; b++)
        {
            printf("%d ", v[a][b]);
        }
        puts("");
    }
    puts("Parabens Voce venceu!!!");
    exit(0);
} else
{
    v[i][j] = 5;
}
system("cls");
for (a = 0; a < 30; a++)
{
    for (b = 0; b < 30; b++)
    {
        printf("%d ", v[a][b]);
    }


    puts("");
}
printf("\nvoce tem %d bombas\n", bomb);
matriz(v, i, j, bomb);
}
    
asked by anonymous 17.07.2015 / 03:45

1 answer

2

First, avoid using getch() or anything that is present in conio.h . Use getchar() that does the same thing in a standard and reliable way.

The problem is not in this function, it is working correctly as can be demonstrated in ideone .

The problem is in logic. The code is not executing what you want, and just debugging to understand what is happening step-by-step and see where the failure occurs.

By the comment it seems that you have already found where the failure was.

    
17.07.2015 / 05:21