做一个简单的小游戏(愉悦一下身心)
思路:
1.两个字符数组,一个用以存原图的雷数据。一个用以扫后展示的数据。
2.dfs来扫过多的冗余状态。
3.还有计时功能和插flag功能要完善。目前判win是通过剩下的不能填的都是雷来判定的。
#pragma once
#include "game.h"
int vis[N][M];///保存访问信息
int dx[8] = { -1,-1,0,1,1,1,0,-1 };
int dy[8] = { 0,1,1,1,0,-1,-1,-1 };
void menu(){
printf("************** 1.play ****************\n");
printf("****************************************\n");
printf("************** 2.exit ****************\n");
printf("****************************************\n");
}
void play(int op) {
int n = 9; int m = 9; int mine = 20;
char ma1[N][M];///原图
char ma2[N][M];///输出信息图
init(ma1, n, m, '0');
init(ma2, n, m, '*');
arrangemine(ma1, n, m , mine);
display(ma1,ma2,n,m,op);
int result=modify(ma1,ma2, n, m , mine ,op);
if (result == 0)
{
printf("扫雷失败!!!\n\n");
}
else {
printf("扫雷成功!!!\n\n");
}
return;
}
void init(char str[N][M], int n, int m,char op)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
str[i][j] = op;
}
}
}
void display(char str1[N][M], char str2[N][M], int n, int m,int op)
{
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (i == 0) printf("%c ",'0'+j);
else if (j == 0) printf("%c ", '0'+i);
else printf("%c ", str2[i][j]);
}
printf("\n");
}printf("\n");
if (0 == op)
{
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
if (i == 0) printf("%c ", '0' + j);
else if (j == 0) printf("%c ", '0' + i);
else printf("%c ", str1[i][j]);
}
printf("\n");
}printf("\n");
}
}
void arrangemine(char str[N][M], int n, int m , int mine) {
srand((unsigned)time(NULL));
while (mine)
{
int x = rand() % 9 + 1;
int y = rand() % 9 + 1;
if (str[x][y] != '1')
{
str[x][y] = '1';
mine--;
}
}
}
int modify(char str1[N][M], char str2[N][M], int n, int m, int mine, int op){
while (1)
{
int x, y;
printf("输入你的坐标:\n\n");
while ( ~scanf_s("%d%d",&x,&y) )
{
if (x<1 || x>n || y<1 || y>m || str2[x][y] != '*' )
{
printf("输入不合法\n\n");
}
else
{
if (str1[x][y] == '1') return 0;
int cnt = 0;
for (int k = 0; k < 8; k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx<1 || nx>n || ny<1 || ny>m) continue;
if (str1[nx][ny] == '1') cnt++;
}
str2[x][y] = '0'+cnt;
if( 0==cnt ) cal( str1 ,str2, n , m , x , y ); ///dfs一并修改周围的8个没炸弹的
if (check(str1, str2, n, m, mine) == true) return 1;
}
display(str1,str2, n, m,op);
printf("输入你的坐标:\n\n");
}
}
return 1;
}
void cal(char str1[N][M], char str2[N][M], int n, int m, int x, int y) {
int cnt = 0;
for (int k = 0; k < 8; k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (nx<1 || nx>n || ny<1 || ny>m) continue;
if (str1[nx][ny] == '1') cnt++;
}
if (cnt == 0)
{
str2[x][y] = '0';
vis[x][y] = true;
}
else return;
for (int k = 0; k < 8; k++)
{
int nx = x + dx[k];
int ny = y + dy[k];
if (vis[nx][ny] || nx<1 || nx>n || ny<1 || ny>m) continue;
cal(str1, str2, n, m, nx, ny);
}
}
bool check(char str1[N][M],char str2[N][M], int n, int m ,int std)
{
int cnt = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (str2[i][j] == '*') cnt++;
}
}
if (cnt == std) return true;
else return false;
}
#pragma once
#include<stdio.h>
#include<random>
#include<time.h>
#include<stdlib.h>
#define N 20
#define M 20
void menu();
void play(int op);
void init(char str[N][M], int n, int m,char op);
void display(char str1[N][M],char str2[N][M], int n, int m,int op);
void arrangemine(char str[N][M], int n, int m , int mine);
int modify(char str1[N][M],char str2[N][M], int n, int m ,int wins,int op);
void cal(char str[N][M],char str2[N][M] , int n, int m, int x, int y);
bool check(char str1[N][M],char str2[N][M], int n, int m, int std);
#pragma once
#include "game.h"
int main()
{
menu();
printf("请输入:\n");
int input;
while ( scanf_s("%d", &input) != EOF)
{
bool flag = 1;
switch (input)
{
case 1:
printf("选择上帝模式(0)or正常模式(1)\n");
int input2;
while (scanf_s("%d", &input2) != EOF) {
bool ok = 1;
switch (input2)
{
case 0:
ok = 0;
play(0);
break;
case 1:
ok = 0;
play(1);
break;
default:
printf("输入不合法\n");
break;
}
if (!ok) break;
}
break;
case 2:
flag = 0;
break;
default:
printf("输入不合法\n");
break;
}
if (!flag)
{
break;
}
printf("请输入:\n");
menu();
}
}
|