扫雷游戏递归出现问题,请大神指教!

Quinn0918 2017-04-10 02:32:50
求大神指教递归那到底哪里出错了!showblank函数那

game.h头文件
#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 11
#define COLS 11

#define MAX 10

#define ROW (ROWS-2)
#define COL (COLS-2)

void init_board(char mine[ROWS][COLS],char set);
void set_mine(char mine[ROWS][COLS]);
void display(char mine[ROWS][COLS]);
int get_mine_count(char mine[ROWS][COLS], int row, int col);
void showblank(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#endif//__GAME_H__




game.c
#include"game.h"  
void init_board(char mine[ROWS][COLS], char set)
{
memset(mine, set, ROWS*COLS*sizeof(mine[0][0]));
}
void set_mine(char mine[ROWS][COLS])
{
int count = MAX;
while (count > 0)
{
int x = rand() % ROW + 1;
int y = rand() % COL + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}

}
void display(char mine[ROWS][COLS])
{
int i = 0;
int j = 0;
printf(" ");
for (i = 1; i <= ROW; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= ROW; i++)
{
printf("%d ",i);
for (j = 1; j <= COL; j++)
{
printf("%c ",mine[i][j]);
}
printf("\n");
}
}
int get_mine_count(char mine[ROWS][COLS],int x,int y)
{
return mine[x - 1][y - 1] + mine[x][y - 1] +
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] - 8 * '0';
}
void showblank( char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
int i = 0;
int j = 0;
int set = 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';
show[x][y] = set+'0';
if (show[x][y] == 0)
{
if (x <= ROW&&x >= 1 && y <= COL&&y >= 1 && '1' == show[x][y])
{
showblank(mine, show, x - 1, y - 1);
showblank(mine, show, x - 1, y);
showblank(mine, show, x - 1, y + 1);
showblank(mine, show, x, y - 1);
showblank(mine, show, x, y + 1);
showblank(mine, show, x + 1, y - 1);
showblank(mine, show, x + 1, y);
showblank(mine, show, x + 1, y + 1);
}
}
}




test.c
#include "game.h"
#include <stdio.h>
#pragma warning (disable:4996)
void menu()
{
printf("*********************************\n");
printf("**********1.play 0.exit*********\n");
printf("*********************************\n");
}

void game()
{
char mine[ROWS][COLS] = { 0 };
char show[ROWS][COLS] = { 0 };
init_board(mine,'0');
init_board(show, '*');
set_mine(mine);
display(show);
int x = 0;
int y = 0;
int count = 0;
while (count < ROW*COL - MAX)
{
printf("你认为哪颗不是雷:\n");
scanf("%d%d", &x, &y);
if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
{
if (mine[x][y] == '1')
{
printf("炸死了!\n");
display(mine);
break;
}

else
{
int set = get_mine_count(mine, x, y);
show[x][y] = set + '0';
showblank(mine, show, x, y);
count++;
display(show);
}
}
else
{
printf("坐标有误,请重新输入!\n");
}
}
if (count == ROW * COL - MAX)
{
printf("排雷完毕,危险解除\n");
display(mine);
}
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择开始还是结束:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
case 0:
break;
default:
printf("选择有误,请重新选择\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}



...全文
231 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
叶恭介叶恭介 2017-04-10
  • 打赏
  • 举报
回复
问题一堆,帮你改成这样了,试试还行,楼主看看吧

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define ROWS 11
#define COLS 11

#define MAX 10

#define ROW (ROWS-2)
#define COL (COLS-2)

void digui(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y);

void init_board(char mine[ROWS][COLS], char set)
{
	memset(mine, set, ROWS*COLS*sizeof(mine[0][0]));
}
void set_mine(char mine[ROWS][COLS])
{
	int count = MAX;
	while (count > 0)
	{
		int x = rand() % ROW;
		int y = rand() % COL;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}

}
void display(char mine[ROWS][COLS])
{
	int i = 0;
	int j = 0;
	printf("  ");
	for (i = 1; i <= ROW; i++)
	{
		printf("%d ", i);
	}
	printf("\n");
	for (i = 1; i <= ROW; i++)
	{
		printf("%d ",i);
		for (j = 1; j <= COL; j++)
		{
			printf("%c ",mine[i - 1][j-1]);
		}
		printf("\n");
	}
}
int get_mine_count(char mine[ROWS][COLS],int x,int y)
{
	int nCount = 0;

	////////////////////////////////////////
	if (((x - 1) >= 0) && ((y - 1) >= 0))
	{
		if (mine[x-1][y - 1] == '1')
		{
			nCount++;
		}
	}

	if ((x - 1) >= 0)
	{
		if (mine[x-1][y] == '1')
		{
			nCount++;
		}
	}

	if (((x - 1) >= 0) && ((y + 1) < COL))
	{
		if (mine[x-1][y+1] == '1')
		{
			nCount++;
		}
	}
	////////////////////////////////////////
	if (((y - 1) >= 0))
	{
		if (mine[x][y - 1] == '1')
		{
			nCount++;
		}
	}


	if (((y + 1) < COL))
	{
		if (mine[x][y+1] == '1')
		{
			nCount++;
		}
	}
	////////////////////////////////////////
	////////////////////////////////////////
	if (((x + 1) < ROWS) && ((y - 1) >= 0))
	{
		if (mine[x+1][y - 1] == '1')
		{
			nCount++;
		}
	}

	if (((x + 1) < ROWS))
	{
		if (mine[x+1][y] == '1')
		{
			nCount++;
		}
	}

	if (((x + 1) < ROWS) && ((y + 1) < COL))
	{
		if (mine[x+1][y+1] == '1')
		{
			nCount++;
		}
	}

	return nCount;
}

void showblank( char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
	if (x < ROW&&x >= 0 && y < COL&&y >= 0 && '0' == mine[x][y] && '*' == show[x][y])
	{
		int set = get_mine_count(mine, x, y);
		show[x][y] = set + '0';
		if (set == 0)
		{
			digui(mine, show, x, y);
		}
	}
}

void digui(char mine[ROWS][COLS],char show[ROWS][COLS], int x, int y)
{
	showblank(mine, show, x - 1, y - 1);
	showblank(mine, show, x - 1, y);
	showblank(mine, show, x - 1, y + 1);
	showblank(mine, show, x, y - 1);
	showblank(mine, show, x, y + 1);
	showblank(mine, show, x + 1, y - 1);
	showblank(mine, show, x + 1, y);
	showblank(mine, show, x + 1, y + 1);
}

void menu()
{
	printf("*********************************\n");
	printf("**********1.play  0.exit*********\n");
	printf("*********************************\n");
}

bool isEnd(char show[ROWS][COLS])
{
	int nCount = 0;

	for (int i = 0; i < ROW; i++)
	{
		for (int j = 0; j < COL; j++)
		{
			if (show[i][j] == '*')
			{
				nCount++;
			}
		}
	}

	if (nCount == MAX)
	{
		return true;
	}
	else
	{
		return false;
	}
}

void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	init_board(mine,'0');
	init_board(show, '*');
	set_mine(mine);
	display(show);
	int x = 0;
	int y = 0;
	int count = 0;
	while (!isEnd(show))
	{
		printf("你认为哪颗不是雷:\n");
		scanf("%d%d", &x, &y);
		if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
		{
			x = x - 1;
			y = y - 1;

			if (mine[x][y] == '1')
			{
				printf("炸死了!\n");
				display(mine);
				return;
			}

			else
			{
				int set = get_mine_count(mine, x, y);
				show[x][y] = set + '0';
				digui(mine, show, x, y);
				count++;
				display(show);
			}
		}
		else
		{
			printf("坐标有误,请重新输入!\n");
		}
	}

	printf("排雷完毕,危险解除\n");
	display(mine);
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择开始还是结束:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
		case 0:
			break;
		default:
			printf("选择有误,请重新选择\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	return 0;
}

改吐了
赵4老师 2017-04-10
  • 打赏
  • 举报
回复
仅供参考:
        void step0(int xx,int yy) {
            byte n;
            if (A[xx+yy*10]!=4) return;
            n=0;
            if (xx>=1 && yy>=1) n+=B[(xx-1)+(yy-1)*10];
            if (         yy>=1) n+=B[(xx  )+(yy-1)*10];
            if (xx<=8 && yy>=1) n+=B[(xx+1)+(yy-1)*10];
            if (xx>=1         ) n+=B[(xx-1)+(yy  )*10];
            if (xx<=8         ) n+=B[(xx+1)+(yy  )*10];
            if (xx>=1 && yy<=8) n+=B[(xx-1)+(yy+1)*10];
            if (         yy<=8) n+=B[(xx  )+(yy+1)*10];
            if (xx<=8 && yy<=8) n+=B[(xx+1)+(yy+1)*10];
            A[xx+yy*10]=(byte)(n+5);
            if (n==0) {
                if (xx>=1 && yy>=1) step0(xx-1,yy-1);
                if (         yy>=1) step0(xx  ,yy-1);
                if (xx<=8 && yy>=1) step0(xx+1,yy-1);
                if (xx>=1         ) step0(xx-1,yy  );
                if (xx<=8         ) step0(xx+1,yy  );
                if (xx>=1 && yy<=8) step0(xx-1,yy+1);
                if (         yy<=8) step0(xx  ,yy+1);
                if (xx<=8 && yy<=8) step0(xx+1,yy+1);
            }
        }

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧