1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| #include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } }
void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0;
printf("\n");
for (i = 0; i <= row; i++) { printf("%d ", i); } printf("\n");
for (i = 1; i <= row; i++) { printf("%d ", i);
for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("\n"); }
void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT;
srand((unsigned int)time(NULL));
while (count) { int x = rand() % row + 1; int y = rand() % col + 1;
if (board[x][y] == '0') { board[x][y] = '1'; count--; } } }
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int x = 0; int y = 0;
while (1) { printf("请输入排查雷的坐标:>>>"); scanf("%d %d", &x, &y); if (x >= 1 && x <= row && y >= 1 && y <= col) { if (mine[x][y] == '1') { printf("\n很不幸,您被炸死了!!!\n\n雷的分布情况如下:\n"); DisplayBoard(mine, row, col); printf("-----------游戏结束----------\n\n"); break; } else if (mine[x][y] == '0') { show[x][y] = GetCount(mine, x, y) + '0'; OpenMine(mine, show, x, y); DisplayBoard(show, row, col); if (Win(show, ROW, COL) == EASY_COUNT) { printf("恭喜你成功排雷,太棒了,太棒了,送你一朵小红花!!!\n"); printf("雷的分布情况如下:\n"); DisplayBoard(mine, row, col); break; } } } else { printf("非法输入!!!\n"); } } }
int GetCount(char mine[ROWS][COLS], int x, int y) { int count= 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] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * '0';
return count; }
void OpenMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) { if (GetCount(mine, x, y) == 0) { show[x][y] = ' '; if ((x - 1) > 0 && (y - 1) > 0 && (show[x - 1][y - 1] == '*')) OpenMine(mine, show, x - 1, y - 1); if ((x - 1) > 0 && (y) > 0 && (show[x - 1][y] == '*')) OpenMine(mine, show, x - 1, y); if ((x - 1) > 0 && (y + 1) > 0 && (show[x - 1][y + 1] == '*')) OpenMine(mine, show, x - 1, y + 1); if ((x) > 0 && (y - 1) > 0 && (show[x][y - 1] == '*')) OpenMine(mine, show, x, y - 1); if ((x) > 0 && (y + 1) > 0 && (show[x][y + 1] == '*')) OpenMine(mine, show, x, y + 1); if ((x + 1) > 0 && (y - 1) > 0 && (show[x + 1][y - 1] == '*')) OpenMine(mine, show, x + 1, y - 1); if ((x + 1) > 0 && (y) > 0 && (show[x + 1][y] == '*')) OpenMine(mine, show, x + 1, y); if ((x + 1) > 0 && (y + 1) > 0 && (show[x + 1][y + 1] == '*')) OpenMine(mine, show, x + 1, y + 1); } else { show[x][y] = GetCount(mine, x, y) + '0'; } }
int Win(char show[ROWS][COLS], int row, int col) { int i = 0, j = 0, count = 0; for (i = 1; i <= row; i++) { for (j = 1; j <= col; j++) { if (show[i][j] == '*') { count++; } } } return count; }
|