*************************************game.h************************************
#ifndef _GAME_H_
#define _GAME_H_
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
void Setmine(char arr[ROWS][COLS], int row, int col);
void Inintmeun(char arr[ROWS][COLS], int row, int col, char set);
void Output(char arr[ROWS][COLS], int row, int col);
int Clean(char arr[ROWS][COLS], char arr1[ROWS][COLS], int row, int col);
int num(char arr[ROWS][COLS], int x, int y);
#endif
*************************************game.c************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void Inintmeun(char arr[ROWS][COLS], int row, int col, char set)
{
memset(arr, set, row*col*sizeof(set));
}
void Output(char arr[ROWS][COLS], int row, int col)
{
int x = 1;
int y = 1;
for (x = 1; x <= row; x++)
{
printf(" %d ", x);
for (y = 1; y <= col; y++)
{
printf(" %c ", arr[x][y]);
}
printf("\n");
}
printf("\n");
}
void Setmine(char arr[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
srand((unsigned)time(NULL));
while (count < COUNT)
{
x = rand() % row + 1;
y = rand() % col+ 1;
if (arr[x][y] =='0')
{
arr[x][y] = '1';
count++;
}
}
}
int num(char arr[ROWS][COLS], int x ,int y)
{
int count = 0;
if (arr[x + 1][y] == '1')
count++;
if (arr[x -1][y] =='1')
count++;
if (arr[x + 1][y+1] == '1')
count++;
if (arr[x + 1][y-1] == '1')
count++;
if (arr[x -1][y+1] == '1')
count++;
if (arr[x - 1][y-1] == '1')
count++;
if (arr[x][y-1] == '1')
count++;
if (arr[x][y+1] == '1')
count++;
return count;
}
int Clean(char arr[ROWS][COLS],char arr1[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int count = 0;
int sum = 0;
printf("请输入想要清除的坐标");
scanf("%d", &x);
scanf("%d", &y);
while(((x >= 1) &&( x <= 9)) && ((y >= 1) && (y <= 9)))
{
if (arr[x][y] == '1')
{
arr1[x][y] = '1';
Output(arr1, ROW, COL);
printf("被炸死了\n");
return 0;
}
else
{
sum++;
count = num(arr, x, y);
arr1[x][y] = count+'0';
Output(arr1, ROW, COL);
if (sum == ROW*COL - COUNT)
{
printf("扫雷成功\n");
return 0;
}
printf("请输入想要清除的坐标");
scanf("%d", &x);
scanf("%d", &y);
}
}
printf("坐标不合法\n");
return 0;
}
*************************************game.c************************************
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("*****************************\n");
printf("**1.play game 0.exit **\n");
printf("*****************************\n");
printf("** 请通过键盘进行选择 **\n");
}
int main()
{
int n = 0;
menu();
scanf("%d", &n);
int i = 0;
char menu[ROWS][COLS];
char game[ROWS][COLS];
char set1 = '*';
char set2 = '0';
printf(" ");
for (i = 1; i <= ROW; i++)
{
printf(" %d ", i);
}
printf("\n");
while (n)
{
Inintmeun(menu, ROWS, COLS,set1);
Inintmeun(game, ROWS, COLS, set2);
Output(menu, ROW, COL);
Output(game,ROW, COL);
Setmine(game, ROW, COL);
Output(game, ROW, COL);
Clean(game,menu, ROW, COL);
Output(menu, ROW, COL);
n = 0;
}
system("pause");
}