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

c语言扫雷程序设计(用c语言编写扫雷程序)

前言

前面写了三子棋的小游戏,感觉不过瘾,今天再来一个扫雷的小游戏。欢迎评论探讨

思路分析

游戏简介

说到扫雷很容易想起很多个方形格子,表面什么也没有,点击其中一个小格子,如果时炸弹,游戏直接,如果没有炸弹,则显示周围八个格子中炸弹个数,看到这里,我们需要棋盘,即二维数组,看来还需要两个,一个放炸弹坐标,一个用来反馈周围炸弹数

棋盘设置

有了前面三子棋的基础我们首先要初始化棋盘并且可以打印棋盘,这个好像没有什么难度。

c语言扫雷程序设计(用c语言编写扫雷程序)

我们用*代表未知,这是一个9的方阵,但是好像用了11X11的数组,这是避免计算点击边界的时候出现数组越界的情况。

炸弹设置

我们需要一个函数来设置炸弹,并且保证它是随机,且不能占用已经设置的炸弹位置,我们将炸弹设置为1,非炸弹设置为0,方便计算周围砸蛋数。

?
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; while (count) { //1. 生成随机下标 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { board[x][y] = '1'; count--; } } }

炸弹反馈

?
1 2 3 4 5 6 7 8 9 10 11 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'); }

玩家操作

初始化棋盘->生成炸弹->玩家点击->判断炸弹或者周围炸弹数->玩家点击->判断炸弹或者周围炸弹数
同时还要记录操作次数,如果操作数等于格子总数减去炸弹数就赢了,点击炸弹直接over。

运行游戏

c语言扫雷程序设计(用c语言编写扫雷程序)

首先排查1,1,但是没有炸弹,接着随机排查几个坐标

c语言扫雷程序设计(用c语言编写扫雷程序)

到此,我们可以推断4,2是一个炸弹

c语言扫雷程序设计(用c语言编写扫雷程序)

被炸死后,显示所有炸弹的未知。
选择0,1是为了方便反馈周围炸弹数量。

源代码

本次程序采用多文件形式

game.h

?
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 #pragma once #include <stdio.h> #include <stdlib.h> #include <time.h> #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 //初始化棋盘 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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

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 103 104 105 #include "game.h" void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) { int i = 0; for (i = 0; i < rows; i++) { int j = 0; for (j = 0; j < cols; j++) { board[i][j] = set; } } } void DisplayBoard(char board[ROWS][COLS], int row, int col) { int i = 0; printf("------------------------\n"); for (i = 0; i <= 9; i++) { printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) { int j = 0; printf("%d ", i); for (j = 1; j <= col; j++) { printf("%c ", board[i][j]); } printf("\n"); } printf("------------------------\n"); } void SetMine(char board[ROWS][COLS], int row, int col) { int count = EASY_COUNT; while (count) { //1. 生成随机下标 int x = rand() % row + 1; int y = rand() % col + 1; if (board[x][y] != '1') { 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 = 0; int y = 0; int 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); } }

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 #define _CRT_SECURE_NO_WARNINGS #include "game.h" void menu() { printf("********************************\n"); printf("********* 1. play ********\n"); printf("********* 0. exit ********\n"); printf("********************************\n"); } void game() { char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息 //初始化一下棋盘 InitBoard(mine, ROWS, COLS, '0');//'0' InitBoard(show, ROWS, COLS, '*');//'*' //布置雷 SetMine(mine, ROW, COL); DisplayBoard(show, 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; }

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

原文链接:https://blog.csdn.net/wang_fm/article/details/119177946

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

为您推荐:

发表评论

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