当前位置:文档之家› Java语言 扫雷游戏完整源代码

Java语言 扫雷游戏完整源代码

import javax.swing.ImageIcon;public class Block {String name; //名字,比如"雷"或数字int aroundMineNumber; //周围雷的数目ImageIcon mineIcon; //雷的图标boolean isMine=false; //是否是雷boolean isMark=false; //是否被标记boolean isOpen=false; //是否被挖开public void setName(String name) {=name;}public void setAroundMineNumber(int n) {aroundMineNumber=n;}public int getAroundMineNumber() {return aroundMineNumber;}public String getName() {return name;}public boolean isMine() {return isMine;}public void setIsMine(boolean b) {isMine=b;}public void setMineIcon(ImageIcon icon){mineIcon=icon;}public ImageIcon getMineicon(){return mineIcon;}public boolean getIsOpen() {return isOpen;}public void setIsOpen(boolean p) {isOpen=p;}public boolean getIsMark() {return isMark;}public void setIsMark(boolean m) {isMark=m;}}import java.util.*;import javax.swing.*;public class LayMines{ImageIcon mineIcon;LayMines() {mineIcon=new ImageIcon("mine.gif");}public void layMinesForBlock(Block block[][],int mineCount){ int row=block.length;int column=block[0].length;LinkedList<Block> list=new LinkedList<Block>();for(int i=0;i<row;i++) {for(int j=0;j<column;j++)list.add(block[i][j]);}while(mineCount>0){int size=list.size(); // list返回节点的个数int randomIndex=(int)(Math.random()*size);Block b=list.get(randomIndex);b.setIsMine(true);b.setName("雷");b.setMineIcon(mineIcon);list.remove(randomIndex); //list删除索引值为randomIndex的节点mineCount--;}for(int i=0;i<row;i++){for(int j=0;j<column;j++){if(block[i][j].isMine()){block[i][j].setIsOpen(false);block[i][j].setIsMark(false);}else {int mineNumber=0;for(int k=Math.max(i-1,0);k<=Math.min(i+1,row-1);k++) {for(int t=Math.max(j-1,0);t<=Math.min(j+1,column-1);t++){if(block[k][t].isMine())mineNumber++;}}block[i][j].setIsOpen(false);block[i][j].setIsMark(false);block[i][j].setName(""+mineNumber);block[i][j].setAroundMineNumber(mineNumber);}}}}}import java.awt.*;import java.awt.event.*;import javax.swing.*;public class MineArea extends JPanel implements ActionListener,MouseListener{ JButton reStart;Block [][] block;BlockView [][] blockView;LayMines lay;int row,colum,mineCount,markMount; //雷区的行数、列数以及地雷个数和用户给出的标记数ImageIcon mark;int grade;JPanel pCenter,pNorth;JTextField showTime,showMarkedMineCount; //显示用时以及标记数Timer time; //计时器int spendTime=0;Record record;public MineArea(int row,int colum,int mineCount,int grade) {reStart=new JButton("重新开始");mark=new ImageIcon("mark.gif"); //探雷标记time=new Timer(1000,this);showTime=new JTextField(5);showMarkedMineCount=new JTextField(5);showTime.setHorizontalAlignment(JTextField.CENTER);showMarkedMineCount.setHorizontalAlignment(JTextField.CENTER);showMarkedMineCount.setFont(new Font("Arial",Font.BOLD,16));showTime.setFont(new Font("Arial",Font.BOLD,16));pCenter=new JPanel();pNorth=new JPanel();lay=new LayMines();initMineArea(row,colum,mineCount,grade); //初始化雷区,见下面的LayMines()reStart.addActionListener(this);pNorth.add(showMarkedMineCount);pNorth.add(reStart);pNorth.add(showTime);setLayout(new BorderLayout());add(pNorth,BorderLayout.NORTH);add(pCenter,BorderLayout.CENTER);}public void initMineArea(int row,int colum,int mineCount,int grade){ pCenter.removeAll();spendTime=0;markMount=mineCount;this.row=row;this.colum=colum;this.mineCount=mineCount;this.grade=grade;block=new Block[row][colum];for(int i=0;i<row;i++){for(int j=0;j<colum;j++)block[i][j]=new Block();}yMinesForBlock(block,mineCount);blockView=new BlockView[row][colum];pCenter.setLayout(new GridLayout(row,colum));for(int i=0;i<row;i++) {for(int j=0;j<colum;j++) {blockView[i][j]=new BlockView();blockView[i][j].giveView(block[i][j]); //给block[i][j]提供视图pCenter.add(blockView[i][j]);blockView[i][j].getBlockCover().addActionListener(this);blockView[i][j].getBlockCover().addMouseListener(this);blockView[i][j].seeBlockCover();blockView[i][j].getBlockCover().setEnabled(true);blockView[i][j].getBlockCover().setIcon(null);}}showMarkedMineCount.setText(""+markMount);validate();}public void setRow(int row){this.row=row;}public void setColum(int colum){this.colum=colum;}public void setMineCount(int mineCount){this.mineCount=mineCount;}public void setGrade(int grade) {this.grade=grade;}public void actionPerformed(ActionEvent e) {if(e.getSource()!=reStart&&e.getSource()!=time) {time.start();int m=-1,n=-1;for(int i=0;i<row;i++) {for(int j=0;j<colum;j++) {if(e.getSource()==blockView[i][j].getBlockCover()){m=i;n=j;break;}}}if(block[m][n].isMine()) {for(int i=0;i<row;i++) {for(int j=0;j<colum;j++) {blockView[i][j].getBlockCover().setEnabled(false);if(block[i][j].isMine())blockView[i][j].seeBlockNameOrIcon();}}time.stop();spendTime=0;markMount=mineCount;}else {show(m,n); //见本类后面的show方法}}if(e.getSource()==reStart) {initMineArea(row,colum,mineCount,grade);}if(e.getSource()==time){spendTime++;showTime.setText(""+spendTime);}inquireWin();}public void show(int m,int n) {if(block[m][n].getAroundMineNumber()>0&&block[m][n].getIsOpen()==false){ blockView[m][n].seeBlockNameOrIcon();block[m][n].setIsOpen(true);return;}elseif(block[m][n].getAroundMineNumber()==0&&block[m][n].getIsOpen()==false){ blockView[m][n].seeBlockNameOrIcon();block[m][n].setIsOpen(true);for(int k=Math.max(m-1,0);k<=Math.min(m+1,row-1);k++) {for(int t=Math.max(n-1,0);t<=Math.min(n+1,colum-1);t++)show(k,t);}}}public void mousePressed(MouseEvent e){JButton source=(JButton)e.getSource();for(int i=0;i<row;i++) {for(int j=0;j<colum;j++) {if(e.getModifiers()==InputEvent.BUTTON3_MASK&&source==blockView[i][j].getBlockCover()){if(block[i][j].getIsMark()) {source.setIcon(null);block[i][j].setIsMark(false);markMount=markMount+1;showMarkedMineCount.setText(""+markMount);}else{source.setIcon(mark);block[i][j].setIsMark(true);markMount=markMount-1;showMarkedMineCount.setText(""+markMount);}}}}}public void inquireWin(){int number=0;for(int i=0;i<row;i++) {for(int j=0;j<colum;j++) {if(block[i][j].getIsOpen()==false)number++;}}if(number==mineCount){time.stop();record=new Record();switch(grade){case 1: record.setGrade("初级");break;case 2: record.setGrade("中级");break;case 3: record.setGrade("高级");break;}record.setTime(spendTime);record.setVisible(true);}}public void mouseReleased(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){}public void mouseClicked(MouseEvent e){}}import java.awt.event.*;import java.awt.*;import javax.swing.*;import javax.swing.border.*;import java.util.*;import java.io.*;public class MineGame extends JFrame implements ActionListener{ JMenuBar bar;JMenu fileMenu;JMenuItem 初级,中级,高级,扫雷英雄榜;MineArea mineArea=null;File 英雄榜=new File("英雄榜.txt");Hashtable hashtable=null;ShowRecord showHeroRecord=null;MineGame(){mineArea=new MineArea(16,16,40,1);add(mineArea,BorderLayout.CENTER);bar=new JMenuBar();fileMenu=new JMenu("游戏");初级=new JMenuItem("初级");中级=new JMenuItem("中级");高级=new JMenuItem("高级");扫雷英雄榜=new JMenuItem("扫雷英雄榜");fileMenu.add(初级);fileMenu.add(中级);fileMenu.add(高级);fileMenu.add(扫雷英雄榜);bar.add(fileMenu);setJMenuBar(bar);初级.addActionListener(this);中级.addActionListener(this);高级.addActionListener(this);扫雷英雄榜.addActionListener(this);hashtable=new Hashtable();hashtable.put("初级","初级#"+999+"#匿名");hashtable.put("中级","中级#"+999+"#匿名");hashtable.put("高级","高级#"+999+"#匿名");if(!英雄榜.exists()) {try{ FileOutputStream out=new FileOutputStream(英雄榜);ObjectOutputStream objectOut=new ObjectOutputStream(out);objectOut.writeObject(hashtable);objectOut.close();out.close();}catch(IOException e){}}showHeroRecord=new ShowRecord(this,hashtable);setBounds(100,100,280,380);setVisible(true);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);validate();}public void actionPerformed(ActionEvent e){if(e.getSource()==初级){mineArea.initMineArea(8,8,10,1);setBounds(100,100,200,280);}if(e.getSource()==中级){mineArea.initMineArea(16,16,40,2);setBounds(100,100,280,380);}if(e.getSource()==高级){mineArea.initMineArea(22,22,99,3);setBounds(100,100,350,390);}if(e.getSource()==扫雷英雄榜){if(showHeroRecord!=null)showHeroRecord.setVisible(true);}validate();}public static void main(String args[]){new MineGame();}}import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class Record extends JDialog implements ActionListener{ int time=0;String grade=null;String key=null;String message=null;JTextField textName;JLabel label=null;JButton 确定,取消;public Record(){setTitle("记录你的成绩");this.time=time;this.grade=grade;setBounds(100,100,240,160);setResizable(false);setModal(true);确定=new JButton("确定");取消=new JButton("取消");textName=new JTextField(8);textName.setText("匿名");确定.addActionListener(this);取消.addActionListener(this);setLayout(new GridLayout(2,1));label=new JLabel("您现在是...高手,输入您的大名上榜");add(label);JPanel p=new JPanel();p.add(textName);p.add(确定);p.add(取消);add(p);setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);}public void setGrade(String grade){this.grade=grade;label.setText("您现在是"+grade+"高手,输入您的大名上榜"); }public void setTime(int time){this.time=time;}public void actionPerformed(ActionEvent e){if(e.getSource()==确定){message=grade+"#"+time+"#"+" "+textName.getText();key=grade;writeRecord(key,message);setVisible(false);}if(e.getSource()==取消){setVisible(false);}}public void writeRecord(String key,String message){File f=new File("英雄榜.txt");try{ FileInputStream in=new FileInputStream(f);ObjectInputStream object_in=new ObjectInputStream(in);Hashtable hashtable=(Hashtable)object_in.readObject();object_in.close();in.close();String temp=(String)hashtable.get(key);StringTokenizer fenxi=new StringTokenizer(temp,"#");fenxi.nextToken();int n=Integer.parseInt(fenxi.nextToken());if(time<n){hashtable.put(key,message);FileOutputStream out=new FileOutputStream(f);ObjectOutputStream object_out=new ObjectOutputStream(out);object_out.writeObject(hashtable);object_out.close();out.close();}}catch(Exception e) {System.out.println(e);}}}import java.io.*;import java.util.*;import javax.swing.*;import java.awt.event.*;import java.awt.*;public class ShowRecord extends JDialog implements ActionListener{ File file=new File("英雄榜.txt");String name=null;Hashtable hashtable=null;JButton 显示,重新记分;JLabel label初级[],label中级[],label高级[];public ShowRecord(JFrame frame,Hashtable h) {setTitle("扫雷英雄榜");hashtable=h;setBounds(100,100,320,185);setResizable(false);setVisible(false);setModal(true);label初级=new JLabel[3];label中级=new JLabel[3];label高级=new JLabel[3];for(int i=0;i<3;i++) {label初级[i]=new JLabel();label初级[i].setBorder(null);label中级[i]=new JLabel();label中级[i].setBorder(null);label高级[i]=new JLabel();label高级[i].setBorder(null);}label初级[0].setText("初级");label初级[1].setText(""+999);label初级[1].setText("匿名");label中级[0].setText("中级");label中级[1].setText(""+999);label中级[1].setText("匿名");label高级[0].setText("高级");label高级[1].setText(""+999);label高级[1].setText("匿名");JPanel pCenter=new JPanel();pCenter.setLayout(new GridLayout(3,3));for(int i=0;i<3;i++)pCenter.add(label初级[i]);for(int i=0;i<3;i++)pCenter.add(label中级[i]);for(int i=0;i<3;i++)pCenter.add(label高级[i]);pCenter.setBorder(BorderFactory.createTitledBorder("扫雷英雄榜"));显示=new JButton("显示成绩");重新记分=new JButton("重新记分");显示.addActionListener(this);重新记分.addActionListener(this);JPanel pSouth=new JPanel();pSouth.setLayout(new FlowLayout(FlowLayout.RIGHT));pSouth.add(重新记分);pSouth.add(显示);add(pCenter,BorderLayout.CENTER);add(pSouth,BorderLayout.SOUTH) ;}public void readAndShow(){try{ FileInputStream in=new FileInputStream(file);ObjectInputStream object_in=new ObjectInputStream(in);hashtable=(Hashtable)object_in.readObject();object_in.close();in.close();String temp=(String)hashtable.get("初级");StringTokenizer fenxi=new StringTokenizer(temp,"#");label初级[0].setText(fenxi.nextToken());label初级[1].setText(fenxi.nextToken());label初级[2].setText(fenxi.nextToken());temp=(String)hashtable.get("中级");fenxi=new StringTokenizer(temp,"#");label中级[0].setText(fenxi.nextToken());label中级[1].setText(fenxi.nextToken());label中级[2].setText(fenxi.nextToken());temp=(String)hashtable.get("高级");fenxi=new StringTokenizer(temp,"#");label高级[0].setText(fenxi.nextToken());label高级[1].setText(fenxi.nextToken());label高级[2].setText(fenxi.nextToken());}catch(Exception e){}}public void actionPerformed(ActionEvent e) {if(e.getSource()==重新记分) {hashtable.put("初级","初级#"+999+"#匿名");label初级[0].setText("初级");label初级[1].setText(""+999);label初级[2].setText("匿名");hashtable.put("中级","中级#"+999+"#匿名");label中级[0].setText("初级");label中级[1].setText(""+999);label中级[2].setText("匿名");hashtable.put("高级","高级#"+999+"#匿名");label高级[0].setText("初级");label高级[1].setText(""+999);label高级[2].setText("匿名");try{ FileOutputStream out=new FileOutputStream(file);ObjectOutputStream object_out=new ObjectOutputStream(out);object_out.writeObject(hashtable);object_out.close();out.close();}catch(IOException event){}setVisible(false);}if(e.getSource()==显示){readAndShow();}}}。

相关主题