Hide bombs in a minefield with ASCII 2

Asked

Viewed 305 times

1

Using a two-dimensional matrix, create a program to implement a minefield game. To fill the matrix positions, use random numbers so that 0 represents a free position and 1 represents a pump. The user must be able to make 3 mistakes. Freely chosen positions shall be marked by (ASCII 2); positions with pumps already chosen shall be marked (ASCII 15) and positions not marked shall 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;
}
  • The image you have placed is not working. Take advantage of it and explain your doubts better. What part you can’t make and what you’ve tried to make it

  • image was of question question. I added

  • doubt is how I hide the positions of the houses with the figures (ANCII 2)

  • Separate the presentation layer from the representation layer. The logical representation of the pump should be treated as unknown terrain

  • 1

    The problem is that it will need more logic to know what the user has already chosen. If the two-dimensional matrix has only free(0) and pumps(1), you have no way of knowing which users have already chosen to show or hide bombs

  • It also helps to separate the map of explored land from the actual land. As if to apply a Fog of War same CLI

  • 1

    try not to use code characters between 0 and 31 - they are "non-printable". Some, such as 0x0a (10) and 0x0d (13) have well-defined behavior in printing, but this is not the case for others. ASCII is 32 until 127.

Show 2 more comments

2 answers

0

By running its program the matrix 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 placed wrong) bombs for any index in the middle of the matrix, 5 for the edges and 3 for the corners. The best thing to do is to create an auxiliary matrix indicating that everything is empty, while the user has not yet chosen any home. When he chooses the house you rebuild the matrix taking what is in the position and next to it, if it is a bomb the program ends.

The auxiliary matrix will have the pumps and the quantity of pumps in the houses without pump.

  • The amount of neighborhood with bombs is 8 at most. Logically only need this binary information, the neighborhood could be calculated on the fly, but pre-calculating it would give a good run time saving

  • that’s the doubt... how do I do it?

0


In C, to compare the equality between two integer values it is necessary to use the equal 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 a printable ASCII code such as 2 and 15, this is correct ?

If I understand the idea correctly, follow its 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;
}
  • Out of curiosity, what the printf("%3c") does? I don’t know what the modifier 3 does next to the c, I only used it for numerical questions, never character questions

  • 1

    @Jeffersonquesado: I believe I am the placeholder optional called Width Filed, representing the quantity minimal of characters in the output, in this case the %3c places 2 spaces in front of the character. Following reference: https://en.wikipedia.org/wiki/Printf_format_string#Format_placeholder_specification

Browser other questions tagged

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