利用模块化函数和宏定义,轻易实现棋盘的改动,令游戏更具有可玩性。
//game.h
#include
#include
#include
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define ezcount 10
void qpcsh(char arr[ROWS][COLS], int rows, int cols, char set);
void printarr(char arr2[ROWS][COLS], int row, int col);
void bzlei(char arr1[ROWS][COLS],int row,int col);
void findlei(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col);
//test.c
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void game()
{
char arr1[ROWS][COLS] = {0};
char arr2[ROWS][COLS] = {0};
//初始化棋盘 arr1为0,arr2为*
qpcsh(arr1, ROWS, COLS, '0');
qpcsh(arr2, ROWS, COLS, '*');
bzlei(arr1, ROW,COL);
//打印arr2
printarr(arr2, ROW, COL);
findlei(arr1, arr2, ROW, COL);
}
void menu()
{
printf("********************n");
printf("*****1.play*********n");
printf("*****0.exit*********n");
printf("********************n");
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
printf("扫雷游戏n");
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束n");
break;
default:
printf("输入错误n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
//game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void qpcsh(char arr[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
for (i = 0; i < rows; i++)
{
int j = 0;
for (j = 0; j < cols; j++)
arr[i][j] = set;
}
}
void printarr(char arr2[ROWS][COLS], int row, int col)
{
int i = 0;
int j = 0;
printf("--------扫雷--------n");
for (i = 0; i <=col; i++)
{
printf("%d ", i);
}
printf("n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", arr2[i][j]);
}
printf("n");
}
printf("--------扫雷--------n");
}
void bzlei(char arr1[ROWS][COLS], int row, int col)
{
int count = ezcount;
int x = 0;
int y = 0;
while (count)
{
x = rand() % row + 1;
y = rand() % col + 1;
if (arr1[x][y]!='1')
{
arr1[x][y] = '1';
count--;
}
}
}
int get_mine_count(char arr1[ROWS][COLS], int x, int y)
{
return arr1[x - 1][y] +
arr1[x - 1][y - 1] +
arr1[x][y - 1] +
arr1[x + 1][y - 1] +
arr1[x + 1][y] +
arr1[x + 1][y + 1] +
arr1[x][y + 1] +
arr1[x - 1][y + 1] - 8 * '0';
}
void findlei(char arr1[ROWS][COLS], char arr2[ROWS][COLS], int row, int col)
{
int win = 0;
int x = 0;
int y = 0;
while (win < row * col - ezcount)
{
printf("请输入要排查的坐标n");
scanf("%d%d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (arr1[x][y] == '1')
{
printf("你被炸死了游戏结束n");
printarr(arr1, ROW, COL);
break;
}
if (arr1[x][y] == '0')
{
int count = get_mine_count(arr1, x, y);
arr2[x][y] = count + '0';
printarr(arr2, ROW, COL);
win++;
}
}
else
printf("坐标非法,重新输入n");
}
if (win == row * col - ezcount)
{
printf("恭喜你,排雷成功n");
printarr(arr1, ROW, COL);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)