扫雷游戏()

 game.h

#pragma once
2#include<stdio.h>
3#include<stdlib.h>
4#include<time.h>
5
6#define ROW 9
7#define COL 9
8#define ROWS ROW+2
9#define COLS ROW+2
10#define EASY_COUNT 10
11
12
13//声明函数
14
15//棋盘初始化函数
16void InitBoard(char arr[ROWS][COLS], int rows, int cols, char set);
17
18//打印棋盘
19void DisplayBoard(char arr[ROWS][COLS], int row, int col);
20
21//布置雷
22void SetMine(char arr[ROWS][COLS],int row,int col );
23
24//排查雷
25void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col);
26

 game.c

#define _CRT_SECURE_NO_WARNINGS

2
3#include "game.h"
4void InitBoard(char arr[ROWS][COLS], int rows, int cols,char set)
5{
6int i = 0;
7for (i = 0; i < rows; i++)
8{
9int j = 0;
10for (j = 0; j < cols; j++)
11{
12arr[i][j] = set;
13}
14}
15}
16
17void DisplayBoard(char arr[ROWS][COLS], int row, int col)
18{
19printf("-----扫雷游戏------\n");
20int i = 0;
21for (i = 0; i <= col; i++)
22{
23printf("%d ", i);
24}
25printf("\n");
26for (i = 1; i < row; i++)
27{
28int j = 0;
29printf("%d ", i);
30for (j = 1; j <= col; j++)
31{
32printf("%c ", arr[i][j]);
33}
34printf("\n");
35}
36}
37void SetMine(char arr[ROWS][COLS], int row, int col)
38{
39int count = EASY_COUNT;
40while (count)
41{
42int x = rand() % row + 1;
43int y = rand() % col + 1;
44if (arr[x][y] == '0')
45{
46arr[x][y] = '1';
47count--;
48}
49}
50}
51//static int GetMineCount(char mine[ROWS][COLS],int x, int y)
52//{
53// 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]
54// + mine[x][y+1] + mine[x - 1][y+1]-8*'0';
55//}
56//上面的函数用循环来写
57static int GetMineCount(char mine[ROWS][COLS], int x, int y)
58{
59int i = 0;
60int count = 0;
61for (i = x - 1; i <= x + 1; x++)
62{
63int j = 0;
64for (j = y - 1; j <= y + 1; j++)
65{
66count += mine[i][j] - '0';
67}
68
69}
70return count;
71
72}
73void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
74{
75int x = 0;
76int y = 0;
77int win = 0;
78
79while (win<(row*col- EASY_COUNT))
80{
81printf("请输入要排查雷的坐标:");
82scanf("%d %d", &x, &y);
83//判断坐标的有效性
84if (x >= 1 && x <= row && y >= 1 && y <= col)
85{
86if (mine[x][y] == '1')
87{
88printf("很遗憾,你被炸死了\n");
89DisplayBoard(mine, ROW, COL);
90break;
91}
92else
93{
94//该坐标不是雷,就统计坐标周围几个雷
95int count = GetMineCount(mine, x, y);
96show[x][y] = count + '0';
97DisplayBoard(show, ROW, COL);
98win++;
99}
100}
101else
102{
103printf("坐标非法,请重新输入\n");
104
105
106}
107}
108if (win = row * col - EASY_COUNT)
109{
110printf("恭喜你,排雷成功\n");
111DisplayBoard(mine, ROW, COL);
112}

}

text.c 

#define _CRT_SECURE_NO_WARNINGS
2#pragma warning(disable:6031)
3
4#include "game.h"
5
6void menu()
7{
8printf("*********************\n");
9printf("*** 1. play ****\n");
10printf("*** 0. exit ****\n");
11printf("*********************\n");
12
13}
14void game()//完成扫雷游戏
15{
16char mine[ROWS][COLS] = { 0 };
17char show[ROWS][COLS] = { 0 };
18//初始化棋盘
19InitBoard(mine, ROWS, COLS,'0');
20InitBoard(show, ROWS, COLS,'*');
21//show存放排查出的雷的信息
22//布置雷
23//9*9随机布置10个雷
24SetMine(mine,ROW,COL);
25//打印棋盘
26//DisplayBoard(mine, ROW, COL);
27//mine存放布置好的雷的信息
28DisplayBoard(show, ROW, COL);
29
30//排查雷
31FindMine(mine, show, ROW, COL);
32}
33void test()
34{
35int input = 0;
36do {
37menu();
38printf("请选择->");
39scanf("%d", &input);
40switch (input)
41{
42case 1:
43game();
44break;
45case 0:
46printf("游戏结束,退出游戏\n");
47break;
48default:printf("选择错误,重新选择\n");
49break;
50
51}
52} while (input);
53
54
55
56
57}
58int main()
59{
60test();
61return 0;
62}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值