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

📄 minegame.java

📁 一个JAVA扫雷程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MineGame extends Frame implements ActionListener
{
	public ImageIcon win=new ImageIcon("win.gif");
	public ImageIcon fail=new ImageIcon("fail.gif");
	public ImageIcon begin=new ImageIcon("begin.gif");
	public ImageIcon common=new ImageIcon("common.gif");
	
	private InputDialog id;
	private ShowDialog sd;
	
	MenuBar mb=new MenuBar();
	Menu m_G=new Menu("游戏");
	Menu m_H=new Menu("帮助");
	MenuItem m1=new MenuItem("开始");
	MenuItem m2=new MenuItem("简单");
	MenuItem m3=new MenuItem("中等");
	MenuItem m4=new MenuItem("困难");
	MenuItem m5=new MenuItem("变态");
	MenuItem m6=new MenuItem("自定义");
	MenuItem m7=new MenuItem("退出");
	MenuItem m10=new MenuItem("帮助");
	Label l=new Label("",Label.LEFT);
	Label l2=new Label("",Label.RIGHT);
	JButton b=new JButton();
	Panel pn=new Panel();
	Game game;
	
	private MineGame()
	{
		m1.addActionListener(this);
		m2.addActionListener(this);
		m3.addActionListener(this);
		m4.addActionListener(this);
		m5.addActionListener(this);
		m6.addActionListener(this);
		m7.addActionListener(this);
		m10.addActionListener(this);
		m_G.add(m1);
		m_G.addSeparator();
		m_G.add(m2);
		m_G.add(m3);
		m_G.add(m4);
		m_G.add(m5);
		m_G.add(m6);
		m_G.addSeparator();
		m_G.add(m7);
		m_H.add(m10);
		mb.add(m_G);
		mb.add(m_H);
		setMenuBar(mb);
		b.addActionListener(this);
		b.setIcon(common);
		pn.add(l);
		pn.add(b);
		pn.add(l2);
		add("North",pn);
		game=new Game(this);
		add("Center",game);
		addWindowListener(new CloseWindow());
	}
	
	public static void main(String args[])
	{
		MineGame f=new MineGame();
		f.InitDlg(f);
		f.setTitle("JAVA扫雷 - SleepySoft");
		f.setBounds(200,100,800,600);
		f.setVisible(true);
	}
	
	private void InitDlg(MineGame mg)
	{
		id=new InputDialog(mg,"自定义雷区",true);
		sd=new ShowDialog(mg,"说明",true);
	}
	
	public void bIcon(String s)
	{
		if ("WIN"==s) b.setIcon(win);
		else if ("FAIL"==s) b.setIcon(fail);
		else b.setIcon(common);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource().equals(m1))
			game.Start(0);
		else if (e.getSource().equals(m2))
			game.Start(0);
		else if (e.getSource().equals(m3))
			game.Start(1);
		else if (e.getSource().equals(m4))
			game.Start(2);
		else if (e.getSource().equals(m5))
			game.Start(3);
		else if (e.getSource().equals(m6))
		{
			id.show();
			game.Start(id.mi,id.mj,id.mc);
			return;
		}
		else if (e.getSource().equals(m7))
			System.exit(0);
		else if (e.getSource().equals(m10))
		{
			sd.show();
			return;
		}
		else if (e.getSource().equals(b))
			if (game.Start()) return;
		b.setIcon(begin);
		add("Center",game);
	}
}

//*************************************************************Dialog*************************************************************

class InputDialog extends Dialog implements ActionListener
{
	Label l1=new Label("行数:");
	Label l2=new Label("列数:");
	Label l3=new Label("雷数:");
	TextField tf1=new TextField(5);
	TextField tf2=new TextField(5);
	TextField tf3=new TextField(5);
	Button b1=new Button("确定");
	Button b2=new Button("取消");
	
	int mi,mj,mc;
	MineGame mg;
	
	public InputDialog(MineGame owner,String title,boolean modal)
	{
		super(((Frame)owner),title,modal);
		mg=owner;
		setLayout(new GridLayout(4,2));
		add(l1);
		add(tf1);
		add(l2);
		add(tf2);
		add(l3);
		add(tf3);
		b1.addActionListener(this);
		b2.addActionListener(this);
		add(b1);
		add(b2);
		setSize(200,150);
		setLocation(400,300);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource().equals(b2))
		{
			hide();
			return;
		}
		try
		{
			mi=Integer.parseInt(tf1.getText());
			mj=Integer.parseInt(tf2.getText());
			mc=Integer.parseInt(tf3.getText());
		}
		catch(NumberFormatException e2)
		{
			return;
		}
		if (0==mi || 0==mj || 0==mc) 
			return;
		if (mi*mj>400) return;
		if (mi*mj<mc) return;
		hide();
//		mg.game.Start(mi,mj,mc);
	}
}


class ShowDialog extends Dialog implements ActionListener
{
	Label l=new Label("JAVA扫雷,Program By SleepySoft,玩法请参考Windows内置的扫雷。",Label.CENTER);
	Button b=new Button("确定");
	
	public ShowDialog(MineGame owner,String title,boolean modal)
	{
		super(((Frame)owner),title,modal);
		b.addActionListener(this);
		add("North",l);
		add("South",b);
		setSize(400,150);
		setLocation(400,300);
	}
	public void actionPerformed(ActionEvent e)
	{
		hide();
	}
}


class CloseWindow extends WindowAdapter
{
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
}


//****************************************************以下为Game.java里面的内容***************************************************

class Game extends Panel
{
	public int Minei,Minej,MineCountC,MineCount,AllCount,AllCountC;
	public Vector MineVector=new Vector();
	public MineGame mg;
	
	public Game(MineGame m)
	{
		mg=m;
	}
	
	public Game(MineGame minegame,int level)
	{
		mg=minegame;
		Start(level);
	}
	
	private boolean SetLevel(int level)
	{
		if (level<0 && level>3) return true;
		if (0==level) 
		{
			Minei=5;
			Minej=5;
			MineCountC=5;
		}
		else if (1==level)
		{
			Minei=8;
			Minej=6;
			MineCountC=12;
		}
		else if (2==level)
		{
			Minei=10;
			Minej=8;
			MineCountC=25;
		}
		else if (3==level)
		{
			Minei=12;
			Minej=10;
			MineCountC=40;
		}
		return false;
	}
	
	public boolean Start(int mi,int mj,int mc)
	{
			Minei=mi;
			Minej=mj;
			MineCountC=mc;
			return Start();
	}
	
	public boolean Start(int level)
	{
		if (SetLevel(level)) return true;
		return Start();
	}
	
	public boolean Start()
	{
		if (0==Minei && 0==Minej) return true;
		
		AllCountC=Minei*Minej;
		AllCount=AllCountC;
		removeAll();
		setLayout(new GridLayout(Minej,Minei));
		
		MineVector.setSize(AllCountC);
		MineVector.removeAllElements();
		Mine m;
		int c;
		for (c=0;c<AllCountC;c++)
		{
			m=new Mine(this);
			add(m);
			MineVector.addElement(m);
			m.setName(new Integer(c).toString());
			m.addMouseListener(new MineListener(m));
			m.addActionListener(new MineListener(m));
		}
		
		int i;
		c=0;
		i=0;
		while(c<MineCountC)
		{
//随机保质保量生成雷
			if (Math.random()<0.05)
				if (!((Mine)MineVector.elementAt(i)).mine) 
				{
					((Mine)MineVector.elementAt(i)).mine=true;
					c++;
				}
			i++;
			if (i>=AllCountC) i=0;
		}
//显示雷数,为确保数量,故用c为MineCount赋值
		MineCount=c;
		mg.l.setText(new Integer(MineCount).toString());
		setEnabled(true);
//不这样按钮出不来
		mg.setSize(0,0);
		mg.setSize(50*Minei,70+50*Minej);
		return false;
	}
	
	public void Win()
	{
		mg.l.setText("获胜!");
		mg.bIcon("WIN");
		setEnabled(false);
	}
	
	public void Fail()
	{
		mg.l.setText("失败!");
		mg.bIcon("FAIL");
		setEnabled(false);
	}
}

class MineListener extends MouseAdapter implements ActionListener
{
	private Mine Self;
	
	public MineListener(Mine self)
	{
		Self=self;
	}
	public void mousePressed(MouseEvent e)
	{
		if (e.getModifiers()==MouseEvent.BUTTON1_MASK)
		{
		}
		else
		{
			Self.Sign();
		}
	}
	public void actionPerformed(ActionEvent e)
	{
		Self.Dig();
	}
}


//**************************************************以下为Mine.java里面的内容*****************************************************

class Mine extends JButton
{
	final byte STATE_COMMON=0;
	final byte STATE_SIGN=1;
	final byte STATE_QUERY=2;
	final byte STATE_DIGGED=3;
	
	private ImageIcon bomb=new ImageIcon("bomb.gif");
	private ImageIcon query=new ImageIcon("query.gif");
	private ImageIcon sign=new ImageIcon("sign.gif");
	private ImageIcon blank=new ImageIcon("blank.gif");
	private ImageIcon none=new ImageIcon("");
	
	private byte	state;
	public boolean mine;
	public Game game;
	
	public Mine(Game g)
	{
		state=STATE_COMMON;
		game=g;
	}
	public void Dig()
	{
		int[] around=new int[9];
		
		if (STATE_DIGGED==state ||STATE_SIGN==state) return;
		if (mine) 
		{
			setIcon(bomb);
			ShowAll();
			game.Fail();
			return;
		}
		state=STATE_DIGGED;
		game.AllCount--;
		if (game.AllCount<=game.MineCountC) game.Win();
		
		try
		{
			around[4]=Integer.parseInt(getName());
		}
		catch(NumberFormatException e)
		{
			around[4]=0;
		}
//计算周围的Mine在Vector里的序数
		for (int i=0,x=0,y=0;i<9;i++)
		{
//计算时注意排除边界的区域
			x=around[4]%game.Minei+(-1+i%3);
			y=around[4]/game.Minei+(-1+i/3);
			if (x>=0 && x<game.Minei &&y>=0 && y<game.Minej) 
				around[i]=x+y*game.Minei;
			else
				around[i]=-1;
		}
				
		int c=0;
		for (int i=0;i<9;i++)
			if (around[i]>=0 && around[i]<game.AllCountC)
				if (((Mine)game.MineVector.elementAt(around[i])).mine) c++;
			
		if (0!=c)
		{
//如果周围有雷,那么显示数字
		setLabel(new Integer(c).toString());
		}
		else
		{
			setIcon(blank);
//否则挖开附近的
			for (int i=0;i<9;i++)
				if (around[i]>=0 && around[i]<game.AllCountC)
					if (STATE_DIGGED!=((Mine)game.MineVector.elementAt(around[i])).state) ((Mine)game.MineVector.elementAt(around[i])).Dig();
		}
	}
	public void Sign()
	{
		if (STATE_DIGGED==state) return;
		if (STATE_SIGN==state) 
		{
			game.MineCount++;
			game.mg.l.setText(new Integer(game.MineCount).toString());
		}
		state++;
		if (state>=3) state=0;
		if (1==state) 
		{
			game.MineCount--;
			game.mg.l.setText(new Integer(game.MineCount).toString());
			setIcon(sign);
		}
		else if (2==state) setIcon(query);
		else setIcon(none);
		repaint();
	}
	public void Show()
	{
		if (mine) setIcon(bomb);
	}
	public void ShowAll()
	{
		for (int i=0;i<game.AllCountC;i++)
			((Mine)game.MineVector.elementAt(i)).Show();
	}
}



⌨️ 快捷键说明

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