扫雷游戏C语言源码


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 10 // 扫雷地图大小
#define BOMBS 10 // 地雷数量

int board[SIZE][SIZE]; // 扫雷地图
int uncover[SIZE][SIZE]; // 标记是否被翻开,1表示已翻开,0表示未翻开

void initBoard() {
    // 初始化地图
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            board[i][j] = 0; // 0表示没有地雷
            uncover[i][j] = 0; // 初始时所有格子未翻开
        }
    }
}

void printBoard() {
    // 打印地图
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (uncover[i][j]) {
                if (board[i][j] == -1) {
                    printf("* "); // 地雷
                } else {
                    printf("%d ", board[i][j]); // 数字
                }
            } else {
                printf(". "); // 未翻开
            }
        }
        printf("\n");
    }
    printf("\n");
}

void generateBombs() {
    // 生成地雷
    int count = 0;
    while (count < BOMBS) {
        int row = rand() % SIZE;
        int col = rand() % SIZE;
        if (board[row][col] != -1) {
            board[row][col] = -1;
            count++;
        }
    }
}

void calculateNumbers() {
    // 计算每个格子周围地雷的数量
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            if (board[i][j] != -1) {
                int count = 0;
                if (i > 0 && j > 0 && board[i - 1][j - 1] == -1) count++;
                if (i > 0 && board[i - 1][j] == -1) count++;
                if (i > 0 && j < SIZE - 1 && board[i - 1][j + 1] == -1) count++;
                if (j > 0 && board[i][j - 1] == -1) count++;
                if (j < SIZE - 1 && board[i][j + 1] == -1) count++;
                if (i < SIZE - 1 && j > 0 && board[i + 1][j - 1] == -1) count++;
                if (i < SIZE - 1 && board[i + 1][j] == -1) count++;
                if (i < SIZE - 1 && j < SIZE - 1 && board[i + 1][j + 1] == -1) count++;
                board[i][j] = count;
            }
        }
    }
}

int main() {
    srand(time(0)); // 设置随机种子

    initBoard();
    generateBombs();
    calculateNumbers();

    int row, col;
    while (1) {
        printBoard();

        printf("请输入要翻开的格子的行号和列号(范围:0-%d):", SIZE - 1);
        scanf("%d %d", &row, &col);

        if (row >= 0 && row < SIZE && col >= 0 && col < SIZE) {
            uncover[row][col] = 1; // 标记为已翻开

            if (board[row][col] == -1) {
                printf("你被炸死了!游戏结束!\n");
                break;
            } else if (board[row][col] > 0) {
                printf("你翻开了一个数字:%d\n", board[row][col]);
            } else {
                printf("你翻开了一个空白格子,请继续探索!\n");
            }
        } else {
            printf("输入的格子位置不合法,请重新输入!\n");
        }
    }

    return 0;
}

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值