简易的扫雷展示
Posted zsQgqdsd1002
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了简易的扫雷展示相关的知识,希望对你有一定的参考价值。
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW + 3
#define COLS COL + 3
#define MINE_COUNT 10
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[][COLS], int row, int col, char c);
void ShowBoard(char board[][COLS], int row, int col);
void SetMine(char board[][COLS], int row, int col);
int GetMineCount(char b[][COLS], int x, int y);
void JudgeMine(char user_display[][COLS], char mine[][COLS], int row, int col);
void Menu()
printf("******************\\n");
printf("***欢迎来到扫雷***\\n");
printf("******0.exit******\\n");
printf("******1.play******\\n");
printf("******************\\n");
void InitBoard(char board[][COLS], int row, int col, char c)
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
board[i][j] = c;
void ShowBoard(char board[][COLS], int row, int col)
printf("--------------begin-----------\\n");
for (int i = 0; i <= col; i++)
printf("%d ", i);
printf("\\n");
for (int i = 1; i <= row; i++)
printf("%d ", i);
for (int j = 1; j <= col; j++)
printf("%c ", board[i][j]);
printf("\\n");
printf("---------------end------------\\n");
void SetMine(char board[][COLS], int row, int col)
int mine_count = MINE_COUNT;
while (mine_count > 0)
int x = rand() % row + 1;
int y = rand() % col + 1;
if(board[x][y] == '0')
board[x][y] = '1';
mine_count--;
int GetMineCount(char b[][COLS], int x, int y)
return (b[x - 1][y - 1]) + (b[x - 1][y]) + (b[x - 1][y + 1]) +
(b[x][y - 1]) + (b[x][y + 1]) + (b[x + 1][y - 1]) +
(b[x + 1][y]) + (b[x + 1][y + 1]) - 8 * '0';
void JudgeMine(char user_display[][COLS], char mine[][COLS], int row, int col)
int step_count = 0;
while (step_count < row * col - MINE_COUNT)
printf("please enter x,y: ");
fflush(stdout);
int x, y;
scanf("%d,%d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
if (mine[x][y] == '1')
printf("踩雷了!\\n");
ShowBoard(mine, ROW, COL);
break;
else
int mine_count = GetMineCount(mine, x, y);
printf("mine_count : %d\\n", mine_count);
user_display[x][y] = mine_count + '0';
ShowBoard(user_display, ROW, COL);
step_count++;
else
printf("输入的坐标错误, 请重新输入\\n");
if (step_count == ROW * COL - MINE_COUNT)
printf("你就是扫雷之王?\\n");
void Game()
char user_display[ROWS][COLS];
char mine_board[ROWS][COLS];
InitBoard(user_display, ROWS, COLS, '?');
InitBoard(mine_board, ROWS, COLS, '0');
ShowBoard(user_display, ROW, COL);
SetMine(mine_board, ROW, COL);
JudgeMine(user_display, mine_board, ROW, COL);
int main()
srand((unsigned int)time(NULL));
setvbuf(stdout, NULL, _IONBF, 0);
int input;
do
Menu();
printf("请输入你的选择:\\n");
scanf("%d", &input);
switch (input)
case 0:
printf("再见拜拜");
break;
case 1:
Game();
break;
default:
printf("请重新输入!\\n");
while (input != 0);
return 0;
以上是关于简易的扫雷展示的主要内容,如果未能解决你的问题,请参考以下文章