#define _CRT_SECURE_NO_WARNINGS |
| 2 | |
| 3 | #include "game.h" |
| 4 | void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set) |
| 5 | { |
| 6 | int i = 0; |
| 7 | for (i = 0; i < rows; i++) |
| 8 | { |
| 9 | int j = 0; |
| 10 | for (j = 0; j < cols; j++) |
| 11 | { |
| 12 | arr[i][j] = set; |
| 13 | } |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | void DisplayBoard(char arr[ROWS][COLS], int row, int col) |
| 18 | { |
| 19 | printf("-----扫雷游戏------\n"); |
| 20 | int i = 0; |
| 21 | for (i = 0; i <= col; i++) |
| 22 | { |
| 23 | printf("%d ", i); |
| 24 | } |
| 25 | printf("\n"); |
| 26 | for (i = 1; i < row; i++) |
| 27 | { |
| 28 | int j = 0; |
| 29 | printf("%d ", i); |
| 30 | for (j = 1; j <= col; j++) |
| 31 | { |
| 32 | printf("%c ", arr[i][j]); |
| 33 | } |
| 34 | printf("\n"); |
| 35 | } |
| 36 | } |
| 37 | void SetMine(char arr[ROWS][COLS], int row, int col) |
| 38 | { |
| 39 | int count = EASY_COUNT; |
| 40 | while (count) |
| 41 | { |
| 42 | int x = rand() % row + 1; |
| 43 | int y = rand() % col + 1; |
| 44 | if (arr[x][y] == '0') |
| 45 | { |
| 46 | arr[x][y] = '1'; |
| 47 | count--; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | //static int GetMineCount(char mine[ROWS][COLS],int x, int y) |
| 52 | //{ |
| 53 | // return mine[x - 1][y] + mine[x - 1][y-1] + mine[x][y-1] + mine[x + 1][y-1] + mine[x + 1][y] + mine[x + 1][y+1] |
| 54 | // + mine[x][y+1] + mine[x - 1][y+1]-8*'0'; |
| 55 | //} |
| 56 | //上面的函数用循环来写 |
| 57 | static int GetMineCount(char mine[ROWS][COLS], int x, int y) |
| 58 | { |
| 59 | int i = 0; |
| 60 | int count = 0; |
| 61 | for (i = x - 1; i <= x + 1; x++) |
| 62 | { |
| 63 | int j = 0; |
| 64 | for (j = y - 1; j <= y + 1; j++) |
| 65 | { |
| 66 | count += mine[i][j] - '0'; |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | return count; |
| 71 | |
| 72 | } |
| 73 | void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) |
| 74 | { |
| 75 | int x = 0; |
| 76 | int y = 0; |
| 77 | int win = 0; |
| 78 | |
| 79 | while (win<(row*col- EASY_COUNT)) |
| 80 | { |
| 81 | printf("请输入要排查雷的坐标:"); |
| 82 | scanf("%d %d", &x, &y); |
| 83 | //判断坐标的有效性 |
| 84 | if (x >= 1 && x <= row && y >= 1 && y <= col) |
| 85 | { |
| 86 | if (mine[x][y] == '1') |
| 87 | { |
| 88 | printf("很遗憾,你被炸死了\n"); |
| 89 | DisplayBoard(mine, ROW, COL); |
| 90 | break; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | //该坐标不是雷,就统计坐标周围几个雷 |
| 95 | int count = GetMineCount(mine, x, y); |
| 96 | show[x][y] = count + '0'; |
| 97 | DisplayBoard(show, ROW, COL); |
| 98 | win++; |
| 99 | } |
| 100 | } |
| 101 | else |
| 102 | { |
| 103 | printf("坐标非法,请重新输入\n"); |
| 104 | |
| 105 | |
| 106 | } |
| 107 | } |
| 108 | if (win = row * col - EASY_COUNT) |
| 109 | { |
| 110 | printf("恭喜你,排雷成功\n"); |
| 111 | DisplayBoard(mine, ROW, COL); |
| 112 | } |
| |
}
|