关于扫雷的简易实现

关于扫雷的简易实现

该扫雷特性:

  • 第一次点击一定不为雷
  • 容易易实现

首先,我们依然是像三子棋游戏一样,创建两个源文件和一个头文件:

请添加图片描述

然后便是我们的主函数部分:

因为是测试部分,写一个测试函数来放置我们的代码:
在这里插入图片描述

接下来是test函数的实现:

在这里插入图片描述

此处的menu函数为打印菜单函数,也即为提示一下玩家该选什么,由此进入到我们的switch case语句中去判断下一步应该执行什么语句

输入1后进入游戏:(扫雷游戏暂时以打印的方式来代替),这样的话基本逻辑我们就写好了,现在重点来写一下扫雷游戏的实现

将printf语句改为game()函数来实现,现在我们要玩扫雷游戏,那扫雷游戏是怎么样的呢,现拿一个已经写好的程序来看一下

在这里插入图片描述

现在打印的是棋盘,而我们又要存储信息,因此,我们需要创建一个二维数组来实现,但我们现在想把字符1是我们的雷,要怎么办呢,因此,我们创建两个数组,一个用来存放雷,一个用来存放排雷信息的数组,但我们排雷的时候,需要向八个方向排雷,但四个角落的坐标怎么办呢?向8个方向进行判断很有可能会越界.因此,我们不创建9,9的数组,而进而创建一个11,11的数组,但我们所使用的仍然是9*9的数组,因此我们定义四个标识符常量。

既然已经在头文件中定义了,不然我们干脆把头文件的包含全都放进去。
在这里插入图片描述

因此,现在来到我们的game函数内部:

首先是我们的初始化函数部分:>
在这里插入图片描述

将mine 和 show数组初始化为我们想要的元素,把布置雷的数组先初始化为0,把展示的数组初始化为’*‘,这样就可以达到我们想要的效果,InitBoard函数定义部分:在这里插入图片描述

既然棋盘已经初始化完成,我们打印出来看看效果如何,编写一个DisplayBoard函数来实现,请看下图
在这里插入图片描述

第一个for循环打印出来的便是每一列的列号,而是在下一个循环中打印出来的。现在我们来看一下打印效果
在这里插入图片描述

在这里插入图片描述

既然已经打印好了,那么现在我们要来布置雷了,布置雷,我们用一个SetMine函数来实现,请看下图

在这里插入图片描述

因为我们要放置十个雷,用count = 10 来计数,并产生随机坐标来放置雷

雷都已经放好了,现在应该开始扫雷了吧,接下来开始我们的扫雷部分,编写一个SweepMine函数来实现
在这里插入图片描述

此处里层的CountMine函数,是用来计算所排的雷的那个点的坐标周围雷的个数,我们这样实现:>

在这里插入图片描述

这样子程序就可以跑起来了,我们来看一下效果:>
在这里插入图片描述

下面是对程序的改进:>

为防止第一次是雷,以及我们容易调试,我们将雷的个数设置为标识符常量,以下是所增加的代码:>

在这里插入图片描述

将雷的个数定义为标识符常量

然后我们创建一个安全扫雷函数
在这里插入图片描述

这样就可以避免第一个为雷了,现在我们来看一下效果:

在这里插入图片描述
在这里插入图片描述
下面放出源代码:
test.c文件函数代码:

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>

#include "game.h"

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


void game()
{
	char mine[ROWS][COLS] = { 0 };
	char show[ROWS][COLS] = { 0 };
	InitBoard(mine, ROWS, COLS, '0');//此处我们将我们想初始化的元素传进去,这样就能通过一个函数来简单初始化。
	InitBoard(show, ROWS, COLS, '*');

	//DisplayBoard(show, ROW, COL);
	SetMine(mine, ROW, COL);
	//DisplayBoard(mine, ROW, COL);
    DisplayBoard(show, ROW, COL);
	SafeMine(mine, show, ROW, COL);
	SweepMine(mine, show, ROW, COL);
}


void test()
{
	srand((unsigned int)time(NULL));
	//游戏至少执行一次,因此用do while循环
	int input = 0;
	do
	{
		menu();//此处的menu函数为自己创建的菜单函数,提示一下玩家
		printf("请选择数字:>");//此然是选择,那必然得输入一个数把?
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏 \n");
			break;
		default:
			printf("输入错误,请重新输入\n");
			break;
		}
	} while (input);
}

int main()
{
	test();
	return 0;
}

下面是game.c文件内容

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
	int i = 0;
	int j = 0;
	for (i = 0; i < rows; i++)
	{
		for (j = 0; j < cols; j++)
		{
			board[i][j] = set;
		}
	}
}


void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
	int i = 0;
	int j = 0;
	for (i = 0; i <= col; i++)
	{
		printf("%d ", i);      //打印列号,以便玩家查看
	}
	printf("\n");
	for (i = 1; i <= row; i++)
	{
		printf("%d ", i);  //打印行号,以便玩家查看
		for (j = 1; j <= col; j++)
		{
			printf("%c ", board[i][j]);
		}
		printf("\n");
	}
}


void SetMine(char mine[ROWS][COLS], int row, int col)
{
	int count = EASYCOUNT;
	while (count)
	{
		int x = rand() % row + 1;
		int y = rand() % col + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}




int CountMine(char mine[ROWS][COLS], int x, int y)
{
	return mine[x - 1][y] +
		mine[x + 1][y] +
		mine[x - 1][y - 1] +
		mine[x][y - 1] +
		mine[x + 1][y - 1] +
		mine[x - 1][y + 1] +
		mine[x][y + 1] +
		mine[x + 1][y + 1] - 8 * '0';
}

void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int newx = 0;
	int newy = 0;
	printf("请输入排查的坐标:>");
	scanf("%d%d", &x, &y);
	while (1)
	{
		if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '0')
		{
			int ret = CountMine(mine, x, y);
			show[x][y] = ret + '0';
			DisplayBoard(show, ROW, COL);

			break;
		}
		newx = rand() % row + 1;
		newy = rand() % col + 1;
		if (x >= 1 && x <= row && y >= 1 && y <= col && mine[x][y] == '1')
		{
				if (mine[newx][newy] == '0')
				{
					mine[newx][newy] = '1';
					mine[x][y] = '0';
					int ret = CountMine(mine, x, y);
					show[x][y] = ret + '0';
					DisplayBoard(show, ROW, COL);
					DisplayBoard(mine, ROW, COL);
					break;
				}
		}
	}
}



void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
	int x = 0;
	int y = 0;
	int count = row * col - EASYCOUNT - 1;
	while (count)
	{
		printf("请输入排查的坐标:>");
		scanf("%d%d", &x, &y);
		if (x >= 1 && x <= row && y >= 1 && y <= col)
		{
			if (mine[x][y] == '1')
			{
				printf("很遗憾,你被炸死了\n");
				break;
			}
			if (mine[x][y] == '0')
			{
				int ret = CountMine(mine, x, y);
				show[x][y] = ret + '0';
				DisplayBoard(show, ROW, COL);
				count--;
			}
		}
		else
		{
			printf("输入有误,请重新输入\n");
		}
	}
	if (count == 0)
	{
		printf("恭喜你,排雷成功\n");
		DisplayBoard(mine, ROW, COL);
	}
}

game.h中的代码:

#pragma once

#define ROW 9
#define COL 9

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

#define EASYCOUNT 10

#include <stdio.h>

#include <stdlib.h>

#include <time.h>
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);

void DisplayBoard(char board[ROWS][COLS], int row, int col);

void SweepMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

void SetMine(char mine[ROWS][COLS], int row, int col);

void SafeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

本文完

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
以下是Java实现简易扫雷小游戏的步骤: 1. 创建一个窗口,包含一个菜单栏和一个游戏区域。 2. 菜单栏包含难度选择和重新开始游戏的选项。 3. 游戏区域包含一个二维*********```java import javax.swing.*; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class Minesweeper extends JFrame { private static final int ROWS = 10; private static final int COLS = 10; private static final int MINES = 10; private static final int CELL_SIZE = 30; private static final int FRAME_WIDTH = COLS * CELL_SIZE; private static final int FRAME_HEIGHT = ROWS * CELL_SIZE + 22; private Cell[][] cells = new Cell[ROWS][COLS]; private int minesLeft = MINES; private JLabel statusbar; public Minesweeper() { initUI(); } private void initUI() { JPanel panel = new JPanel(); panel.setLayout(new GridLayout(ROWS, COLS)); for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col] = new Cell(row, col); panel.add(cells[row][col]); } } statusbar = new JLabel("Mines left: " + minesLeft); add(panel, BorderLayout.CENTER); add(statusbar, BorderLayout.SOUTH); setResizable(false); pack(); setTitle("Minesweeper"); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void newGame() { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col].reset(); } } minesLeft = MINES; statusbar.setText("Mines left: " + minesLeft); } private void gameOver() { for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { cells[row][col].reveal(); } } JOptionPane.showMessageDialog(this, "Game over", "Minesweeper", JOptionPane.INFORMATION_MESSAGE); newGame(); } private void checkWin() { boolean win = true; for (int row = 0; row < ROWS; row++) { for (int col = 0; col < COLS; col++) { if (!cells[row][col].isMine() && !cells[row][col].isRevealed()) { win = false; } } } if (win) { JOptionPane.showMessageDialog(this, "You win!", "Minesweeper", JOptionPane.INFORMATION_MESSAGE); newGame(); } } private class Cell extends JPanel { private int row; private int col; private boolean mine; private boolean revealed; private boolean marked; public Cell(int row, int col) { this.row = row; this.col = col; this.mine = Math.random() < (double) MINES / (ROWS * COLS); this.revealed = false; this.marked = false; setPreferredSize(new Dimension(CELL_SIZE, CELL_SIZE)); setBackground(Color.LIGHT_GRAY); setBorder(BorderFactory.createLineBorder(Color.GRAY)); addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent e) { if (SwingUtilities.isLeftMouseButton(e)) { if (mine) { setBackground(Color.RED); gameOver(); } else { reveal(); checkWin(); } } else if (SwingUtilities.isRightMouseButton(e)) { mark(); } } }); } public void reset() { this.mine = Math.random() < (double) MINES / (ROWS * COLS); this.revealed = false; this.marked = false; setBackground(Color.LIGHT_GRAY); } public boolean isMine() { return mine; } public boolean isRevealed() { return revealed; } public void reveal() { if (revealed || marked) { return; } revealed = true; if (mine) { setBackground(Color.RED); return; } int count = 0; for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) { for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) { if (cells[r][c].isMine()) { count++; } } } if (count > 0) { setText(Integer.toString(count)); } else { setBackground(Color.WHITE); for (int r = Math.max(0, row - 1); r <= Math.min(ROWS - 1, row + 1); r++) { for (int c = Math.max(0, col - 1); c <= Math.min(COLS - 1, col + 1); c++) { cells[r][c].reveal(); } } } } public void mark() { if (revealed) { return; } marked = !marked; if (marked) { setBackground(Color.YELLOW); minesLeft--; } else { setBackground(Color.LIGHT_GRAY); minesLeft++; } statusbar.setText("Mines left: " + minesLeft); } } public static void main(String[] args) { EventQueue.invokeLater(() -> { Minesweeper game = new Minesweeper(); game.setVisible(true); }); } } ```
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值