赞
踩
扫雷游戏可以说是非常经典的小游戏了,今天我们就试着用C语言实现这个小游戏
目录
写代码前要先想想框架怎么能够实现这个游戏,首先它有着9*9的棋盘,棋盘上有随机放置的雷,
点击其中一个坐标判断它是不是雷,当不是雷时判断周围8个坐标并返回周围8个坐标下雷的个数。想到这里我们就不难发现其实就是通过二维数组来实现的,但在随机放雷时判断周围坐标时可能存在越界,所以我们将棋盘改为11*11就实现了如下图效果,知道了数组大小就可以开始完成代码了
为了提高代码的可读性我们依旧分为3个文件分别是1.game.h 2.game.c 3.test.c
我们分别来看看不同文件的内容
1.game.h
- #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 MY_MINE 10
- void Initboard(char board[ROWS][COLS], int rows, int cols, char ret);//初始化
- void Displayboard(char board[ROWS][COLS], int row, int col);//打印
- void Set_mine(char board[ROWS][COLS], int row, int col);//埋雷
- void Find_mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//扫雷
在头文件game.h中有#include包含的库函数,#define定义的常量以及实现游戏的函数声明
2.game.c
- #define _CRT_SECURE_NO_WARNINGS 1
- #include"game.h"
- void Initboard(char board[ROWS][COLS], int rows, int cols, char ret)
- {
- int i = 0;
- int j = 0;
- for (i = 0; i < rows; i++)
- {
- for (j = 0; j < cols; j++)
- {
- board[i][j] = ret;
- }
- }
- }
- void Displayboard(char board[ROWS][COLS], int row, int col)
- {
- int i = 0;
- int j = 0;
- 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 Set_mine(char board[ROWS][COLS], int row, int col)
- {
- int count = MY_MINE;
- while (count)
- {
- int x = rand() % row + 1;
- int y = rand() % col + 1;
- if (board[x][y] == '0')
- {
- board[x][y] = '1';
- count--;
- }
- }
- }
- static int get_mine_count(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';
- }
- static void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
- {
- if (0 == get_mine_count(mine, x, y))
- {
- show[x][y] = ' ';
- int i = 0;
- int j = 0;
- for (i = x - 1; i <= x + 1; i++)
- {
- for (j = y - 1; j <= y + 1; j++)
- {
- if (i > 0 && i <= ROW && j > 0 && j <= COL && mine[i][j] != '1'&&show[i][j] == '*')
- {
- blank(mine, show, i, j);
- }
- }
- }
- }
- else
- {
- show[x][y] = (get_mine_count(mine, x, y)) + '0';
- }
- }
- void Find_mine(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-MY_MINE)
- {
- printf("请输入要查找的坐标:");
- scanf("%d %d", &x, &y);
- if (x > 0 && x <= row && y > 0 && y <= col)
- {
- if (mine[x][y] == '1')
- {
- printf("恭喜你被炸死了!\n");
- Displayboard(mine, row, col);
- break;
- }
- else
- {//坐标不是雷
- blank(mine, show, x, y);
- Displayboard(show, row, col);
- win++;
- }
- }
- else
- {
- printf("坐标非法,请重新输入:");
- }
- }
- if (win == row * col - MY_MINE)
- {
- printf("游戏胜利!\n");
- Displayboard(mine, row, col);
- }
- }

在game.c中就是游戏实现的具体函数了,像数组初始化,打印棋盘,埋雷,扫雷等等函数的实现
3.test.c
- #define _CRT_SECURE_NO_WARNINGS 1
- #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');//初始化
- Initboard(show, ROWS, COLS, '*');
- //Displayboard(mine, ROW,COL);//打印
- Displayboard(show, ROW, COL);//打印
- Set_mine(mine, ROW,COL);//埋雷
- Displayboard(mine, ROW, COL);
- Find_mine(mine,show,ROW,COL);//扫雷
- }
- void test()
- {
- 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);
- }
- int main()
- {
- test();
- return 0;
- }

在test.c中就是游戏的框架了,做到连接各个函数的功能
这么多的代码写出来可能还是不理解是怎么实现的接着我就简单梳理一下逻辑方便理解
为了开始就能玩游戏并且能连着玩,选择了do while();循环,menu()为菜单打印函数,通过玩家选择1/0判断游戏是否进行,玩家选择了1时进入了game():函数,首先我们要将两个二维数组初始化(mine数组为游戏埋雷标记,show数组为玩家展示),初始化完成接着就可以打印出来看看了,接着我们完成埋雷,埋雷就是随机数对应坐标还是比较好理解的,埋雷完成也可以打印出来,方便及时纠错,最后就是扫雷了,扫雷通过输入坐标进行判断是否为雷,简单的逻辑就是这样的。
游戏的基本实现很简单可是就是缺点那味,就是不能展开一片什么意思呢我们上图看
这是怎么实现的呢其实就是用到了递归的想法,想写出来就得知道递归的条件
1.坐标不是雷
2.该坐标周围没有雷
3.该坐标没被排查过
4.不能越界
知道这些就可以完成对于扫雷代码的优化了
- static void blank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
- {
- if (0 == get_mine_count(mine, x, y))
- {
- show[x][y] = ' ';
- int i = 0;
- int j = 0;
- for (i = x - 1; i <= x + 1; i++)
- {
- for (j = y - 1; j <= y + 1; j++)
- {
- if (i > 0 && i <= ROW && j > 0 && j <= COL && mine[i][j] != '1'&&show[i][j] == '*')
- {
- blank(mine, show, i, j);
- }
- }
- }
- }
- else
- {
- show[x][y] = (get_mine_count(mine, x, y)) + '0';
- }
- }

这个就是实现递归的代码了,要注意避免死递归要将坐标置空以及判断递归的条件。希望对你有所帮助,当然也可以复制代码自己跑一跑加深理解,上面的代码就已经是优化后的代码了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。