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

📄 jminearth.java

📁 开源Java扫雷游戏JMine2.0Jar新版功能完善
💻 JAVA
字号:
/**
 * This program is written by Jerry Shen(Shen Ji Feng) use the technology of
 * SWING GUI and the OO design
 * 
 * @author Jerry Shen all rights reserved.
 * Email:jerry.shen@cognizant.com; jerry_shen_sjf@yahoo.com.cn
 * Please report bug to these emails.
 * Open source under GPLv3
 * 
 * version 2.0
 */
class JMineArth {
	public int [][] mine;

	private boolean fMineSet = false;	

	//minefield width
	private int width;
	//minefiled height
	private int height;

	public boolean isFMineSet() {
		return fMineSet;
	}

	private void setFMineSet(boolean mineSet) {
		fMineSet = mineSet;
	}

	// the arithmetic to set a JMine game with minNum mines, [col, row] is start
	// point without mine, and mine field is colCount width, rowCount high
	// the colCount, rowCount counts from 1
	// the col, row counts from 0
	//
	// The JMineAth class is generating a JMine map for the game
	JMineArth(int mineNum, int col, int row, int colCount, int rowCount) {
		try {
			// check whether the game setting is logic
			if (col >= colCount || row >= rowCount || mineNum >= colCount*rowCount )
				throw new Exception("Game setting error!");
			width = colCount;
			height = rowCount;
			mine = new int[width][height];
			setMine(mineNum, col, row);
			setMineNum();
		}
		catch (Exception e) {
			System.err.println("Game setting error!");
		}
	}

	// set number = mineNum mines inside the minefield border
	// Outrow and Outcol stands for the click start point
	// there should not be any mine
	// 9 stands for the mine
	private void setMine(int mineNum, int Outcol, int Outrow) {
		int col=0, row = 0, i=0;
		//Math.srand(now);
		while (i < mineNum) {
			col = (int)(Math.random()*100)%width;
			row = (int)(Math.random()*100)%height;
			if (mine[col][row]==0 && (row!=Outrow || col!=Outcol || Outrow==10 )) {
				mine[col][row]=9;
				i++;
			}
		}
	}

	// 9 stands for the mine
	// this arithmetic set all the 1-8 mine number of every cell
	private void setMineNum() {
		for ( int i=0 ; i <height; i++) {
			for (int j=0; j < width; j++) {
				mine[j][i]=mine[j][i]==9?9:checkMineNum(j,i);
			}
		}
		setFMineSet(true);
	}

	// this arithmetic check how many mines are there in the neighboring 8 cells 
	// if the cell is near border, we only count the cells inside minefield 
	private int checkMineNum(int col,int row) {
		int top,bottom, left, right, count=0;
		left=col-1>0?col-1:0;
		right=col+1<width?col+1:width-1;
		top=row-1>0?row-1:0;
		bottom=row+1<height?row+1:height-1;
		for (int i=top; i<=bottom; i++) {
			for(int j=left; j<= right; j++) {
				if (mine[j][i]==9) count++;
			}
		}
		return(count);
	}

	// The method to print out all the mine matrix
	// using the text format
	public void printMine() {
		for (int i = 0; i < height; i++) {
			for (int j=0; j < width; j++) {
				System.out.print(this.mine[j][i] + " ");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		JMineArth mine = new JMineArth(Integer.parseInt(args[0]),Integer.parseInt(args[1]),Integer.parseInt(args[2]),Integer.parseInt(args[3]),Integer.parseInt(args[4]));
		mine.printMine();
	}
}

⌨️ 快捷键说明

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