扫雷游戏
当选择的位置不是雷时,该位置显示周围的雷的数目,踩到雷则游戏结束
头文件mine.h
#ifndef _MINE_H_
#define _MINE_H_
#define ROWS 9
#define COLS 9
#define NUM 10 //设置雷的个数
enum ENUM;
void menu();
void sweep(int rows, int cols, char b[ROWS][COLS]);
void mine(int rows, int cols, char array[ROWS][COLS]);
int player_play(int rows, int cols, char b[ROWS][COLS], char array[ROWS][COLS]);
void display(int rows, int cols, char b[ROWS][COLS]);
void display1(int rows, int cols, char array[ROWS][COLS]);
int end(char b[ROWS][COLS], char array[ROWS][COLS]);
#endif
mine.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"mine.h"
#include<stdio.h>
#include<memory.h>
#include<stdlib.h>
#include<time.h >
void display(int rows,int cols,char b[ROWS][COLS]) //将数组b的全部内容赋值为X
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS;i++)
for (j = 0; j < COLS; j++)
b[i][j] = 'X';
}
void display1(int rows, int cols, char array[ROWS][COLS]) //将数组array的全部内容赋值为字符1
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
array[i][j] = '1';
}
void sweep(int rows, int cols, char b[ROWS][COLS])
{
int i, j;
printf(" ");
for (i = 1; i <= cols; i++)
printf(" %d ", i);
printf("\n");
for (i = 1; i <= rows; i++)
{
printf(" %d ", i);
for (j = 1; j <= cols; j++)
{
printf(" %c ",b[i-1][j-1]);
}
if (j > cols)
printf("\n");
}
}
void mine(int rows,int cols,char array[ROWS][COLS]) //随机设置雷的位置
{
int x = 0;
int y = 0;
int i = 0;
int j = 1;
printf(" ");
for (i = 0; i < NUM; i++)
{
while (j)
{
x = rand() % 9;
y = rand() % 9;
if (array[x][y] != '*')
{
array[x][y] = '*';
j = 0;
}
else j = 1;
}
j = 1;
}
for (i = 1; i <= cols; i++)
printf(" %d ", i);
printf("\n");
for (i = 1; i <= ROWS; i++)
{
printf(" %d ", i);
for (j = 1; j <= ROWS; j++)
{
printf(" %c ", array[i-1][j-1]);
}
if (j > COLS)
printf("\n");
}
}
int player_play(int rows, int cols, char b[ROWS][COLS], char array[ROWS][COLS])
{
int i = 0;
int j = 0;
char count = '0';
if (array[rows-1][cols-1] == '*')//判断是否踩雷
{
printf("踩雷,游戏结束\n");
return 1;
}
else
{
for (i = rows-2; i <= rows; i++)//计算选择的位置周围雷的个数
{
for (j = cols-2; j <= cols; j++)
{
if ((i>=0)&&(j>=0)&&array[i][j] == '*')
count++;
}
}
b[rows-1][cols-1] = count;
}
sweep(ROWS, COLS,b);
return 0;
}
int end(char b[ROWS][COLS], char array[ROWS][COLS]) //判断是不是已经将所有雷的位置查找出来
{
int i = 0;
int j = 0;
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
{
if (b[i][j] == 'X' && array[i][j] != '*')
return 0;
}
}
return 1;
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"mine.h"
#include<stdio.h>
#include<memory.h>
enum ENUM
{
EXIT,
PLAY,
};
void menu()
{
printf("***************************************\n");
printf("*****************1 play****************\n");
printf("*****************0 exit****************\n");
printf("***************************************\n");
}
int main()
{
char array[ROWS][COLS];
char b[ROWS][COLS];
int input = 0;
int i = 0;
int j = 0;
display(ROWS, COLS, b);
display1(ROWS, COLS, array);
srand((unsigned)time(NULL));
menu();
/*mine(ROWS, COLS, array);*/ //该语句在测试的时候可以让其运行,打印出雷的位置
printf("请选择> \n");
scanf("%d", &input);
switch (input)
{
case PLAY:
{
while (1)
{
printf("请输入位置>");
scanf("%d%d", &i, &j);
if (player_play(i, j, b, array) == 1)//判断是否踩到雷,踩到雷则返回值为1,退出循环
{
break;
}
if (end(b, array) == 1) //若已找出所有雷,则退出循环,结束程序
{
printf("已完成扫雷\n");
break;
}
}
}
break;
case EXIT:
break;
}
system("pause");
return 0;
}