Hiding bombs in a minefield with ASCII 2

1

Using a two-dimensional array, create a program to implement a minefield game. To fill the array positions, use random numbers so that 0 represents a free position and 1 a pump. The user must be able to commit 3 errors. Free chosen positions should be marked by (ASCII 2); positions with pumps already chosen must be marked (ASCII 15) and unmarked positions should be marked with (ASCII 63).

How do I hide the positions of the houses with the figures (ASCII 2)?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define tam 10

int matri[tam][tam];

void forma()
{
    int i, e;
    for(i=0; i<10; i++)
    {
        for(e=0; e<10; e++)
        {
            matri[e][i]= rand()%2;
        }
    }
}

void mostra()
{
    int i, e;
    for(i=0; i<10; i++)
    {
        for(e=0; e<10; e++)
        {
            printf("%3d",matri[e][i]);
        }
        printf("\n");
    }
}
//
void jogar()
{

    int L,C, erros=0;
    char a = 2, b = 15;
    do
    {
        printf("\n\n linha L ?");
        scanf("%d", &L);
        printf("\n\n coluna C ?");
        scanf("%d", &C);

        if(matri[L][C] = 0)
        {
            matri[L][C] = a;
        }
        else if(matri[L][C] = 1)
        {
            erros++;
            matri[L][C] = b;
        }
    }
    while(erros = 3);
}


int main(int argc, char *argv[])
{
    forma();
    mostra();
    esconde();
    jogar();

    return 0;
}
    
asked by anonymous 11.10.2017 / 12:26

2 answers

0

In C , to compare the equality between two integer values it is necessary to use the equality operator == . You are using the assignment operator = in your comparisons and this will not work as you expect.

To solve your problem, you can create an auxiliary matrix to control the display of your minefield.

The statement talks about non-printable ASCII codes such as 2 and 15 , is this correct?

If I understand the idea well, it follows your modified code, commented and tested so that your game is "alive":

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

#define tam 10

#define BOMBA           'B'      /* ASCII 66 (Deveria ser ASCII 15) */
#define LIVRE           '?'      /* ASCII 63 (OK)*/
#define ESCOLHIDA       ' '      /* ASCII 32 (Deveria ser ASCII 2) */

int matri[tam][tam];      /* Mapeamento das bombas  */
int tabuleiro[tam][tam];  /* Tabuleiro somente para exibicao na tela */

void forma()
{
    int i, e;

    srand(time(NULL));     /* O gerador de numeros aleatorios
                             inicializado com uma semente baseada
                             na hora local do computador */
    for(i=0; i<tam; i++)
    {
        for(e=0; e<tam; e++)
        {
            matri[e][i] = rand() % 2;
            tabuleiro[e][i] = LIVRE;    /* Inicializa tabuleiro com celula livre */
        }
    }
}

void mostra()
{
    int i, e;
    for(i=0; i<tam; i++)
    {
        for(e=0; e<tam; e++)
        {
            printf("%3c",tabuleiro[e][i]); /* Exibe tabuleiro */
        }
        printf("\n");
    }
    printf("\n");
}

int vitoria()
{
    int i, e;
    for(i=0; i<tam; i++)
    {
        for(e=0; e<tam; e++)
        {
            if( tabuleiro[e][i] == LIVRE ) /* Verifica se exista alguma celula livre no tabuleiro */
            {
                return 0;
            }
        }
    }
    return 1;  /* Nenhuma celula livre */
}

int jogar()
{
    int L,C, erros=0;

    /* Antes de jogar, inicializa campo minado */
    forma();

    while(1)
    {
        mostra(); /* Exibe tabuleiro a cada rodada */

        printf("Linha? ");
        scanf("%d", &L);
        printf("Coluna? ");
        scanf("%d", &C);

        if(matri[L][C] == 0) /* Celula sem bomba */
        {
            tabuleiro[L][C] = ESCOLHIDA;  /* Ajusta tabuleiro */

            if(vitoria()) /* Verifica Vitoria */
                return 1;   /* Venceu! */
        }
        else if(matri[L][C] == 1) /* Celula com bomba */
        {
            erros++;
            tabuleiro[L][C] = BOMBA;   /* Ajusta Tabuleiro */

            printf("\nBOMBA EXPLODIU! (%d de 3)\n\n", erros );

            if( erros >= 3 ) /* Verifica fim de jogo */
                return 0;   /* Fim de Jogo! */
        }

    }
}


int main(void)
{
    if(jogar())
    {
        printf("Parabens! Voce venceu!\n");
    }
    else
    {
        printf("Voce Perdeu! Game Over!\n");
    }

    return 0;
}
    
11.10.2017 / 13:55
0

By running your program the array has only 0's and 1's, but the minefield needs to have the amount of bombs it has next to an empty house, which are at most 8 (had misplaced) bombs for any index in the half of the matrix, 5 for the edges and 3 for the edges. The best thing to do is to create an auxiliary array indicating that everything is empty, while the user has not yet chosen any home. When he chooses the house you rebuild the matrix by picking up what is in the position and next to it, if it is a bomb the program ends.

The help matrix will have the bombs and the number of bombs in the houses without bomb.

    
11.10.2017 / 13:03