1.Block.java 文件
public class Block {
String name ;
int number;
boolean haveMine = false;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setNumber(int number) {
this.number = number ;
}
public int getNumber() {
return number;
}
public void setMine(boolean haveMine) {
this.haveMine = haveMine;
}
public boolean blockHaveMine() {
return this.haveMine;
}
}
2.LayMines.java 文件
import java.util.LinkedList;
public class LayMines {
public void layMine(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 randomIndex = (int)(Math.random()*list.size());
Block b = (Block)(list.get(randomIndex));
b.setName("雷");
b.setMine(true);
list.remove(randomIndex);
mineCount --;
}
//每个非雷的 block 设置其显示的雷数目
for(int i=0;i<row;i++) {
for(int j=0;j<column;j++) {
if(block[i][j].blockHaveMine()) {}
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++) {
mineNumber ++;
}
}
block[i][j].setName(""+mineNumber);
block[i][j].setNumber(mineNumber);
}
}
}
}
}
3.BlockView.java 文件
import java.awt.*;;
public class BlockView extends Panel{
Label blockName;
Button blockCover;
CardLayout card ;
BlockView() {
card = new CardLayout();
setLayout(card);
blockName = new Label();
blockCover = new Button();
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");
validate();
}
public void seeBlockCover() {
card.show(this, "cover");
validate();
}
public Button getBlockCover() {
return blockCover;
}
}
4.MineFrame.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MineFrame extends Frame implements ActionListener {
Button reStart;
Block[][] block;
BlockView[][] blockView;
LayMines lay;
int row = 10;
int column = 12;
int mineCount = 22;
int colorSwith = 0;
Panel pCenter , pNorth;
public MineFrame() {
reStart = new Button("重新开始");
pCenter = new Panel();
pNorth = new Panel();
pNorth.setBackground(Color.cyan);
block = new Block[row][column];
for(int i=0;i<row;i++) {
for(int j=0;j<column;j++) {
block[i][j] = new Block();
}
}
lay = new LayMines();
lay.layMine(block, mineCount);
blockView = new BlockView[row][column];
pCenter.setLayout(new GridLayout(row,column));
for(int i=0;i<row;i++) {
for(int j=0;j<column;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);
}
}
reStart.addActionListener(this);
pNorth.add(reStart);
add(pNorth,BorderLayout.NORTH);
add(pCenter,BorderLayout.CENTER);
setSize(200,232);
setLocation(100,100);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
validate();
评论0