首页 > 代码库 > Java课程设计——扫雷(winmine)

Java课程设计——扫雷(winmine)

因为是我的课程设计,要是有冲突就不好了,转载注明出处!!!

程序很简单,毕竟我是搞acm的,我就只介绍一下闪光点。

中心空白搜索的时候,我用的DFS;

API文档之后补上。

技术分享
package com.TreeDream.MyGame;public class Block {        String name;    int number;    boolean boo = false;        public void setName(String name) {        this.name = name;    }        public void setNumber(int n) {        number = n;    }        public int getNumber() {        return number;    }        public String getName() {        return name;    }        public void setIsMine(boolean boo) {        this.boo = boo;    }        public boolean isMine() {        return boo;    }            }
Block.java
技术分享
package com.TreeDream.MyGame;import java.awt.CardLayout;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JPanel;public class BlockView extends JPanel {    JLabel blockName;    //标签    JButton blockCover;    //按钮    CardLayout card;        public BlockView() {        card = new CardLayout();        setLayout(card);                blockName = new JLabel();        blockCover = new JButton();                add("cover",blockCover);        add("name",blockName);            }        public void setName(String name) {        blockName.setText(name);    }        public String getName() {        return blockName.getText();    }        public void seeBlockName() {        card.show(this, "name");    }        public void seeBlockCover() {        card.show(this, "cover");        validate();    }        public JButton getBlockCover() {        return blockCover;    }    }
BlockView
技术分享
package com.TreeDream.MyGame;import java.util.LinkedList;  public class LayMines {             public void layMinesForBlock(Block[][] block,int mineCount){                int row=block.length;        int colum=block[0].length;        LinkedList<Block> list=new LinkedList<Block>();//创建空链表                        for(int i=0;i<row;i++){            for(int j=0;j<colum;j++){                list.add(block[i][j]);            }        }        while(mineCount>0){            int size=list.size();            int randomIndex=(int)(Math.random()*size);            Block b=(Block)list.get(randomIndex);            b.setName("*");            b.setIsMine(true);            list.remove(randomIndex);            mineCount--;        }                        for(int i=0;i<row;i++){//非雷            for(int j=0;j<colum;j++){                if(block[i][j].isMine()==false){                    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,colum-1);t++){                            if(block[k][t].isMine()==true){                                mineNumber++;                            }                        }                    }                                        if(mineNumber==0){                         block[i][j].setName("");                         block[i][j].setNumber(mineNumber);                    }                                           else{                        block[i][j].setName(""+mineNumber);                        block[i][j].setNumber(mineNumber);                    }                                    }            }        }    }    }
LayMines
技术分享
package com.TreeDream.MyGame;import java.awt.BorderLayout;import java.awt.Color;import java.awt.GridLayout;import java.awt.Label;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.security.cert.TrustAnchor;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTable;import javax.swing.JTextField;import javax.swing.RowFilter;public class MineMainFrame extends JFrame implements ActionListener {    JTextField inputRow = new JTextField(5);    JTextField inputColum = new JTextField(5);    JTextField inputMineCount = new JTextField(5);    JButton start;    Block block[][];    BlockView blockView[][];        boolean vis[][];    int row, colum, mineCount;    JPanel pCenter, pNorth;    LayMines lay;    public MineMainFrame(String title) {        setTitle(title);                start = new JButton("开始");        pCenter = new JPanel();        pNorth = new JPanel();        pNorth.setBackground(Color.red);                start.addActionListener(this);        inputColum.addActionListener(this);        inputMineCount.addActionListener(this);        inputRow.addActionListener(this);                pNorth.add(new JLabel("row"));        pNorth.add(inputRow);        pNorth.add(new JLabel("col"));        pNorth.add(inputColum);        pNorth.add(new JLabel("count"));        pNorth.add(inputMineCount);        pNorth.add(start);        add(pNorth, BorderLayout.NORTH);        setBounds(50, 50, 550, 550);        setVisible(true);        validate();        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);    }    @Override    public void actionPerformed(ActionEvent e) {        String str = e.getActionCommand();        if (e.getSource() == inputRow) {            row = Integer.parseInt(str);            System.out.println(row);        }        if (e.getSource() == inputColum) {            colum = Integer.parseInt(str);            System.out.println(colum);        }        if (e.getSource() == inputMineCount) {            mineCount = Integer.parseInt(str);            System.out.println(mineCount);        }        else {            JButton source = (JButton) e.getSource();            if (source != start) {                int m = -1, n = -1;                for (int i = 0; i < row; i++) {                    for (int j = 0; j < colum; j++) {                        if (source == 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().removeActionListener(this);                            if (block[i][j].isMine()) {                                blockView[i][j].seeBlockName();                            }                        }                    }                } else {                    if (block[m][n].getNumber() > 0) {                        vis[m][n] = true;                        blockView[m][n].seeBlockName();                    } else if (block[m][n].getNumber() == 0) {                        dfs(m,n);                    }                }            }            if (source == start) {                System.out.println("hello");                block = new Block[row][colum];                vis = new boolean[row][colum];                                for (int i = 0; i < row; i++) {                    for (int j = 0; j < colum; j++) {                        block[i][j] = new Block();                        vis[i][j] = false;                    }                }                lay = new LayMines();                lay.layMinesForBlock(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].setName(block[i][j].getName());                        pCenter.add(blockView[i][j]);                        blockView[i][j].getBlockCover().addActionListener(this);                    }                }                                add(pCenter, BorderLayout.CENTER);                setVisible(true);                validate();            }        }    }        int dr [] = {-1,-1,-1,0,0,1,1,1};    int dc [] = {-1,0,1,-1,1,-1,0,1};        public void dfs(int x,int y) {                vis[x][y] = true;        blockView[x][y].seeBlockName();                for(int i=0;i<8;i++) {            int dx = x + dr[i];            int dy = y + dc[i];                        if(dx>=0&&dx<row&&dy>=0&&dy<colum&&!block[dx][dy].isMine()&&!vis[dx][dy]) {                if(block[dx][dy].getNumber()==0)                    dfs(dx, dy);                else {                    vis[dx][dy] = true;                    blockView[dx][dy].seeBlockName();                }            }        }            }        public static void main(String[] args) {        new MineMainFrame("winmine");    }}
MineMainFrame

Java课程设计——扫雷(winmine)