Mine Sweeper

描述:

The game Minesweeper is played on an n by n grid. In this grid are hidden m mines, each at a distinct grid location. The player repeatedly touches grid positions. If a position with a mine is touched, the mine explodes and the player loses. If a positon not containing a mine is touched, an integer between 0 and 8 appears denoting the number of adjacent or diagonally adjacent grid positions that contain a mine. A sequence of moves in a partially played game is illustrated below.

Here, n is 8, m is 10, blank squares represent the integer 0, raised squares represent unplayed positions, and the figures resembling asterisks represent mines. The leftmost image represents the partially played game. From the first image to the second, the player has played two moves, each time choosing a safe grid position. From the second image to the third, the player is not so lucky; he chooses a position with a mine and therefore loses. The player wins if he continues to make safe moves until only m unplayed positions remain; these must necessarily contain the mines.

Your job is to read the information for a partially played game and to print the corresponding board. 


输入:

The first line of input contains a single postitive integer n <= 10. The next n lines represent the positions of the mines. Each line represents the contents of a row using n characters: a period indicates an unmined positon while an asterisk indicates a mined position. The next n lines are each n characters long: touched positions are denoted by an x, and untouched positions by a period. The sample input corresponds to the middle figure above


输出:

Your output should represent the board, with each position filled in appropriately. Positions that have been touched and do not contain a mine should contain an integer between 0 and 8. If a mine has been touched, all positions with a mine should contain an asterisk. All other positions should contain a period. 


样例输入:

8
...**..*
......*.
....*...
........
........
.....*..
...**.*.
.....*..
xxx.....
xxxx....
xxxx....
xxxxx...
xxxxx...
xxxxx...
xxx.....
xxxxx...


样例输出:

001.....
0013....
0001....
00011...
00001...
00123...
001.....
00123...


题目大意:

输入一个正整数n接下去n行是n*n的地图*表示地雷位置,接下去n行n列为地图的位置如果为x表示为探索过的位置。如果探索过的位置为*则输出地图中所有雷的位置,如果为.输出周围的雷的数量。


代码如下:

#include<stdio.h>
int n;
char map[15][15],map1[15][15];
int find(int a,int b)						//搜索map[a][b]周围的地雷数量 
{
	int e=0;
	for(int x=-1;x<=1;x++)
	{
		for(int y=-1;y<=1;y++)
		{
			if(x==0&&y==0)
			continue;
			else
			{
				if(x+a>=0&&x+a<n&&y+b>=0&&y+b<n&&map[x+a][y+b]=='*')
				e++;
			}
		}
	}
	return e;
}
void print()								 
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			printf("%c",map1[i][j]);
		}
		printf("\n");
	}
}
void sove()									//打印所有雷的位置
{
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(map[i][j]=='*')
			{
				map1[i][j]='*';
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%s",map[i]);
	}
	for(int i=0;i<n;i++)
	{
		scanf("%s",map1[i]);
	}	
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(map1[i][j]=='x'&&map[i][j]!='*')
			{
				int count=find(i,j);
				map1[i][j]=count+'0';
			}
			if(map1[i][j]=='x'&&map[i][j]=='*')				 
			{
				sove();
			}			
		}
	}
	print();
	return 0;
}



在C语言中创建一个图形用户界面(GUI)Minesweeper游戏是一个复杂的过程,因为它通常涉及到使用像GTK+、Qt或SDL这样的专门库。然而,由于C语言本身并不直接支持GUI,我们可以选择使用一种结合了C语言和GUI库的解决方案。这里我将给出一个简化的示例,展示如何使用C写的命令行版本的Minesweeper,而GUI版本的实现则需要引入额外的库和更复杂的编程。 首先,了解基本的命令行版Minesweeper游戏,我们需要定义一个二维数组表示游戏区域,每个元素代表一个格子,可能是地雷(*),数字(周围的地雷数),或者空格(.)。然后我们会有一个循环,让用户输入坐标并标记格子。 ```c #include <stdio.h> #include <stdlib.h> #define BOARD_WIDTH 10 #define BOARD_HEIGHT 10 #define MINES 10 int board[BOARD_WIDTH][BOARD_HEIGHT] = {0}; void init_board() { for (int i = 0; i < MINES; i++) { int row = rand() % BOARD_HEIGHT; int col = rand() % BOARD_WIDTH; board[row][col] = '*'; } } void print_board() { for (int i = 0; i < BOARD_HEIGHT; i++) { for (int j = 0; j < BOARD_WIDTH; j++) { printf("%c ", board[i][j]); } printf("\n"); } } int main() { init_board(); print_board(); while (true) { int row, col; printf("Enter row and column (separated by space): "); scanf("%d %d", &row, &col); if (board[row][col] == '.') { // Mark the cell as visited board[row][col] = 'O'; // Check neighbors for mines int count = 0; for (int x = -1; y <= 1; y++) { if (x == 0 && y == 0) continue; if (board[row + x][col + y] == '*') count++; } board[row][col] = count ? count : ' '; } print_board(); char command; printf("Press 'Q' to quit, any other key to continue: "); scanf(" %c", &command); if (command == 'Q') break; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值