c语言编写扫雷

时间:2021-06-21 09:15:05
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#pragma warning(disable:4996)
#define COL  10
#define ROW  10
void menu();
void game();
void mineField(char board[][ROW + 2], int col);
void showAll(char board[][ROW+2], int col);
void showMine(char board[][ROW + 2], int col, char aboard[ROW + 2][COL + 2]);
void playerFind(char board[][ROW + 2], int col, char aboard[ROW + 2][COL + 2]);
void menu()
{
printf("###########################\n");
printf("###  1.play     2.exit  ###\n");
printf("###########################\n");
printf("please enter the select:\n");
}
int main()
{
menu();
int select=0;
scanf("%d",&select);
do{
switch (select)
{
case 1:
game();
case 2:
exit(0);
default:
printf("plese enter 1 or 2!\n");
}
} while (1);
}
void game()
{
char board[COL+2][ROW+2];
memset(board, ' ', (ROW + 2)*(COL + 2));                
int i = 0;                                                        
mineField(board, ROW+2);
int aboard[ROW + 2][COL + 2];
memset(aboard,' ', (ROW + 2)*(COL + 2));
do{
showMine(board, ROW+2,aboard);           //扫雷以后显示的界面
playerFind(board, ROW+2,aboard);            //选择扫雷坐标
i++;
system("CLS");
} while (i<80);                                                //扫雷80次以后仍未踩到雷跳出循环
printf("you win!\n");
showAll(board, COL+2);                                //显示所有雷区
}
void mineField(char board[][ROW + 2], int col)       //随机函数种雷
{
memset(board, '0', (ROW + 2)*(COL + 2));
int i = 0;
int x = 0;
int y = 0;
int j = 0;
int k = 0;
srand((unsigned long)time(NULL));
for (i = 0; i < 20; i++)
{
x = rand() % 10;
y = rand() % 10;
board[x][y] = '1';
}
}
void playerFind(char board[][ROW + 2], int col,char aboard[ROW+2][COL+2])               //玩家输入坐标后显示数组
{                                                                                                    //aboard[ROW+2][COL+2]玩家的雷区提示数组   
int x;
int y;
printf("please enter the<x,y>:");
scanf("%d%d", &x, &y);
if (board[x][y]!='1')
{
if ((x > 1 && x < 10) && (y > 1 && y < 10))
{
aboard[x][y] =(( (board[x - 1][y - 1] - '0') +
(board[x - 1][y - 1] - '0') +
(board[x - 1][y] - '0') +
(board[x - 1][y + 1] - '0') +
(board[x][y - 1] - '0') +
(board[x][y + 1] - '0') +
(board[x + 1][y - 1] - '0') +
(board[x + 1][y] - '0') +
(board[x + 1][y + 1] - ' 0'))+48);
board[x][y] = aboard[x][y];
    }
}
else if (board[x][y]=1)                                   //玩家踩到雷,打印雷区并终止程序
{
int i=0;
int j = 0;
printf("you lose!\n");
showAll(board, COL);
exit(0);
}
else
{
}
}
void showMine(char board[][ROW + 2], int col,char aboard[ROW+2][COL+2])    //玩家雷区提示显示
{
int i = 1;
int j = 0;
for (i = 1; i <= 10; i++)
{
for (j = 1; j < 10; j++)
{
printf(" %c |",aboard[i][j]);
}
printf("\n");
}
}
void showAll(char board[][ROW+2], int col)                   //雷区面板数组
{
int j = 0;
int k = 0;
for (j = 0; j < 10; j++)
{
for (k = 0; k < 10; k++)


{
printf(" %c |", board[j][k]);
}
printf("\n");
}
}