⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mineframe.java

📁 简单的防XP扫雷代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		if (time >= 100) {
			time_num_ten.setIcon(lednum[(time % 100) / 10]);
			time_num_bit.setIcon(lednum[time % 10]);
		} else {
			time_num_ten.setIcon(lednum[time / 10]);
			time_num_bit.setIcon(lednum[time % 10]);
		}
	}

	/*
	 * 布雷方法
	 */
	private void setMine() {

		for (int i = 0; i < minesnum; i++) {
			int x = (int) (Math.random() * rows);
			int y = (int) (Math.random() * cols);
			if (grid[x][y].isMine()) {
				i--;
			} else {
				grid[x][y].setMine(true);
			}
		}
	}

	/*
	 * 设置格子应该显示的数字
	 */
	private void setGridShowNum() {
		int numIcon = 0;

		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				if (grid[i][j].isMine()) {
					numIcon = 9;// 9就为雷
					grid[i][j].setShownum(numIcon);
				}
				if (!grid[i][j].isMine()) {
					for (int m = Math.max(i - 1, 0); m <= Math.min(i + 1,
							rows - 1); m++) {
						for (int n = Math.max(j - 1, 0); n <= Math.min(j + 1,
								cols - 1); n++) {
							if (grid[m][n].isMine()) {
								numIcon++;
							}
						}
					}
					grid[i][j].setShownum(numIcon);
				}
				numIcon = 0;
			}
		}

		this.showGrid();// 为了显示所有雷区数字,方便调试
	}

	/*
	 * 由格子的X,Y坐标确定格子是否为空,如果为空,以当前格子为中心,继续确认上下左右周围四个格子是否为空,或数字,或雷
	 */
	private void showGridConinute(int i, int j) {
		if (!grid[i][j].isExpand() && !grid[i][j].isMark()) {
			grid[i][j].setIcon(iconnum[grid[i][j].getShownum()]);
			grid[i][j].setExpand(true);
			if (grid[i][j].getShownum() == 0) {
				for (int m = Math.max(i - 1, 0); m <= Math.min(i + 1, rows - 1); m++) {
					for (int n = Math.max(j - 1, 0); n <= Math.min(j + 1,
							cols - 1); n++) {
						showGridConinute(m, n);
					}
				}
			}
		}
	}

	/*
	 * 左右键点击时调用方法
	 */
	private void leftAndRightPressed(int i, int j) {
		int temp = 0, count = 0;

		temp = grid[i][j].getShownum();
		if (grid[i][j].isExpand()) {
			for (int m = Math.max(i - 1, 0); m <= Math.min(i + 1, rows - 1); m++) {
				for (int n = Math.max(j - 1, 0); n <= Math.min(j + 1, cols - 1); n++) {
					if (grid[m][n].isMark()) {
						count++;
					}
				}
			}
			// System.out.println("点击格子数字:" + temp + " 周围标记数:" + count);
			if (temp == count) {
				// System.out.println("周围格子坐标");
				for (int m = Math.max(i - 1, 0); m <= Math.min(i + 1, rows - 1); m++) {
					for (int n = Math.max(j - 1, 0); n <= Math.min(j + 1,
							cols - 1); n++) {
						if (!grid[m][n].isMark()) {
							if (grid[m][n].isMine()) {
								grid[m][n].setIcon(iconmine[1]);
								grid[m][n].setExpand(true);
								endGame();
								break;
							} else {
								grid[m][n].setIcon(iconnum[grid[m][n]
										.getShownum()]);
								showGridConinute(m, n);
							}
						}
					}
				}
			}
		}
		temp = 0;
		count = 0;
	}

	// 调试使用,按rows * cols 格式打印出格子显示的数字,0为空
	private void showGrid() {
		int count = 0;
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				count++;
				if (count % cols == 1) {
					System.out.println();
				}
				System.out.print(grid[i][j].getShownum() + "  ");
			}
		}
		System.out.println();
	}

	/**
	 * 监听类,监听鼠标左键和右键监听.
	 * 
	 * @author brt
	 * 
	 */
	class MineListener extends MouseAdapter {

		/**
		 * 监听鼠标点下.
		 * 
		 * @see java.awt.event.MouseAdapter#mousePressed(java.awt.event.MouseEvent)
		 */
		@Override
		public void mousePressed(MouseEvent e) {

			if (e.getSource() instanceof Grid) {
				Grid g = (Grid) e.getSource();
				if (e.getModifiersEx() == e.BUTTON1_DOWN_MASK
						+ e.BUTTON3_DOWN_MASK) {
					leftAndRightPressed(g.getLoc_x(), g.getLoc_y());
					if (!gameover) {
						winGame();
					}

				} else if (e.getButton() == MouseEvent.BUTTON1) {// 左键
					if (!gameover) {
						jRestart.setIcon(face[1]);
					}
				}
			} else {
				if (e.getButton() == MouseEvent.BUTTON1) {
					if (e.getSource() == jRestart) {// 监听重新开始
						jRestart.setIcon(face[4]);
						System.out.println("重新开始");
					}
				}
			}
		}

		/**
		 * 监听鼠标松开.
		 * 
		 * @see java.awt.event.MouseAdapter#mouseReleased(java.awt.event.MouseEvent
		 */
		@Override
		public void mouseReleased(MouseEvent e) {

			if (e.getSource() instanceof Grid) {
				Grid g = (Grid) e.getSource();
				if (e.getButton() == MouseEvent.BUTTON1) {

					if (!timer_start && !timer_stop) {
						timer.start();
						timer_start = true;
						timer_stop = false;
					}
					if (g.isCanclick()) {
						jRestart.setIcon(face[0]);
					}
					if (!g.isMark() && g.isCanclick()) {
						if (g.isMine()) {
							g.setIcon(iconmine[1]);
							g.setExpand(true);
							endGame();
						} else {
							showGridConinute(g.getLoc_x(), g.getLoc_y());
						}
					}
				} else if (e.getButton() == MouseEvent.BUTTON3) {
					if (!g.isExpand() && g.isCanclick()) {
						if (!g.isMark() && !g.isQuestion()) {
							markcount++;
							reShowLeftStatus();
							g.setIcon(iconmarks[0]);
							g.setMark(true);
							g.setQuestion(false);
						} else if (g.isMark()) {
							markcount--;
							reShowLeftStatus();
							g.setIcon(iconmarks[1]);
							g.setQuestion(true);
							g.setMark(false);
						} else if (g.isQuestion() && !g.isMark()) {
							g.setIcon(iconmine[2]);
							g.setQuestion(false);
							g.setMark(false);
						}
					}
				}
			} else {
				if (e.getButton() == MouseEvent.BUTTON1) {

					if (e.getSource() == jRestart) {
						jRestart.setIcon(face[0]);
						initGame();
					} else if (e.getSource() == mi_start) {
						level = getLevel();
						initGame();
					} else if (e.getSource() == mi_easy) {
						level = 1;
						setGridPanel();
					} else if (e.getSource() == mi_normal) {
						level = 2;
						setGridPanel();
					} else if (e.getSource() == mi_hard) {
						level = 3;
						setGridPanel();
					} else if (e.getSource() == mi_exit) {
						System.exit(0);
					} else if (e.getSource() == mi_setuself) {
						new SetDialog();
					} else if (e.getSource() == mi_hero) {
						new HeroDialog();
					} else if (e.getSource() == mi_copyright) {
						new CopyrightDialog();
					}
				}
			}
			if (!gameover) {
				winGame();
			}
		}
	}

	/**
	 * 时间计数类.
	 * 
	 * @author brt
	 * 
	 */
	class TimerAL implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			if (time == 999) {
				endGame();
			} else {
				time++;
				reShowTime();
			}
		}
	}

	/**
	 * 返回雷区的列数.
	 * 
	 * @return 雷区列数
	 */
	public int getCols() {
		return cols;
	}

	/**
	 * 设置雷区的列数.
	 * 
	 * @param cols
	 *            雷区列数
	 */
	public void setCols(int cols) {
		this.cols = cols;
	}

	/**
	 * 返回雷区的行数.
	 * 
	 * @return 雷区的行数
	 */
	public int getRows() {
		return rows;
	}

	/**
	 * 设置雷区的行数.
	 * 
	 * @param rows
	 *            雷区行数
	 */
	public void setRows(int rows) {
		this.rows = rows;
	}

	/**
	 * 返回雷区的雷总数.
	 * 
	 * @return 雷总数
	 */
	public int getMinesnum() {
		return minesnum;
	}

	/**
	 * 设置雷区的雷总数.
	 * 
	 * @param minesnum
	 *            雷总数
	 */
	public void setMinesnum(int minesnum) {
		this.minesnum = minesnum;
	}

	/**
	 * 返回当前进行的扫雷的难度级别.
	 * 
	 * @return 扫雷难度级别
	 */
	public int getLevel() {
		return level;
	}

	/**
	 * 设置当前进行的扫雷的难度级别.
	 * 
	 * @param level
	 *            扫雷难度级别
	 */
	public void setLevel(int level) {
		this.level = level;
	}

	/**
	 * 返回当前进行的扫雷所花费的时间.
	 * 
	 * @return 当前进行的扫雷所花费的时间
	 */
	public int getTime() {
		return time;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -