用c语言写一个排雷游戏
时间: 2023-05-17 14:03:57 浏览: 195
好的,以下是一个简单的排雷游戏的C语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROWS 10
#define COLS 10
#define MINES 10
void init_board(char board[][COLS], int rows, int cols, char ch);
void print_board(char board[][COLS], int rows, int cols);
void set_mines(char board[][COLS], int rows, int cols, int num_mines);
void get_neighbors(char board[][COLS], int rows, int cols, int row, int col, int *n, int *s, int *e, int *w, int *ne, int *nw, int *se, int *sw);
void reveal(char board[][COLS], char mask[][COLS], int rows, int cols, int row, int col);
int count_mines(char board[][COLS], int rows, int cols, int row, int col);
int main(void)
{
char board[ROWS][COLS];
char mask[ROWS][COLS];
int row, col, num_mines, num_revealed;
srand((unsigned)time(NULL));
init_board(board, ROWS, COLS, '-');
init_board(mask, ROWS, COLS, '*');
num_mines = MINES;
set_mines(board, ROWS, COLS, num_mines);
num_revealed = 0;
while (num_revealed < ROWS * COLS - num_mines) {
print_board(mask, ROWS, COLS);
printf("Enter row and column (e.g. 3 4): ");
scanf("%d %d", &row, &col);
if (board[row][col] == '*') {
printf("BOOM! Game over.\n");
break;
}
reveal(board, mask, ROWS, COLS, row, col);
num_revealed++;
}
if (num_revealed == ROWS * COLS - num_mines) {
printf("Congratulations! You win!\n");
}
return 0;
}
void init_board(char board[][COLS], int rows, int cols, char ch)
{
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
board[i][j] = ch;
}
}
}
void print_board(char board[][COLS], int rows, int cols)
{
int i, j;
printf(" ");
for (j = 0; j < cols; j++) {
printf("%d ", j);
}
printf("\n");
for (i = 0; i < rows; i++) {
printf("%d ", i);
for (j = 0; j < cols; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void set_mines(char board[][COLS], int rows, int cols, int num_mines)
{
int i, j, k;
for (k = 0; k < num_mines; k++) {
do {
i = rand() % rows;
j = rand() % cols;
} while (board[i][j] == '*');
board[i][j] = '*';
}
}
void get_neighbors(char board[][COLS], int rows, int cols, int row, int col, int *n, int *s, int *e, int *w, int *ne, int *nw, int *se, int *sw)
{
*n = (row > 0) ? board[row - 1][col] : 0;
*s = (row < rows - 1) ? board[row + 1][col] : 0;
*e = (col < cols - 1) ? board[row][col + 1] : 0;
*w = (col > 0) ? board[row][col - 1] : 0;
*ne = (row > 0 && col < cols - 1) ? board[row - 1][col + 1] : 0;
*nw = (row > 0 && col > 0) ? board[row - 1][col - 1] : 0;
*se = (row < rows - 1 && col < cols - 1) ? board[row + 1][col + 1] : 0;
*sw = (row < rows - 1 && col > 0) ? board[row + 1][col - 1] : 0;
}
void reveal(char board[][COLS], char mask[][COLS], int rows, int cols, int row, int col)
{
int n, s, e, w, ne, nw, se, sw, num_mines;
if (mask[row][col] == '-') {
mask[row][col] = board[row][col];
if (board[row][col] == ' ') {
get_neighbors(board, rows, cols, row, col, &n, &s, &e, &w, &ne, &nw, &se, &sw);
if (n == '-') reveal(board, mask, rows, cols, row - 1, col);
if (s == '-') reveal(board, mask, rows, cols, row + 1, col);
if (e == '-') reveal(board, mask, rows, cols, row, col + 1);
if (w == '-') reveal(board, mask, rows, cols, row, col - 1);
if (ne == '-') reveal(board, mask, rows, cols, row - 1, col + 1);
if (nw == '-') reveal(board, mask, rows, cols, row - 1, col - 1);
if (se == '-') reveal(board, mask, rows, cols, row + 1, col + 1);
if (sw == '-') reveal(board, mask, rows, cols, row + 1, col - 1);
}
}
}
int count_mines(char board[][COLS], int rows, int cols, int row, int col)
{
int n, s, e, w, ne, nw, se, sw, count;
get_neighbors(board, rows, cols, row, col, &n, &s, &e, &w, &ne, &nw, &se, &sw);
count = 0;
if (n == '*') count++;
if (s == '*') count++;
if (e == '*') count++;
if (w == '*') count++;
if (ne == '*') count++;
if (nw == '*') count++;
if (se == '*') count++;
if (sw == '*') count++;
return count;
}
阅读全文