Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. void GetData (int *A, int *B, int *C);
  6. char **MemoryAllocation (char **D,int E,int F);
  7. void BlankSpace (char **G,int H,int I);
  8. void Bombs (char **J,int K,int L,int M);
  9. void CountBlocks (char **O,int P,int Q);
  10. void Present (char **R,int S,int T);
  11. int isInside (int rows, int columns, int row, int column);
  12. void printToText (FILE *FP, char **U, int V, int W);
  13.  
  14. int main(){
  15. int *M,*N,*K,a,b,c;
  16. char **A;
  17. FILE *fp;
  18. a=b=c=0;
  19. A=0;
  20. M=&a;
  21. N=&b;
  22. K=&c;
  23. GetData (M,N,K);
  24. A=MemoryAllocation(A,*M,*N);
  25. BlankSpace (A,*M,*N);
  26. Bombs(A,*M,*N,*K);
  27. CountBlocks(A,*M,*N);
  28. Present(A,*M,*N);
  29. fp=fopen("minesweep","w");
  30. printToText(fp,A,*M,*N);
  31. fclose(fp);
  32. free(A);
  33. return 0;
  34. }
  35.  
  36. void GetData (int *A,int *B,int *C){
  37. printf ("Δώσε τον αριθμό γραμμών.\n");
  38. scanf ("%d",A);
  39. printf ("Δώσε τον αριθμό στηλών.\n");
  40. scanf ("%d",B);
  41. printf ("Δώσε τον αριθμό βομβών.\n");
  42. scanf ("%d",C);
  43. }
  44.  
  45. char **MemoryAllocation (char **D,int E,int F){
  46. int i;
  47. D=(char**)malloc(E*sizeof(char*));
  48. for (i=0;i<F;i++){
  49. *(D+i)=(char*)malloc(F*sizeof(char));
  50. }
  51. return D;
  52. }
  53.  
  54. void BlankSpace (char **G,int H,int I){
  55. int i,j;
  56. for (i=0;i<H;i++){
  57. for (j=0;j<I;j++){
  58. G[i][j]=' ';
  59. }
  60. }
  61. }
  62.  
  63. void Bombs (char **J,int K,int L,int M){
  64. int i,x,y;
  65. srand(time(NULL));
  66. for (i=1;i<=M;i++) {
  67. x = rand() % K;
  68. y = rand() % L;
  69. while (J[x][y]!='K')
  70. J[x][y] = 'K';
  71. }
  72. }
  73.  
  74. void CountBlocks (char **O,int P,int Q){
  75. int i,j;
  76. for (i=0;i<P;i++){
  77. for (j=0;j<Q;j++){
  78. if (O[i][j-1]=='K' && isInside(P,Q,i,j-1)==1){
  79. O[i][j]='1';
  80. if (O[i][j+1]=='K' && isInside(P,Q,i,j+1)==1){
  81. O[i][j]='2';
  82. if (O[i-1][j]=='K' && isInside(P,Q,i-1,j)==1){
  83. O[i][j]='3';
  84. if (O[i-1][j-1]=='K' && isInside(P,Q,i-1,j-1)==1){
  85. O[i][j]='4';
  86. if (O[i-1][j+1]=='K' && isInside(P,Q,i-1,j+1)==1){
  87. O[i][j]='5';
  88. if (O[i+1][j]=='K' && isInside(P,Q,i+1,j)==1){
  89. O[i][j]='6';
  90. if (O[i+1][j-1]=='K' && isInside(P,Q,i+1,j-1)==1){
  91. O[i][j]='7';
  92. if (O[i+1][j+1]=='K' && isInside(P,Q,i+1,j+1)==1){
  93. O[i][j]='8';
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105.  
  106. void Present (char **R,int S,int T){
  107. int i,j;
  108. for (i=0;i<S;i++){
  109. for (j=0;j<T;j++)
  110. printf ("%c",R[i][j]);
  111. printf ("\n");
  112. }
  113. }
  114.  
  115. int isInside (int rows,int columns,int row,int column) {
  116. return !(row < 0 || row >= rows || column < 0 || column >= columns);
  117. }
  118.  
  119. void printToText (FILE *FP,char **U, int V,int W){
  120. int i,j;
  121. for (i=0;i<V;i++){
  122. for (j=0;j<W;j++)
  123. fprintf(FP,"%c",U[i][j]);
  124. printf ("\n");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement