Problem using getch() in C

Asked

Viewed 2,024 times

2

I’m writing a simple game that relies on crossing a number in the first element of the matrix to the last element. However, I’m willing to use the function getch() so that the element moves through the matrix immediately after some key is typed, but the element only moves after I press the character "b".

#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("\7");
            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);
}
  • There seems to be no problem with getch() there seems to be problem with other case logic or other part of the code. Have you debugged to see how it is running? Anyway use another function, do not use anything that is in the conio.h, prefer the getchar(), for example. See how this is "working" (is, has problems), the problem must be in logic, but I’m not sure what you want. http://ideone.com/uGBxNC

  • Actually, the error was not in getch() but in while. When I did While( condition == 0) and added the break at the end of case 'b', it worked perfect. thanks

  • I’ll put an answer so it doesn’t stay open

1 answer

2


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

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

The problem is in logic. The code is not running what you want and only thrashing to understand what is happening step-by-step and see where the fault occurs.

From the comment it seems that already found where the fault was.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.