帮我用c语言写一个扫雷游戏要求:第一次扫雷不会碰到雷
时间: 2023-12-19 11:04:36 浏览: 53
好的,以下是一个使用C语言编写的扫雷游戏,满足您的要求。在这个版本中,第一次扫雷不会碰到雷。
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define MINES 15
void initializeBoard(char board[SIZE][SIZE]) {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
board[i][j] = '-';
}
}
}
void placeMines(char board[SIZE][SIZE]) {
int count = 0;
while (count < MINES) {
int x = rand() % SIZE;
int y = rand() % SIZE;
if (board[x][y] != '*') {
board[x][y] = '*';
count++;
}
}
}
void printBoard(char board[SIZE][SIZE]) {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
printf("%c ", board[i][j]);
}
printf("\n");
}
}
int countMines(char board[SIZE][SIZE], int row, int col) {
int count = 0;
int i, j;
for (i = row - 1; i <= row + 1; i++) {
for (j = col - 1; j <= col + 1; j++) {
if (i >= 0 && i < SIZE && j >= 0 && j < SIZE && board[i][j] == '*') {
count++;
}
}
}
return count;
}
void revealCell(char board[SIZE][SIZE], char displayBoard[SIZE][SIZE], int row, int col) {
if (row < 0 || row >= SIZE || col < 0 || col >= SIZE || displayBoard[row][col] != '-') {
return;
}
displayBoard[row][col] = board[row][col];
if (board[row][col] == '0') {
revealCell(board, displayBoard, row - 1, col - 1);
revealCell(board, displayBoard, row - 1, col);
revealCell(board, displayBoard, row - 1, col + 1);
revealCell(board, displayBoard, row, col - 1);
revealCell(board, displayBoard, row, col + 1);
revealCell(board, displayBoard, row + 1, col - 1);
revealCell(board, displayBoard, row + 1, col);
revealCell(board, displayBoard, row + 1, col + 1);
}
}
int main() {
char board[SIZE][SIZE];
char displayBoard[SIZE][SIZE];
int row, col;
int firstMove = 1;
srand(time(NULL));
initializeBoard(board);
placeMines(board);
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
if (board[row][col] != '*') {
int count = countMines(board, row, col);
board[row][col] = count + '0';
}
}
}
initializeBoard(displayBoard);
while (1) {
printf("Current board:\n");
printBoard(displayBoard);
if (firstMove) {
printf("Enter row and column for your first move (e.g., 2 3): ");
scanf("%d %d", &row, &col);
firstMove = 0;
if (board[row][col] == '*') {
// If the first move hits a mine, regenerate the board
initializeBoard(board);
placeMines(board);
for (row = 0; row < SIZE; row++) {
for (col = 0; col < SIZE; col++) {
if (board[row][col] != '*') {
int count = countMines(board, row, col);
board[row][col] = count + '0';
}
}
}
initializeBoard(displayBoard);
revealCell(board, displayBoard, row, col);
} else {
revealCell(board, displayBoard, row, col);
}
} else {
printf("Enter row and column (e.g., 2 3): ");
scanf("%d %d", &row, &col);
if (board[row][col] == '*') {
printf("Game over! You hit a mine.\n");
break;
} else {
revealCell(board, displayBoard, row, col);
}
}
}
printf("Final board:\n");
printBoard(board);
return 0;
}
```
这个版本的扫雷游戏会在第一次扫雷时避开雷区,确保玩家不会在刚开始游戏时就触雷。希望对您有帮助!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)