第一次写c,发篇博客纪念一下。
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#include
//定义数组
#define ROWS 11
#define COLS 11
//实际数组
#define ROW (ROWS - 2)
#define COL (COLS - 2)
#define EASY 10 //难度
//声明函数
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret);
void Disaplay(char board[ROWS][COLS], int row, int col);
void MineBoard(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void CountBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
//菜单
void menu()
{
printf("*********************************\n");
printf("*********************************\n");
printf("********** 1. play **********\n");
printf("********** 0. exit **********\n");
printf("*********************************\n");
printf("*********************************\n");
}
//游戏函数
void game()
{
//选择游戏后进行一次清屏
system("cls");
char mine[ROWS][COLS]; //存放地雷
char show[ROWS][COLS]; //显示界面
//初始化数组
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
//打印显示界面
Disaplay(show, ROW, COL);
//随机存放地雷
MineBoard(mine, ROW, COL);
//玩家进行扫雷
FindMine(mine, show, ROW, COL);
}
//主函数
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu(); //打印菜单
printf("请选择是否进行游戏(1/0):>");
scanf("%d", &input);
switch (input)
{
case 1:
//开始游戏
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入!\n");
}
} while (input); //若input为0(假),退出循环
return 0;
}
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
// 周围雷的个数
int ADD(char mine[ROWS][COLS],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';
}
//初始化数组
void InitBoard(char board[ROWS][COLS], int rows, int cols, char ret)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
}
}
//打印
void Disaplay(char board[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("............扫雷游戏............\n");
printf(" ");
for (i = 1; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
if (board[i][j] != '0')
{
printf("%c ", board[i][j]); //显示周围地雷数
}
else
{
printf("%c ", ' '); //将0优化为空格
}
}
printf("\n");
}
printf("............扫雷游戏............\n");
}
//随机存放地雷
void MineBoard(char board[ROWS][COLS], int row, int col)
{
int count = EASY;
while(count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '0')
{
board[x][y] = '1';
count--;
}
}
}
//玩家进行输入
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int c = 0;
int i = 0;
int j = 0;
do //重复扫雷过程
{
while(1) //实现输入以及标记和取消标记的功能
{
printf("扫雷(输入坐标) 标记(0 0) 取消标记(0 1)\n");
printf("请选择:>");
scanf("%d %d", &x, &y);
//实现标记功能
if (x == 0 && y == 0)
{
printf("请输入标记坐标:>");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col && show[x][y] == '*')
{
show[x][y] = '?';
system("cls");
Disaplay(show, ROW, COL);
}
else
{
printf("输入值非法,请重新选择!\n");
}
}
else if (x == 0 && y ==1) //实现取消标记功能
{
printf("请输入取消标记的坐标:>");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col && show[x][y] == '?')
{
show[x][y] = '*';
system("cls");
Disaplay(show, ROW, COL);
}
else
{
printf("输入值非法,请重新选择!\n");
}
}
else if (x > 0 && x <= row && y > 0 && y <= col) //跳出循环,直接扫雷
{
break;
}
else
{
printf("输入值非法,请重新选择!\n");
}
}
//踩雷
if (x > 0 && x <= row && y > 0 && y <= col && show[x][y] == '*')
{
if (mine[x][y] == '1')
{
system("cls");
printf(" boom!\n");
printf("很遗憾, 你被炸死了!\n");
Disaplay(mine, ROW, COL);
break;
}
else
{
CountBoard(mine, show, x, y);
system("cls");
Disaplay(show, ROW, COL);
c = 0; //重置c的值
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*' || show[i][j] == '?')
{
c++; //判断地雷是否排空
}
}
}
}
if (c == EASY)
{
break; //只剩地雷,跳出循环
}
}
else
{
printf("输入值非法,请重新选择!\n");
}
} while (1);
if (c == EASY)
{
printf("扫雷成功,你赢了!\n");
}
}
//计算周围的地雷数并递归实现大片扫功能
void CountBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
int count = 0;
count = ADD(mine, x, y); //调用计数函数
show[x][y] = count + '0';
if (show[x][y] == '0' && x <= ROW && y <= COL && x > 0 && y > 0)
{
if (show[x + 1][y] == '*' || show[x + 1][y] == '?') //只递归未扫雷地块
{
CountBoard(mine, show, x + 1, y);
}
if (show[x - 1][y] == '*' || show[x - 1][y] == '?')
{
CountBoard(mine, show, x - 1, y);
}
if (show[x][y + 1] == '*' || show[x][y + 1] == '?')
{
CountBoard(mine, show, x, y + 1);
}
if (show[x][y - 1] == '*' || show[x][y - 1] == '?')
{
CountBoard(mine, show, x, y - 1);
}
if (show[x + 1][y + 1] == '*' || show[x + 1][y + 1] == '?')
{
CountBoard(mine, show, x + 1, y + 1);
}
if (show[x + 1][y - 1] == '*' || show[x + 1][y - 1] == '?')
{
CountBoard(mine, show, x + 1, y - 1);
}
if (show[x - 1][y + 1] == '*' || show[x - 1][y + 1] == '?')
{
CountBoard(mine, show, x - 1, y + 1);
}
if (show[x - 1][y - 1] == '*' || show[x - 1][y - 1] == '?')
{
CountBoard(mine, show, x - 1, y - 1);
}
}
}