当前位置:首页 > 通信资讯 > 正文

c语言程序设计扫雷(C语言实现扫雷)

前言

《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。

多文件形式

在实现游戏的首先,需要创建test.c game.c game.h三个文件。
test.c主要进行游戏的测试。
game.c主要进行游戏内部一些函数的具体实现。
game.h主要是一些声明,宏定义。

游戏逻辑

1、打印简易菜单
2、定义及初始化数组
3、随机生成布置雷
4、玩家排雷

游戏实现

c语言程序设计扫雷(C语言实现扫雷)

打印简易菜单

打印菜单让玩家选择,输入1为开始游戏,输入0位退出游戏

?
1 2 3 4 5 6 7 8 9 void meun() { printf("*********************************\n"); printf("*********************************\n"); printf("*********** 1. play ***********\n"); printf("*********** 0. exit ***********\n"); printf("*********************************\n"); printf("*********************************\n"); }

初始化数组

程序生成两个99的方块矩阵,mine数组用来保存雷的布局,show数组用来展示玩家实时的排雷进度。
mine数组中,‘1'代表雷,'0‘代表无雷。
show数组中,'‘代表雷,'#'代表无雷。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 //初始化数组 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; for (i = 0; i < rows; i++) { for (j = 0; j < cols; j++) { board[i][j] = set; } } }

打印方块矩阵

玩家每确定一次排雷坐标,就要在屏幕上显示排雷情况。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; int j = 0; for (i = 0; i < 10; 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"); } }

布置雷

利用C语言库中的rand函数生成随机数来布置雷。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }

玩家排雷

玩家输入排雷的坐标,程序会在屏幕显示当前坐标周围8个方块存在的雷的数量。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 int MineCount(char mine[ROWS][COLS], int x, int y) { return (mine[x + 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x - 1][y] + mine[x - 1][x - 1]) - 8 * '0'; } void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int win = 0; int x = 0; int y = 0; while (win < row * col - EASY_COUNT) { printf("请输入要排查的坐标:>"); scanf("%d %d", &x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { if (mine[x][y] == '0') { int count = MineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } else { printf("很遗憾,你被炸死了\n"); break; } } else { printf("下标错误,请重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }

完整代码

test.c

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void meun() { printf("*********************************\n"); printf("*********************************\n"); printf("*********** 1. play ***********\n"); printf("*********** 0. exit ***********\n"); printf("*********************************\n"); printf("*********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 }; char show[ROWS][COLS] = { 0 }; //初始化数组 InitBoard(mine, ROWS, COLS, '0'); InitBoard(show, ROWS, COLS, '*'); //布置雷 SetMine(mine, ROW, COL); DisplayBoard(mine, ROW, COL); DisplayBoard(show, ROW, COL); //排雷 FindBoard(mine, show, ROW, COL); DisplayBoard(show, ROW, COL); } int main() { srand((unsigned int)time(NULL)); int input = 0; do { meun(); printf("请选择:>"); scanf("%d", &input); switch (input) { case 1: game(); break; case 0: printf("退出游戏\n"); break; default: printf("选择错误,请重新输入\n"); break; } } while (input); return 0; }

game.c

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #define _CRT_SECURE_NO_WARNINGS 1 #include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; int j = 0; 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 = 0; int j = 0; for (i = 0; i < 10; 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; int x = 0; int y = 0; while (count) { x = rand() % row + 1; y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } } int MineCount(char mine[ROWS][COLS], int x, int y) { return (mine[x + 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x][y - 1] + mine[x][y + 1] + mine[x - 1][y + 1] + mine[x - 1][y] + mine[x - 1][x - 1]) - 8 * '0'; } void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) { int win = 0; int x = 0; int y = 0; while (win < row * col - EASY_COUNT) { printf("请输入要排查的坐标:>"); scanf("%d %d", &x, &y); if (x <= row && x >= 1 && y <= col && y >= 1) { if (mine[x][y] == '0') { int count = MineCount(mine, x, y); show[x][y] = count + '0'; DisplayBoard(show, ROW, COL); win++; } else { printf("很遗憾,你被炸死了\n"); break; } } else { printf("下标错误,请重新输入\n"); } } if (win == row * col - EASY_COUNT) { printf("恭喜你,排雷成功\n"); DisplayBoard(mine, ROW, COL); } }

game.h

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 #pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define EASY_COUNT 10 #define ROWS ROW+2 #define COLS COL+2 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set); void DisplayBoard(char board[ROWS][COLS], int row, int col); void SetMine(char board[ROWS][COLS], int row, int col); void FindBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/weixin_53027918/article/details/119258667

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。