《扫雷》是一款大众类的益智小游戏,于1992年发行。游戏目标是在最短的时间内根据点击格子出现的数字找出所有非雷格子,同时避免踩雷,踩到一个雷即全盘皆输。
扫雷最原始的版本可以追溯到1973年一款名为“方块”的游戏。
不久,“方块”被改写成了游戏“Rlogic”。在“Rlogic”里,玩家的任务是作为美国海军陆战队队员,为指挥中心探出一条没有地雷的安全路线,如果路全被地雷堵死就算输。两年后,汤姆·安德森在“Rlogic”的基础上又编写出了游戏“地雷”,由此奠定了现代扫雷游戏的雏形。
1981年,微软公司的罗伯特·杜尔和卡特·约翰逊两位工程师在Windows3.1系统上加载了该游戏,扫雷游戏才正式在全世界推广开来。
这款游戏的玩法是在一个99(初级),1616(中级),16*30(高级),或自定义大小的方块矩阵中随机布置一定量的地雷(初级为10个,中级为40个,高级为99个)。由玩家逐个翻开方块,以找出所有地雷为最终游戏目标。如果玩家翻开的方块有地雷,则游戏结束。
void menu()
{
printf("******************************\n");
printf("****** 1. play *******\n");
printf("****** 0. exit *******\n");
printf("******************************\n");
}
char mine[ROWS][COLS] = {
0 };//存放布置好的雷的信息
char show[ROWS][COLS] = {
0 };//存放排查出的雷的信息
void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
int i = 0;
int j = 0;
for (i = 0;i < rows;i++)
{
for (j = 0;j < cols;j++)
{
board[i][j] = sz;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------扫雷游戏-----------\n");
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");
}
printf("---------扫雷游戏-----------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count= EASY_COUNT;
while (count)
{
x = rand() % row + 1;
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';
}
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<COL * ROW - 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 = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
if (win == COL * ROW - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
int main()
{
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
//game();
printf("游戏开始\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
一、game.c
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
int i = 0;
int j = 0;
for (i = 0;i < rows;i++)
{
for (j = 0;j < cols;j++)
{
board[i][j] = sz;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------扫雷游戏-----------\n");
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");
}
printf("---------扫雷游戏-----------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count= EASY_COUNT;
while (count)
{
x = rand() % row + 1;
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';
}
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<COL * ROW - 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 = get_mine_count(mine, x, y);
show[x][y] = count + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
if (win == COL * ROW - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
二、game.h
#include
#include
#include
#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 sz);
//打印棋盘
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);
三、test.c
#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(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game(); //扫雷游戏
//printf("游戏开始\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
1.如果坐标不是雷且周围也没有雷可以展开一片
2.如果确定那个地方是雷可以做标记
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char sz)
{
int i = 0;
int j = 0;
for (i = 0;i < rows;i++)
{
for (j = 0;j < cols;j++)
{
board[i][j] = sz;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("---------扫雷游戏-----------\n");
//打印列号
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");
}
printf("---------扫雷游戏-----------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count= EASY_COUNT;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
static void sign()
{
printf("******************************\n");
printf("****** 1. mark *******\n");
printf("****** 0. exit *******\n");
printf("******************************\n");
}
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 lp(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count = get_mine_count(mine, x, y);
if (count == 0)
{
show[x][y] = ' ';
if (x - 1 >= 0 && x-1 <= ROW && y >= 0 && y <= COL && show[x - 1][y] == '*')
{
lp(mine, show, x - 1, y);
}
if (x + 1 >= 0 && x+1 <= ROW && y >= 0 && y <= COL && show[x + 1][y] == '*')
{
lp(mine, show, x + 1, y);
}
if (x-1 >= 0 && x-1 <= ROW && y - 1 >= 0 && y-1 <= COL && show[x-1][y - 1] == '*')
{
lp(mine, show, x - 1, y-1);
}
if (x-1 >= 0 && x-1 <= ROW && y + 1 >= 0 && y+1 <= COL && show[x-1][y + 1] == '*')
{
lp(mine, show, x - 1, y+1);
}
if (x+1 >= 0 && x+1 <= ROW && y + 1 >= 0 && y+1 <= COL && show[x+1][y + 1] == '*')
{
lp(mine, show, x + 1, y+1);
}
if (x+1 >= 0 && x+1 <= ROW && y -1 >= 0 && y-1 <= COL && show[x+1][y -1] == '*')
{
lp(mine, show, x + 1, y-1);
}
if (x >= 0 && x <= ROW && y - 1 >= 0 && y - 1 <= COL && show[x ][y - 1] == '*')
{
lp(mine, show, x , y-1);
}
if (x >= 0 && x <= ROW && y + 1 >= 0 && y + 1 <= COL && show[x ][y + 1] == '*')
{
lp(mine, show, x , y+1);
}
}
else
{
show[x][y] = count + '0';
}
}
static int win(char show[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
int gs = 0;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] != '*'&& show[i][j] != '@')
{
gs++;
}
}
}
return gs;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int input = 0;
int x = 0;
int y = 0;
int w = 0;
while (1)
{
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
{
lp(mine, show, x, y);
int Is_win(char show[ROWS][COLS], int row, int col);
int w=win(show, row, col);
if (w == COL * ROW - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
break;
}
DisplayBoard(show, row, col);
do
{
sign();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
printf("请输入要标记的坐标:>");
scanf("%d %d", &x, &y);
if (show[x][y] == '*')
{
show[x][y] = '@';
DisplayBoard(show, row, col);
}
else
{
printf("输入错误,输入选择\n");
}
break;
case 0:
printf("退出标记\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
}
}
else
{
printf("坐标不合法,请重新输入\n");
}
}
}
game.h
#pragma once
#include
#include
#include
#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 sz);
//打印棋盘
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);
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(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排查雷
FindMine(mine, show, ROW, COL);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game(); //扫雷游戏
//printf("游戏开始\n");
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}