目录
Ⅰ 前言
Ⅱ 模块化编程
Ⅲ 游戏思路与逻辑
Ⅳ 实现游戏步骤/过程
① 创建颜色函数
0=黑色 8=灰色
1=蓝色 9=淡蓝色 十六进制
2=绿色 10=淡绿色 A
3=湖蓝色 11=淡浅绿色 B
4=红色 12=淡红色 C
5=紫色 13=淡紫色 D
6=黄色 14=淡黄色 E
7=白色 15=亮白色 F
void color(short x)
{
if (x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
② 菜单界面(menu)
void menu()
{
color(0); //Black 黑色
system("cls"); //清屏.
color(10);
printf("|-----------|扫雷游戏|-----------|\n");
printf("|********************************|\n");
printf("|★★★★★★★★★★★★★★★★|\n");
printf("|★★ 1.开始 0.退出 ★★|\n");
printf("|★★★★★★★★★★★★★★★★|\n");
printf("|0 = 不是雷 ---------- 1 = 它是雷|\n");
printf("|--------------------------------|\n");
}
③ 实现多行多列扫雷
#define ROW 9
#define COL 9
④ 实现多个雷
#define Thunder 10
⑤ 棋盘初始化
void Initialization(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 Print_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
color(7);
printf("════════════════════\n");
for (i = 0; i <= row; i++)
{
if (i == 0)
{
printf("%d|", i);
}
else
{
printf("%d|", i);
}
}
printf("\n/|══════════════════");
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d|", i);
for (j = 1; j <= col; j++)
{
printf("%c|", board[i][j]);
}
printf("\n");
}
color(6);
printf("\n-----扫雷游戏------\n");
}
⑦ 布置雷的信息
void Lay_thunder(char Findout[ROWS][COLS], int row, int col)
{
//布置雷
int count = Thunder;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Findout[x][y] = '0')
{
Findout[x][y] = '1';
count--;
}
}
}
⑧ 玩家输入雷实现步骤
void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col)
{
//1.输入排查雷的坐标
//2.检查坐标处是不是雷,布置雷存放的是字符'1',没有放置雷存放的是字符'0'。
int x, y;
int win = 0;
while (win<row*col - Thunder)
{
static int j = 1;//延长生明周期,
while (j)
{
color(8);
printf("--------------------------\n");
printf("[输入第一个坐标记得空一格!]\n");
printf("--------------------------\n");
j--;
break;
}
color(11);
printf("---------------\n");
printf("请输入坐标>:");
//x与y坐标范围 1~9
scanf("%d %d", &x, &y);
printf("---------------\n");
//判断坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Layouts[x][y] == '1')
{
printf("|══════════════════|\n");
printf("|很遗憾,你被炸死了!|\n");
printf("|══════════════════|\n");
Print_board(Layouts, ROW, COL);
Sleep(5000);
break;
}
if (Findout[x][y] == '0')
{
color(6);
printf("|═══════════════════════════════════|\n");
printf("|宁已经在这里输入过坐标了,请重新输入!|\n");
printf("|═══════════════════════════════════|\n");
}
if (Findout[x][y] == '1')
{
color(6);
printf("|════════════════════════════════════|\n");
printf("|宁已经在这里输入过坐标了,请重新输入!|\n");
printf("|════════════════════════════════════|\n");
}
else
{
//不是雷情况下,统计x,y周围坐标有几个雷
int Count = Statistics(Layouts, x, y);
Findout[x][y] = Count + '0';
Print_board(Findout, row, col);
win++;
}
}
else
{
printf("|═════════════════════════════|\n");
printf("|宁输入的坐标范围错误!重新输入|\n");
printf("|═════════════════════════════|\n");
}
}
if (win == row*col - Thunder)
{
printf("|═══════════════════════|\n");
printf("|恭喜你,排雷成功!太优秀了!|\n");
printf("|═══════════════════════|\n");
Print_board(Findout, ROW, COL);
}
}
⑨ 排查 x,y 周围有多少雷
static int Statistics(char Layouts[ROWS][COLS], int x, int y)
{
return Layouts[x-1][y-1]+
Layouts[x][y-1] +
Layouts[x+1][y-1]+
Layouts[x-1][y]+
Layouts[x+1][y]+
Layouts[x-1][y+1]+
Layouts[x][y+1]+
Layouts[x+1][y+1] - 8*'0';
}
Ⅴ 结果演示
Ⅵ 模块化代码实现
(一)、test.c
//扫雷游戏的测试
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include "game.h"
void menu()
{
color(0); //Black 黑色
system("cls"); //清屏.
color(10);
printf("|-----------|扫雷游戏|-----------|\n");
printf("|********************************|\n");
printf("|★★★★★★★★★★★★★★★★|\n");
printf("|★★ 1.开始 0.退出 ★★|\n");
printf("|★★★★★★★★★★★★★★★★|\n");
printf("|0 = 不是雷 ---------- 1 = 它是雷|\n");
printf("|--------------------------------|\n");
}
void game()
{
printf(" ---------\n");
printf("|PLAY GAME|\n");
printf(" ---------\n");
char Layouts[ROWS][COLS] = { 0 };//存放布置好雷的信息
char Findout[ROWS][COLS] = { 0 };//存放排查出雷的信息
//初始化棋盘
Initialization(Layouts, ROWS, COLS, '0');//mine
Initialization(Findout, ROWS, COLS, 'x');//show
//打印棋盘
/*Print_board(Layouts, ROW, COL);*/
Print_board(Findout, ROW, COL);
//布置雷
Lay_thunder(Layouts, ROW, COL);
/*Print_board(Findout, ROW, COL);*/
//排查雷
Check(Layouts,Findout,ROW,COL);
}
void test()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
color(5);
printf("\n");
printf("|═════════════════════════════════|\n");
printf("|Please enter the interface number|:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("|════════|\n");
printf("|退出游戏|\n");
printf("|════════|\n");
break;
default:
printf("\n");
printf("|═════════════════════════════════|\n");
printf("|由于你输入错误罚你5s不能玩(→_→)|\n");
printf("|═════════════════════════════════|\n");
Sleep(5000);
}
} while (input);
}
int main(void)
{
test();
return 0;
}
(二)、game.h
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//颜色函数
void color(short x);
//初始化函数,初始化11*11,因为 行 & 列 都需要加1
void Initialization(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘,最终打印 9*9 棋盘即可
void Print_board(char board[ROWS][COLS], int row, int col);
//布置雷
void Lay_thunder(char Findout[ROWS][COLS], int row, int col);
//排查雷
void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col);
(三)、game.c
#define _CRT_SECURE_NO_WARNINGS 1
//游戏的函数的实现
#include "game.h"
#include<stdio.h>
void color(short x)
{
if (x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
void Initialization(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 Print_board(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
color(7);
printf("════════════════════\n");
for (i = 0; i <= row; i++)
{
if (i == 0)
{
printf("%d|", i);
}
else
{
printf("%d|", i);
}
}
printf("\n/|══════════════════");
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d|", i);
for (j = 1; j <= col; j++)
{
printf("%c|", board[i][j]);
}
printf("\n");
}
color(6);
printf("\n-----扫雷游戏------\n");
}
void Lay_thunder(char Findout[ROWS][COLS], int row, int col)
{
//布置雷
int count = Thunder;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (Findout[x][y] = '0')
{
Findout[x][y] = '1';
count--;
}
}
}
//静态局部变量去修饰函数的时候,让这个函数只能在自己所在的源文件内看到,其它的内部当中是看不到的。
static int Statistics(char Layouts[ROWS][COLS], int x, int y)
{
return Layouts[x-1][y-1]+
Layouts[x][y-1] +
Layouts[x+1][y-1]+
Layouts[x-1][y]+
Layouts[x+1][y]+
Layouts[x-1][y+1]+
Layouts[x][y+1]+
Layouts[x+1][y+1] - 8*'0';
}
void Check(char Layouts[ROWS][COLS], char Findout[ROWS][COLS], int row, int col)
{
//1.输入排查雷的坐标
//2.检查坐标处是不是雷,布置雷存放的是字符'1',没有放置雷存放的是字符'0'。
int x, y;
int win = 0;
while (win<row*col - Thunder)
{
static int j = 1;//延长生明周期,
while (j)
{
color(8);
printf("--------------------------\n");
printf("[输入第一个坐标记得空一格!]\n");
printf("--------------------------\n");
j--;
break;
}
color(11);
printf("---------------\n");
printf("请输入坐标>:");
//x与y坐标范围 1~9
scanf("%d %d", &x, &y);
printf("---------------\n");
//判断坐标的合法性
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (Layouts[x][y] == '1')
{
printf("|══════════════════|\n");
printf("|很遗憾,你被炸死了!|\n");
printf("|══════════════════|\n");
Print_board(Layouts, ROW, COL);
Sleep(5000);
break;
}
if (Findout[x][y] == '0')
{
color(6);
printf("|═══════════════════════════════════|\n");
printf("|宁已经在这里输入过坐标了,请重新输入!|\n");
printf("|═══════════════════════════════════|\n");
}
if (Findout[x][y] == '1')
{
color(6);
printf("|════════════════════════════════════|\n");
printf("|宁已经在这里输入过坐标了,请重新输入!|\n");
printf("|════════════════════════════════════|\n");
}
else
{
//不是雷情况下,统计x,y周围坐标有几个雷
int Count = Statistics(Layouts, x, y);
Findout[x][y] = Count + '0';
Print_board(Findout, row, col);
win++;
}
}
else
{
printf("|═════════════════════════════|\n");
printf("|宁输入的坐标范围错误!重新输入|\n");
printf("|═════════════════════════════|\n");
}
}
if (win == row*col - Thunder)
{
printf("|═══════════════════════|\n");
printf("|恭喜你,排雷成功!太优秀了!|\n");
printf("|═══════════════════════|\n");
Print_board(Findout, ROW, COL);
}
}
