?? gamemodel.java
字號:
package mine.model;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import mine.listener.*;
import mine.*;
public class GameModel
implements ModelAction {
private BlockModel[][] blockArray;
private Set minePositions;
private GameConfigure gameConfigure = GameConfigure.getInstance();
private int xx;
private int yy;
private int mineleaved;
private int gametime = 0;
private int realmineleaved = gameConfigure.getMineNumber();
private TimeListener gameTimeListener;
private MineLeaveCountListener mineleaveListener;
private InitListener initlistener;
private javax.swing.Timer timer;
private ActionListener timeactionListener;
private Set whenpress = new HashSet();
private Set wrongOptionSet = new HashSet();
public GameModel() {
}
public void setInitListener(InitListener initlistener) {
this.initlistener = initlistener;
}
private int calculateNumber(int x, int y, int xx, int yy) {
int counter = 0;
int position = 0;
//左上
if ( (x - 1) >= 0 && (y - 1) >= 0) {
position = (y - 1) * xx + (x - 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//上
if ( (y - 1) >= 0) {
position = (y - 1) * xx + x;
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//右上
if ( (x + 1) < xx && (y - 1) >= 0) {
position = (y - 1) * xx + (x + 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//左
if ( (x - 1) >= 0) {
position = y * xx + (x - 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//右
if ( (x + 1) < xx) {
position = y * xx + (x + 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//左下
if ( (x - 1) >= 0 && (y + 1) < yy) {
position = (y + 1) * xx + (x - 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//下
if ( (y + 1) < yy) {
position = (y + 1) * xx + x;
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
//右下
if ( (x + 1) < xx && (y + 1) < yy) {
position = (y + 1) * xx + (x + 1);
if (minePositions.contains(new Integer(position))) {
counter++;
}
}
return counter;
}
public void initBlock(int firstX, int firstY) {
whenpress.clear();
wrongOptionSet.clear();
gametime = 0;
xx = gameConfigure.getXx();
yy = gameConfigure.getYy();
int blockcount = xx * yy;
realmineleaved=gameConfigure.getMineNumber();
mineleaved = gameConfigure.getMineNumber(); //
int firstposition = firstY * xx + firstX;
minePositions = getPositions(gameConfigure.getMineNumber(), blockcount,
firstposition);
blockArray = new BlockModel[xx][yy];
for (int x = 0; x < xx; x++) {
for (int y = 0; y < yy; y++) {
blockArray[x][y] = new BlockModel(x, y);
int position = y * xx + x;
if (minePositions.contains(new Integer(position))) { //此位置是雷
blockArray[x][y].setMine(true);
}
else { //此位置不是雷
int numbersize = calculateNumber(x, y, xx, yy);
blockArray[x][y].setNumber(numbersize);
}
}
}
gameTimeListener.gameTimeStart();
initTimer();
}
private void initTimer() {
timeactionListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
gametime++;
gameTimeListener.gameTimeChange(gametime);
}
};
timer = new javax.swing.Timer(1000, timeactionListener);
timer.start();
}
public void open(int x, int y) {
if (blockArray[x][y].isOpened()) {
return; //已經打開直接返回
}
if (blockArray[x][y].isMine()) { //踩雷了
//雷上是否有旗子
blockArray[x][y].setOnmine(true);
blockArray[x][y].setOpened(true); //打開
gameTimeListener.gameTimeEnd(1);
timer.stop();
openAllMine(); //打開所有未打開的雷
openWrongOption(); //顯示錯誤的推斷
}
else { //是數字
if (blockArray[x][y].getNumber() == 0) { //如果數字是0,則遞歸打開
recursionOpen(x, y);
}
else { //不是0則只打開一個
blockArray[x][y].setOpened(true);
}
}
}
//打開錯誤判斷
private void openWrongOption() {
int xx = gameConfigure.getXx();
Iterator it = wrongOptionSet.iterator();
while (it.hasNext()) {
int position = ( (Integer) it.next()).intValue();
int y = position / xx;
int x = position - y * xx;
blockArray[x][y].setWrongMineOpinion(true);
blockArray[x][y].setOpened(true);
}
}
//打開未標出的雷
private void openAllMine() {
int xx = gameConfigure.getXx();
Iterator it = minePositions.iterator();
while (it.hasNext()) {
int position = ( (Integer) it.next()).intValue();
int y = position / xx;
int x = position - y * xx;
if (!blockArray[x][y].isOpened()) { //如果還沒有打開
if (blockArray[x][y].getMark() != 10) { //是否有旗
blockArray[x][y].setOpened(true);
}
}
}
}
public void pressAround(int x, int y) {
//左上
if ( (x - 1) >= 0 && (y - 1) >= 0) {
blockArray[x - 1][y - 1].setdown(true);
whenpress.add(blockArray[x - 1][y - 1]);
}
//上
if ( (y - 1) >= 0) {
blockArray[x][y - 1].setdown(true);
whenpress.add(blockArray[x][y - 1]);
}
//右上
if ( (x + 1) < xx && (y - 1) >= 0) {
blockArray[x + 1][y - 1].setdown(true);
whenpress.add(blockArray[x + 1][y - 1]);
}
//左
if ( (x - 1) >= 0) {
blockArray[x - 1][y].setdown(true);
whenpress.add(blockArray[x - 1][y]);
}
//右
if ( (x + 1) < xx) {
blockArray[x + 1][y].setdown(true);
whenpress.add(blockArray[x + 1][y]);
}
//左下
if ( (x - 1) >= 0 && (y + 1) < yy) {
blockArray[x - 1][y + 1].setdown(true);
whenpress.add(blockArray[x - 1][y + 1]);
}
//下
if ( (y + 1) < yy) {
blockArray[x][y + 1].setdown(true);
whenpress.add(blockArray[x][y + 1]);
}
//右下
if ( (x + 1) < xx && (y + 1) < yy) {
blockArray[x + 1][y + 1].setdown(true);
whenpress.add(blockArray[x + 1][y + 1]);
}
}
public void releaseAround(int x, int y) {
Iterator t = whenpress.iterator();
while (t.hasNext()) {
BlockModel bm = (BlockModel) t.next();
bm.setdown(false);
}
whenpress.clear();
}
private void recursionOpen(int x, int y) {
if (!blockArray[x][y].isOpened()) {
blockArray[x][y].setOpened(true);
}
if (blockArray[x][y].getNumber() != 0) {
return;
}
//左上
if ( (x - 1) >= 0 && (y - 1) >= 0) {
if (blockArray[x - 1][y - 1].isOpened() == false) {
recursionOpen( (x - 1), (y - 1));
}
}
//上
if ( (y - 1) >= 0) {
if (blockArray[x][y - 1].isOpened() == false) {
recursionOpen( (x), (y - 1));
}
}
//右上
if ( (x + 1) < xx && (y - 1) >= 0) {
if (blockArray[x + 1][y - 1].isOpened() == false) {
recursionOpen( (x + 1), (y - 1));
}
}
//左
if ( (x - 1) >= 0) {
if (blockArray[x - 1][y].isOpened() == false) {
recursionOpen( (x - 1), (y));
}
}
//右
if ( (x + 1) < xx) {
if (blockArray[x + 1][y].isOpened() == false) {
recursionOpen( (x + 1), (y));
}
}
//左下
if ( (x - 1) >= 0 && (y + 1) < yy) {
if (blockArray[x - 1][y + 1].isOpened() == false) {
recursionOpen( (x - 1), (y + 1));
}
}
//下
if ( (y + 1) < yy) {
if (blockArray[x][y + 1].isOpened() == false) {
recursionOpen( (x), (y + 1));
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -