#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
char show[ROWS][COLS];
char mine[ROWS][COLS];
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu();
void test();
void game();
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
void DispalyBoard(char board[ROWS][COLS], int row, int col);
void SetMine(char board[ROWS][COLS], int row, int col);
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
int get_mine_count(char mine[ROWS][COLS], int x, int y);
void KuoZhan(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int syzds();
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) {
int i = 0, j = 0;
for(i = 0; i < rows ; i++) {
for(j = 0; j < cols ; j++) {
board[i][j] = set;
}
}
}
void DispalyBoard(char board[ROWS][COLS], int row, int col) {
int i = 0, j = 0;
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 ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char board[ROWS][COLS], int row, int col) {
int count = EASY_COUNT;
while(count) {
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if(board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
}
int syzds() {
int zd = 0;
int i = 0, j = 0;
for(i = 1; i <= ROW; i++)
{
for(j = 1; j <= COL; j++)
{
if(show[i][j] == '*')
{
zd++;
}
}
}
return zd;
}
void KuoZhan(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y) {
int ret;
ret = get_mine_count(mine, x, y);
if(ret == 0) {
show[x][y] = ' ';
if((x-1) > 0 && (y-1) > 0 && (show[x-1][y-1] == '*')) {
KuoZhan(mine, show, x-1, y-1);
}
if((x-1) > 0 && (y) > 0 && (show[x-1][y] == '*')) {
KuoZhan(mine, show, x-1, y);
}
if((x-1) > 0 && (y+1) > 0 && (y+1) <= COL && (show[x-1][y+1] == '*')) {
KuoZhan(mine, show, x-1, y+1);
}
if((x) > 0 && (y-1) > 0 && (show[x][y-1] == '*')) {
KuoZhan(mine, show, x, y-1);
}
if((x) > 0 && (y+1) > 0 && (y+1) <= COL && (show[x][y+1] == '*')) {
KuoZhan(mine, show, x, y+1);
}
if((x+1) > 0 && (y-1) > 0 && (x+1) <= ROW && (show[x+1][y-1] == '*')) {
KuoZhan(mine, show, x+1, y-1);
}
if((x+1) > 0 && (y) > 0 && (x+1) <= ROW && (show[x+1][y] == '*')) {
KuoZhan(mine, show, x+1, y);
}
if((x+1) > 0 && (y+1) > 0 && (x+1) <= ROW && (y+1) <= COL && (show[x+1][y+1] == '*')) {
KuoZhan(mine, show, x+1, y+1);
}
} else {
show[x][y] = ret + '0';
}
}
int get_mine_count(char mine[ROWS][COLS], int x, int y) {
return mine[x - 1][y]+
mine[x - 1][y - 1]+
mine[x - 1][y + 1]+
mine[x][y - 1]+
mine[x][y + 1]+
mine[x + 1][y - 1]+
mine[x + 1][y]+
mine[x + 1][y + 1] - 8*'0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) {
int x = 0, y = 0;
int win = 0;
while(1)
{
if(win == EASY_COUNT)
{
printf("掃雷成功\n");
break;
}
else
{
printf("請輸入座標:");
scanf("%d%d", &x, &y);
printf("\n-------------------\n");
if(x >= 1 && x <= ROW && y >= 1 && y <= COL)
{
if(mine[x][y] == '1')
{
printf("很遺憾,您踩雷了.雷區情況如下:\n");
DispalyBoard(mine, ROW, COL);
break;
}
else
{
int count = get_mine_count(mine, x, y);
show[x][y] = count + '0';
KuoZhan(mine,show,x,y);
DispalyBoard(show, ROW, COL);
win = syzds();
printf("-------------------\n");
}
}
else
{
printf("輸入座標錯誤,請重新輸入:\n");
}
}
}
}
void game() {
InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
printf("=====================\n");
DispalyBoard(show, ROW, COL);
SetMine(mine, ROW, COL);
FindMine(mine, show, ROW, COL);
}
void menu()
{
printf("************************************\n");
printf("************** 1.開始 **************\n");
printf("************** 0.退出 **************\n");
printf("************************************\n");
}
void test()
{
int input = 0;
srand((unsigned int )time(NULL));
do
{
menu();
printf("\n選擇:>");
scanf("%d",&input);
switch(input)
{
case 1:
game();
break;
case 0:
printf("退出遊戲\n");
break;
default:
printf("選擇錯誤,請重新選擇:\n");
break;
}
}while(input);
}
int main (void)
{
test();
return 0;
}