Windows经典小游戏--扫雷精装版
Windows经典小游戏–扫雷精装版
最开始玩的电脑游戏就是蜘蛛纸牌和扫雷了,那时候觉得很神奇的事情现在自己也可以做了。下面就展示一下C语言扫雷的代码。
首先创建一个“扫雷”的项目,这个项目中包含三个文件:一个game.h头文件,两个源文件test.c、game.c。
- “game.h”:game.h文件中包含了扫雷用的所有头文件以及声明的函数。如下:
#ifndef __GAME_H__
#define __GAME_H__
#define ROW 9//展示雷盘的行
#define COL 9//展示雷盘的列
#define ROWS ROW+2//雷盘的行
#define COLS COL+2//雷盘的列
#define MINE_NUM 10//扫雷游戏对应的游戏难度
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char c);//初始化雷盘
void ShowBoard(char show[ROWS][COLS], int row, int col);//展示雷盘
void SetMine(char mine[ROWS][COLS], int rows, int cols, int count);//埋雷
void Computer(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y);//计算雷数
void OpenMine(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y);//展开雷阵
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], char test[ROWS][COLS], int x, int y);//排雷
#pragma once
#endif //__GAME_H__
- “test.c”:test.c文件中包含打印菜单函数、游戏规则函数和玩游戏的主函数。如下:
#include"game.h"
//打印菜单
void menu()
{
printf("***********************************\n");
printf("******* 1、play *******\n");
printf("******* 0、exit *******\n");
printf("***********************************\n");
}
//制定游戏规则
void game()
{
int x = 0;
int y = 0;
int count = MINE_NUM;
char mine[ROWS][COLS] = { 0 };//存放雷盘
char show[ROWS][COLS] = { 0 };//排雷盘
char test[ROWS][COLS] = { 0 };//测试是否展开的雷盘
InitBoard(test, ROWS, COLS,'0');//初始化测试雷盘
InitBoard(mine,ROWS,COLS,'0');//初始化雷盘
InitBoard(show, ROWS, COLS, '*');//初始化展示雷盘
SetMine(mine,ROWS,COLS,count);//埋雷
FindMine(mine,show,test,x,y);//排雷
}
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;
}
- “game.c”:game.c文件中包含了game函数所实现功能的所有函数的定义。如下:
初始化雷盘:InitBoard(char board[ROWS][COLS], int rows, int cols, char c);
展示雷盘:ShowBoard(char show[ROWS][COLS], int row , int col);
埋雷函数:SetMine(char mine[ROWS][COLS], int rows, int cols, int count);
计算雷数:Computer(char show[ROWS][COLS],char mine[ROWS][COLS], int x, int y);
展开雷阵:OpenMine(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y);
排雷函数:FindMine(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y);
#include"game.h"
//初始化雷盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char c)
{
memset(&board[0][0], c, rows*cols * sizeof(board[0][0]));
}
//展示雷盘
void ShowBoard(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i <= row; i++)
{
printf("%d ", i);//列标
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//行标
for (j = 1; j <= col; j++)
{
printf("%c ", show[i][j]);//打印每个格子
}
printf("\n");
}
printf("\n\n");
}
//埋雷
void SetMine(char mine[ROWS][COLS], int rows, int cols,int count)
{
int i = 0;
int j = 0;
while (count != 0)
{
i = rand() % 9 + 1;
j = rand() % 9 + 1;
if (mine[i][j] == '0')
{
mine[i][j] = '1';
count--;
}
}
}
//计算雷数
void Computer(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y)
{
show[x][y] = 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]
- 7 * '0';
}
//展开雷阵
void OpenMine(char show[ROWS][COLS], char mine[ROWS][COLS], char test[ROWS][COLS], int x, int y)
{
test[x][y] = '1';
if (x > 0 && x < ROWS - 1 && y>0 && y < COLS - 1)
{
Computer(show, mine, x, y);
if (show[x][y] == '0')
{
Computer(show, mine, x-1, y);
Computer(show, mine, x-1, y-1);
Computer(show, mine, x-1, y+1);
Computer(show, mine, x, y-1);
Computer(show, mine, x, y+1);
Computer(show, mine, x+1, y);
Computer(show, mine, x+1, y-1);
Computer(show, mine, x+1, y+1);
if (show[x - 1][y] == '0')
{
if (test[x - 1][y] == '0')
{
OpenMine(show, mine, test, x-1, y);
}
}
if (show[x - 1][y - 1] == '0')
{
if (test[x - 1][y - 1] == '0')
{
OpenMine(show, mine, test, x - 1, y - 1);
}
}
if (show[x - 1][y + 1] == '0')
{
if (test[x - 1][y + 1] == '0')
{
OpenMine(show, mine, test, x - 1, y + 1);
}
}
if (show[x][y - 1] == '0')
{
if (test[x][y - 1] == '0')
{
OpenMine(show, mine, test, x , y - 1);
}
}
if (show[x][y + 1] == '0')
{
if (test[x][y + 1] == '0')
{
OpenMine(show, mine, test, x, y + 1);
}
}
if (show[x + 1][y] == '0')
{
if (test[x + 1][y] == '0')
{
OpenMine(show, mine, test, x + 1, y);
}
}
if (show[x + 1][y - 1] == '0')
{
if (test[x + 1][y - 1] == '0')
{
OpenMine(show, mine, test, x + 1, y - 1);
}
}
if (show[x + 1][y + 1] == '0')
{
if (test[x + 1][y + 1] == '0')
{
OpenMine(show, mine, test, x + 1, y + 1);
}
}
}
}
}
//排雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], char test[ROWS][COLS], int x, int y)//排雷
{
//ShowBoard(mine, ROW, COL);
int ret = ROW * COL - MINE_NUM;
int count = 0;
while (1)
{
int time0 = 0;
int time1 = 0;
int time2 = 0;
ShowBoard(show, ROW, COL);
time1 = ((unsigned int)time(NULL));
printf("请输入您要排的坐标->\n");
scanf("%d%d",&x,&y);
if (x > 0 && x < ROWS - 1 && y>0 && y < COLS - 1)
{
count++;
if (mine[x][y] == '1')
{
if (count == 1)
{
SetMine(mine, ROWS, COLS, count);
mine[x][y] = '0';
OpenMine(show, mine, test, x, y);
}
else
{
time2 = ((unsigned int)time(NULL));
time0 = time2 - time1;
printf("很遗憾你被炸死了!所用时间为:%ds\n", time0);
ShowBoard(mine, ROW, COL);
break;
}
}
else
{
ret--;
OpenMine(show, mine, test, x, y);
}
}
else
{
printf("输入坐标错误,请重新输入!\n");
}
if (ret == 0)
{
time2 = ((unsigned int)time(NULL));
time0 = time2 - time1;
printf("恭喜你,找到了所有的雷\n");
printf("所用时间为:%ds\n", time0);
ShowBoard(mine, ROW, COL);
break;
}
}
}
测试结果如下: