用c语言写一个扫雷游戏
时间: 2023-03-19 11:24:04 浏览: 41
好的,下面是一份简单的扫雷游戏的代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define MINES 10
void init(char board[ROW][COL], int row, int col);
void printBoard(char board[ROW][COL], int row, int col);
void plantMines(char board[ROW][COL], int row, int col, int mines);
void getInput(char board[ROW][COL], int row, int col);
int main() {
char board[ROW][COL];
srand(time(0));
init(board, ROW, COL);
plantMines(board, ROW, COL, MINES);
printBoard(board, ROW, COL);
getInput(board, ROW, COL);
return 0;
}
void init(char board[ROW][COL], int row, int col) {
int i, j;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
board[i][j] = '0';
}
}
}
void printBoard(char board[ROW][COL], int row, int col) {
int i, j;
printf(" ");
for (i = 0; i < col; i++) {
printf("%d ", i);
}
printf("\n");
for (i = 0; i < row; i++) {
printf("%2d ", i);
for (j = 0; j < col; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void plantMines(char board[ROW][COL], int row, int col, int mines) {
int i, x, y;
for (i = 0; i < mines; i++) {
x = rand() % row;
y = rand() % col;
if (board[x][y] == '*') {
i--;
} else {
board[x][y] = '*';
}
}
}
void getInput(char board[ROW][COL], int row, int col) {
int x, y;
while (1) {
printf("Enter row and col: ");
scanf("%d%d", &x, &y);
if (board[x][y] == '*') {
printf("Boom!\n");
break;
} else {
printf("Safe!\n");
}
}
printBoard(board, ROW, COL);
}
```
这份代码可以帮助你了解如何写一个扫雷游戏。该代码实现了扫雷游
阅读全文