📄 jmine.java
字号:
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
// the methods to check if there were mines, to be nested
void checkMine(int col, int row, int colCount, int rowCount){
int i, j;
j = col < 0 ? 0 : col;
j = j > colCount-1 ? colCount-1 : j;
i = row < 0 ? 0 : row;
i = i > rowCount-1 ? rowCount-1 : i;
//System.out.println("Check Mine row:"+i + ",col:" +j);
if (mine.mine[j][i] == 9) {
bomb(j, i,colCount,rowCount);
} else if (mine.mine[j][i] == 0
&& mineButton[j][i].getClickFlag() == false) {
mineButton[j][i].setClickFlag(true);
showLabel(j, i);
for (int ii = i - 1; ii <= i + 1; ii++)
for (int jj = j - 1; jj <= j + 1; jj++)
checkMine(jj, ii, colCount, rowCount);
} else {
showLabel(j, i);
mineButton[j][i].setClickFlag(true);
}
if (isWin(colCount, rowCount)) {
win();
}
}
private void clearAll(int col, int row , int colCount, int rowCount ){
int top, bottom, left, right;
left = col - 1 > 0 ? col - 1 : 0;
right = col + 1 < colCount ? col + 1 : colCount - 1;
top = row - 1 > 0 ? row - 1 : 0;
bottom = row + 1 < rowCount ? row + 1 : rowCount - 1;
if (countFlagedMine(col, row, colCount, rowCount)==mine.mine[col][row]){
for (int i = top; i <= bottom; i++) {
for (int j = left; j <= right; j++) {
if (mineButton[j][i].getFlag() != 1)
checkMine(j, i,colCount, rowCount);
}
}
}
}
public int countFlagedMine(int col, int row, int colCount, int rowCount){
int top, bottom, left, right;
left = col - 1 > 0 ? col - 1 : 0;
right = col + 1 < colCount ? col + 1 : colCount - 1;
top = row - 1 > 0 ? row - 1 : 0;
bottom = row + 1 < rowCount ? row + 1 : rowCount - 1;
int count = 0;
for (int i = top; i <= bottom; i++) {
for (int j = left; j <= right; j++) {
if (mineButton[j][i].getFlag() == 1)
count++;
}
}
System.out.println("JMine::CountFlagedMine::count::"+ count);
return count;
}
// to flag the mine you want to flag out
void flagMine(int col, int row, int colCount, int rowCount) {
int i, j;
j = col < 0 ? 0 : col;
j = j > colCount-1 ? colCount-1 : j;
i = row < 0 ? 0 : row;
i = i > rowCount-1 ? rowCount-1 : i;
if (mineButton[j][i].getFlag() == 0) {
numFlaged++;
} else if (mineButton[j][i].getFlag() == 1) {
numFlaged--;
}
mineCounter.resetCounter(numMine - numFlaged >= 0 ? numMine - numFlaged : 0);
mineButton[j][i].setFlag((mineButton[j][i].getFlag() + 1) % 3);
showFlag(j, i);
if (isWin(colCount, rowCount)) {
win();
}
}
// check if you win() {
private boolean isWin(int colCount, int rowCount) {
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
if (mine.mine[j][i] == 9 && mineButton[j][i].getFlag() != 1) {
return (false);
}
if (mine.mine[j][i] != 9 && mineButton[j][i].getFlag() == 1) {
return (false);
}
if (mine.mine[j][i] != 9
&& mineButton[j][i].getClickFlag() == false) {
return (false);
}
}
}
return (true);
}
// the event handle to deal with the mouse click
public void mouseClicked(MouseEvent e) {
if (e.getSource() == bTest) {
setNewGame(numMine,width,height);
return;
}else if (e.getSource()==getCustomerFrame().getOk()){
numMine = getCustomerFrame().getMineNum();
width = getCustomerFrame().getColCount();
height = getCustomerFrame().getRowCount();
setNewGame(numMine,width,height);
return;
}
int col, row;
col = ((JMineButton) e.getSource()).getCol();
row = ((JMineButton) e.getSource()).getRow();
if (!gameStarted) {
startNewGame(numMine, col, row, width,height);
}
// this block is detect the two mouse button down
// whether there is two mouse click within 150 micro seconds
// we will clear all the cells beside this button
nowClick = System.currentTimeMillis();
if (lastClick == 0L) {
lastClick = nowClick;
}else if(nowClick - lastClick <= 150L){
lastClick = 0L;
System.out.println("HA1");
clearAll(col, row,width,height);
return;
}else if(nowClick - lastClick > 150L) {
lastClick = nowClick;
}
// the following is detect the second mouse button down
// if so we regard this same as two mouse button down
if (e.getButton()==MouseEvent.BUTTON2) {
System.out.println("HA");
clearAll(col, row,width,height);
return;
}if (!mineButton[col][row].getClickFlag()) {
if (e.getModifiers() == InputEvent.BUTTON1_MASK) {
if (mineButton[col][row].getFlag() == 1) {
return;
} else {
checkMine(col, row,width,height);
}
} else if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
flagMine(col, row,width,height);
} else {
}
}
}
// the mouse events listener methods
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
// reset the game
private void resetAll() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
mineButton[j][i].setFlag(0);
mineButton[j][i].setClickFlag(false);
mineButton[j][i].setIcon(mineStatus[0]);
mineButton[j][i].setEnabled(true);
mineButton[j][i].setVisible(true);
}
}
}
public void setNewGame(int num, int colCount, int rowCount) {
resetAll();
numMine = num;
numFlaged = 0;
gameStarted = false;
mineCounter.resetCounter(numMine);
timeCounter.resetCounter(0);
timer.cancel();
setSize(250+(colCount-10)*24, 350 +(rowCount-10)*24);
setLocation(200-(colCount-10)*6, 200-(colCount-10)*6);
pane.removeAll();
// Control Panel
controlPane = new JPanel();
bTest = new JButton(faceIcon[0]);
bTest.setSize(26, 27);
bTest.setMargin(space);
bTest.addMouseListener(this);
bTest.setPressedIcon(faceIcon[1]);
mineCounter = new JCounter(numMine);
timeCounter = new JCounter();
controlPane.add(mineCounter);
controlPane.add(bTest);
controlPane.add(timeCounter);
buildConstraints(constraints, 0, 0, colCount, 2, 100, 100);
gridbag.setConstraints(controlPane, constraints);
pane.add(controlPane);
mineButton = new JMineButton[colCount][rowCount];
for (int i = 0; i < rowCount; i++) {
for (int j = 0; j < colCount; j++) {
mineButton[j][i] = new JMineButton(j, i, blankIcon);
mineButton[j][i].addMouseListener(this);
mineButton[j][i].setMargin(space);
buildConstraints(constraints, j, i + 3, 1, 1, 80, 80);
gridbag.setConstraints(mineButton[j][i], constraints);
pane.add(mineButton[j][i]);
}
}
setContentPane(pane);
repaint();
}
// circle the flag with blank, flaged, questioned
void showFlag(int col, int row) {
mineButton[col][row]
.setIcon(mineStatus[mineButton[col][row].getFlag()]);
}
// show the numbers of the nearby mines
void showLabel(int col, int row) {
//System.out.println("ShowLabel row:" + row + ",col:" + col);
int toShow;
toShow = mine.mine[col][row];
if (toShow != 0) {
mineButton[col][row].setIcon(mineNumIcon[toShow]);
mineButton[col][row].setClickFlag(true);
} else {
mineButton[col][row].setIcon(mineNumIcon[0]);
mineButton[col][row].setEnabled(false);
}
}
// method to start the new game
private void startNewGame(int num, int col, int row, int colCount, int rowCount){
mine = new JMineArth(num, col, row, colCount, rowCount);
gameStarted = true;
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask(){
public void run() {
timeCounter.counterAdd();
}
},1000,1000);
}
// You Win
private void win(){
timer.cancel();
winFrame.setVisible(true);
winTimer.schedule(new TimerTask(){
public void run() {
while(!winFrame.getWinOk()){
}
if (winFrame.isCustomer()){
setNewGame(winFrame.getCustomerFrame().getMineNum(),
winFrame.getCustomerFrame().getColCount(),
winFrame.getCustomerFrame().getRowCount());
}else {
setNewGame(winFrame.getMineNum(),width,height);
}
winFrame.setVisible(false);
this.cancel();
winFrame.setWinOk(false);
}
},0L);
}
public CustomerFrame getCustomerFrame() {
return customerFrame;
}
public void setCustomerFrame(CustomerFrame customerFrame) {
this.customerFrame = customerFrame;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -