Search code examples
cminesweeper

Board in minesweeper in C


I'm learning programming. I started from basics. Ok, I have to do minesweeper. I think it will be good, but in real I don't see the board. I don't know why. I think I wrote the function which shows the board.

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



struct board
{
    int value;
    bool outdoor;
};
struct board field[9][9];

bool load ()/*generuje plansze*/
{
    int x,y;
    for(x=0; x<9; x++)
    for(y=0; y<9; y++)
        {
            field[x][y].value = 0;
            field[x][y].outdoor = false;
        }
return true;
}

bool mina (int pozx, int pozy)/* ustaw mine*/
{
int k,l;
if(field[pozx][pozy].value!=9)
    {
        field[pozx][pozy].value=9;
        for(l=-1;l<2;l++)/*wyjscie poza zakres*/
            for(k=-1;k<2;k++)
                {
                 if((pozx+l)<0 || (pozy+k)<0)
                    continue;
                 if((pozx+l)>9 || (pozy+k)>9)
                    continue;
                 if(field[pozx+l][pozy+k].outdoor == 9)
                    continue;
                 field[pozx+l][pozy+k].value=pole[pozx+l][pozy+k].value+1;
                }
    }
    return true;
}

void position()
{
int pozx, pozy;
int ilosc=0;
srand(time(NULL));

while(ilosc>9)
{
    pozx=rand()%9;
    pozy=rand()%9;
    if(field[pozx][pozy].value!=9)
        {
            mina(pozx,pozy);
            ilosc++;
        }
    }

}

void discovering (int x,int y)
{
if(x<0 || x>9)
    return;
if(y<0 || y>9)
    return;
if(field[x][y].outdoor==true)
    return;
if(field[x][y].value!=9 && field[x][y].outdoor==false)
    field[x][y].outdoor=true;
if(field[x][y].value!=0) return;

discovering field(x,y);
discovering field(x+1,y);
discovering field(x,y+1);
discovering field(x+1,y+1)
discovering field(x-1,y);
discovering field(x,y-1);
discovering field(x-1,y-1);
discovering field(x-1,y+1);
discovering field(x+1,y-1);

}
void show ()
{
    int i,j;
    system("clear");
    for(i = 0; i<9; i++)
        for(j=0; j<9; j++)
            {
                if(field[i][j].outdoor==true)
                {   if(field[i][j].value==0)
                        printf(" "); /*print space*/
                    else
                        printf("%d",field[i][j].value);
                }
            }
            if(field[i][j].outdoor==false)
                printf("#");

}

void control ()
{
    int x,y;
    printf("Podaj wiersz i kolumne:  ");
    scanf("%d %d",&x,&y);
    discovering(x,y);
    show();


}



bool check()
{
    int i,j;
    int mina=0;
    for(i=0; i<9; i++)
        for(j=0; j<9; j++)
            {
                if(field[i][j].outdoor==false)
                    mina++;
            }
            if(mina==9)
                return true;
            else
                return false;
}

int main()
{

load();
position();
int end;
while(end==0)
{
    control();
    if(check()==true)
        end=1;
}
if(end==1)
    printf("Win!");
if(end==2)
    printf("Lose!");

return 0;
}

Solution

  • Initialize variable int end=0; in main to start your while loop. Use this show function to view print your board.

    void show ()
    {
        system("clear");
        for( int i=0; i < 9; i++ )
        {
            for( int j=0; j < 9; j++ )
            {
                if( field[i][j].outdoor )
                {   
                    if ( field[i][j].value==0 )
                        printf(" "); /*print space*/
                    else
                        printf("%d",field[i][j].value);
                }
                else
                    printf("#");
            }
            printf( "\n" );
        }
    }
    

    Further change if((pozx+l)>9 || (pozy+k)>9) to if((pozx+l)>=9 || (pozy+k)>=9) in function mina.

    A similar problem is in function discovering:

    void discovering (int x,int y)
    {
        if(x<0 || x>=9) return;
                // ^^ >= instead of >
        if(y<0 || y>=9) return;
                // ^^ >= instead of >