本文实例为大家分享了C语言实现扫雷游戏的具体代码,供大家参考,具体内容如下
扫雷(第一次多文件应用)
扫雷的思路
game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include <time.h>
#include<string.h>
#include<windows.h>
#pragma warning(disable:4996)
#define ROW 12
#define COL 12
//定义20个雷
#define NUMS 20
void Menu();
void Game();
#endif
|
main.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
|
#include "game.h"
int main(){
int quit = 0;
int select = 0;
while (!quit){
Menu();
scanf ( "%d" , &select);
switch (select){
case 1:
Game();
break ;
case 2:
quit = 1;
break ;
default :
printf ( "请重新输入" );
break ;
}
}
system ( "pause" );
return 0;
}
|
game.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
|
#include "game.h"
void Menu()
{
printf ( "##########################\n" );
printf ( "## 1. Play 2. Exit ##\n" );
printf ( "##########################\n" );
printf ( "请输入# " );
}
//设置20个随机雷
void SetMines( char mine_board[][COL], int row, int col)
{
int count = NUMS;
while (count){
int x = rand () % 10 + 1;
int y = rand () % 10 + 1;
if (mine_board[x][y] == '0' ){
mine_board[x][y] = '1' ;
count--;
}
}
}
//判断周围有几个雷
int GetMines( char mine[][COL], int row, int col, int x, int y)
{
return 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] - 8 * '0' ;
}
//设置界面的下划线
static void ShowLine( int nums)
{
printf ( "---" );
for ( int i = 0; i < nums; i++){
printf ( "-" );
}
printf ( "\n" );
}
//一个显示界面,传入界面数组显示扫雷界面,传入布雷数组显示雷区界面
void ShowBoard( char show_board[][COL], int row, int col)
{
printf ( " " );
for ( int i = 1; i < row - 1; i++){
printf ( " %d " , i);
}
printf ( "\n" );
ShowLine(2 * col + col + 4);
for ( int i = 1; i < row - 1; i++){
printf ( "%2d|" , i);
for ( int j = 1; j < col - 1; j++){
printf ( " %c |" , show_board[i][j]);
}
printf ( "\n" );
ShowLine(2 * col + col + 4);
}
}
void Game()
{
char show_board[ROW][COL];
char mine_board[ROW][COL];
memset (show_board, '*' , sizeof (show_board));
memset (mine_board, '0' , sizeof (mine_board));
srand ((unsigned long ) time (NULL));
SetMines(mine_board, ROW, COL);
int count = (ROW - 2)*(COL - 2) - NUMS;
int x = 0;
int y = 0;
do {
ShowBoard(show_board, ROW, COL);
printf ( "请输入位置# " );
scanf ( "%d %d" , &x, &y);
if (x < 1 || x > 10 || y < 1 || y >10){
printf ( "输入越界,请重新输入!\n" );
continue ;
}
if (show_board[x][y] != '*' ){
printf ( "该位置已经被排除,请重新输入!\n" );
continue ;
}
if (mine_board[x][y] == '1' ){
break ;
}
int num = GetMines(mine_board, ROW, COL, x, y);
show_board[x][y] = num + '0' ;
count--;
system ( "cls" );
} while (count > 0);
//count>0说明坐标是雷,break提前退出了
if (count > 0){
printf ( "你被炸死了!\n" );
}
else {
printf ( "你赢了!\n" );
}
printf ( "下面是雷区的排布!\n" );
ShowBoard(mine_board, ROW, COL);
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_45928272/article/details/109562635