C语言实现扫雷游戏,供大家参考,具体内容如下
game1.h中的代码如下:
里边放置的是相关的函数声明
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
void InitBoard( char board[ROWS][COLS], int rows, int cols, char set);
void DisplayBoard( char board[ROWS][COLS], int row, int col);
void SetMine( char board[ROWS][COLS], int row, int col);
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
|
saolei.c中的代码如下:
这里主要是实现菜单的打印以及棋盘的打印
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#define _CRT_SECURE_NO_WARNINGS 1
#include"game1.h"
void menu()
{
printf ( "**************************\n" );
printf ( "******* 1.play *********\n" );
printf ( "******* 0.exit *********\n" );
printf ( "**************************\n" );
}
void game1()
{
//雷的信息存储
//布置好雷的信息
char mine[ROWS][COLS]={0};
//排查出雷的信息
char show[ROWS][COLS]={0};
//初始化
InitBoard(mine,ROWS,COLS, '0' );
InitBoard(show,ROWS,COLS, '*' );
DisplayBoard(show,ROW,COL);
//布置雷
SetMine(mine,ROW,COL);
//扫雷
FindMine(mine,show,ROW,COL);
}
void saolei()
{
int input=0;
srand ((unsigned int ) time (NULL));
do
{
menu();
printf ( "请选择:" );
scanf ( "%d" ,&input);
switch (input)
{
case 1:
game1();
break ;
case 0:
printf ( "退出游戏\n" );
break ;
default :
printf ( "选择错误,请重新选择!\n" );
break ;
}
} while (input);
}
int main()
{
saolei();
return 0;
}
|
game1.c中的代码如下:
主要是玩家玩游戏排雷的整个游戏过程的相关函数操作。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#define _CRT_SECURE_NO_WARNINGS 1
#include"game1.h"
#include<stdio.h>
void InitBoard( char board[ROWS][COLS], int rows, int cols, char set)
{
int i=0;
int j=0;
for (i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
board[i][j]=set;
}
}
}
void DisplayBoard( char board[ROWS][COLS], int row, int col)
{
int i=0;
int j=0;
for (i=0;i<=col;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" );
}
}
void SetMine( char board[ROWS][COLS], int row, int col)
{
int count=EASY_COUNT;
while (count)
{
int x= rand ()%row+1;
int y= rand ()%col+1;
if (board[x][y]== '0' )
{
board[x][y]= '1' ;
count--;
}
}
}
int get_mine_count( char mine[ROWS][COLS], int x, int y)
{
return mine[x-1][y]+
mine[x-1][y-1]+
mine[x][y-1]+
mine[x+1][y-1]+
mine[x+1][y]+
mine[x+1][y+1]+
mine[x][y+1]+
mine[x-1][y+1]-8* '0' ;
}
void FindMine( char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x=0;
int y=0;
int win=0;
while (win<row*col-EASY_COUNT)
{
printf ( "请输入排查雷的坐标:" );
scanf ( "%d%d" ,&x,&y);
if (x>=1&&x<=row&&y>=1&&y<=col)
{
if (mine[x][y]== '1' )
{
printf ( "很遗憾,你被炸死了\n" );
DisplayBoard(mine,row,col);
break ;
}
else
{
int count=get_mine_count(mine,x,y);
show[x][y]=count+ '0' ;
DisplayBoard(show,row,col);
win++;
}
}
else
{
printf ( "输入坐标非法,请重新输入!" );
}
}
if (win==win<row*col-EASY_COUNT)
{
printf ( "恭喜你,排雷成功!\n" );
DisplayBoard(mine,row,col);
}
}
|
代码运行结果如图所示:
原文链接:https://blog.csdn.net/Sconnie/article/details/113646787