fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 8
  4.  
  5. char bd[N][N];
  6.  
  7. int
  8. countAroundBombs(int r, int c)
  9. {
  10. int cnt = 0, i, j;
  11. for (i = r - 1; i <= r + 1; i++)
  12. for (j = c - 1; j <=c + 1; j++)
  13. if (r >= 0 && r < N && c >= 0 && c < N)
  14. if (bd[i][j] == 'x')
  15. cnt++;
  16. return cnt;
  17. }
  18.  
  19. void
  20. pp(int mode)
  21. {
  22. int i, j;
  23.  
  24. printf(" ");
  25. for (i = 0; i < N; i++)
  26. printf("%d", i);
  27. putchar('\n');
  28. for (i = 0; i < N; i++) {
  29. printf("%d ", i % N);
  30. for (j = 0; j < N; j++)
  31. if (mode) { /* display all */
  32. if(bd[i][j]!=' ') {
  33. putchar(bd[i][j]);
  34. } else {
  35. putchar('-');
  36. };
  37. } else {/* display limited */
  38. if (bd[i][j] == 'x' || bd[i][j] == ' ')
  39. putchar('-');
  40. else
  41. putchar(bd[i][j]);
  42. }
  43. putchar('\n');
  44. }
  45. }
  46.  
  47. int
  48. main()
  49. {
  50. int i, r, c;
  51.  
  52. /* clear initialize */
  53. for (i = 0; i < N * N; i++)
  54. bd[i / N][i % N] = ' ';
  55. /* set bombs */
  56. for(i=10; i;) {
  57. r=rand()%(N*N);
  58. c=r%N;
  59. r/=N;
  60. if(bd[r][c]==' ') {
  61. bd[r][c]='x';
  62. i--;
  63. }
  64. }
  65. while (1) {
  66. pp(0);
  67. printf("position row col = ");
  68. scanf("%d %d", &r, &c);
  69. if (bd[r][c] == 'x') {
  70. pp(1);
  71. printf("Bomb!\n");
  72. break;
  73. }
  74. bd[r][c]='0'+countAroundBombs(r,c);
  75. }
  76.  
  77. return 0;
  78. }
Success #stdin #stdout 0.01s 1680KB
stdin
Standard input is empty
stdout
  01234567
0 --------
1 --------
2 --------
3 --------
4 --------
5 --------
6 --------
7 --------
position row col =   01234567
0 ------x-
1 --x--x--
2 -x------
3 --------
4 -------x
5 -x--x---
6 ---x----
7 --x----x
Bomb!