C语言扫雷-创新互联
一、游戏规则
当前标题:C语言扫雷-创新互联
转载来于:http://scyanting.com/article/dgiejp.html
一张9×9的棋盘上,有若干位置不明的地雷。玩家通过输入坐标,就会显示该坐标周围8个格子的地雷数;如果该坐标周围没有地雷,电脑会自动选择该坐标周围的格子,将范围扩大到周围有地雷的格子处。周围没有地雷的格子不显示数字,用空格来表示。如果玩家成功排除所有地雷(即剩下的格子全是地雷),则玩家胜利;反则,玩家失败,同时显示棋盘上的所有地雷。
网站建设公司,为您提供网站建设,网站制作,网页设计及定制网站建设服务,专注于成都企业网站定制,高端网页制作,对成都高空作业车租赁等多个行业拥有丰富的网站建设经验的网站建设公司。专业网站设计,网站优化推广哪家好,专业成都网站营销优化,H5建站,响应式网站。二、效果图如果选择的坐标处没有地雷,会自动扩大到周围有地雷的坐标。
三、主要思路流程介绍创建菜单
创建board来显示棋盘
创建mine来设置地雷
玩家输入坐标
判定游戏是否结束
没有踩雷,则继续游戏
………………
当棋盘上剩下的全部是地雷后,判定玩家胜利
这个部分主要显示各个函数
该游戏的主要函数#define ROW 9//定义扫雷的行数
#define COL 9//定义扫雷的列数
#define NUM 10//定义地雷数量
void init_board(char board[ROW + 2][COL + 2], int row, int col);//初始化游戏界面
void init_mine(char mine[ROW + 2][COL + 2], int row, int col);//初始化地雷数组
void set_mine(char mine[ROW + 2][COL + 2], int row, int col);//设置地雷,用 * 表示地雷
void show_board(char board[ROW + 2][COL + 2], int row, int col);//显示游戏界面
void show_mine(char mine[ROW + 2][COL + 2], int row, int col);//显示地雷
//计算地雷数
int count_mine(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y);
//玩家进行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col);
//递归运算,玩家每输入一个坐标,电脑会自动扩大区域到周围有地雷的坐标
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2]);
1、游戏菜单void menu()//菜单
{
printf("****************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("****************************\n");
}
2、game函数void game()
{
//可以定义两个二维数组,一个用来显示游戏界面,数字字符(周围的地雷数);另一个用来保存地雷位置
//将9×9扩大为11×11,可以简化9×9的边界处理问题
char board[ROW + 2][COL + 2];//游戏界面
char mine[ROW + 2][COL + 2];//记录地雷位置
init_board(board, ROW, COL);//初始化游戏界面
init_mine(mine, ROW, COL);//初始化地雷数组
set_mine(mine, ROW, COL);//设置地雷,用 * 表示地雷
show_board(board, ROW, COL);//显示游戏界面
//show_mine(mine, ROW, COL);//显示地雷
player(board, mine, ROW, COL);//玩家进行操作
}
3、init_board函数//用 # 来初始化游戏棋盘
void init_board(char board[ROW + 2][COL + 2], int row, int col)
{
for (int i = 0; i< row + 2; i++)
{
for (int j = 0; j< col + 2; j++)
{
board[i][j] = ' ';
}
}
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
board[i][j] = '#';
}
}
}
4、init_mine函数//初始化地雷数组
void init_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
for (int i = 0; i< row + 2; i++)
{
for (int j = 0; j< col + 2; j++)
{
mine[i][j] = ' ';
}
}
}
5、set_mine函数//设置地雷,用 * 表示地雷
void set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
srand((unsigned int)time(NULL));
int num = 0;
while (num< NUM)
{
int r = rand() % 9 + 1;
int c = rand() % 9 + 1;
if (mine[r][c] == ' ')
{
mine[r][c] = '*';
num++;
}
}
}
6、show_board函数//显示棋盘
void show_board(char board[ROW + 2][COL + 2], int row, int col)
{
printf("0 1 2 3 4 5 6 7 8 9\n");
for (int i = 1; i<= row; i++)
{
if (i == 1)
{
printf(" ");
for (int j = 1; j<= col; j++)
{
printf("----");
}
printf("--->y\n");
}
printf("%d", i);
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("| %c |",board[i][j]);
}
else
{
printf("| %c ", board[i][j]);
}
}
printf("\n");
printf(" ");
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("|---|");
}
else
{
printf("|---");
}
}
printf("\n");
}
printf(" |\n");
printf(" %c\n", 25);
printf(" x\n");
}
7、show_mine函数//显示地雷
void show_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
printf("0 1 2 3 4 5 6 7 8 9\n");
for (int i = 1; i<= row; i++)
{
if (i == 1)
{
printf(" ");
for (int j = 1; j<= col; j++)
{
printf("----");
}
printf("--->y\n");
}
printf("%d", i);
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("| %c |", mine[i][j]);
}
else
{
printf("| %c ", mine[i][j]);
}
}
printf("\n");
printf(" ");
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("|---|");
}
else
{
printf("|---");
}
}
printf("\n");
}
printf(" |\n");
printf(" %c\n", 25);
printf(" x\n");
}
8、player函数//玩家进行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col)
{
int x, y;
while (1)
{
while (1)
{
printf("请选择坐标(x,y)>:");
scanf("%d %d", &x, &y);
if (x >= 1 && x<= 9 && y >= 1 && y<= 9 && board[x][y] == '#')
{
break;
}
else
{
printf("警告:坐标非法,请重新输入\n");
}
}
if (mine[x][y] == '*')
{
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (mine[i][j] == '*')
{
board[i][j] = '*';
}
}
}
system("cls");
//show_mine(mine, ROW, COL);
show_board(board, ROW, COL);
printf("你被炸死了\n");
break;
}
else if (mine[x][y] != '*')
{
board[x][y] = count_mine(board,mine,x,y);
//mine[x][y]= count_mine(board, mine, x, y);
system("cls");
int arr[ROW + 2][COL + 2] = { 0 };
if (board[x][y] == ' ')
{
arr[x][y] = 1;
fit_play(board, mine, x, y, arr);
}
show_board(board, row, col);
}
int num = 0;
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (board[i][j] == '#')
{
num++;
}
}
}
if (num == NUM)
{
system("cls");
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (mine[i][j] == '*')
{
board[i][j] = '*';
}
}
}
show_board(board, ROW, COL);
printf("游戏结束,你赢了\n");
break;
}
}
}
9、count_mine函数//计算地雷数
int count_mine(char board[ROW+2][COL+2],char mine[ROW + 2][COL + 2], int x, int y)
{
int count = (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1]
+ mine[x + 1][y] + mine[x + 1][y + 1] - 32 * 8) / 10 + 48;
if (count == 48)
{
return 32;
}
return count;
}
10、fit_play函数//递归运算,玩家每输入一个坐标,电脑会自动扩大区域到周围有地雷的坐标
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2])
{
while (x >= 1 && x<= ROW && y >= 1 && y<= COL)
{
if (count_mine(board,mine, x - 1, y - 1) == 32 && arr[x - 1][y - 1] == 0)
{
board[x - 1][y - 1] = count_mine(board, mine, x - 1, y - 1);
while (arr[x - 1][y - 1] == 0)
{
arr[x - 1][y - 1] = 1;
fit_play(board, mine, x - 1, y - 1, arr);
}
}
else if (count_mine(board,mine, x - 1, y - 1) != 32)
{
board[x - 1][y - 1] = count_mine(board,mine, x - 1, y - 1);
}
if (count_mine(board,mine, x - 1, y) == 32 && arr[x - 1][y] == 0)
{
board[x - 1][y] = count_mine(board, mine, x - 1, y);
while (arr[x - 1][y] == 0)
{
arr[x - 1][y] = 1;
fit_play(board, mine, x - 1, y, arr);
}
}
else if (count_mine(board,mine, x - 1, y) != 32)
{
board[x - 1][y] = count_mine(board,mine, x - 1, y);
}
if (count_mine(board,mine, x - 1, y + 1) == 32 && arr[x - 1][y + 1] == 0)
{
board[x - 1][y + 1] = count_mine(board, mine, x - 1, y + 1);
while (arr[x - 1][y + 1] == 0)
{
arr[x - 1][y + 1] = 1;
fit_play(board, mine, x - 1, y + 1, arr);
}
}
else if (count_mine(board,mine, x - 1, y + 1) != 32)
{
board[x - 1][y + 1] = count_mine(board,mine, x - 1, y + 1);
}
if (count_mine(board,mine, x, y - 1) == 32 && arr[x][y - 1] == 0)
{
board[x][y - 1] = count_mine(board, mine, x, y - 1);
while (arr[x][y - 1] == 0)
{
arr[x][y - 1] = 1;
fit_play(board, mine, x, y - 1, arr);
}
}
else if (count_mine(board,mine, x, y - 1) != 32)
{
board[x][y - 1] = count_mine(board,mine, x, y - 1);
}
if (count_mine(board,mine, x, y + 1) == 32 && arr[x][y + 1] == 0)
{
board[x][y + 1] = count_mine(board, mine, x, y + 1);
while (arr[x][y + 1] == 0)
{
arr[x][y + 1] = 1;
fit_play(board, mine, x, y + 1, arr);
}
}
else if (count_mine(board,mine, x, y + 1) != 32)
{
board[x][y + 1] = count_mine(board,mine, x, y + 1);
}
if (count_mine(board,mine, x + 1, y - 1) == 32 && arr[x + 1][y - 1] == 0)
{
board[x + 1][y - 1] = count_mine(board, mine, x + 1, y - 1);
while (arr[x + 1][y - 1] == 0)
{
arr[x + 1][y - 1] = 1;
fit_play(board, mine, x + 1, y - 1, arr);
}
}
else if (count_mine(board,mine, x + 1, y - 1) != 32)
{
board[x + 1][y - 1] = count_mine(board,mine, x + 1, y - 1);
}
if (count_mine(board,mine, x + 1, y) == 32 && arr[x + 1][y] == 0)
{
board[x + 1][y] = count_mine(board, mine, x + 1, y);
while (arr[x + 1][y] == 0)
{
arr[x + 1][y] = 1;
fit_play(board, mine, x + 1, y, arr);
}
}
else if (count_mine(board,mine, x + 1, y) != 32)
{
board[x + 1][y] = count_mine(board,mine, x + 1, y);
}
if (count_mine(board,mine, x + 1, y + 1) == 32 && arr[x + 1][y + 1] == 0)
{
board[x + 1][y + 1] = count_mine(board, mine, x + 1, y + 1);
while (arr[x + 1][y + 1] == 0)
{
arr[x + 1][y + 1] = 1;
fit_play(board, mine, x + 1, y + 1, arr);
}
}
else if (count_mine(board,mine, x + 1, y + 1) != 32)
{
board[x + 1][y + 1] = count_mine(board,mine, x + 1, y + 1);
}
break;
}
}
五、总代码#define _CRT_SECURE_NO_WARNINGS
#include#include#include#define ROW 9//定义扫雷的行数
#define COL 9//定义扫雷的列数
#define NUM 10//定义地雷数量
void init_board(char board[ROW + 2][COL + 2], int row, int col);//初始化游戏界面
void init_mine(char mine[ROW + 2][COL + 2], int row, int col);//初始化地雷数组
void set_mine(char mine[ROW + 2][COL + 2], int row, int col);//设置地雷,用 * 表示地雷
void show_board(char board[ROW + 2][COL + 2], int row, int col);//显示游戏界面
void show_mine(char mine[ROW + 2][COL + 2], int row, int col);//显示地雷
//计算地雷数
int count_mine(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y);
//玩家进行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col);
//递归运算,玩家每输入一个坐标,电脑会自动扩大区域到周围有地雷的坐标
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2]);
void menu()//菜单
{
printf("****************************\n");
printf("******** 1.play ********\n");
printf("******** 0.exit ********\n");
printf("****************************\n");
}
void game()
{
//可以定义两个二维数组,一个用来显示游戏界面,数字字符(周围的地雷数);另一个用来保存地雷位置
//将9×9扩大为11×11,可以简化9×9的边界处理问题
char board[ROW + 2][COL + 2];//游戏界面
char mine[ROW + 2][COL + 2];//记录地雷位置
init_board(board, ROW, COL);//初始化游戏界面
init_mine(mine, ROW, COL);//初始化地雷数组
set_mine(mine, ROW, COL);//设置地雷,用 * 表示地雷
show_board(board, ROW, COL);//显示游戏界面
//show_mine(mine, ROW, COL);//显示地雷
player(board, mine, ROW, COL);//玩家进行操作
}
int main()
{
int choice;
do
{
menu();
printf("请选择>:");
scanf("%d", &choice);
printf("\n");
switch (choice)
{
case 1:
system("cls");
game();
system("pause");
system("cls");
break;
case 0:
break;
default:
printf("输入错误,请重新输入\n");
system("pause");
system("cls");
}
} while (choice);
return 0;
}
//用 # 来初始化游戏棋盘
void init_board(char board[ROW + 2][COL + 2], int row, int col)
{
for (int i = 0; i< row + 2; i++)
{
for (int j = 0; j< col + 2; j++)
{
board[i][j] = ' ';
}
}
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
board[i][j] = '#';
}
}
}
//初始化地雷数组
void init_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
for (int i = 0; i< row + 2; i++)
{
for (int j = 0; j< col + 2; j++)
{
mine[i][j] = ' ';
}
}
}
//设置地雷,用 * 表示地雷
void set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
srand((unsigned int)time(NULL));
int num = 0;
while (num< NUM)
{
int r = rand() % 9 + 1;
int c = rand() % 9 + 1;
if (mine[r][c] == ' ')
{
mine[r][c] = '*';
num++;
}
}
}
//显示棋盘
void show_board(char board[ROW + 2][COL + 2], int row, int col)
{
printf("0 1 2 3 4 5 6 7 8 9\n");
for (int i = 1; i<= row; i++)
{
if (i == 1)
{
printf(" ");
for (int j = 1; j<= col; j++)
{
printf("----");
}
printf("--->y\n");
}
printf("%d", i);
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("| %c |",board[i][j]);
}
else
{
printf("| %c ", board[i][j]);
}
}
printf("\n");
printf(" ");
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("|---|");
}
else
{
printf("|---");
}
}
printf("\n");
}
printf(" |\n");
printf(" %c\n", 25);
printf(" x\n");
}
//显示地雷
void show_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
printf("0 1 2 3 4 5 6 7 8 9\n");
for (int i = 1; i<= row; i++)
{
if (i == 1)
{
printf(" ");
for (int j = 1; j<= col; j++)
{
printf("----");
}
printf("--->y\n");
}
printf("%d", i);
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("| %c |", mine[i][j]);
}
else
{
printf("| %c ", mine[i][j]);
}
}
printf("\n");
printf(" ");
for (int j = 1; j<= col; j++)
{
if (j == col)
{
printf("|---|");
}
else
{
printf("|---");
}
}
printf("\n");
}
printf(" |\n");
printf(" %c\n", 25);
printf(" x\n");
}
//计算地雷数
int count_mine(char board[ROW+2][COL+2],char mine[ROW + 2][COL + 2], int x, int y)
{
int count = (mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y + 1] + mine[x + 1][y - 1]
+ mine[x + 1][y] + mine[x + 1][y + 1] - 32 * 8) / 10 + 48;
if (count == 48)
{
return 32;
}
return count;
}
//递归运算,玩家每输入一个坐标,电脑会自动扩大区域到周围有地雷的坐标
void fit_play(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int x, int y, int arr[ROW + 2][COL + 2])
{
while (x >= 1 && x<= ROW && y >= 1 && y<= COL)
{
if (count_mine(board,mine, x - 1, y - 1) == 32 && arr[x - 1][y - 1] == 0)
{
board[x - 1][y - 1] = count_mine(board, mine, x - 1, y - 1);
while (arr[x - 1][y - 1] == 0)
{
arr[x - 1][y - 1] = 1;
fit_play(board, mine, x - 1, y - 1, arr);
}
}
else if (count_mine(board,mine, x - 1, y - 1) != 32)
{
board[x - 1][y - 1] = count_mine(board,mine, x - 1, y - 1);
}
if (count_mine(board,mine, x - 1, y) == 32 && arr[x - 1][y] == 0)
{
board[x - 1][y] = count_mine(board, mine, x - 1, y);
while (arr[x - 1][y] == 0)
{
arr[x - 1][y] = 1;
fit_play(board, mine, x - 1, y, arr);
}
}
else if (count_mine(board,mine, x - 1, y) != 32)
{
board[x - 1][y] = count_mine(board,mine, x - 1, y);
}
if (count_mine(board,mine, x - 1, y + 1) == 32 && arr[x - 1][y + 1] == 0)
{
board[x - 1][y + 1] = count_mine(board, mine, x - 1, y + 1);
while (arr[x - 1][y + 1] == 0)
{
arr[x - 1][y + 1] = 1;
fit_play(board, mine, x - 1, y + 1, arr);
}
}
else if (count_mine(board,mine, x - 1, y + 1) != 32)
{
board[x - 1][y + 1] = count_mine(board,mine, x - 1, y + 1);
}
if (count_mine(board,mine, x, y - 1) == 32 && arr[x][y - 1] == 0)
{
board[x][y - 1] = count_mine(board, mine, x, y - 1);
while (arr[x][y - 1] == 0)
{
arr[x][y - 1] = 1;
fit_play(board, mine, x, y - 1, arr);
}
}
else if (count_mine(board,mine, x, y - 1) != 32)
{
board[x][y - 1] = count_mine(board,mine, x, y - 1);
}
if (count_mine(board,mine, x, y + 1) == 32 && arr[x][y + 1] == 0)
{
board[x][y + 1] = count_mine(board, mine, x, y + 1);
while (arr[x][y + 1] == 0)
{
arr[x][y + 1] = 1;
fit_play(board, mine, x, y + 1, arr);
}
}
else if (count_mine(board,mine, x, y + 1) != 32)
{
board[x][y + 1] = count_mine(board,mine, x, y + 1);
}
if (count_mine(board,mine, x + 1, y - 1) == 32 && arr[x + 1][y - 1] == 0)
{
board[x + 1][y - 1] = count_mine(board, mine, x + 1, y - 1);
while (arr[x + 1][y - 1] == 0)
{
arr[x + 1][y - 1] = 1;
fit_play(board, mine, x + 1, y - 1, arr);
}
}
else if (count_mine(board,mine, x + 1, y - 1) != 32)
{
board[x + 1][y - 1] = count_mine(board,mine, x + 1, y - 1);
}
if (count_mine(board,mine, x + 1, y) == 32 && arr[x + 1][y] == 0)
{
board[x + 1][y] = count_mine(board, mine, x + 1, y);
while (arr[x + 1][y] == 0)
{
arr[x + 1][y] = 1;
fit_play(board, mine, x + 1, y, arr);
}
}
else if (count_mine(board,mine, x + 1, y) != 32)
{
board[x + 1][y] = count_mine(board,mine, x + 1, y);
}
if (count_mine(board,mine, x + 1, y + 1) == 32 && arr[x + 1][y + 1] == 0)
{
board[x + 1][y + 1] = count_mine(board, mine, x + 1, y + 1);
while (arr[x + 1][y + 1] == 0)
{
arr[x + 1][y + 1] = 1;
fit_play(board, mine, x + 1, y + 1, arr);
}
}
else if (count_mine(board,mine, x + 1, y + 1) != 32)
{
board[x + 1][y + 1] = count_mine(board,mine, x + 1, y + 1);
}
break;
}
}
//玩家进行操作
void player(char board[ROW + 2][COL + 2], char mine[ROW + 2][COL + 2], int row, int col)
{
int x, y;
while (1)
{
while (1)
{
printf("请选择坐标(x,y)>:");
scanf("%d %d", &x, &y);
if (x >= 1 && x<= 9 && y >= 1 && y<= 9 && board[x][y] == '#')
{
break;
}
else
{
printf("警告:坐标非法,请重新输入\n");
}
}
if (mine[x][y] == '*')
{
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (mine[i][j] == '*')
{
board[i][j] = '*';
}
}
}
system("cls");
//show_mine(mine, ROW, COL);
show_board(board, ROW, COL);
printf("你被炸死了\n");
break;
}
else if (mine[x][y] != '*')
{
board[x][y] = count_mine(board,mine,x,y);
//mine[x][y]= count_mine(board, mine, x, y);
system("cls");
int arr[ROW + 2][COL + 2] = { 0 };
if (board[x][y] == ' ')
{
arr[x][y] = 1;
fit_play(board, mine, x, y, arr);
}
show_board(board, row, col);
}
int num = 0;
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (board[i][j] == '#')
{
num++;
}
}
}
if (num == NUM)
{
system("cls");
for (int i = 1; i<= row; i++)
{
for (int j = 1; j<= col; j++)
{
if (mine[i][j] == '*')
{
board[i][j] = '*';
}
}
}
show_board(board, ROW, COL);
printf("游戏结束,你赢了\n");
break;
}
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前标题:C语言扫雷-创新互联
转载来于:http://scyanting.com/article/dgiejp.html