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

📄 mainmine.java

📁 模拟扫雷小游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*;
class MainMine extends JFrame implements ActionListener,MouseListener
{ 
 
	public JTextField text;
	public Label nowMine,setMine;
	public Icon icon_Mine = new ImageIcon("Mine.gif");           //Mine picture
	public Icon icon_Mine_big = new ImageIcon("Mine_big.gif");   //explosive picture
	public Icon icon_flag = new ImageIcon("flag.gif");           //flag picture
	public Icon icon_question = new ImageIcon("question.gif");   //question picture
	public JButton start = new JButton("Start"); 
	public Panel MenuPamel = new Panel(); 
	public Panel mainPanel = new Panel(); 
	public Mine[][]MineButton;
	public int BlockNum;              //the number of squares(buttons)                                               
	public int MineNum;               //the number of Mines
	public boolean iswin=false;
 
    public MainMine() 
	{
		super("MineSweeper  JiuJiu developed in 2008.5");
		BlockNum = 100; 	//set the initial numbers of the squares
		MineNum = 10; 		//set the initial numbers of the Mines
		Container c=getContentPane();
		c.setBackground(Color.gray); 
		c.setLayout(new BorderLayout()); 
		text=new JTextField("10",3);
		nowMine = new Label("Mines remaining: "+"  "+MineNum+"");
		setMine= new Label("Set Mines numbers");
		start.addActionListener(new ActionListener(){ 
			public void actionPerformed(ActionEvent e) 
			{   
				MineNum = Integer.parseInt(text.getText().trim());
				if(MineNum >= 5 &&MineNum <=30)
					replay();
				else
				{
					JOptionPane.showMessageDialog(null,"You set too many or few Mines,please reset !","I am sorry",2); 
				}
			} 
		} );
		MenuPamel.add(setMine);
		MenuPamel.add(text); 
		MenuPamel.add(start); 
		MenuPamel.add(nowMine); 
		MenuPamel.setBackground(Color.lightGray);
		c.add(MenuPamel,"North"); 

		mainPanel.setLayout(new GridLayout( (int)Math.sqrt(BlockNum) ,
				                            (int)Math.sqrt(BlockNum)) );
		MineButton=new Mine[ (int)Math.sqrt(BlockNum) ][];
		for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++)
		{
			MineButton[ i ]=new Mine[ (int)Math.sqrt(BlockNum) ];
		}
		for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ ) 
			for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
			{ 
				MineButton[ i ][ j ]=new Mine(i,j);
				MineButton[ i ][ j ].setForeground( Color.gray); 
				MineButton[ i ][ j ].addActionListener(this); 
				MineButton[ i ][ j ].addMouseListener(this); 
			} 

		for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++ ) 
			for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++ )
				mainPanel.add(MineButton[ i ][ j ]); 
		c.add(mainPanel,"Center");
		startMine();
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		setSize(550,550);
		setLocation(250,100);
		setResizable(false);
	}
 
 /*set the Mines randomly*/
	public void startMine()
	{

		for(int i=0;i<MineNum;i++) 
		{ 
			int x =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1));  
			int y =(int)(Math.random()*(int)(Math.sqrt(BlockNum)-1)); 
			if(MineButton[ x ][ y ].isMine==true) 
				i--;
			else 
				MineButton[ x ][ y ].isMine=true ; 
		} 
	}

/*Restart to play again*/ 
	public void replay() 
	{ 
		nowMine.setText(""+"Mines remaining: "+MineNum+""); 
		for(int i = 0 ; i < (int)Math.sqrt(BlockNum) ; i++) 
			for(int j = 0 ; j < (int)Math.sqrt(BlockNum) ; j++)
			{ 
				MineButton[ i ][ j ].isMine=false; 
				MineButton[ i ][ j ].isClicked=false; 
				MineButton[ i ][ j ].setEnabled(true);
				MineButton[ i ][ j ].isEnabled=true;
				MineButton[ i ][ j ].setText(""); 
				MineButton[ i ][ j ].setIcon(null);
                MineButton[ i ][ j ].isRight = false;
                MineButton[ i ][ j ].MineFlag=0;
			}            
		startMine();
	} 

/*Win or not*/
	public void isWin() 
	{ 
		int findMine=0;  //how many Mines have been founded
		int notMine=0;   //how many squares without Mines have been clicked
		for(int i = 0;i < (int)Math.sqrt(BlockNum) ; i++) 
			for(int j = 0;j < (int)Math.sqrt(BlockNum ); j++)
			{ 
				if(MineButton[ i ][ j ].isMine == true &&MineButton[ i ][ j ].isRight == true)
					findMine++; 
			} 
		if( findMine == Integer.parseInt(text.getText().trim()) ) 
		{ 
			JOptionPane.showMessageDialog(this,"Congratulation on you, you win !","You Win",2); 
                     iswin=true;
		}
		for(int i = 0;i < (int)Math.sqrt(BlockNum) ; i++) 
			for(int j = 0;j < (int)Math.sqrt(BlockNum ); j++)
			{ 
				if(MineButton[ i ][ j ].isMine == false &&MineButton[ i ][ j ].isEnabled==false)
					notMine++;
			} 
		if( notMine==(BlockNum-Integer.parseInt( text.getText().trim())))
		{
			for(int i = 0;i < (int)Math.sqrt(BlockNum) ; i++) 
				for(int j = 0;j < (int)Math.sqrt(BlockNum ); j++)
				{ 
					if(MineButton[ i ][ j ].isMine == true)
						MineButton[ i ][ j ].setIcon(icon_flag);
				} 
			JOptionPane.showMessageDialog(this,
		    "Congratulation on you, you win !","You Win",2);
                     iswin=true; 
		}
	} 

/*calculate the Mines number around*/
	public void CountRoundMine() 
	{ 
		for (int i = 0; i < (int)Math.sqrt(BlockNum); i++) 
		{
			for (int j = 0; j < (int)Math.sqrt(BlockNum); j++) 
			{
				int count = 0;
				if (MineButton[ i ][ j ].isMine != true) 
				{ 
					if ( (i - 1 >= 0) && (j - 1 >= 0)) //check the top left corner
					{
						if (MineButton[i - 1][j - 1].isMine == true) 
						{
							count += 1; 
						}
					}
					if ( (i - 1 >= 0)) //check the top corner
					{
						if (MineButton[i - 1][ j ].isMine == true) 
						{
							count += 1; 
						}
					}
					if ( (i - 1 >= 0) && (j + 1 <= (int)Math.sqrt(BlockNum)-1)) //check the top right corner
					{
						if (MineButton[i - 1][j + 1] .isMine == true) 
						{
							count += 1;
						}
					}
					if ( (j - 1 >= 0)) //check the left corner
					{
						if (MineButton[ i ][j - 1] .isMine == true) 
						{
							count += 1; 
						}
					}
					if  (j + 1 <= (int)Math.sqrt(BlockNum)-1)//check the right corner
					{
						if (MineButton[ i ][j + 1].isMine == true) 
						{
							count += 1;
						}
					}
					if ( (j - 1 >= 0) && (i + 1 <= (int)Math.sqrt(BlockNum)-1))//check the down left corner
					{
						if (MineButton[i + 1][j - 1].isMine == true) 
						{
							count += 1;
						}
					}
					if ( (i + 1 <= (int)Math.sqrt(BlockNum)-1))//check the down corner
					{
						if (MineButton[i + 1][ j ].isMine == true) 
						{
							count += 1; 
						}
					}
					if ( (j + 1 <= (int)Math.sqrt(BlockNum)-1) && (i + 1 <= Math.sqrt(BlockNum)-1))//check the down right corner
					{
						if (MineButton[i + 1][j + 1].isMine == true) 
						{
							count += 1; 
						}
					}
					MineButton[ i ][ j ].MineRoundCount = count;
				}
			}
		}
	}

/**Stepping on a square that has no neighbors that contain mines should cause all neighbors of the square to reveal themselves**/
	public void isNull(Mine[][]MineButton,Mine ClickecButton) 
	{
		int i,j;
		i=ClickecButton.num_x;
		j=ClickecButton.num_y;
		if (ClickecButton.isMine==true) 
		{}
		else 
		{
			if ( (i - 1 >= 0) && (j - 1 >= 0)) //check the top left corner
			{ 
				if (MineButton[i - 1][j - 1].isMine == false &&MineButton[i - 1][j - 1].isClicked == false &&MineButton[i - 1][j - 1].isRight == false) 
				{
					if(MineButton[i - 1][j - 1].MineRoundCount==0)
					{
						MineButton[i - 1][j - 1].setText("");
						MineButton[i - 1][j - 1].setEnabled(false);
						MineButton[ i -1][ j-1 ].isEnabled=false;
						MineButton[i - 1][j - 1].isClicked=true;
						isNull(MineButton,MineButton[i - 1][j - 1]);
					}
					else
					{
						MineButton[i - 1][j - 1].setText
						((MineButton[i - 1][j - 1].MineRoundCount)+"");

⌨️ 快捷键说明

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