⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gamemodel.java

📁 windows自带的扫雷游戏
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
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 + -