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

📄 mine.java

📁 扫雷游戏 JAVA编写 可以进行算雷数等功能
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Mine extends JFrame{
	private static JButton[][] buttons;
	private static String[][] label;
	private static  int height=20;
	private static  int width=20;
	private static  int size=30;
	private static boolean map[][];
	private static int MINENUMBER=50;
	private static int counter = 0;// how many places you have make sure there is mine in it
	private static int counter2 = 0;// how many places is clear that there is a mine
	private static int[][] step = {{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};

	public static void main (String args[]){
		Mine user = new Mine();
		user.setSize(1024,768);
		user.show();
	}

	public Mine(){
		this.setBackground(Color.white);
		buttons = new JButton[height][width];
		label = new String[height][width];
		map = new boolean[height][width];
		setMine();
		getLabel(height,width);
		this.getContentPane().setLayout(null);
		JTextArea area = new JTextArea(30,10);
		area.setLocation(20,20);
		area.setSize(40,40);
		this.getContentPane().add(area);
		for ( int i=0;i<height ;i++ ){
			for ( int j=0;j<width ;j++ ){
				buttons[i][j]=new JButton();
				this.getContentPane().add(buttons[i][j],null);
				findLocation(buttons[i][j],i,j);
				buttons[i][j].setBorder(BorderFactory.createRaisedBevelBorder());
				buttons[i][j].addMouseListener(new Listener());
				buttons[i][j].setBackground(Color.white);
			}
		}
		//this.getContentPane().setBackground(Color.white);
		this.setBackground(Color.white);
	}

	public void findLocation(JButton button , int i ,int j){
		int tpx = j ;
		int tpy = i ;
		button.setBounds(new Rectangle(200+tpx*size,80+tpy*size,size,size));
	}

	public void getLabel(int height,int width){
		for ( int i=0;i<height ;i++ ){
			for ( int j=0;j<width ;j++ ){
				if ( !label[i][j].equals("#") )
					label[i][j]=getNum(i,j);
			}
		}
	}

	public String getNum(int h,int w){
		int counter=0;
		for ( int i=0;i<8 ;i++ ){
			int tpx = h+step[i][0];
			int tpy = w+step[i][1];
			if ((tpx>=0)&&(tpx<height)&&(tpy>=0)&&(tpy<width)&&(label[tpx][tpy].equals("#")))
				counter++;
		}
		return counter+"";
	}

	public void setMine(){
		for ( int i=0;i<height ;i++ ){
			for ( int j=0;j<width ;j++ ){
				label[i][j]=" ";
				map[i][j]=false;
			}
		}
		for ( int i=0;i<MINENUMBER ;i++ ){
			int tph = (int)(Math.random()*(height-1))+1;
			int tpw = (int)(Math.random()*(width-1))+1;
			label[tph][tpw]="#";
		}
	}

	public class Listener implements MouseListener{
		public void mouseClicked(MouseEvent e){}
		public void mouseReleased(MouseEvent e){}
		public void mouseEntered(MouseEvent e){}
		public void mouseExited(MouseEvent e){}
		public void mousePressed(MouseEvent e){
			if ( e.getButton()==e.BUTTON1 ){
				stop:for ( int i=0;i<height ;i++ ){
					for ( int j=0;j<width ;j++ ){
						if ( e.getSource()==buttons[i][j]){
							if ( label[i][j].equals("#"))
								clear();
							else if (!label[i][j].equals("9")&&map[i][j]==false){
								check(i,j);
							}
							break stop;
						}
					}
				}
			}
			else{
				stop :for ( int i=0;i<height ;i++ ){
					for ( int j=0;j<width ;j++ ){
						if ( e.getSource()==buttons[i][j] ){
							if ( map[i][j]==false){
								if (!buttons[i][j].getText().equals("@")){
									counter++;
									buttons[i][j].setText("@");
								}
								else{
									counter--;
									buttons[i][j].setText("");
								}
							}
							break stop;
						}
					}
				}
			}
			ifEnd();
		}
	}

	public void clear(){
		for ( int i=0;i<height ;i++ ){
			for ( int j=0;j<width ;j++ ){
				if ( label[i][j].equals("#"))
					buttons[i][j].setText(label[i][j]);
				else
				    buttons[i][j].setBorder(BorderFactory.createEmptyBorder());
			}
		}
		JOptionPane.showMessageDialog(null , " You lose ");
		System.exit(0);
	}

	public void ifEnd(){
		boolean temp = true;
		for ( int i=0;i<height ;i++ ){
			for ( int j=0;j<width ;j++ ){
				if (!label[i][j].equals("#")&&(map[i][j]==false))
					temp = false;
			}
		}
		if ( temp ){
			JOptionPane.showMessageDialog(null , " You win ");
			System.exit(0);
		}
	}

	public void check(int h,int w){
		if ( !label[h][w].equals("9")&&!label[h][w].equals("0")&&(!label[h][w].equals("#"))){
			buttons[h][w].setBorder(BorderFactory.createEmptyBorder());
			buttons[h][w].setText(label[h][w]);
			map[h][w]=true;
		}
		else if (!label[h][w].equals("#")&& !label[h][w].equals("9")&&(!label[h][w].equals("9"))){			
			map[h][w]=true;
			label[h][w]="9";
			buttons[h][w].setBorder(BorderFactory.createEmptyBorder());
		}
		char temp = label[h][w].charAt(0);
		if ( temp<'1' || temp>'8' ){
			for ( int i=0;i<8 ;i++ ){
				int tph = h+ step[i][0];
				int tpw = w+ step[i][1];
				if (( tph>=0)&&(tph<height)&&(tpw>=0)&&(tpw<width)&& !label[tph][tpw].equals("9")){
					if ( (!label[tph][tpw].equals("0")) && (!label[tph][tpw].equals("#"))&& !label[tph][tpw].equals("9")){
						map[tph][tpw]=true;
						buttons[tph][tpw].setBorder(BorderFactory.createEmptyBorder());
						buttons[tph][tpw].setText(label[tph][tpw]);
					}
					else if (label[tph][tpw].equals("0")&&!label[tph][tpw].equals("9")){
						map[tph][tpw]=true;
						label[tph][tpw]="9";
						buttons[tph][tpw].setBorder(BorderFactory.createEmptyBorder());
						check(tph,tpw);
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

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