Simple minesweeper implementation

Count the number of mines around the coordinates (x,y)

Look at the coordinates around the coordinates x, y


int get_mine_count(char mine[ROWS][COLS], int x, int y)//Count the number of mines around
{
	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';
}

game.h

#define _CRT_SECURE_NO_WARNINGS 1

#ifndef _GAME__H__
#define _GAME__H__

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

#define COUNT 50 //Total number of mines
#define ROW 9
#define COL 9

#define ROWS ROW+2
#define COLS COL+2

void init_board(char board[ROWS][COLS], char set, int row, int col);//Initialize the board
void display_board(char board[ROWS][COLS], int row, int col);//打印
void set_mine(char mine[ROWS][COLS]);//Bray
int get_mine_count(char mine[ROWS][COLS], int x, int y);//Count the number of mines around
#endif

game.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"


void init_board(char board[ROWS][COLS],char set, int row, int col)//初始化棋盘
{
memset(board, set, row*col*sizeof(board[0][0]));
}


void display_board(char board[ROWS][COLS], int row, int col)//打印
{
int i = 0;
int j = 0;
printf("  ");
for (i = 1; i <= col;i++)//打印列号
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i < row + 1; i++)
{
printf("%d ", i);//打印行号
for (j = 1; j < col + 1; j++)//打印一行
{
printf("%c ", board[i][j]);
}
printf("\n");//换行
}
}


void set_mine(char mine[ROWS][COLS])//Bray
{
int count = COUNT;
int x = 0;
int y = 0;
while (count > 0)
{
x = rand() % ROW + 1;// Randomly generate row coordinates
y = rand() % COL + 1;//Randomly generate column coordinates


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


int get_mine_count(char mine[ROWS][COLS], int x, int y)//Count the number of mines around
{
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';

}

test.c

#define _CRT_SECURE_NO_WARNINGS 1

#include "game.h"

void menu()//Print menu
{
	printf("*****************************\n");
	printf("***** 1.play     0.exit *****\n");//选择1玩扫雷,选择0要退出
	printf("*****************************\n");
}
void play()
{
	char mine[ROWS][COLS] = { 0 };//雷的信息
	char show[ROWS][COLS] = { 0 };//排出的雷的信息
	int x = 0;//存储行坐标
	int y = 0;//存储列坐标
	int win = 0;//存储现在排除的非雷的个数


	init_board(mine,'0', ROWS, COLS);//初始化雷
	init_board(show, '*', ROWS, COLS);//初始化排出的雷的信息

	set_mine(mine);//布雷
	display_board(mine, ROW, COL);//打印雷的信息

	while (win < (ROW * COL - COUNT))
	{
		printf("请输入您要排查的坐标:>");
		scanf("%d%d", &x, &y);
		if (((x >= 1) && (x <= 9)) && ((y >= 1) && (y <= 9)))//判断输入的坐标是否合理
		{
			if (mine[x][y] == '1')//判断是否有雷
			{
				printf("不好意思,您被炸死了\n");
				break;
			}
			else
			{
				int count = get_mine_count(mine, x, y);//统计周围雷的个数
				show[x][y] = count + '0';
				win++;
				display_board(show, ROW, COL);//打印排出的雷的信息
			}
		}
		else
		{
			printf("您输入的坐标非法,请重新输入\n");
		}
	}
	if (win == (ROW * COL - COUNT))
	{
		printf("恭喜你,排雷成功\n");
	}
}
void test()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();//打印菜单
		printf("请选择:>");
		scanf("%d", &input);//获取输入的字符
		switch(input)
		{
		case 1://输入1
			play();
			break;
		case 0://输入0
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}
int main()
{
	test();
	system("pause");
	return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782974&siteId=291194637