C语言扫雷小游戏

这个游戏会在控制台中运行,并且包含基本的功能,如生成雷区、显示雷区状态、用户输入和判断是否踩到雷。

cs 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10
#define MINE_COUNT 15

void initializeBoard(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE]);
void placeMines(char board[SIZE][SIZE], int mineCount);
void printBoard(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE]);
int countAdjacentMines(char board[SIZE][SIZE], int row, int col);
void revealCell(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE], int row, int col);
int checkWin(char visibleBoard[SIZE][SIZE]);

int main() {
    char board[SIZE][SIZE];
    char visibleBoard[SIZE][SIZE];
    int row, col;

    srand(time(NULL));
    initializeBoard(board, visibleBoard);
    placeMines(board, MINE_COUNT);

    do {
        printBoard(board, visibleBoard);
        printf("\\n请输入坐标(行 列): ");
        scanf("%d %d", &row, &col);

        if (board[row][col] == '*') {
            printf("很遗憾,你踩到了地雷!\\n");
            return 0;
        }

        revealCell(board, visibleBoard, row, col);
    } while (!checkWin(visibleBoard));

    printBoard(board, visibleBoard);
    printf("恭喜你,你赢了!\\n");

    return 0;
}

void initializeBoard(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            board[i][j] = ' ';
            visibleBoard[i][j] = '#';
        }
    }
}

void placeMines(char board[SIZE][SIZE], int mineCount) {
    int minesPlaced = 0;
    while (minesPlaced < mineCount) {
        int row = rand() % SIZE;
        int col = rand() % SIZE;

        if (board[row][col] != '*') {
            board[row][col] = '*';
            minesPlaced++;
        }
    }
}

void printBoard(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE]) {
    printf("  ");
    for (int i = 0; i < SIZE; i++) {
        printf("%2d ", i);
    }
    printf("\\n");

    for (int i = 0; i < SIZE; i++) {
        printf("%2d ", i);
        for (int j = 0; j < SIZE; j++) {
            printf("%2c ", visibleBoard[i][j]);
        }
        printf("\\n");
    }
}

int countAdjacentMines(char board[SIZE][SIZE], int row, int col) {
    int count = 0;
    for (int i = -1; i <= 1; i++) {
        for (int j = -1; j <= 1; j++) {
            if (i == 0 && j == 0) continue;
            int newRow = row + i;
            int newCol = col + j;
            if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE && board[newRow][newCol] == '*') {
                count++;
            }
        }
    }
    return count;
}

void revealCell(char board[SIZE][SIZE], char visibleBoard[SIZE][SIZE], int row, int col) {
    if (visibleBoard[row][col] != '#') return;

    int adjacentMines = countAdjacentMines(board, row, col);
    if (adjacentMines > 0) {
        visibleBoard[row][col] = '0' + adjacentMines;
    } else {
        visibleBoard[row][col] = '.';
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                int newRow = row + i;
                int newCol = col + j;
                if (newRow >= 0 && newRow < SIZE && newCol >= 0 && newCol < SIZE) {
                    revealCell(board, visibleBoard, newRow, newCol);
                }
            }
        }
    }
}

int checkWin(char visibleBoard[SIZE][SIZE]) {
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (visibleBoard[i][j] == '#') return 0;
        }
    }
    return 1;
}

游戏说明:

  • 玩家需要输入要打开的格子的行和列坐标。
  • 如果玩家打开的位置有地雷,则游戏结束。
  • 如果打开的位置没有地雷,程序会自动显示周围地雷的数量或递归地打开周围所有没有地雷的格子。
  • 当所有非雷格子都被打开时,玩家胜利。

注意事项:

  • 本代码假设输入的坐标是有效的,即在0到9之间。
  • 这个版本的游戏没有实现撤销功能或者标记地雷功能,这些可以作为进一步的扩展。
相关推荐
CoovallyAIHub7 分钟前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
ACP广源盛1392462567323 分钟前
(ACP广源盛)GSV2231---DisplayPort 1.4 MST 到 HDMI 2.0/DP/Type-C 转换器(带嵌入式 MCU)
c语言·开发语言·单片机·嵌入式硬件·音视频·mst
quant_198624 分钟前
【教程】使用加密货币行情接口 - 查询比特币实时价格
开发语言·后端·python·websocket·网络协议
熊猫_豆豆32 分钟前
Python 写一个标准版和程序员版计算器
开发语言·python·计算器
得物技术34 分钟前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
Mr.Jessy39 分钟前
Web APIs 学习第四天:DOM事件进阶
开发语言·前端·javascript·学习·ecmascript
QT 小鲜肉42 分钟前
【QT/C++】Qt网络编程进阶:UDP通信和HTTP请求的基本原理和实际应用(超详细)
c语言·网络·c++·笔记·qt·http·udp
studyForMokey1 小时前
【Kotlin内联函数】
android·开发语言·kotlin
小虚竹1 小时前
Rust日志系统完全指南:从log门面库到env_logger实战
开发语言·后端·rust
星释1 小时前
Rust 练习册 8:链表实现与所有权管理
开发语言·链表·rust