扫雷
目录
菜单界面代码
打印棋盘代码
埋雷代码
扫雷代码
总代码
相信大家或多或少都玩过扫雷这个小游戏叭!
思路:先制作一个菜单让玩家选择是玩游戏还是退出游戏,菜单做好了,接着我们开始制作扫雷的棋盘并初始化,初始化弄完了我们下一步开始埋雷,雷埋好了就开始扫雷。
菜单界面代码
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("**************************\n");
printf("*** 1. play 0. exit ***\n");
printf("*** 2. clear ***\n");
printf("**************************\n");
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1:
game();//游戏实现
break;
case 2://清屏选项
system("cls");
break;
case 0:
printf("退出程序!\n");
break;
default:
printf("输入错误,请重新输入!\n");
Sleep(1000);
system("cls");
break;
}
} while (input);
return 0;
}
效果展示图:
?制作好菜单那我们开始实现整个游戏的逻辑框架了,定义两个二维数组,一个用于显示,一个用于存放地雷。如果这两个东西都只用一个二维数组的话后面的实现逻辑会比较麻烦所以我选择使用两个二维数组。
char mine[ROWS][COLS] = { 0 };//用于埋雷
char show[ROWS][COLS] = { 0 };//用于游戏的画面显示
进行数组的初始化:
//初始化
void initzeboard(char mine[ROWS][COLS], int rows, int cols, char val)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
mine[i][j] = val;
}
}
}
打印棋盘代码
//显示棋盘
void Display_board(char mine[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf(" 扫雷游戏\n");
printf("---------------------------------\n");
for (j = 0; j <= col; j++)
{
printf("%d ", j);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", mine[i][j]);
}
printf("\n");
}
}
此时我们来看下执行结果:
?
紧接着我们进行埋雷
埋雷代码
//埋雷
void random_mine(char mine[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = EASILY;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (mine[x][y] == '0')//用于判断是否正确的埋雷,只有我们这没被埋过的雷我们才自减
{
mine[x][y] = '1';
count--;
}
}
}
此时我们再来看下执行效果:
?
现在我们要做的就是写出扫雷的代码:
扫雷代码
//扫雷
void mine_sweeping(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
while (count < row * col - EASILY)
{
system("cls");
Display_board(show, COL, ROW);
printf("请输入坐标:>");
scanf("%d %d", &x, &y);
if ((1 <= x && x <= row) && (1 <= y && y <= col))
{
if (mine[x][y] == '0')
{
int leng = statistics_mine(mine, x, y);
show[x][y] = leng + '0';
count++;
}
else
{
printf("很遗憾你被炸死了\n");
Display_board(mine, ROW, COL);
break;
}
}
else
{
printf("请输入有效数字!\n");
}
}
if (count == row * col - EASILY)
{
printf("恭喜你扫雷成功!\n");
Display_board(mine, ROW, COL);
}
}
让我们来看下此时的执行效果:
总代码
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <graphics.h>
#define COL 16//宽
#define ROW 30//长
#define MINE_NUM 60//雷数
#define SIZE 25//图片大小
#define IMAGE_NUM 14//图片数量
int map[ROW+2][COL+2];//+2是添加了辅助区,用来防止数组越界。
int count = 0;
IMAGE img[IMAGE_NUM];
//生成雷
int gameInit()
{
srand((unsigned)time(NULL));
//初始化格子
for (int row = 0; row < ROW + 2; row++)
{
for (int col = 0; col < COL + 2; col++)
{
map[row][col] = 0;
}
}
//生成雷
for (int n = 0; n < MINE_NUM;)
{
int row = rand() % ROW+1;
int col = rand() % COL+1;
if (map[row][col] == 0)
{
map[row][col] = -1;
n++;
}
}
//遍历数组,找空格
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
if (map[i][j] != -1)
{
for (int m = i - 1; m <= i + 1; m++)
{
for (int n = j - 1; n <= j + 1; n++)
{
if (map[m][n] == -1)
{
map[i][j]++;
}
}
}
}
}
}
//加密
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
map[i][j] += 20;
}
}
}
//打印游戏区
void gameDraw()
{
for (int i = 1; i <= ROW; i++)
{
for (int j = 1; j <= COL; j++)
{
printf("%3d", map[i][j]);
//贴雷
if (map[i][j] == -1)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[11]);
}
//贴数字
else if (map[i][j] >= 0 && map[i][j] <= 8)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[map[i][j]]);
}
//贴空白
else if (map[i][j] >= 19 && map[i][j] <= 28)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[9]);
}
//标记
else if (map[i][j] >= 39 && map[i][j] <= 48)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[10]);
}
//贴问号
else if (map[i][j] >= 59 && map[i][j] <= 68)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[12]);
}
//贴炸雷
else if (map[i][j] == -2)
{
putimage((i - 1) * SIZE, (j - 1) * SIZE, &img[13]);
}
}
printf("\n");
}
}
//加载图片
void image()
{
for (int i = 0; i < IMAGE_NUM; i++)
{
char fileName[20] = "";
sprintf(fileName, "./image/%d.gif", i);
loadimage(&img[i], fileName, SIZE, SIZE);
}
}
//连续展开
void blankOpen(int r,int c)
{
//打开格子
map[r][c] -= 20;
count++;
//点开后遍历九宫格
for (int m=r-1;m<=r+1;m++)
{
for (int n=c-1;n<=c+1;n++)
{
if (m >= 1 && m <= ROW && n >= 1 && n <= COL) //保证是游戏区
{
if (map[m][n] >= 19 && map[m][n] <= 28) //必须为空白格
{
if (map[m][n]!=20)
{
map[m][n] -= 20;
count++;
}
else
{
blankOpen(m,n);
}
}
}
}
}
}
//左键点击已经点开的块,遍历周围的块
int open(int r,int c)
{
int flag = 0;
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (m >= 1 && m <= ROW && n >= 1 && n <= COL) //保证是游戏区
{
if (map[m][n] == 19)
{
flag = 1;
}
}
}
}
if (flag == 0)
{
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (map[m][n] >= 19 && map[m][n] <= 28)
{
map[m][n] -= 20;
blankOpen(r, c);
}
}
}
}
else if (flag == 1)
{
for (int m = r - 1; m <= r + 1; m++)
{
for (int n = c - 1; n <= c + 1; n++)
{
if (map[m][n] > 39 && map[m][n] <= 48)
{
return -1;
}
}
}
}
}
//输的时候显示所有雷
void boom()
{
for (int r = 1; r <= ROW; r++)
{
for (int c = 1; c <= COL; c++)
{
if (map[r][c]==19)
{
map[r][c] -= 21;
}
else if (map[r][c] == -1)
{
map[r][c] -= 1;
}
}
}
}
//显示剩余雷数
int print()
{
char num[MINE_NUM];
int n = 0;
for (int r = 1; r <= ROW; r++)
{
for (int c = 1; c <= COL; c++)
{
if (map[r][c] == 19 || map[r][c] == -1)
{
n++;
}
}
}
outtextxy(770, 200, "剩余的雷:");
sprintf(num, "%02d", n);
outtextxy(790, 230, num);
printf("\n\n\n");
printf("%02d",n);
}
//鼠标点击开玩
int play()
{
MOUSEMSG msg = { 0 };
int r, c;
while (1)
{
msg = GetMouseMsg();
switch (msg.uMsg)
{
//翻开
case WM_LBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
if (map[r][c]==20)
{
blankOpen(r,c);
return map[r][c];
}
else
{
map[r][c] -= 20;
count++;
return map[r][c];
}
}
else if (map[r][c] >= 0 && map[r][c] <= 8)
{
open(r, c);
if (open(r,c)==-1)
{
return -1;
}
else
{
return map[r][c];
}
}
break;
//插旗子,拔旗子
case WM_RBUTTONDOWN:
r = msg.x / SIZE + 1;
c = msg.y / SIZE + 1;
if (map[r][c] >= 19 && map[r][c] <= 28)
{
map[r][c] += 20;
}
else if (map[r][c]>=39 && map[r][c]<=48)
{
map[r][c] += 20;
}
else if (map[r][c]>=59 && map[r][c]<=68)
{
map[r][c] -= 40;
}
return map[r][c];
break;
}
}
}
//游戏主函数
int main()
{
HWND scanmine = initgraph(ROW * SIZE + 100, COL * SIZE);
image();
gameInit();
while (1)
{
gameDraw();
print();
if (play() == -1)
{
boom();
gameDraw();
MessageBox(scanmine, "你输了", "", MB_OK);
break;
}
if (ROW * COL - MINE_NUM == count)
{
MessageBox(scanmine, "你赢了", "", MB_OK);
break;
}
}
closegraph();
return 0;
}
|