Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- void GetData (int *A, int *B, int *C);
- char **MemoryAllocation (char **D,int E,int F);
- void BlankSpace (char **G,int H,int I);
- void Bombs (char **J,int K,int L,int M);
- void CountBlocks (char **O,int P,int Q);
- void Present (char **R,int S,int T);
- int isInside (int rows, int columns, int row, int column);
- void printToText (FILE *FP, char **U, int V, int W);
- int main(){
- int *M,*N,*K,a,b,c;
- char **A;
- FILE *fp;
- a=b=c=0;
- A=0;
- M=&a;
- N=&b;
- K=&c;
- GetData (M,N,K);
- A=MemoryAllocation(A,*M,*N);
- BlankSpace (A,*M,*N);
- Bombs(A,*M,*N,*K);
- CountBlocks(A,*M,*N);
- Present(A,*M,*N);
- fp=fopen("minesweep","w");
- printToText(fp,A,*M,*N);
- fclose(fp);
- free(A);
- return 0;
- }
- void GetData (int *A,int *B,int *C){
- printf ("Δώσε τον αριθμό γραμμών.\n");
- scanf ("%d",A);
- printf ("Δώσε τον αριθμό στηλών.\n");
- scanf ("%d",B);
- printf ("Δώσε τον αριθμό βομβών.\n");
- scanf ("%d",C);
- }
- char **MemoryAllocation (char **D,int E,int F){
- int i;
- D=(char**)malloc(E*sizeof(char*));
- for (i=0;i<F;i++){
- *(D+i)=(char*)malloc(F*sizeof(char));
- }
- return D;
- }
- void BlankSpace (char **G,int H,int I){
- int i,j;
- for (i=0;i<H;i++){
- for (j=0;j<I;j++){
- G[i][j]=' ';
- }
- }
- }
- void Bombs (char **J,int K,int L,int M){
- int i,x,y;
- srand(time(NULL));
- for (i=1;i<=M;i++) {
- x = rand() % K;
- y = rand() % L;
- while (J[x][y]!='K')
- J[x][y] = 'K';
- }
- }
- void CountBlocks (char **O,int P,int Q){
- int i,j;
- for (i=0;i<P;i++){
- for (j=0;j<Q;j++){
- if (O[i][j-1]=='K' && isInside(P,Q,i,j-1)==1){
- O[i][j]='1';
- if (O[i][j+1]=='K' && isInside(P,Q,i,j+1)==1){
- O[i][j]='2';
- if (O[i-1][j]=='K' && isInside(P,Q,i-1,j)==1){
- O[i][j]='3';
- if (O[i-1][j-1]=='K' && isInside(P,Q,i-1,j-1)==1){
- O[i][j]='4';
- if (O[i-1][j+1]=='K' && isInside(P,Q,i-1,j+1)==1){
- O[i][j]='5';
- if (O[i+1][j]=='K' && isInside(P,Q,i+1,j)==1){
- O[i][j]='6';
- if (O[i+1][j-1]=='K' && isInside(P,Q,i+1,j-1)==1){
- O[i][j]='7';
- if (O[i+1][j+1]=='K' && isInside(P,Q,i+1,j+1)==1){
- O[i][j]='8';
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- void Present (char **R,int S,int T){
- int i,j;
- for (i=0;i<S;i++){
- for (j=0;j<T;j++)
- printf ("%c",R[i][j]);
- printf ("\n");
- }
- }
- int isInside (int rows,int columns,int row,int column) {
- return !(row < 0 || row >= rows || column < 0 || column >= columns);
- }
- void printToText (FILE *FP,char **U, int V,int W){
- int i,j;
- for (i=0;i<V;i++){
- for (j=0;j<W;j++)
- fprintf(FP,"%c",U[i][j]);
- printf ("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement