赞
踩
目录
效果展示
Windows.h头文件用于清屏和控制颜色
time.h头文件生成随机数埋雷
- #include <stdio.h>
- #include <stdlib.h>
- #include <Windows.h>
- #include <time.h>
地图大小的长宽各加一,方便每一次扫雷是计算周围的雷数
- #define ROW 8
- #define COL 8
- #define ROWS (ROW + 2)
- #define COLS (COL + 2)
- #define BOOM 8
-
- int g_mine[ROWS][COLS] = { 0 };
- char g_show[ROWS][COLS] = { 0 };
-
- void Init(char c)
- {
- int boom = BOOM;
- while (boom)
- {
- int x = rand() % ROW + 1;
- int y = rand() % COL + 1;
- if (g_mine[x][y] == 0)
- {
- g_mine[x][y] = 1;
- --boom;
- }
- }
-
- int i, j;
- for (i = 1; i <= ROW; ++i)
- for (j = 1; j <= COL; ++j)
- g_show[i][j] = c;
- }
Color函数通过系统的API接口控制输出端颜色
两个打印函数,一个用于游戏中每次操作后打印,一个用于游戏结束后打印初始地图
注:windows系统的颜色接口中,土黄色是6,绿色是10,红色是12,白色是15
- void Color(int x)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
- }
-
- void Display()
- {
- Color(10);
- system("cls");
- printf("----- Mine Sweeping -----\n");
- int i, j;
- for (i = 0; i <= COL; ++i)
- printf("%2d ", i);
-
- printf("\n");
- for (i = 1; i <= ROW; ++i)
- {
- Color(10);
- printf("%2d ", i);
- for (j = 1; j <= COL; ++j)
- {
- Color(6);
- printf(" %c ", g_show[i][j]);
- }
- printf("\n");
- }
- }
-
- void ShowMine()
- {
- Color(10);
- printf("\n");
- int i, j;
- for (i = 0; i <= COL; ++i)
- printf("%2d ", i);
-
- printf("\n");
- for (i = 1; i <= ROW; ++i)
- {
- Color(10);
- printf("%2d ", i);
- for (j = 1; j <= COL; ++j)
- {
- if (g_mine[i][j] == 1)
- {
- Color(12);
- printf(" B ");
- }
- else
- {
- Color(6);
- printf(" * ");
- }
- }
- printf("\n");
- }
- }
GetMineCount函数获取周围雷数,并将其转化为char类型返回
Sweep是扫雷函数,通过循环控制
- char GetMineCount(int x, int y)
- {
- return g_mine[x - 1][y - 1] +
- g_mine[x][y - 1] +
- g_mine[x + 1][y - 1] +
- g_mine[x - 1][y] +
- g_mine[x + 1][y] +
- g_mine[x - 1][y + 1] +
- g_mine[x][y + 1] +
- g_mine[x + 1][y + 1] + '0';
- }
-
- void Sweep()
- {
- int count = ROW * COL - BOOM;
- while (count)
- {
- Color(15);
- printf("请输入坐标扫雷: ");
- int x, y;
- scanf("%d%d", &x, &y);
- if (x < 1 || x > ROW || y < 1 || y > COL)
- {
- Color(15);
- printf("输入错误,请重新输入\n");
- continue;
- }
- if (g_mine[x][y] == 1)
- {
- Color(15);
- printf("很遗憾,你踩到了地雷\n");
- ShowMine();
- return;
- }
- else
- {
- if (g_show[x][y] == '*')
- {
- g_show[x][y] = GetMineCount(x, y);
- Display();
- --count;
- }
- else
- {
- Color(15);
- printf("该坐标已经标记过,请重新输入\n");
- }
- }
- if (count == 0)
- {
- Color(15);
- printf("\n恭喜你,扫雷成功!\n");
- ShowMine();
- }
- }
- }
-
- void Game()
- {
- Init('*');
- Display();
- Sweep();
- }
- void Menu()
- {
- printf("\n");
- printf("-------------------\n");
- printf("-------------------\n");
- printf("---- 1. Game ----\n");
- printf("---- 0. Exit ----\n");
- printf("-------------------\n");
- printf("-------------------\n");
- printf("请选择:");
- }
-
- int main()
- {
- srand((unsigned int)time(NULL));
- int input;
- do
- {
- Color(15);
- Menu();
- scanf("%d", &input);
- switch (input)
- {
- case 1:
- Game();
- break;
- case 0:
- system("cls");
- printf("退出游戏\n");
- return 0;
- default:
- printf("输入错误, 请重新选择\n");
- }
- } while (input);
- return 0;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。