java 扫雷 课程设计_Java课程设计——扫雷(winmine)

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");

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值