扫雷

game.h
#ifndef __GAME_H__
#define __GAME_H__

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

#define ROWS  6
#define COLS  6
#define num   4  //雷数

void game();
void set_mine(int arr[ROWS][COLS], int row, int col);
void show_set(int arr[ROWS][COLS], int ar[ROWS][COLS], int row, int col);
int find_mine(int arr[ROWS][COLS],int ar[ROWS][COLS], int row, int col);
void show_find(int arr[ROWS][COLS], int ar[ROWS][COLS], int row, int col);
int check_win(int ar[ROWS][COLS], int row, int col);
#endif

game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include"game.h"
void set_mine(int arr[ROWS][COLS], int row, int col)//设置的雷比arr小一圈,为了算出每个格子周围的雷数目
{
	//输入雷(1)的坐标,然后把arr中的对应坐标设为雷
	int i = 0;
	int count=0;
	int x = 0, y = 0;
	int m = 0, n = 0;
	printf("输入雷的坐标\n");
	while (1)
	{
		int x = rand() % row;
		int y = rand() % col;
		if (x == 0){
			x = x + 1;
		}
		if (y == 0){
			y = y + 1;
		}
		if (arr[x][y]==0)
		{
			arr[x][y] = 1;
			count++;
			printf("%d %d\n", x, y);
		}
		m = x;
		n = y;
		if (count == num)
			break;
	}
	printf("\n");
}
int find_mine(int arr[ROWS][COLS],int ar[ROWS][COLS], int row, int col)
{
	int x = 0, y = 0;
	int k = 0, h = 0;
	int count = 0;
	/*show_find(arr, ar, row, col);*/
	while (1)
	{
		printf("输入\n");
		scanf("%d %d", &x, &y);
		if (arr[x][y] == 0)
			break;
		else
			printf("有雷请重新输入");
	}
		/*for (i = 0; i < (row*col); i++)*/
	ar[x][y] = arr[x - 1][y]
		+ arr[x - 1][y - 1]
		+ arr[x][y - 1]
		+ arr[x + 1][y - 1]
		+ arr[x + 1][y]
		+ arr[x + 1][y + 1]
		+ arr[x][y + 1]
		+ arr[x - 1][y + 1];
	while (1)
	{
		/*show_find(arr, ar, row, col);*/
		if (arr[x][y] != 1)
		{
			for (k = x - 1; k < (x + 2); k++)//显示x y周围雷的情况
			{
				for (h = y - 1; h< (y + 2); h++)
				{
					if ((arr[k][h] == 0) && (ar[k][h] == 0))  /*((k!=x)&&(h !=y))*/
						ar[k][h] = 9;
				}
			}
			show_find(arr, ar, row, col);
			if (check_win(ar, row, col)>=(row*col - num))
				return 0;//赢了
		}
		   else
			   return 1;//输了
		   printf("输入\n");
		   scanf("%d %d", &x, &y);
   }
return 0;
}
void show_set(int arr[ROWS][COLS], int ar[ROWS][COLS], int row, int col)
{
	printf("雷阵\n\n");
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		/*printf("------------------\n");*/
		printf("%d", i);
		for (j = 1; j <= col; j++)
		printf("|%d|", arr[i][j]);
		printf("\n");
	}
}
void show_find(int arr[ROWS][COLS], int ar[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 1; i <= row; i++)
	{
		printf("%d", i);
			for (j = 1; j <= col; j++)
		printf("|%d|", ar[i][j]);
			printf("\n");
	}
}
int check_win(int ar[ROWS][COLS], int row, int col)
{
	int i = 0; int j = 0;
	int count = 0;
	for (i = 1; i <=row; i++)
	{
		for (j = 1; j <=col; j++)
		{
			if (ar[i][j] !=0)
				count++;
		}
	}
	return count;
}

test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include"game.h"
void menu()
{
	printf("*******1.play**********\n");
	printf("*******0.exit**********\n");
}
void game()
{
	int ret = 0;
	srand((unsigned int)time(NULL));
	int ar[ROWS][COLS];
	int arr[ROWS][COLS]; 
	int i = 0;
	int j = 0;
	int row = ROWS-2, col = COLS-2;
	for (i = 0; i < ROWS; i++)
	{
		for (j = 0; j < COLS; j++)
		{
			arr[i][j] = 0;
			ar[i][j] = 0;
		}
	}
	set_mine(arr, row, col);
	show_set(arr,ar, row, col);
	printf("扫雷开始\n");
	show_find(arr, ar, row, col);
	if (ret == find_mine(arr, ar, row, col))
	{
		printf("扫雷成功\n");
		show_find(arr, ar, row, col);
	}
	else {
		printf("失败\n");
		show_find(arr, ar, row, col);
	}
}
int main()
{
	int input = 0;
	do
	{
		menu();
		printf("请输入\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game(); break;
		case 0:
			; break;
		default:
			printf("输入有误\n"); break;
		}
	} while (input);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值