Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include<stdio.h> // interger to pointer without cast PROBLEM....
  2. #include<stdlib.h> // SOLUTION : whenever using string the data type
  3. // of the array must be char
  4.  
  5. void welcome();
  6. void rand_mines(char msweep[12][12]);
  7. void printmatrix(char msweep[12][12],int r,char user_chart[12][12]);
  8. int process(char msweep[12][12],int r,int c,char user_chart[12][12]);
  9.  
  10. int main()
  11.  
  12. {
  13. welcome();
  14. char msweep[12][12] = {{'0'}};
  15. int i,r,c;
  16. char user_chart[12][12] = {{'0'}};
  17. rand_mines(msweep);
  18. printf("Where is your first guess?\n");
  19. printf("Enter your location between 1 - 11.\n");
  20. printf("Enter x variable or the row of your choice\n");
  21. scanf("%d",&r);
  22. printf("Enter y variable or the column of your choice\n");
  23. scanf("%d",&c);
  24. printmatrix(msweep,12,user_chart);
  25. i = process(msweep,r,c,user_chart);
  26. while(i == 1)
  27. {
  28. printf("Congratulations! You survived, well for now.\n");
  29. printf(" %c Surrounding MINEs\n\n",msweep[r][c]);
  30. printmatrix(msweep,12,user_chart);
  31. printf("Enter your location between 1 - 11.\n");
  32. printf("Enter x variable or the row of your choice\n");
  33. scanf("%d",&r);
  34. printf("Enter y variable or the column of your choice\n");
  35. scanf("%d",&c);
  36. i=0;
  37. i = process(msweep,r,c,user_chart);
  38. }
  39. if(i==0)
  40. printf("You Died.\n");
  41. return 0;
  42. }
  43. void welcome()
  44. {
  45. char op; // opereation
  46. printf("Welcome to MINESWEEPER programmed in C.\n");
  47. printf("Enter i for instructions or any other key to enter game\n");
  48. scanf("%c",&op);
  49. if(op == 'i')
  50. {
  51. printf("Well well well, you are unfortunately in the midst of a minefield.\n ");
  52. printf("You need to traverse your way through without stepping on any mines.\n");
  53. printf("Enter the coordinates of the x and y plane between 1 to 11\n");
  54. printf("May fate and luck be always on your side.\n");
  55. }
  56. else
  57. return;
  58. }
  59. void rand_mines(char msweep[12][12])
  60. {
  61. int r,c,m;
  62. for(m=0;m<20;m++) // plant 20 rand mines(m
  63. {
  64. r = rand() % 13; // this is mine planting
  65. c = rand() % 13; // so 0 to 13 is APPROPRIATE.
  66. msweep[r][c] = '9';
  67. }
  68. return;
  69. }
  70. void printmatrix(char msweep[][12],int r,char user_chart[12][12])
  71. {
  72. int i,j;
  73. printf(" ##########\n");
  74. for(i=1;i<r;i++)
  75. {
  76. printf("#");
  77. for(j=1;j<12;j++)
  78. {
  79. printf("%c ",user_chart[i][j]);
  80. }
  81. printf("#");
  82. printf("\n");
  83. }
  84. printf(" ###########\n\n");
  85. return;
  86. }
  87. int process(char msweep[12][12],int r,int c,char user_chart[12][12])
  88. {
  89. int i=r,j=c,b=0,k;
  90. char C;
  91. if(msweep[r][c] == '9')
  92. { k=0;
  93. return k;
  94. }
  95. else
  96. {
  97. if(msweep[i-1][j-1] == '9')
  98. b++;
  99. if(msweep[i-1][j] == '9')
  100. b++;
  101. if(msweep[i-1][j+1] == '9')
  102. b++;
  103. if(msweep[i][j-1] == '9')
  104. b++;
  105. if(msweep[i][j+1] == '9')
  106. b++;
  107. if(msweep[i+1][j-1] == '9')
  108. b++;
  109. if(msweep[i+1][j] == '9')
  110. b++;
  111. if(msweep[i+1][j+1] == '9')
  112. b++;
  113. C = (char)(((int)'0')+b);
  114. msweep[r][c] = C;
  115. user_chart[r][c] = C;
  116. }
  117. return 1;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement