Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- char** createMatrix(int,int);
- void freeMatrix(char**,int,int);
- void initMatrix(char**,int,int,int);
- void drawMatrix(char**,int,int);
- int tesztCOORD(char**,int,int );
- void reveal(char**,char**,int,int,int,int);
- void drawBombs(char**,int,int);
- int checkWin(char**,char**,int,int);
- int main()
- {
- char **graf,**mines;
- int width,height,countmines;
- int cordx,cordy;
- int isRunning =0;
- printf("Palya meret,bombak(Alak:width,height,mines): ");
- scanf("%d,%d,%d",&width,&height,&countmines);
- graf = createMatrix(width, height);
- mines = createMatrix(width, height);
- initMatrix(graf, 0, width, height);
- initMatrix(mines, countmines, width, height);
- drawMatrix(graf, width, height);
- while(isRunning == 0)
- {
- scanf("%d,%d",&cordx,&cordy);
- switch(tesztCOORD(mines,cordx-1, cordy-1))
- {
- case 0: isRunning = 1;system("cls");drawBombs(mines, width, height);break;
- case 1: graf[cordy-1][cordx-1] = mines[cordy-1][cordx-1];break;
- case 2: reveal(graf, mines, cordx-1, cordy-1, width, height);break;
- }
- if(isRunning ==0)
- drawMatrix(graf, width, height);
- if(checkWin(graf, mines, width, height)==0)
- {
- isRunning = 2;
- }
- }
- switch(isRunning)
- {
- case 1: printf("\n YOU DIED");break;
- case 2: printf("\n nyertel");break;
- }
- return 0;
- }
- char** createMatrix(int width,int height)
- {
- char **matrix = (char**)malloc(height*sizeof(char*));
- for(int i=0;i<height;++i)
- {
- *(matrix+i)=(char*)malloc(width*sizeof(char));
- }
- return matrix;
- }
- void freeMatrix(char** matrix,int width,int height)
- {
- for(int i =0;i<height;++i)
- {
- free(*(matrix+i));
- }
- free(matrix);
- }
- void initMatrix(char** matrix,int Mines,int width,int height)
- {
- srand(time(0));
- int mx;
- int my;
- int num;
- //Mines=0 Az igaz
- if(Mines==0)
- {
- for(int i=0;i<height;++i)
- {
- for(int j =0;j<width;++j)
- {
- matrix[i][j]='X';
- }
- }
- }
- else
- {
- //Bombak lehelyezese
- for(int i =0 ;i<Mines;++i)
- {
- my=rand()%height;
- mx=rand()%width;
- matrix[my][mx]='o';
- }
- //Szamok kitoltese
- for(int i= 0;i<height;++i)
- {
- for(int j=0;j<width;++j)
- {
- num='0';
- if(matrix[i][j]!='o')
- {
- //Balfelsor resz
- if(((i-1)>=0)&&((j-1)>=0)&&(matrix[i-1][j-1]=='o'))num++;
- //Felso resz
- if(((i-1)>=0)&&(matrix[i-1][j]=='o'))num++;
- //Jobb felso
- if(((i-1)>=0)&&((j+1)<width)&&(matrix[i-1][j+1]=='o'))num++;
- //Bal oldal
- if(((j-1)>=0)&&(matrix[i][j-1]=='o'))num++;
- //Jobb oldal
- if(((j+1)<width)&&(matrix[i][j+1]=='o'))num++;
- //Bal also
- if(((i+1)<height)&&((j-1)>=0)&&(matrix[i+1][j-1]=='o'))num++;
- //Also
- if(((i+1)<height)&&(matrix[i+1][j]=='o'))num++;
- //Jobb also
- if(((i+1)<height)&&((j+1)<width)&&(matrix[i+1][j+1]=='o'))num++;
- //Kitolt
- matrix[i][j]=num;
- }
- }
- }
- }
- }
- void drawMatrix(char** matrix,int width,int height)
- {
- //kepernyo torles
- system("cls");
- for(int i =0;i<height;++i)
- {
- for(int j = 0;j < width;++j)
- {
- printf("%c",matrix[i][j]);
- }
- printf("\n");
- }
- printf("Selection coords: ");
- }
- int tesztCOORD(char** matrix,int x,int y)
- {
- //return 0 ha bomba
- if(matrix[y][x] == 'o')return 0;
- //return 1 ha nem bomba es nem 0 all ott,return 2 ha nembomba es nulla all ott
- if(matrix[y][x] == '0')
- {
- return 2;
- }
- else
- {
- return 1;
- }
- }
- void reveal(char**matrixGraf,char**matrixMine,int x,int y,int width,int height)
- {
- if((x>=0)&&(y>=0)&&(x<width)&&(y<height))
- {
- if((matrixMine[y][x] == '0')&&(matrixGraf[y][x]!='0'))
- {
- matrixGraf[y][x] = '0';
- reveal(matrixGraf,matrixMine,x+1,y,width,height);
- reveal(matrixGraf,matrixMine,x,y+1,width,height);
- reveal(matrixGraf,matrixMine,x-1,y,width,height);
- reveal(matrixGraf,matrixMine,x,y-1,width,height);
- }
- else
- {
- matrixGraf[y][x]=matrixMine[y][x];
- return;
- }
- }
- else
- {
- return;
- }
- }
- void drawBombs(char** matrix,int width,int height)
- {
- for(int i=0;i<height;++i)
- {
- for(int j =0;j<width;++j)
- {
- if(matrix[i][j] == 'o')
- {
- printf("%c",'o');
- }
- else
- {
- printf("%c",' ');
- }
- }
- printf("\n");
- }
- }
- int checkWin(char** matrixGraf,char**matrixMine,int width,int height)
- {
- int countGraf=0,countMine=0;
- for(int i =0;i<height;++i)
- {
- for(int j =0;j < width;++j)
- {
- if(matrixGraf[i][j]=='X')countGraf++;
- if(matrixMine[i][j]=='o')countMine++;
- }
- }
- if(countGraf==countMine)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement