当前位置: 代码迷 >> 综合 >> New Code day9 -扫雷
  详细解决方案

New Code day9 -扫雷

热度:26   发布时间:2023-12-12 01:34:10.0

扫雷游戏的实现三部分

~尝鲜版

版本优点

1: 可以大片展开

2: 第一次选择位置一定不被炸死,给运气差玩家留点面子

3:每次清屏,干净整洁大气

扫雷游戏头文件game.h

//game.h#ifndef __GAME_H__
#define __GAME_H__#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>void InitBoard(char arr[ROWS][COLS],int row,int col,char set);
void DisplayBoard(char arr[ROWS][COLS],int row,int col);
void SetMine(char arr[ROWS][COLS],int row,int col,int diff);
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col,int diff);
int GetMineCount(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y);
int MuchMine(char mine[ROWS][COLS],int x,int y);
void Ass(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y,int *Num);
int IsWin(char show[ROWS][COLS],int row,int col,int diff);#endif //__GAME_H__

功能函数实现文件game.c

//game.c
#include "game.h"void InitBoard(char arr[ROWS][COLS],int row,int col,char set)
{int i = 0;int j = 0;for(i=0; i<row; i++){for(j=0; j<col; j++){arr[i][j] = set;}}//memset(arr,set,ROWS*COLS*sizeof(arr[0][0]));
}
void DisplayBoard(char arr[ROWS][COLS],int row,int col)
{int i = 0;int j = 0;system("CLS");printf("       扫雷        \n");printf("-------------------\n");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 ",arr[i][j]);}printf("\n");}
}void SetMine(char arr[ROWS][COLS],int row,int col,int diff)
{int x = 0;int y = 0;while(diff){x = rand()%row+1;y = rand()%col+1;if(arr[x][y] == '0'){arr[x][y] = 1+'0';diff--;}}
}void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row,int col,int diff)
{int x = 0;int y = 0;int safenum = 0;do	{printf("请输入要排除的位置>:");scanf("%d%d",&x,&y);if (x>=1 && x<= row && y>=1  && y<= col){if (mine[x][y] == '1'){if (!safenum)//是否是第一次扫雷{mine[x][y] = '0';SetMine(mine, ROW, COL, 1);Ass(mine, show, x, y, &safenum);//如果第一次扫到雷,避免第一次被炸死,将1变成0,并且将一个雷重新随机生成到mine里边去,让雷的数量不变。DisplayBoard(show, ROW, COL);}else{printf("很抱歉,你被炸死了!\n");DisplayBoard(mine, ROW, COL);return;}}else{Ass(mine, show, x, y, &safenum);DisplayBoard(show, ROW, COL);}}elseprintf("请重新输入>:");}while(IsWin(show,row,col,diff)-diff);printf("你赢了!恭喜!!\n");
}
int IsWin(char show[ROWS][COLS],int row,int col,int diff)
{int i = 0;int j = 0;int sum = 0;for(i=1; i<=row; i++){for(j=1; j<=col; j++){if(show[i][j] == '*')sum++;}}return sum;
}
int MuchMine(char mine[ROWS][COLS],int x,int y)
{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';
}
void Ass(char mine[ROWS][COLS],char show[ROWS][COLS],int x,int y,int *Num)
{int i = 0;	int j = 0;int AroundCount = 0;if(x>0 && x<=ROW && y>0 && y<=COL){if (!MuchMine(mine,x,y)){(*Num)++;show[x][y] = '0';for (i = -1; i <= 1; i++){for (j = -1; j <= 1; j++){if (x + i >= 1 && x + i <= ROW && y + j >= 1 && y + j <= COL){if (i != 0 || j != 0){if(show[x+i][y+j] != '0')Ass(mine, show, x + i, y + j,Num);//对此坐标周围的八个坐标依次判断是否为0,只要有0,继续在相应的坐标判断并且扫雷,直到没有坐标等于0为止。}                                               //这也是递归的思想。}}}}else{(*Num)++;show[x][y] = MuchMine(mine,x,y) + '0';}}
}

测试函数test.c

//test.c#include "game.h"void menu()
{printf("----------------------- \n");printf("|      扫雷小游戏     |\n");printf("-----------------------\n");printf("|       1.play        |\n");printf("|       0.exit        |\n");printf("-----------------------\n");}void menu2()
{printf("------------------\n");printf("|    1. 初级     |\n");printf("|    2. 中级     |\n");printf("|    3. 噩梦     |\n");printf("------------------\n");
}void game()
{int input2 = 0;int diff = 0;//创建棋盘,雷盘char mine[ROWS][COLS] ={0};char show[ROWS][COLS] ={0};InitBoard(mine,ROWS,COLS,'0');InitBoard(show,ROWS,COLS,'*');/*DisplayBoard(mine,ROW,COL);*/DisplayBoard(show,ROW,COL);menu2();printf("请选择难度>:");scanf("%d",&input2);switch(input2){case 1 :diff = ROW*COL/ROW;break;case 2 :diff = ROW*COL/4;break;case 3 :diff = ROW*COL/2;break;default:printf("选择错误,默认为初级难度\n");diff = ROW*COL/ROW;break;}SetMine(mine,ROW,COL,diff);FindMine(mine,show,ROW,COL,diff);
}
int main()
{int input = 0;srand((unsigned int)time(NULL));do{menu();printf("请选择:>");scanf("%d",&input);switch(input){case 1:game();break;case 0:printf("退出游戏!\n");break;default:printf("你选错了,重输!\n");break;}}while(input);return 0;
}

 

  相关解决方案