扫雷解析 C语言
Posted 万物皆为二叉树
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扫雷解析 C语言相关的知识,希望对你有一定的参考价值。
扫雷
扫雷流程思路
1打印菜单
void menu()
printf("********************\\n");
printf("*****1.paly**********\\n");
printf("*****0.excit*********\\n");
printf("********************\\n");
2选择语句
int input = 0;
do
srand((unsigned int)time(NULL));
menu();
scanf("%d", &input);
switch (input)
case 1:
game();
break;
case 0:
break;
default:
printf("错误\\n");
break;
while (input);
3打印棋盘
void init(char board[ROWS][COLS], int rows, int cols,char t)
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
j = 0;
for (j = 0; j < cols; j++)
board[i][j] = t;
void loadboard(char board[ROWS][COLS], int row, int col)
int i = 0;
int j = 0;
for (i = 0; i <row+1; 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");
初始化和布置棋盘
4放置地雷
void placeboard(char board[ROWS][COLS], int row, int col)
int i = 0;
while (i<15)
int x = rand() % (row);
int y = rand() % (col);
if (board[x][y] == '0'&&x>0&&y>0)
board[x][y] = '1';
i++;
5地雷检查
void checkboard(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col)
int x = 0;
int y = 0;
int sum = 0;
char sums = 0;
int i = 0;
do
scanf("%d%d", &x, &y);
if (0 < x && x <= 9 && 0 < y && y <= 9)
if (show[x][y] == '*')
sum = mine[x - 1][y] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x + 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] - 8 * '0';
show[x][y] = sum + '0';
i++;
loadboard(show, ROW, COL);
else if (mine[x][y] =='1')
printf("你被炸死了\\n");
break;
else
printf("棋子已经被占用了\\n");
else
printf("输入错误\\n");
break;
while (i<=15);
if (i == 15)
printf("good");
字符1到数字1的转换方法
戳这里
数字转换指南
附上一整套
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void game()
printf("开始游戏\\n");
char mine[ROWS][COLS] = 0 ;
char show[ROWS][COLS] = 0 ;
init(mine, ROWS, COLS,'0');
init(show, ROWS, COLS,'*');
placeboard(mine, ROW, COL);
loadboard(mine, ROW, COL);
loadboard(show, ROW, COL);
checkboard(mine, show, ROWS, COLS,ROW,COL);
loadboard(show, ROW, COL);
loadboard(mine, ROW, COL);
void menu()
printf("********************\\n");
printf("*****1.paly**********\\n");
printf("*****0.excit*********\\n");
printf("********************\\n");
int main()
int input = 0;
do
srand((unsigned int)time(NULL));
menu();
scanf("%d", &input);
switch (input)
case 1:
game();
break;
case 0:
break;
default:
printf("错误\\n");
break;
while (input);
return 0;
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void init(char board[ROWS][COLS], int rows, int cols,char t);
void loadboard(char board[ROWS][COLS], int row, int col);
void placeboard(char board[ROWS][COLS], int row, int col);
void checkboard(char mine[ROWS][COLS],char show[ROWS][COLS], in
#define _CRT_SECURE_NO_WARNINGS
#include"game.h"
void init(char board[ROWS][COLS], int rows, int cols,char t)
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
j = 0;
for (j = 0; j < cols; j++)
board[i][j] = t;
void loadboard(char board[ROWS][COLS], int row, int col)
int i = 0;
int j = 0;
for (i = 0; i <row+1; 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 placeboard(char board[ROWS][COLS], int row, int col)
int i = 0;
while (i<15)
int x = rand() % (row);
int y = rand() % (col);
if (board[x][y] == '0'&&x>0&&y>0)
board[x][y] = '1';
i++;
void checkboard(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col)
int x = 0;
int y = 0;
int sum = 0;
char sums = 0;
int i = 0;
do
scanf("%d%d", &x, &y);
if (0 < x && x <= 9 && 0 < y && y <= 9)
if (show[x][y] == '*')
sum = mine[x - 1][y] + mine[x - 1][y - 1] + mine[x - 1][y + 1] + mine[x + 1][y] + mine[x + 1][y - 1] + mine[x + 1][y + 1] + mine[x][y - 1] + mine[x][y + 1] - 8 * '0';
show[x][y] = sum + '0';
i++;
loadboard(show, ROW, COL);
else if (mine[x][y] =='1')
printf("你被炸死了\\n");
break;
else
printf("棋子已经被占用了\\n");
else
printf("输入错误\\n");
break;
while (i<=15);
if (i == 15)
printf("good");
以上是关于扫雷解析 C语言的主要内容,如果未能解决你的问题,请参考以下文章
扫雷游戏(C语言实现)初级版和优化版(增加了自动展开标记地雷功能,同时排除了第一次排到地雷的情况)