#define _CRT_SECURE_NO_WARNINGS
//#include <stdio.h>
#include "game.h"
void menu()
{
printf("--------------\n");
printf("--1.开始游戏--\n");
printf("--0.退出游戏--\n");
printf("--------------\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布置好的雷的信息
char show[ROWS][COLS] = { 0 };//存放排查出的的雷的信息
//mine数组没布置时都是'0'
InitBoard(mine, ROWS, COLS,'0');
//show数组没布置时都是''
InitBoard(show, ROWS, COLS,'');
//DisplayBoard(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//设置雷
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);
//排查雷
Findmine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
scanf("%d", &input);
switch(input)
{
case 1:game(); break;
case 0:printf("游戏结束"); break;
default:printf("输入错误请重新选择\n"); break;
}
} while (input);
}
源.c(上面的)
game.c(下面的)
#define _CRT_SECURE_NO_WARNINGS
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char ret)
{
int i, j = 0;
for(i=0;i<rows;i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = ret;
}
printf("\n");
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i, j = 0;
printf("-------扫雷-------\n");
printf("0 ");
for (int n = 0; n < col; n++)
printf("%d ", n + 1);
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <=col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
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 get_mine_count(char mine[ROWS][COLS], int x, int y) // 注意'1'-'0'=0
{
/return mine[x - 1][y - 1] + mine[x - 1][y] +
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';/
int i, j,n = 0;
for(i=-1;i<=1;i++)
{
for (j = -1; j <= 1; j++)
{
if (mine[x + i][y + j]'1')
n++;
}
}
return n;
}
int get_show_count(char show[ROWS][COLS], int row, int col)
{
int i, j ,count1= 1;
for (i = 1; i <= row; i++)
{
for (j = 1; j <= col; j++)
{
if (show[i][j] == '*')
count1++;
}
}
return count1;
}
int Expand(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y)
{
int i, j, n = 0;
for (i = -1; i <= 1; i++)
{
for (j = -1; j <= 1; j++)
{
if(y+j>0&&y+j<=COL&&x+i<=ROW&&x+i>0&&show[x+i][y+j]'')
{
show[x + i][y + j] = get_mine_count(mine, x + i, y + j) + '0';
if (show[x + i][y + j] == '0')
return Expand(show, mine, x + i, y + j);//还应防止死递归
}
}
}
}
void Findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x, y,count/win/= 0;
while(get_show_count(show,row,col)> EASY_COUNT)
{
printf("请输入坐标-> ");
scanf("%d %d", &x, &y);
if (x > 0 && x <= row && y > 0 && y <= col)
{
if (show[x][y] != '')
{
printf("被排查过了,重新选择");
continue; //跳出本次循环
}
if (mine[x][y] == '1')
{
printf("很遗憾,你被炸死了");
DisplayBoard(mine, ROW, COL);
break;
}
else //(board[x][y] == '0')
{
/win++;/
count = get_mine_count(mine, x, y);
if (count == 0)
{
Expand(show,mine, x, y);
}
show[x][y] = count+'0'; //切记存放的必须是字符
DisplayBoard(show, ROW, COL);
}
}
else
printf("输入错误,请重新输入:");
}
//if (get_show_count=EASY_COUNT) //运行至此已满足本条件
printf("恭喜你,排雷成功");
}
game.h
#pragma once
#include <stdio.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
#include<stdlib.h>
#include<time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols,char ret);
void DisplayBoard(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 Expand(char show[ROWS][COLS], char mine[ROWS][COLS], int x, int y);
int get_mine_count(char mine[ROWS][COLS], int x, int y); // 注意'1'-'0'=0
int get_show_count(char show[ROWS][COLS], int row, int col);
//标记功能
//展开一片(坐标不是雷,周围没有雷,没被排查过防止死递归)-->已完成