用C语言实现扫雷

开发工具:VS2022;

创建三个文件
第一个头文件:game.h

游戏的数据类型和函数声明的位置

//游戏的数据类型和函数声明
#define _CRT_SECURE_NO_WARNINGS
#pragma

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

#define EASY_COUNT 10

#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

//初始化棋盘
void InitBoard(char board[ROWS], int rows, int cols, char set);

//打印棋盘
void DisloayBoard(char board[ROWS][COLS], int row, int col);

//步骤雷
void SetMine(char board[ROWS][COLS], int row, int col);

//排查雷
void FindMine(char mine[ROWS][COLS], int show[ROWS][COLS], int row, int col);

第二个:game.c文件

//游戏函数的实现
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
    int i, j;
    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {
            board[i][j] = set;
        }
    }
}

void DisplayBoard(char board[ROWS][COLS], int row, int col) {
    int i, j;
    printf("--------扫雷游戏-------\n");
    for (i = 0; i <= col; i++) {
        printf("%d ", i);
    }
    printf("\n");
    for (i = 1; i <= row; i++) {
        printf("%d ", i);
        for (j = 1; j <= col; j++) {
            printf("%c ", board[i][j]);
        }
        printf("\n");
    }
}

void SetMine(char board[ROWS][COLS], int row, int col) {
    int count = EASY_COUNT;
    while (count) {
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        if (board[x][y] == '0') {
            board[x][y] = '1';
            count--;
        }
    }
}

int GetMineCount(char mine[ROWS][COLS], int x, int y) {
    return (mine[x - 1][y] + mine[x - 1][y - 1] + mine[x][y - 1] + mine[x + 1][y - 1] +
        mine[x + 1][y] + mine[x + 1][y + 1] + mine[x][y + 1] + mine[x - 1][y + 1] -
        8 * '0');
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
    int x, y, win = 0;
    while (win < row * col - EASY_COUNT) {
        printf("请输入要排查的坐标:>");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col) {
            if (mine[x][y] == '1') {
                printf("很遗憾,你被炸死了\n");
                DisplayBoard(mine, ROW, COL);
                break;
            }
            else {
                int count = GetMineCount(mine, x, y);
                show[x][y] = count + '0';
                DisplayBoard(show, ROW, COL);
                win++;
            }
        }
        else {
            printf("坐标非法,重新输入\n");
        }
    }
    if (win == row * col - EASY_COUNT) {
        printf("恭喜你,排雷成功\n");
        DisplayBoard(mine, ROW, COL);
    }
}

第三个:text.c

//游戏的测试逻辑
#include "game.h"

void menu() {
    printf("***********************\n");
    printf("***** 1. play *****\n");
    printf("***** 0. exit *****\n");
    printf("***********************\n");
}

void game() {
    char mine[ROWS][COLS]; //存放布置好的雷
    char show[ROWS][COLS]; //存放排查出的雷的信息
    //初始化棋盘
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');
    //打印棋盘
    DisplayBoard(show, ROW, COL);
    //布置雷
    SetMine(mine, ROW, COL);
    //排查雷
    FindMine(mine, show, ROW, COL);
}

int main() {
    int input = 0;
    srand((unsigned int)time(NULL));
    do {
        menu();
        printf("请选择:>");
        scanf("%d", &input);
        switch (input) {
        case 1:
            game();
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("选择错误,重新选择\n");
            break;
        }
    } while (input);
    return 0;
}

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是一个简单的扫雷程序的C语言代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROWS 10 #define COLS 10 #define MINES 10 void print_board(char board[ROWS][COLS], int reveal_all); void place_mines(char board[ROWS][COLS]); int count_adjacent_mines(char board[ROWS][COLS], int row, int col); void reveal_cell(char board[ROWS][COLS], int row, int col, int *remaining_cells); void play_game(char board[ROWS][COLS]); int main() { char board[ROWS][COLS]; srand(time(NULL)); place_mines(board); print_board(board, 0); play_game(board); return 0; } void print_board(char board[ROWS][COLS], int reveal_all) { int i, j; printf(" "); for (i = 0; i < COLS; i++) { printf("%d ", i + 1); } printf("\n"); for (i = 0; i < ROWS; i++) { printf("%c ", 'A' + i); for (j = 0; j < COLS; j++) { if (board[i][j] == '*' && !reveal_all) { printf(". "); } else { printf("%c ", board[i][j]); } } printf("\n"); } } void place_mines(char board[ROWS][COLS]) { int i, j, count = 0; for (i = 0; i < ROWS; i++) { for (j = 0; j < COLS; j++) { board[i][j] = '.'; } } while (count < MINES) { i = rand() % ROWS; j = rand() % COLS; if (board[i][j] == '.') { board[i][j] = '*'; count++; } } } int count_adjacent_mines(char board[ROWS][COLS], int row, int col) { int i, j, count = 0; for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS && board[i][j] == '*') { count++; } } } return count; } void reveal_cell(char board[ROWS][COLS], int row, int col, int *remaining_cells) { int i, j; if (board[row][col] == '.') { (*remaining_cells)--; board[row][col] = '0' + count_adjacent_mines(board, row, col); if (board[row][col] == '0') { for (i = row - 1; i <= row + 1; i++) { for (j = col - 1; j <= col + 1; j++) { if (i >= 0 && i < ROWS && j >= 0 && j < COLS) { reveal_cell(board, i, j, remaining_cells); } } } } } } void play_game(char board[ROWS][COLS]) { int row, col, remaining_cells = ROWS * COLS - MINES; while (remaining_cells > 0) { printf("Enter row and column: "); scanf("%d %d", &row, &col); row--; col--; if (row >= 0 && row < ROWS && col >= 0 && col < COLS) { if (board[row][col] == '*') { printf("Game over!\n"); print_board(board, 1); return; } else { reveal_cell(board, row, col, &remaining_cells); print_board(board, 0); } } } printf("You won!\n"); } ``` 这个程序使用了一个二维字符数组来表示扫雷的棋盘,其中每个字符表示该位置的状态,'.'表示未揭开,'*'表示地雷,'0'-'8'表示相邻地雷的数量。在程序开始时,使用`place_mines`函数随机放置地雷。然后使用`print_board`函数打印当前的棋盘。在玩家选择一个位置后,使用`reveal_cell`函数揭开该位置,如果该位置是地雷则游戏结束,否则更新棋盘并继续游戏,直到所有非地雷位置都被揭开。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兵马俑学编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值