test.c
#include
#include "game.h"
#include
#include
void menu()
{
printf("******** 欢迎进入本游戏 ********\n");
printf("******** 1.开始游戏 ********\n");
printf("******** 0.退出游戏 ********\n");
}
enum Option
{
EXIT,
PLAY
};
void menu2()
{
printf("------请选择游戏等级------\n");
printf("------ 1.一般难度 ------\n");
printf("------ 2.中等难度 ------\n");
printf("------ 3.高级难度 ------\n");
}
void general_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows , cols );
display(board, rows , cols );
while (win < rows *cols - DEFAULT_COUNT1)
{
printf("请输入坐标: ");
scanf("%d%d", &x, &y);
n++;
if ((n == 1) && (real[x][y] = '1'))
{
real[x][y] = '0';
real[x - 1][y - 1] = '1';
}
if (real[x][y] == '1')
{
printf("很抱歉,您被炸死了!\n");
break;
}
else
{
win++;
int count = get_real_count(real, x, y);
board[x][y] = count + '0';
}
display(board, rows, cols);
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT1)
{
printf("恭喜您,排雷成功 \n");
}
}
void medium_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows , cols );
display(board, rows , cols );
while (win < (cols )*(rows ) - DEFAULT_COUNT2)
{
printf("请输入坐标: ");
scanf("%d%d", &x, &y);
n++;
if ((n == 1) && (real[x][y] = '1'))
{
real[x][y] = '0';
real[x - 1][y - 1] = '1';
}
if (real[x][y] == '1')
{
printf("很抱歉,您被炸死了!\n");
break;
}
else
{
int count = get_real_count(real, x, y);
board[x][y] = count + '0';
win++;
}
display(board, rows , cols );
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT2)
{
printf("恭喜您,排雷成功 \n");
}
}
void advanced_game()
{
int x = 0;
int y = 0;
int win = 0;
int n = 0;
char real[rows+2][cols+2] = { 0 };
char board[rows+2][cols+2] = { 0 };
srand((unsigned)time(NULL));
memset(real, '0', sizeof(char)*(rows+2)*(cols+2));
memset(board, '*', sizeof(char)*(rows+2)*(cols+2));
//display(board, rows , cols );
set_real1(real, rows , cols );
display(real, rows, cols );
display(board, rows , cols);
while (win < (cols)*(rows) - DEFAULT_COUNT3)
{
printf("请输入坐标: ");
scanf("%d%d", &x, &y);
n++;
if ((n == 1) && (real[x][y] = '1'))
{
real[x][y] = '0';
real[x - 1][y - 1] = '1';
}
if (real[x][y] == '1')
{
printf("很抱歉,您被炸死了!\n");
break;
}
else
{
win++;
int count = get_real_count(real, x, y);
board[x][y] = count + '0';
}
display(board, rows , cols );
}
if (win >= (cols )*(rows ) - DEFAULT_COUNT3)
{
printf("恭喜您,排雷成功 \n");
}
}
int main()
{
int input = 0;
int n = 0;
do
{
menu();
printf("请选择: ");
scanf("%d", &input);
switch (input)
{
case PLAY:
menu2();
printf("请选择难度等级: ");
scanf("%d", &n);
switch (n)
{
case 1: general_game();
break;
case 2: medium_game();
break;
case 3: advanced_game();
break;
default:
printf("选择错误,请重新输入\n");
break;
}
case EXIT: break;
default:
printf("选择错误,请重新输入\n");
break;
}
} while (input);
return 0;
}
game .c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
#include
#include
#include
void display(char arr[rows +2][cols+2 ], int ROW, int COL)
{
int i = 0;
int j = 0;
printf("\n");
printf(" 0 1 2 3 4 5 6 7 8 9\n");
printf(" _________________________________________\n");
for (i = 0; i < rows ; i++)
{
printf(" %d | %c | %c | %c | %c | %c | %c | %c | %c | %c | %c |\n",i, arr[i][0], arr[i][1], arr[i][2], arr[i][3], arr[i][4], arr[i][5], arr[i][6], arr[i][7], arr[i][8], arr[i][9]);
if (i != rows - 1)
{
printf(" |---|---|---|---|---|---|---|---|---|---|");
}
printf("\n");
}
}
int get_rand_num()
{
return rand() % 10+ 1 ;
}
void set_real1(char arr[rows+2 ][cols+2 ], int ROW, int COL)
{
int count = DEFAULT_COUNT1;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
void set_real2(char arr[rows +2][cols+2 ], int ROW, int COL)
{
int count = DEFAULT_COUNT2;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
void set_real3(char arr[rows +2][cols+2], int ROW, int COL)
{
int count = DEFAULT_COUNT3;
while (count)
{
int x = get_rand_num();
int y = get_rand_num();
if (arr[x][y] == '0')
{
arr[x][y] = '1';
count--;
}
}
}
int get_real_count(char arr[rows+2 ][cols+2 ], int x, int y)
{
int count = 0;
for (x = rows - 2; x<= rows; x++)//计算选择的位置周围雷的个数
{
for (y = cols - 2; y <= cols; y++)
{
if ((x >= 0) && (y >= 0) && arr[x][y] == '1')
count++;
}
{
return count;
}
}
}