代码如下:
? ? ? ? 第一个文件:?test.c? ?(是整个程序大致运行思路)
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void game()
{
//初始化
init_board(yours, '*');
init_board(mine, '0');
init_board(mark, '0');
//打印棋盘
display_board(yours, Row, Col);
//布置地雷
set_mine(mine, Row, Col);
//调试时打开
display_board(mine, Row, Col);
display_board(mark, Row, Col);
//找地雷
find_mine(yours, Row, Col);
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu1();
printf("根据菜单,请输入数字:>");
scanf("%d", &input);
printf("---------------------------------------\n");
switch (input)
{
case 1:
game();
break;
case 0:
printf("成功退出游戏\n");
break;
default :
printf("选项输入错误,请从新输入\n");
printf("---------------------------------------\n");
break;
}
} while (input);
return 0;
}
? ? ? ? ? ? ? ?第二个文件: game.c? ?(实现具体函数)
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void init_board(char board[][Cols], char set)
{
int i = 0;
int j = 0;
for (i = 0; i < Rows; i++)
{
for (j = 0; j < Cols; j++)
{
board[i][j] = set;
}
}
}
void display_board(char board[][Cols], int row, int col)
{
int i = 0;
int j = 0;
printf("---------------------------------------\n");
for (i = 0; i <= row; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("---------------------------------------\n");
}
void set_mine(char mine[][Cols], int row, int col)
{
int num = Count;
while (num)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
mark[x][y] = '!';
Mark(mine, mark, x, y);
--num;
}
}
}
void Mark(char mine[][Cols], char mark[][Cols], int x, int y)
{
int i = 0;
int j = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if (mark[x + i][y + j] != '!')
{
mark[x + i][y + j] += 1;
}
}
}
}
void find_mine( char yours[][Cols], int row, int col)
{
int x = 0;
int y = 0;
int input = 0;
while(1)
{
menu2();
scanf("%d", &input);
if (input == 2)
{
printf("请输入你要找的位置坐标:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (yours[x][y] == '*' || yours[x][y] == '?')
{
if (mark[x][y] == '!')
{
printf("很遗憾你被炸死了!\n");
printf("---------------------------------------\n");
display_board(mine, Row, Col);
break;
}
else
{
if (mark[x][y] != '0')
{
yours[x][y] = mark[x][y];
if (Win())
break;
//++win;
display_board(yours, Row, Col);
}
else
{
gone_blank(x, y);
if (Win())
break;
display_board(yours, Row, Col);
}
}
}
else
{
printf("---------------------------------------\n");
printf("输入坐标已输入过,请从新输入\n");
printf("---------------------------------------\n");
}
}
else
{
printf("---------------------------------------\n");
printf("你输入的坐标有误,请重新输入\n");
printf("---------------------------------------\n");
}
}
if (input == 1)
do_mark();
}
}
void gone_blank( int x, int y)
{
if (yours[x][y] == '*'||yours[x][y]=='?')
{
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
if (mark[x][y] == '0')
{
yours[x][y] = mark[x][y];
gone_blank(x - 1, y - 1);
gone_blank(x - 1, y);
gone_blank(x - 1, y + 1);
gone_blank(x, y - 1);
gone_blank(x, y + 1);
gone_blank(x + 1, y - 1);
gone_blank(x + 1, y);
gone_blank(x + 1, y + 1);
}
else
{
yours[x][y] = mark[x][y];
}
}
}
int Win()
{
int ret = 0;
int i = 0;
int j = 0;
for (i = 1; i <= Row; i++)
{
for (j = 1; j <= Col; j++)
{
if (yours[i][j] == '*' )
{
++ret;
}
}
}
if (ret == Count)
{
printf("-------恭喜你游戏胜利!--------\n");
return 1;
}
else
return 0;
}
void menu1()
{
printf("菜单********************\n");
printf("**** 1.play *****\n");
printf("**** 0.exit *****\n");
printf("************************\n");
}
void menu2()
{
//printf("你是否想要对某个点进行标记?\n");
printf("******* 1.mark *****\n");
printf("******* 2.find *****\n");
printf("输入你选择的数字序号:>");
}
void do_mark()
{
int x = 0;
int y = 0;
while (1)
{
printf("请输入你想标记点的坐标:>");
scanf("%d %d", &x, &y);
if (yours[x][y] == '*')
{
yours[x][y] = '?';
display_board(yours, Row, Col);
break;
}
else
{
printf("输入有误,请从新输入\n");
}
}
}
? ? ? ? ? ? ? ? 第三个文件:game.h (头文件声明)
#define _CRT_SECURE_NO_WARNINGS 1
//头文件声明
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//宏定义
#define Row 9
#define Col 9
#define Rows Row+2
#define Cols Col+2
#define Count 80
//全局变量
char mark[Rows][Cols];//地雷盘
char yours[Rows][Cols];//玩家盘
char mine[Rows][Cols];//标记地雷(!)、地雷周围(用数字)盘
//函数声明
//初始化
void init_board(char board[][Cols], char set);
//打印棋盘
void display_board(char board[][Cols], int row, int col);
//布置地雷
void set_mine(char mine[][Cols], int row, int col);
//标记地雷周围的格子
void Mark(char mine[][Cols], char mark[][Cols], int x, int y);
//找地雷
void find_mine( char yours[][Cols], int row, int col);
//消除大片空白
void gone_blank ( int x, int y);
//判断胜利
int Win();
//菜单1
void menu1();
//菜单2
void menu2();
//做记号
void do_mark();
注意:代码还是有些问题的,比如:输入选择菜单时,只能输入1个数字,而输入坐标时,只能输入2个数字,。
|