packagecom.TreeDream.MyGame;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.GridLayout;importjava.awt.Label;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.security.cert.TrustAnchor;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTable;importjavax.swing.JTextField;importjavax.swing.RowFilter;public class MineMainFrame extends JFrame implementsActionListener {
JTextField inputRow= new JTextField(5);
JTextField inputColum= new JTextField(5);
JTextField inputMineCount= new JTextField(5);
JButton start;
Block block[][];
BlockView blockView[][];booleanvis[][];introw, colum, mineCount;
JPanel pCenter, pNorth;
LayMines lay;publicMineMainFrame(String title) {
setTitle(title);
start= new JButton("开始");
pCenter= newJPanel();
pNorth= newJPanel();
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);
}
@Overridepublic voidactionPerformed(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= newBlock[row][colum];
vis= new boolean[row][colum];for (int i = 0; i < row; i++) {for (int j = 0; j < colum; j++) {
block[i][j]= newBlock();
vis[i][j]= false;
}
}
lay= newLayMines();
lay.layMinesForBlock(block, mineCount);
blockView= newBlockView[row][colum];
pCenter.setLayout(newGridLayout(row, colum));for (int i = 0; i < row; i++) {for (int j = 0; j < colum; j++) {
blockView[i][j]= newBlockView();
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,inty) {
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=0&&dy
dfs(dx, dy);else{
vis[dx][dy]= true;
blockView[dx][dy].seeBlockName();
}
}
}
}public static voidmain(String[] args) {new MineMainFrame("winmine");
}
}