小游戏扫雷

game.h
#define _CRT_SECURE_NO_WARNINGS 1
#ifndef __GAME_H__  
#define __GAME_H__  

#define COLS 11  
#define ROWS 11  

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

#define MAX 14  

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


void init_board(char mine[ROWS][COLS], char set, int row, int col);
void set_mine(char mine[ROWS][COLS]);
void display(char mine[ROWS][COLS], int row, int col);
void finput(char mine[ROWS][COLS], int x, int y);            //first input  
void search_road(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int mine_count(char mine[ROWS][COLS], int x, int y);
int check_win(char show[ROWS][COLS]);

#endif//__GAME_H__  

game.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"  
void  init_board(char mine[ROWS][COLS], char set, int row, int col)//chushihua  
{
	memset(mine, '0', row*col*sizeof(mine[0][0]));
}
void set_mine(char mine[ROWS][COLS])
{
	int x, y;
	int count = 0;
	do{
		x = rand() % 9 + 1;
		y = rand() % 9 + 1;
		if (mine[x][y] != '1')
		{
			mine[x][y] = '1';
			count++;
		}

	} while (count<MAX);

}
void display(char board[ROWS][COLS], int row, int col)//打印棋盘  
{
	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("---");
	}
	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 finput(char mine[ROWS][COLS], int x, int y)
{
	int a, b;
	if (mine[x][y] == '1')//如果第一次踩到雷  不炸  替换掉  
	{
		mine[x][y] = '0';
		//printf("替换\n");  
		// display(mine,ROWS,COLS);  
		while (1)
		{
			a = rand() % 9 + 1;
			b = rand() % 9 + 1;
			if (mine[a][b] != '1')
			{
				mine[a][b] = '1';
				//printf("转移\n");  
				//display(mine,ROWS,COLS);  
				break;
			}
		}
	}
}
void search_road(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{

	if (0 == mine_count(mine, x, y))
	{
		show[x][y] = ' ';//如何实现调用自己  
		if ((x - 1)>0 && (y - 1)>0 && show[x - 1][y - 1] == '*')               //x-1,y-1  
			search_road(mine, show, x - 1, y - 1);

		if ((y - 1)>0 && show[x][y - 1] == '*')                          //x,y-1  
			search_road(mine, show, x - 1, y - 1);

		if ((x + 1) <= ROW && (y - 1)>0 && show[x + 1][y - 1] == '*')            //x+1,y-1  
			search_road(mine, show, x + 1, y - 1);

		if ((x + 1) <= ROW&&show[x + 1][y] == '*')                        //x+1,y  
			search_road(mine, show, x + 1, y);

		if ((x + 1) <= ROW && (y + 1) <= COL&&show[x + 1][y + 1] == '*')           //x+1,y+1  
			search_road(mine, show, x + 1, y + 1);

		if ((y + 1) <= COL&&show[x][y + 1] == '*')                        //x,y+1  
			search_road(mine, show, x, y + 1);

		if ((x - 1)>0 && (y + 1) <= COL&&show[x - 1][y + 1] == '*')             //x-1,y+1  
			search_road(mine, show, x - 1, y + 1);

		if ((x - 1)>0 && show[x - 1][y] == '*')                          //x-1,y  
			search_road(mine, show, x - 1, y);

	}
	else
		show[x][y] = mine_count(mine, x, y) + '0';
}
int mine_count(char mine[ROWS][COLS], int x, int y)
{
	int rcount;   //rand mine count  
	rcount = 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 - 1][y + 1]
		+ mine[x - 1][y] - 8 * 48;                         //返回值问题  
	//printf("%d\n",rcount);  
	return rcount;


}
int check_win(char show[ROWS][COLS])
{
	int i, j, c_count = 0;
	for (i = 1; i <= ROW; i++)
	{
		for (j = 1; j <= COL; j++)
		{
			if (show[i][j] != '*')
				c_count++;
		}

	}
	return c_count;

}
#define _CRT_SECURE_NO_DEPRECATE 1  
#include<stdio.h>  
#include<stdlib.h>  
#include"game.h"  
void menu()
{
	printf("*******************************\n");
	printf("*******************************\n");
	printf("********1.PLAY  0.EXIT*********\n");
	printf("            请选择             \n");
}
void game()
{

	char mine[ROWS][COLS];
	char show[ROWS][COLS];
	int x, y;
	int icount = 0;         //input count  
	srand((unsigned int)time(NULL));
	memset(mine, '0', ROWS*COLS*sizeof(mine[0][0]));
	memset(show, '*', ROWS*COLS*sizeof(show[0][0]));
	set_mine(mine);
	display(mine, ROWS, COLS);
	display(show, ROWS, COLS);
	while (1)
	{
		printf("请输入扫雷坐标:");                 //输入坐标  
		scanf("%d%d", &x, &y);
		if (((x >= 1) && (x <= ROW)) && ((y >= 1) && (y <= COL)))//chongfushurukongbai  
		{
			icount++;
			if (icount == 1)                           //第一次输入  遇到炸弹自动转移炸弹。再展开  
			{
				if (mine[x][y] == 1)
				{
					finput(mine, x, y);
				}
				show[x][y] = mine_count(mine, x, y) + '0';
				search_road(mine, show, x, y);
				display(show, ROWS, COLS);
			}
			else
			{                                 //不满足第一次输入  
				if (mine[x][y] == '1')                   //遇到炸弹    
				{
					printf("boom!!!\n");
					display(mine, ROWS, COLS);
					break;
				}
				else
				{
					show[x][y] = mine_count(mine, x, y) + '0';
					search_road(mine, show, x, y);
					display(show, ROWS, COLS);
				}
			}
		}

		else
			printf("坐标有误请重新输入\n");
		if ((ROW*COL - MAX) == check_win(show))
		{
			printf("恭喜你扫雷成功\n");
			break;
		}
	}
}
enum {
	EXIT,
	PLAY
};
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#include <string.h>  
int main()
{

	int input;
	do{
		menu();
		scanf("%d", &input);
		switch (input)
		{
		case 1:game(); break;
		case 0:                                  break;
		default:printf("输入有误,请重新选择"); break;
		}
	} while (input != 0);
	system("pause");
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值