要求:
(1)第一次下子,不炸死。
(2)坐标周围没雷时,可以实现展开。
game.h
#ifndef __GAME_H__
#define __GAME_H__
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define _CRT_SECURE_NO_WARNINGS
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 50
void Mine(char mine[ROWS][COLS],int row,int col);
void Show(char show[ROWS][COLS],int row,int col);
void Board(char board[ROWS][COLS],int rows,int cols, char set);
void Display(char board[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int row, int col);
char GetMineCount(char mine[ROWS][COLS], int x, int y);
void Sweep(char mine[ROWS][COLS],char show[ROWS][COLS]);
void Rmove(char mine[ROWS][COLS],int x,int y);
void Expand(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y);
#endif//__GAME_H__
game.c
#include "game.h"
void Mine(char mine[ROWS][COLS],int row,int col){
int i = 0;
int j = 0;
char set;
scanf("%c",&set);*/
for(i=0;i<row;i++){
for(j=0;j<col;j++){
mine[i][j]='0';
}
}
}
void Show(char show[ROWS][COLS],int row,int col){
int i = 0;
int j = 0;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
show[i][j]='*';
}
}
}
void Board(char board[ROWS][COLS],int rows,int cols, char set){
int i = 0;
int j = 0;
for(i=0;i<rows;i++){
for(j=0;j<cols;j++){
board[i][j]=set;
}
}
}
void Display(char board[ROWS][COLS], int row, int col){
int i = 0;
int j = 0;
printf(" ");
for(i=1;i<row-1;i++){
printf(" %d",i);
}
printf("\n");
for(i=1;i<row-1;i++){
printf("%2d ",i);
for(j=1;j<col-1;j++){
printf("%c ",board[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col){
int x = 0;
int y = 0;
int minecount = COUNT;
srand((unsigned)time(NULL));
while(minecount){
x = rand()%(ROW)+1;
y = rand()%(COL)+1;
if(mine[x][y] == '0'){
mine[x][y] = '1';
minecount--;
}
}
}
char GetMineCount(char mine[ROWS][COLS], int x, int y){
int minecount = 0;
if(mine[x-1][y] == '1')
minecount++;
if(mine[x-1][y+1] =='1')
minecount++;
if(mine[x-1][y+1] == '1')
minecount++;
if(mine[x][y+1] == '1')
minecount++;
if(mine[x+1][y+1] == '1')
minecount++;
if(mine[x+1][y] == '1')
minecount++;
if(mine[x+1][y-1] == '1')
minecount++;
if(mine[x][y-1] == '1')
minecount++;
if(mine[x-1][y-1] == '1')
minecount++;
return minecount;
}
void Sweep(char mine[ROWS][COLS],char show[ROWS][COLS]){
int x = 0;
int y = 0;
int clear = 0;
int minecount = 0;
while(clear < (ROW)*(COL)-EASY_COUNT){
printf("开始排雷,请输入坐标:\n");
scanf("%d%d",&x,&y);
if(mine[x][y] == '1'){
if(clear == 0){
Rmove(mine,x,y);
Display(mine,ROWS,COLS);
printf("\n");
minecount = GetMineCount(mine,x,y);
if(minecount == 0){
show[x][y] = ' ';
clear++;
Expand(mine,show,x,y);
Display(show,ROWS,COLS);
}
else{
show[x][y] = minecount + '0';
Display(show,ROWS,COLS);
}
}
else{
printf("很遗憾,你被炸死了!\n");
Display(mine,ROWS,COLS);
break;
}
}
else if(mine[x][y] != '1'){
minecount = GetMineCount(mine,x,y);
if(minecount == 0){
show[x][y] = ' ';
}
else{
show[x][y] = minecount + '0';
}
clear++;
Expand(mine,show,x,y);
Display(show,ROWS,COLS);
}
if(clear == (ROW)*(COL)-COUNT){
printf("扫雷成功\n");
}
}
}
void Rmove(char mine[ROWS][COLS],int x,int y)
{
mine[x][y] = '0';
GetMineCount(mine,x,y);
while(1){
int x1 = rand()%(ROW)+1;
int y1 = rand()%(COL)+1;
if(mine[x1][y1] != '1' && ((x1!=x) && (y1!=y))){
mine[x1][y1] = '1';
break;
}
}
}
void Expand(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y){
if((x>=1)&&(y>=1)&&(x<=ROW)&&(y<=COL)){
if(GetMineCount(mine,x,y) == 0){
show[x][y] = ' ';
if(show[x-1][y-1] == '*'){
Expand(mine,show,x-1,y-1);
}
if(show[x-1][y] == '*'){
Expand(mine,show,x-1,y);
}
if(show[x-1][y+1] == '*'){
Expand(mine,show,x-1,y+1);
}
if(show[x][y+1] == '*'){
Expand(mine,show,x,y+1);
}
if(show[x+1][y+1] == '*'){
Expand(mine,show,x+1,y+1);
}
if(show[x+1][y] == '*'){
Expand(mine,show,x+1,y);
}
if(show[x+1][y-1] == '*'){
Expand(mine,show,x+1,y-1);
}
if(show[x][y-1] == '*') {
Expand(mine,show,x,y-1);
}
}
}
}
test.c
#include "game.h"
void menu(){
printf("*****************************\n");
printf("**** 欢迎进入扫雷界面 ****\n");
printf("*****************************\n");
printf("**** 1 play 0 exit ****\n");
printf("*****************************\n");
}
void game(){
int x = 0;
int y = 0;
int count = 0;
char mine[ROWS][COLS]={0};
char show[ROWS][COLS]={0};
Board(show,ROWS,COLS,'*');
Display(show,ROWS,COLS);
printf("\n");
Board(mine,ROWS,COLS,'0');
Display(mine,ROWS,COLS);
SetMine(mine,ROW,COL);
DisplayBoard(mine,ROWS,COLS);
printf("\n");
Sweep(mine,show);
}
void test(){
int input = 0;
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()
{
test();
system("pause");
return 0;
}