小游戏之扫雷
程序员文章站
2022-04-07 17:30:22
...
扫雷游戏实现
1.头文件game.h
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#pragma warning(disable:4996)
#define ROWS 10
#define COLS 10
void game();
void setMines(char board[ROWS + 2][COLS + 2], int rows, int cols, int bomb);
int getMines(char board[ROWS + 2][COLS + 2], int x, int y);
void display(char board[ROWS + 2][COLS + 2], int rows, int cols);
int eSelf(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2], int rows, int cols, int x, int y);
int eRound(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2], int rows, int cols, int x, int y);
#endif
2.源文件game.c
#include"game.h"
1)getRandomNum --> 获得指定区间的随机数
static int getRandomNum(int start, int end)
{
return (rand() % (end - start + 1) + start);
}
2)setMines --> 埋雷
void setMines(char board[ROWS + 2][COLS + 2], int rows, int cols, int bomb)
{
int x = 0;
int y = 0;
srand((unsigned long)time(NULL));
for (int i = 0; i < bomb; i++)
{
x = getRandomNum(1, rows);
y = getRandomNum(1, cols);
if ('0' == board[x][y])
{
board[x][y] = '1';
}
else
{
i -= 1;
}
}
}
3)display --> 显示界面
void display(char board[ROWS + 2][COLS + 2], int rows, int cols)
{
int i = 0;
int j = 0;
printf(" ");
for (i = 0; i < cols; i++)
{
printf("%2d ", i + 1);
}
printf("\n");
printf(" ");
for (i = 0; i < cols; i++)
{
printf("---");
}
printf("\n");
for (i = 1; i <= rows; i++)
{
printf("%2d|", i);
for (j = 1; j <= cols; j++)
{
printf("%2c|", board[i][j]);
}
printf("\n");
}
}
4)getMines --> 计算四周的雷数
int getMines(char board[ROWS + 2][COLS + 2], int x, int y)
{
return (board[x - 1][y - 1] + board[x - 1][y] + board[x - 1][y + 1] \
+ board[x][y - 1] + board[x][y + 1] \
+ board[x + 1][y - 1] + board[x + 1][y] + board[x + 1][y + 1] - 8 * '0');
}
5)eRound --> 向四周扩展
int eRound(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2], int rows, int cols, int x, int y)
{
int number = eSelf(mine, show, rows, cols, x, y);
if ('0' == show[x][y])
{
int a = 0;
begin:
if ((x - 1) >= 1 && (y - 1) >= 1)
{
a = eSelf(mine, show, rows, cols, --x, --y);
number += a;
if ((show[x][y] == '0') && (a != 0))
{
goto begin;
}
}
if ((x - 1) >= 1 && (y + 1) <= cols)
{
a = eSelf(mine, show, rows, cols, --x, ++y);
number += a;
if (show[x][y] == '0' && (a != 0))
{
goto begin;
}
}
if ((x + 1) <= cols && (y - 1) >= 1)
{
a = eSelf(mine, show, rows, cols, ++x, --y);
number += a;
if (show[x][y] == '0' && (a != 0))
{
goto begin;
}
}
if ((x + 1) <= cols && (y + 1) <= cols)
{
a = eSelf(mine, show, rows, cols, ++x, ++y);
number += a;
if (show[x][y] == '0' && (a != 0))
{
goto begin;
}
}
}
return number;
}
6)eself --> 展开自己
int eSelf(char mine[ROWS + 2][COLS + 2], char show[ROWS + 2][COLS + 2], int rows, int cols, int x, int y)
{
int num = 0;
if ((x >= 1 && x <= rows) && (y >= 1 && y <= cols))
{
if (('*' == show[x][y]) && mine[x][y] != 1)
{
int count = getMines(mine, x, y);
show[x][y] = count + '0';
num++;
}
else if (mine[x][y] != 1)
{
int count = getMines(mine, x, y);
show[x][y] = count + '0';
}
else
{
return 0;
}
if ('0' == show[x][y])
{
if (('*' == show[x - 1][y - 1]) && ((x - 1) >= 1 && (x - 1) <= rows) && ((y - 1) >= 1 && (y - 1) <= cols))
{
show[x - 1][y - 1] = '/';
num++;
}
if (('*' == show[x - 1][y]) && ((x - 1) >= 1 && (x - 1) <= rows) && (y >= 1 && y <= cols))
{
show[x - 1][y] = '/';
num++;
}
if (('*' == show[x - 1][y + 1]) && ((x - 1) >= 1 && (x - 1) <= rows) && ((y + 1) >= 1 && (y + 1) <= cols))
{
show[x - 1][y + 1] = '/';
num++;
}
if (('*' == show[x][y - 1]) && (x >= 1 && x <= rows) && ((y - 1) >= 1 && (y - 1) <= cols))
{
show[x][y - 1] = '/';
num++;
}
if (('*' == show[x][y + 1]) && (x >= 1 && x <= rows) && ((y + 1) >= 1 && (y + 1) <= cols))
{
show[x][y + 1] = '/';
num++;
}
if (('*' == show[x + 1][y - 1]) && ((x + 1) >= 1 && (x + 1) <= rows) && ((y - 1) >= 1 && (y - 1) <= cols))
{
show[x + 1][y - 1] = '/';
num++;
}
if (('*' == show[x + 1][y]) && ((x + 1) >= 1 && (x + 1) <= rows) && (y >= 1 && y <= cols))
{
show[x + 1][y] = '/';
num++;
}
if (('*' == show[x + 1][y + 1]) && ((x + 1) >= 1 && (x + 1) <= rows) && ((y + 1) >= 1 && (y + 1) <= cols))
{
show[x + 1][y + 1] = '/';
num++;
}
}
}
return num;
}
7)game --> 扫雷函数
void game()
{
char choose;//选择难度
int bomb = 0;//炸弹数目
int x = 0;//横坐标
int y = 0;//纵坐标
int win = 0;//排弹成功
char mine[ROWS + 2][COLS + 2];
char show[ROWS + 2][COLS + 2];
memset(mine, '0', (ROWS + 2)*(COLS + 2));
memset(show, '*', (ROWS + 2)*(COLS + 2));
START:
printf("Please select level(E/M/D):");
fflush(stdin);
choose = getchar();
switch (choose)
{
case 'E':
bomb = 20;
break;
case 'M':
bomb = 40;
break;
case 'D':
bomb = 60;
break;
default:
printf("Error,please try again!\n");
goto START;
}
setMines(mine, ROWS, COLS, bomb);
display(show, ROWS, COLS);
//display(mine, ROWS, COLS);
while ((ROWS*COLS - bomb) > win)
{
printf("please choose locate:");
scanf("%d %d", &x, &y);
if ((x >= 1 && x <= ROWS) && (y >= 1 && y <= COLS))
{
if (mine[x][y] == '0')
{
system("CLS");
win += eRound(mine, show, ROWS, COLS, x, y);
display(show, ROWS, COLS);
}
else
{
system("CLS");
printf("It's mine!Defeat!!!\n");
display(mine, ROWS, COLS);
break;
}
}
else
{
printf("Error,please try again!\n");
}
}
if ((ROWS*COLS - bomb) == win)
{
printf("Congratulation!Victory!!!\n");
}
else
{
printf("bug!\n");
}
}
3.源文件main.c
#include"game.h"
1)mane --> 菜单界面
void manu()
{
printf("#########################################\n");
printf("###### 1.START 0.EXIT ######\n");
printf("###### E(易) M(中) D(难) ######\n");
printf("#########################################\n");
}
2)main --> 主函数
int main()
{
int input = 0;
do
{
manu();
printf("Please input(1/0):");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
exit(0);
default:
printf("Error,please try again!\n");
break;
}
} while (input);
system("pause");
return 0;
}
4.游戏界面
上一篇: Python基本数据类型(笔记)
下一篇: 消砖块游戏(EasyX重构进阶版)