游戏头文件:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROWS 11
#define COLS 11
#define ROW 9
#define COL 9
void Init_Board(char arr[ROWS][COLS], int rows, int cols,char set);
void Set_Mine(char mine[ROWS][COLS], int row, int col);
void Display_Board(char arr[ROWS][COLS],int row,int col);
void Find_Mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
游戏源文件:
#include"game.h"
void Init_Board(char arr[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++)
{
arr[i][j] = set;
}
}
}//初始化棋盘
void Display_Board(char arr[ROWS][COLS],int row,int col)
{
int i = 0;
int j = 0;
for (j = 0; j <=col; j++)
{
printf("%d ", j);//打列印标记数
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d", i);//打印行标记数
printf(" ");//调整行排列
for (j = 1; j <=col; j++)
{
printf("%c ", arr[i][j]);
}
printf("\n");
}
}//打印棋盘
void Set_Mine(char mine[ROWS][COLS], int row, int col)
{
int count = 10;
while(count)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}////布置地雷
void Find_Mine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0; int y = 0;
int count = 0;
while (count < row * col - 10)
{ printf("输入你要查找的坐标");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了\n");
break;
}
else
{
if (show[x][y] == '*')
{
int ret= Count_Mine(mine, x, y);//计算周围的地雷数目
show[x][y] = ret + '0';
Display_Board(show, ROW, COL);
count++;
}
else
{
printf("输入的坐标已被占用请重新输入\n");
}
}
}
else
{
printf("输入的坐标有误请重新输入\n");
}
}
if (count == row * col - 10)
{
printf("玩家游戏获得胜利\n");
}
}
int Count_Mine(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';
}
游戏测试总模块:
#include"game.h"
void menu()
{
printf("*******************\n");
printf("******1.play*******\n");
printf("*******************\n");
printf("******0.exit*******\n");
printf("*******************\n");
printf("请输入你选择的游戏模式");
}
void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
Init_Board(mine, ROWS, COLS, '0');
Init_Board(show, ROWS, COLS,'*');//初始化棋盘
Display_Board(show, ROW, COL);
Set_Mine(mine, ROW, COL);
Find_Mine(mine, show, ROW, COL);
Display_Board(show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//时间戳
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束退出程序");
break;
default:
printf("输入的数字有误,请重新输入");
}
} while (input);
return 0;
}