赞
踩
test.c文件
#include “game.h”
int main()
{
int n = 0;
srand((unsigned int)time(NULL));
menu();
do
{
printf(“请输入一个数字选择\n”);
scanf("%d", &n);
switch (n)
{
case 0:
printf("游戏结束\n"); break; case 1: game(); break; default: printf("输入错误,请重新输入\n"); break; } } while (n); system("pause"); return 0;
}
game.h文件
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void game();
#define ROW 9
#define COL 9
#define LEI 10
void mine();
void show();
void empty();
void mine_set();
void show_set();
void play();
void show_print();
void mine_print();
int find();
void explore();
game.c文件
#include “game.h”
menu()
{
printf("******\n");
printf(" 1 . play \n");
printf(" 0 . exit \n");
printf("\n");
}
void explore(char mine[ROW + 2][COL + 2], char show[ROW][COL], int row, int col, int x, int y)
{
int c;
c = find(mine, row, col, x, y);
if (c == 0) //通过递归函数来向外延展没有雷的区域,直到有雷为止
{
show[x][y] = ‘0’;
if ((x - 1)>0 && (x - 1) <= row - 2 && y>0 && y <= col - 2 && show[x - 1][y] == ‘’)
explore(mine, show, row, col, (x - 1), y);
if ((x - 1)>0 && (x - 1) <= row - 2 && (y - 1)>0 && (y - 1) <= col - 2 && show[x - 1][y - 1] == '’)
explore(mine, show, row, col, (x - 1), (y - 1));
if ((x)>0 && (x) <= row - 2 && (y - 1) >= 1 && (y - 1) <= col - 2 && show[x][y - 1] == ‘’)
explore(mine, show, row, col, x, (y - 1));
if ((x + 1)>0 && (x + 1) <= row - 2 && (y - 1)>0 && (y - 1) <= col - 2 && show[x + 1][y - 1] == '’)
explore(mine, show, row, col, (x + 1), (y - 1));
if ((x + 1)>0 && (x + 1) <= row - 2 && y>0 && y <= col - 2 && show[x + 1][y] == ‘’)
explore(mine, show, row, col, (x + 1), (y));
if ((x + 1)>0 && (x + 1) <= row - 2 && (y + 1)>0 && (y + 1) <= col - 2 && show[x + 1][y + 1] == '’)
explore(mine, show, row, col, (x + 1), (y + 1));
if ((x)>0 && (x) <= row - 2 && (y + 1)>0 && (y + 1) <= col - 2 && show[x][y + 1] == ‘’)
explore(mine, show, row, col, x, (y + 1));
if ((x - 1)>0 && (x - 1) <= row - 2 && (y + 1)>0 && (y + 1) <= col - 2 && show[x - 1][y + 1] == '’)
explore(mine, show, row, col, (x - 1), (y + 1));
}
else
{
show[x][y] = c + '0';
}
}
int find(char mine[ROW + 2][COL + 2], int row, int col, int x, int y)
{
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] + mine[x][y + 1] + mine[x - 1][y + 1] - 8 * ‘0’; //有雷的地方放’1’,没雷的地方放’0’,所以此时返回的值就位周围一圈有雷的个数
}
void show_print(char show[ROW + 2][COL + 2], int row, int col)
{
int i, j;
for (i = 0; i < row - 1; i++)
{
for (j = 0; j < col - 1; j++)
{
printf("%c “, show[i][j]);
}
printf(”\n");
}
}
void mine_print(char mine[ROW + 2][COL + 2], int row, int col)
{
int i, j;
for (i = 1; i < 10; i++)
{
for (j = 1; j < 10; j++)
{
printf("%c “, mine[i][j]);
}
printf(”\n");
}
}
void empty(char a[ROW + 2][COL + 2], int row, int col)
{
memset(&a[0][0], ‘\0’, rowcolsizeof(a[0][0]));
}
void mine_set(char mine[ROW + 2][COL + 2], int row, int col)
{
int h = LEI;
int x;
int y;
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{
mine[i][j] = ‘0’;
}
}
while (i)
{
x = rand() % ROW + 1;
y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
h--;
}
if (h == 0)
{
break;
}
}
}
void show_set(char show[(ROW + 2)][(COL + 2)], int row, int col)
{
int i = 0;
int j = 0;
for (i = 0; i < row - 1; i++)
{
show[0][i] = i + ‘0’;
}
for (i = 1; i < col - 1; i++)
{
show[i][0] = i + ‘0’;
}
for (i = 1; i < row - 1; i++)
{
for (j = 1; j < col - 1; j++)
{
show[i][j] = ‘*’;
}
}
}
void play(char mine[ROW + 2][COL + 2], char show[ROW + 2][COL + 2], int row, int col)
{
int x, y;
int c = 0;
int d = 0;
int a;
int b;
int input = 1;
while (1)
{
again:
printf(“请输入要扫描的坐标”);
scanf("%d%d", &x, &y);
while (input)
{
if (input == 1 && mine[x][y] == ‘1’)
{
mine[x][y] = ‘0’;
while (1) { a = rand() % 9 + 1; b = rand() % 9 + 1; if (mine[a][b] == '0') { mine[a][b] == '1'; break; } } } input--; } if (show[x][y] != '*') { printf("输入错误,请重新输入\n"); goto again; } if (x >= 1 && x <= ROW &&y >= 1 && y <= COL) { if (mine[x][y] == '1') { printf("你被炸死了\n"); mine_print(mine, row, col); break; } else { c = find(mine, row, col, x, y); if (c == 0) { explore(mine, show, row, col, x, y); show_print(show, row, col); } else { show[x][y] = c + '0'; show_print(show, row, col); d++; } } if (d == ROW*COL - LEI) { printf("你赢了\n"); break; } } if (x > ROW || y > COL) printf("输入错误,请重新输入\n"); }
}
void game()
{
int i, j;
char mine[ROW + 2][COL + 2] = { 0 };
char show[ROW + 2][COL + 2] = { 0 };
empty(mine, (ROW + 2), (COL + 2));
empty(show, (ROW + 2), (COL + 2));
mine_set(mine, (ROW + 2), (COL + 2));
show_set(show, (ROW + 2), (COL + 2));
//mine_print(mine, (ROW + 2), (ROW + 2)); //直接调出雷阵在制作过程中方便检测程序是否正确
play(mine, show, (ROW + 2), (COL + 2));
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。