c语言小游戏扫雷代码
时间: 2024-04-16 12:21:39 浏览: 161
c语言小游戏代码-扫雷代码
4星 · 用户满意度95%
当然,我可以给你介绍一下C语言的扫雷游戏代码。下面是一个简单的C语言扫雷游戏代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 10
#define MINES 10
int board[SIZE][SIZE];
int revealed[SIZE][SIZE];
void initializeBoard() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
board[i][j] = 0;
revealed[i][j] = 0;
}
}
}
void placeMines() {
int count = 0;
while (count < MINES) {
int x = rand() % SIZE;
int y = rand() % SIZE;
if (board[x][y] != -1) {
board[x][y] = -1;
count++;
}
}
}
void printBoard() {
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (revealed[i][j]) {
if (board[i][j] == -1) {
printf("* ");
} else {
printf("%d ", board[i][j]);
}
} else {
printf(". ");
}
}
printf("\n");
}
}
int countAdjacentMines(int x, int y) {
int count = 0;
int i, j;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
if (x + i >= 0 && x + i < SIZE && y + j >= 0 && y + j < SIZE) {
if (board[x + i][y + j] == -1) {
count++;
}
}
}
}
return count;
}
void revealCell(int x, int y) {
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
return;
}
if (revealed[x][y]) {
return;
}
revealed[x][y] = 1;
if (board[x][y] == 0) {
int i, j;
for (i = -1; i <= 1; i++) {
for (j = -1; j <= 1; j++) {
revealCell(x + i, y + j);
}
}
}
}
int main() {
srand(time(NULL));
initializeBoard();
placeMines();
int gameOver = 0;
while (!gameOver) {
printBoard();
int x, y;
printf("Enter the coordinates of the cell to reveal (x y): ");
scanf("%d %d", &x, &y);
if (board[x][y] == -1) {
printf("Game Over! You hit a mine.\n");
gameOver = 1;
} else {
revealCell(x, y);
int revealedCount = 0;
int i, j;
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
if (revealed[i][j]) {
revealedCount++;
}
}
}
if (revealedCount == SIZE * SIZE - MINES) {
printf("Congratulations! You won the game.\n");
gameOver = 1;
}
}
}
return 0;
}
```
这是一个简单的命令行扫雷游戏,游戏板大小为10x10,总共有10个地雷。你可以根据需要进行修改和扩展。希望对你有帮助!
阅读全文