本文实例为大家分享了C语言实现扫雷游戏的详细代码,供大家参考,具体内容如下
一、思想实现
本文的扫雷游戏主要实现以下的功能:
1.显示雷和其信息
2.输入一个坐标可观测其周围九格内的地雷数
3.点击到地雷则游戏结束
用到的IDE为VS2013

二、源代码
创建一个头文件saolei.h
存放关于所有自定义函数的声明
| 1 2 3 4 5 6 7 |
#define COL 9
char** creatMat(int row, int col);
void intMat(char** Mat, int row, int col, char ch);
void setMine(char** Mat, int row, int col);
void showMat(char** Mat, int row, int col);
int getMineNum(char** Mat, int row, int col, int x, int y);
void game(char** mineMat, char** mineInfo, int row, int col);
|
创建一个源文件SaoLei.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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "saolei.h"
#include <Windows.h>
char** creatMat(int row,int col)
{
//创建一个指针数组
char** Mat = (char**)malloc(sizeof(char *)*(row+2));//多创建2行2列方便计算边界角落的数值
//开辟每一行元素的空间
for (int i = 0; i < row+2; ++i)
{
Mat[i] = (char*)malloc(sizeof(char)*(col+2));
}
return Mat;
}
void intMat(char** Mat, int row, int col, char ch)
{
for (int i = 0; i < row+2; ++i)
{
for (int j = 0; j < col+2; ++j)
{
Mat[i][j] = ch;
}
}
}
void setMine(char** Mat, int row, int col)//雷的位置
{
int count = MINE_NUM;//布置雷的个数
while (count)
{
//x:1`row,y:1~col
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Mat[x][y] != '*')
{
Mat[x][y] = '*';
--count;
}
}
}
void showMat(char** Mat, int row, int col)
{
printf("------------------------------\n");
//显示列
for (int i = 0; i <= col; ++i)
{
printf("%d ", i);
}
printf("\n");
//显示行
for (int i = 1; i <= row; ++i)
{
printf("%d ", i);
for (int j = 1; j <= col; ++j)
{
printf("%c ", Mat[i][j]);
}
printf("\n");
}
printf("------------------------------\n");
}
int getMineNum(char** Mat, int row, int col, int x, int y)
{//旋转矩阵
static int posOffset[8][2] = { { -1, -1 }, { -1, 0 }, { -1, 1 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 0 }, { 1, 1 } };
int count = 0;
for (int i = 0; i < 8; ++i)
{
int nx = x + posOffset[i][0];
int ny = y + posOffset[i][1];
if (Mat[nx][ny] == '*')
{
++count;
}
}
return 0;
}
void game(char** mineMat, char** mineInfo, int row, int col)
{
int step = 0;
while (step < row * col - MINE_NUM)
{
int x, y;
printf("请输入一个坐标:\n");
scanf("%d%d", &x, &y);
if (mineMat[x][y] == '*')
{
printf("game over!\n");
showMat(mineMat, row, col);
break;
}
//获取x,y周围雷的个数
int num = getMineNum(mineMat, row, col, x, y);
mineInfo[x][y] = num + '0';
showMat(mineInfo, row, col);
++step;//成功走了一步
}
if (step == row * col - MINE_NUM)
{
printf("win!\n");
}
}
void test()
{
char** mineMat = creatMat(ROW, COL);
char** mineInfo = creatMat(ROW, COL);
intMat(mineMat, ROW, COL, '0');
intMat(mineInfo, ROW, COL, '*');
setMine(mineMat, ROW, COL);
printf("雷:");
showMat(mineMat, ROW, COL);
printf("雷的信息:");
showMat(mineInfo, ROW, COL);
game(mineMat, mineInfo, ROW, COL);
}
int main()
{
test();
system("pause");
return 0;
}
|
三、游戏结果


END
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/CYQAQ/article/details/109659133








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