J2ME伪高手先锋开讲——扫雷游戏的设计

一文教你从零开始设计并实现一个Java扫雷游戏,java中级开发工程师面试题 4.3.1鼠标事件我们通过点击鼠标左键或者鼠标右键来完成游戏,使用系统自带的MouseDown和MouseUp事件来响应玩家的操作。函数原型如下:组件名称:组件名称:这两个原型中的参数,Button参数值分别表示玩家用鼠标的左键或者右键进行点击的。其意义如下:1:左键 2:右键如果同时按两个按键,那么系统就会传回3,因为点左键是1,点右键是2,一起点就是两个值相加了。 阅读详情

J2ME伪高手先锋开讲——扫雷游戏的设计
 
 
首先我要装得像高手一样,来假装把系统稍微分析一下。

一般,按照java得开发模式,这种程序一般是分为三个模块来开发。

如下三个:

一个程序运作的主文件,也就是一个midlet的继承;

一个界面的表示类,也就是一个canvas的继承,界面上应该有些菜单,如new、exit 什么的,那就应该要 implements一个 commandListener消息监听类(大家可以把java的消息监听理解为一个线程,一直像倭寇那样对看得顺眼的东西虎视耽耽,当然这里指的是他所能触及到的消息,当收到消息的时候,会调用一个抽象函数public void commandAction(Command c, Displayable d),而这个抽象函数使得我们可以通过对他的实现来处理收到的消息,即消息响应)

最后一个当然就是与界面无关的逻辑单元了,在这里我们定义整个游戏的逻辑,做到逻辑与界面分开。这是我学java的最大收获,呵呵。

首先正式开始第一讲 <扫雷游戏的逻辑>

我的设想是,扫雷的地图一般是一个矩形,因为,圆形屏幕的手机看起来蛮变态的,没有必要迁就他,所以,我用一个a*b的二维数组就完全可以表示整个地图。

有了地图以后地图里面的类容自然就有一部分是表示地雷啦,既然这样,那不如就这样<废话来的,小朋友不要学>

/**

* 20 标志该位置为地雷

* <=10的数字表示未翻开的方块及周围的地雷数目

* >=10的数字表示已翻开的方块及周围的地雷数目

* */

表示方法就出来了,逻辑也明朗起来了。

我要将某个块翻开,只要将他加上10就可以了。

Java编程第一步,当然是先要class啊

package games;

import java.util.Random;

import java.lang.Math;

class gamelogic {

    /**表示一个10*10的棋盘*/

    private int[][] pan = new int;

    private Random random;//一个随机变量,主要作用是用来指定哪些位置为地雷

    private int BombNum = 0; //统计地雷总数

 

    /**游戏是否结束*/

    private boolean GameOver;


来自:低调一贱男, 时间:2003-10-25 23:38:00, ID:2252871 | 编辑

好的,讲到这里,该作甚么了呢,我想想,等等,大家先泡杯茶。

如花---------,来给各位大爷配种--------------

爽了吧,大家。

接下来就是要初始化地图了,地图首先要扔一个雷在上面啊,不然怎么叫扫雷呢,扔完了地雷以后接下来当然是遍历一次地图(我们还是很仁慈地,我们得告诉扫雷的同志,某某位置,有多少雷,比如这样:"01、01、12点中方向有地雷,14点钟方向有幺鸡,2点钟方向有东风之类的啊")。

/**初始化数组,生成地图*/

public void InitArray() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            pan[i][j] = 0;
        }
    }

    RandomArray();
    CountBomb();
    BombNum = Bomb();
}


/**统计地雷总数
* @return int 返回地雷总数 */

private int Bomb() {
    int count = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (pan[i][j] == 20) {
                count += 1;
            }
        }
    }

    return count;
}


/**随机决定地雷的位置*/

private void RandomArray() {
    int i, j, k;

    // 先扔15个左右的地雷吧,注意,这里不一定有15个哦,因为随机值可能重复,我不管啦
    for (int r = 0; r < 15; r++) {
        k = java.lang.Math.abs(random.nextInt()) % 64; //random.nextInt(100);
        i = k / 8;
        j = k % 8;
        this.pan[i][j] = 20; //指定该位置为地雷
    }

}


/**统计棋盘上的数据*/

private void CountBomb() {
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            int count = 0;

            //当需要检测的单元格本身无地雷的情况下,统计周围的地雷个数
            if (pan[i][j] != 20) {
                if ( (i - 1 >= 0) && (j - 1 >= 0)) {
                    if (pan[i - 1][j - 1] == 20) {
                        count += 1; //检测左上方空格是否是地雷
                    }
                }

                if ( (i - 1 >= 0)) {
                    if (pan[i - 1][j] == 20) {
                        count += 1; //检测上方空格是否为地雷
                    }
                }

                if ( (i - 1 >= 0) && (j + 1 <= 7)) {
                    if (pan[i - 1][j + 1] == 20) {
                        count += 1; //检测右上方是否为地雷
                    }
                }

                if ( (j - 1 >= 0)) {
                    if (pan[i][j - 1] == 20) {
                        count += 1; //检测左边是否为地雷
                    }
                }

                if ( (i >= 0) && (j + 1 <= 7)) {
                    if (pan[i][j + 1] == 20) {
                        count += 1; //右边
                    }
                }

                if ( (j - 1 >= 0) && (i + 1 <= 7)) {
                    if (pan[i + 1][j - 1] == 20) {
                        count += 1; //左下
                    }
                }

                if ( (i + 1 <= 7)) {
                    if (pan[i + 1][j] == 20) {
                        count += 1; //下
                    }
                }

                if ( (j + 1 <= 7) && (i + 1 <= 7)) {
                    if (pan[i + 1][j + 1] == 20) {
                        count += 1; //右下
                    }
                }

                pan[i][j] = count;
            }
        }
    }
}


来自:低调一贱男,时间:2003-10-25 23:41:00, ID:2252872 | 编辑

/**检测已经被揭开的位置总和

* @return 返回被揭开的数量 */

private int countOpen() {
    int count = 0;
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            if (pan[i][j] < 20 && pan[i][j] > 9) {
                count += 1;
            }
        }
    }

    return count;
}

 

/**检测是否胜利

* @return 是否胜利boolean值 */

public boolean isWin() {

    // System.out.println(BombNum +""+ countOpen());
    if ( (BombNum + countOpen()) == 64) {
        this.GameOver = true;
        return true;
    }
    else {
        return false;
    }
}

 

来自:低调一贱男,时间:2003-10-25 23:42:00, ID:2252873 | 编辑

/**选中棋盘上的位置,并翻开

* @param matrix 位置 */

public void openpan(int matrix) {
    switch (getBomb(matrix)) {
        case 20: //当选中的位置为地雷,游戏结束
            setGameOver();
            break;
        case 0:
            isNull(matrix); //当选中的位置为空,则翻开周围的地图
            break;
        default:
            this.isNotNull(matrix); //否则,翻开当前位置,并作上翻开的标记
    }
}

 

/**当选中的位置为空,则翻开周围的地图

* @param matrix 位置 */

private void isNull(int matrix) {
    int i, j;
    i = matrix / 8;
    j = matrix % 8;

    if (pan[i][j] < 9) {
        pan[i][j] += 10;
    }

    if ( (i - 1 >= 0) && (j - 1 >= 0)) { //检测左上方空格是否是空
        if (pan[i - 1][j - 1] == 0) {
            isNull( (i - 1) * 8 + (j - 1));
        }
        if (pan[i - 1][j - 1] < 9) {
            pan[i - 1][j - 1] += 10;
        }
    }

    if ( (i - 1 >= 0)) { //检测上方空格是否为空
        if (pan[i - 1][j] == 0) {
            isNull( (i - 1) * 8 + j);
        }
        if (pan[i - 1][j] < 9) {
            pan[i - 1][j] += 10;
        }
    }

    if ( (i - 1 >= 0) && (j + 1 <= 7)) { //检测右上方是否为空
        if (pan[i - 1][j + 1] == 0) {
            isNull( (i - 1) * 8 + (j + 1));
        }
        if (pan[i - 1][j + 1] < 9) {
            pan[i - 1][j + 1] += 10;
        }
    }

    if ( (j - 1 >= 0)) { //检测左边是否为空
        if (pan[i][j - 1] == 0) {
            isNull(i * 8 + (j - 1));
        }
        if (pan[i][j - 1] < 9) {
            pan[i][j - 1] += 10;
        }
    }

    if ( (i >= 0) && (j + 1 <= 7)) { //右边
        if (pan[i][j + 1] == 0) {
            isNull(i * 8 + (j + 1));
        }
        if (pan[i][j + 1] < 9) {
            pan[i][j + 1] += 10;
        }
    }

    if ( (j - 1 >= 0) && (i + 1 <= 7)) { //左下
        if (pan[i + 1][j - 1] == 0) {
            isNull( (i + 1) * 8 + (j - 1));
        }
        if (pan[i + 1][j - 1] < 9) {
            pan[i + 1][j - 1] += 10;
        }
    }

    if ( (i + 1 <= 7)) { //下
        if (pan[i + 1][j] == 0) {
            isNull( (i + 1) * 8 + j);
        }
        if (pan[i + 1][j] < 9) {
            pan[i + 1][j] += 10;
        }
    }

    if ( (j + 1 <= 7) && (i + 1 <= 7)) { //右下
        if (pan[i + 1][j + 1] == 0) {
            isNull( (i + 1) * 8 + (j + 1));
        }
        if (pan[i + 1][j + 1] < 9) {
            pan[i + 1][j + 1] += 10;
        }
    }
}

 

/**选中棋盘上的位置,并翻开当前位置

* @param matrix 位置 */

private void isNotNull(int matrix) {
    int i, j;
    i = matrix / 8;
    j = matrix % 8;
    pan[i][j] += 10;
}


/**取得指定位置的数据

* @param matrix 位置

* @return int 数据 */

public int getBomb(int matrix) {
    int i, j;
    i = matrix / 8;
    j = matrix % 8;
    return this.pan[i][j];
}


/**检测游戏是否结束

* @return boolean 游戏是否结束的状态 */

public boolean isGameOver() {
    return GameOver;
}


/**设置游戏结束 */

private void setGameOver() {
    GameOver = true;
}


/**开新局*/

public void setNewGame() {
    this.GameOver = false;
}


/**指定位置是否被揭开

* @param matrix 位置

* @return boolean 返回是否可以被揭开 */

public boolean isFree(int matrix) {
    int i, j;
    i = matrix / 8;
    j = matrix % 8;
    return pan[i][j] < 8 || pan[i][j] == 20;
}

}


来自:低调一贱男, 时间:2003-10-25 23:46:00, ID:2252881 | 编辑

public void openpan(int matrix) 是整个程序的核心所在,他描述了所有的动作,

1、有可能你踩到屎,踩到屎当然就gameover啦

2、有可能踩到钱,有一大片空地给你玩

3、命好,没有炸死你,继续探索

 

来自:低调一贱男, 时间:2003-10-25 23:47:00, ID:2252882 | 编辑

呵呵,第一讲先到这里,下次讲界面的实现

 

 

来自:低调一贱男, 时间:2003-10-27 17:38:00, ID:2255425 | 编辑

今天考试,所以现在才开始第二讲,见谅,不过好像感兴趣的人不多,所以,我讲完这个例子就停了,如转载请著名 作者 刘昆

 

来自:yanyandt2, 时间:2003-10-27 17:45:00, ID:2255447

请问你这个东西我在PC上能实现吗?

我最近也想学java,可是我不知道能用java来做什么。

jbuilder太大,我的机器跑不动,

做网页又不喜欢,

还希望楼主能给迷茫的我指导一下,谢谢!!

 

来自:低调一贱男, 时间:2003-10-27 17:57:00, ID:2255484 | 编辑

用jbuilder配置j2me比较方便,几乎不用配置,用记事本也可以写,用命令行运行,但是要装j2se,j2ee,然后配置一堆环境变量,然后还要用命令行来编译,审核,运行,一个基本上会死人,这也是我不用的原因,呵呵,具体步骤你到

http://www.cnjm.net/ 

上去看看

 

来自:低调一贱男, 时间:2003-10-28 14:19:00, ID:2257384 | 编辑

呵呵,无人问津就只贴代码算了,搞到我都没有动力

 

来自:低调一贱男, 时间:2003-10-28 14:22:00, ID:2257393 | 编辑

//gamecCanvans.java

package games;

import java.lang.System.*;

import java.util.Random;

import java.util.Vector;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

 

/**

* <p>标题: Canvas测试</p>

* <p>描述: 游戏的界面</p>

* <p>版权: 2003 </p>

* <p>公司: none</p>

* @author 刘昆

* @version 1.0*/

 

/**游戏动作类*/

class gameCanvas

extends Canvas

implements CommandListener {

    /**黑*/
    private static final int BLACK = 0x00000000;

    /**白*/
    private static final int WHITE = 0x00FFFFFF;

    /**红*/
    private static final int RED = 0x00FF0000;

    /**蓝*/
    private static final int BLUE = 0x000000FF;

    /**没有移动的标志*/
    private static final int NO_MOVE = -1;

    /**主窗体类*/
    private final control midlet;

    /**逻辑类*/
    private final gamelogic game;

    /**退出菜单*/
    private final Command exitCommand;

    /**重新开始菜单*/
    private final Command newGameCommand;

    /**随机数*/
    private final Random random = new Random();

    /**屏幕宽度*/
    private int screenWidth;

    /**屏幕高度*/
    private int screenHeight;


    /**boardCellSize 正方形单元格的大小,
    * boardTop 棋盘top的位置,
    * boardLeft 棋盘left位置*/
    private int boardCellSize, boardTop, boardLeft;


    /**preCursorPosition 前一次光标选择的位置,cursorPosition 当前光标选择的位置*/
    private int preCursorPosition, cursorPosition;

    /**用于存储被标记的地雷的位置 */
    private Vector BombVector = new Vector();

    private boolean isRestart;


    /**构造器

    * @param midlet 主窗体类 */

    public gameCanvas(control midlet) {
        this.midlet = midlet;
        game = new gamelogic(random);
        initializeBoard();

        /**初始化菜单*/
        exitCommand = new Command("退出", Command.EXIT, 1);
        newGameCommand = new Command("新游戏", Command.SCREEN, 2);
        addCommand(exitCommand);
        addCommand(newGameCommand);
        setCommandListener(this);

        /**开始游戏*/
        initialize();
    }

 

    /**添加一个地雷位置标记

    *@param matrix 位置标记 */

    private void addBomb(int matrix) {
        BombVector.addElement(Integer.toString(matrix));
    }

 

    /**删除一个地雷位置标记

    *@param matrix 位置标记 */

    private void delBomb(int matrix) {
        BombVector.removeElement(Integer.toString(matrix));
    }

 

    /**搜索该位置是否被标记

    * @param matrix 位置标记

    * @return boolean 该位置是否被记录,false为被记录 */

    private boolean searchBomb(int matrix) {
        return BombVector.indexOf(Integer.toString(matrix)) == -1; //-1表示没有找到该位置的信息
    }


    /**初始化屏幕,取得棋盘的初始位置*/

    private void initializeBoard() {
        screenWidth = getWidth(); //取得屏幕宽度
        screenHeight = getHeight(); //取得屏幕高度
        if (screenWidth > screenHeight) {
            boardCellSize = (screenHeight - 1) / 8;
            boardLeft = (screenWidth - (boardCellSize * 8)) / 2;
            boardTop = 1;
        }
        else {
            boardCellSize = (screenWidth - 1) / 8;
            boardLeft = 1;
            boardTop = (screenHeight - boardCellSize * 8) / 2;
        }
    }

 

    /** 初始化游戏和屏幕. 使游戏重新启动*/

    private void initialize() {
        preCursorPosition = cursorPosition = 0;
        game.setNewGame();
        game.InitArray();
        isRestart = true;
        BombVector.removeAllElements();
        repaint();
    }

 

    /**重画canvas

    * @param g 重画的Graphics对象*/

    public void paint(Graphics g) {
        game.isWin();

        if (!game.isGameOver()) {
            paintGame(g);
        }
        else {
            paintGameOver(g);
        }
    }

 

    /**游戏未结束时的重画动作

    * @param g 重画的Graphics对象*/

    private void paintGame(Graphics g) {
        if (isRestart) {
            /*清除画板*/
            g.setColor(0xbbbbbb);
            g.fillRect(0, 0, screenWidth, screenHeight);
            drawBoard(g);
            paintAll(g);
            //System.out.println("sss");//test
            drawBombTag(g);
        }

        drawCursor(g);
    }

 

    /**游戏结束时的重画动作,画出游戏统计结果

    * @param g 重画的Graphics对象 */

    private void paintGameOver(Graphics g) {
        if (game.isGameOver()) {
            if (game.isWin()) {
                GameOver(g);
            }
            else {
                for (int i = 0; i < 8; i++) {
                    for (int j = 0; j < 8; j++) {
                        if (game.getBomb(i * 8 + j) == 20) {
                            drawCircle(g, i * 8 + j);
                        }
                    }
                }
            }
        }
    }

 

    /**游戏结束时的重画动作

    * @param g 重画的Graphics对象 */

    private void GameOver(Graphics g) {

        String tallyMsg = "你赢了,恭喜!";
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN,Font.SIZE_LARGE);
        int strHeight = font.getHeight();
        int tallyMsgWidth = font.stringWidth(tallyMsg);
        int strWidth = tallyMsgWidth;

        /*计算绘制文本的起始位置*/
        int x = (screenWidth - strWidth) / 2;
        x = x < 0 ? 0 : x;
        int y = (screenHeight - 2 * strHeight) / 2;
        y = y < 0 ? 0 : y;

        /* 清除画板*/
        g.setColor(WHITE);
        g.fillRect(0, 0, screenWidth, screenHeight);

        /* 画出文本结果*/
        g.setColor(RED);
        g.drawString(tallyMsg, x + 5, (y + 1 + strHeight),(Graphics.TOP | Graphics.LEFT));
    }

 

    /**监听器响应接口

    * @param c 命令

    * @param d 消息源 */

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            midlet.quit();
        }
        else if (c == newGameCommand) {
            initialize();
        }
    }

 

    /** 画圆,表示地雷位置

    * @param g 图形物件

    * @param matrix 位置*/

    private void drawCircle(Graphics g, int matrix) {
        int x, y;
        y = matrix / 8;
        x = matrix % 8;
        g.setColor(RED);
        g.fillArc(x * boardCellSize + boardLeft + 3, y * boardCellSize + boardTop + 2, boardCellSize - 2, boardCellSize - 2, 0, 360);
    }

 

    /** 画叉,表示地雷爆炸

    * @param g 图形物件

    * @param x x坐标

    * @param y y坐标 */

    private void drawCross(Graphics g, int x, int y) {
        g.setColor(RED);
        g.drawLine(x + 1 + boardLeft, y + boardTop, x + boardCellSize - 4 - 4 + boardLeft, y + boardCellSize - 5 + boardTop);
        g.drawLine(x + 1 + boardLeft, y + boardCellSize - 5 + boardTop, x + boardCellSize - 4 - 4 + boardLeft, y + boardTop);
    }

 

    /**根据玩家和电脑的移动重画棋盘*/

    private void doPlayerMove() {
        if (game.isFree(cursorPosition)) {
            game.openpan(cursorPosition);
        }
        repaint(); //画出移动的图形结果
    }

 

    /**重画所有被揭开的文字

    * @param g 图形物件 */

    private void paintAll(Graphics g) {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (game.getBomb(i * 8 + j) >= 10 && game.getBomb(i * 8 + j) < 20) {
                    paintNum(g, i * 8 + j);
                }
            }
        }
    }

 

    /**画出文字

    * @param g 图形物件

    * @param matrix 位置 */

    private void paintNum(Graphics g, int matrix) {
        int i, j, s;
        Font font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
        s = game.getBomb(matrix);
        //System.out.println(s);
        i = matrix / 8;
        j = matrix % 8;
        g.setColor(WHITE);
        if (this.searchBomb(matrix)) {
            if (s != 20) {
                g.drawString(String.valueOf(s - 10), boardLeft + j * boardCellSize + 5, boardTop + i * boardCellSize - 2, (Graphics.TOP | Graphics.LEFT));
            }
        }
    }

 

    /**捕获按键消息

    * @param keyCode 按键代码 */

    protected void keyPressed(int keyCode) {
        /**当游戏结束时返回*/
        if (game.isGameOver()) {
            return;
        }

        int gameAction = getGameAction(keyCode);

        switch (gameAction) {
            case FIRE:
                if (searchBomb(cursorPosition)) { //如果该位置被做出了地雷标志,则该位置不能被揭开
                    doPlayerMove();
                }
                break;
            case RIGHT:
                doMoveCursor(1, 0);
                break;
            case DOWN:
                doMoveCursor(0, 1);
                break;
            case LEFT:
                doMoveCursor( -1, 0);
                break;
            case UP:
                doMoveCursor(0, -1);
                break;
            default:
                if (searchBomb(cursorPosition)) {
                    addBomb(cursorPosition);
                }
                else {
                    delBomb(cursorPosition);
                }
                repaint();
                break;
        }
    }

 

    /**画出棋盘上的地雷标志

    * @param g 图形物件 */

    private void drawBombTag(Graphics g) {
        int s, j, k;
        g.setColor(RED);
        for (int i = 0; i < BombVector.size(); i++) {
            s = Integer.parseInt( (String) BombVector.elementAt(i));
            j = s % 8;
            k = s / 8;
            g.drawString("!", boardLeft + j * boardCellSize + 7, boardTop + k * boardCellSize - 2, (Graphics.TOP | Graphics.LEFT));
        }
    }

 

    /**指定棋盘上的坐标

    * @param dx 左右偏移量x

    * @param dy 上下偏移量y */

    private void doMoveCursor(int dx, int dy) {

        int newCursorPosition = cursorPosition + dx + 8 * dy;
        if ( (newCursorPosition >= 0) && (newCursorPosition < 64)) {
            preCursorPosition = cursorPosition;
            cursorPosition = newCursorPosition;
            repaint();
        }
    }

 

    /** 在棋盘上画出选择光标

    * @param g 图形物件 */

    private void drawCursor(Graphics g) {

        /** 清除之前的选择光标*/
        g.setColor(0xbbbbbb);
        g.drawRect( ( (preCursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (preCursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2);

        /**在当前选择位置画出选择光标*/
        g.setColor(this.BLUE);
        g.drawRect( ( (cursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (cursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2);
    }

 

    /** 画出空位置

    * @param g 图形物件 */

    private void drawNull(Graphics g) {

        /**在当前选择位置画出选择光标*/
        g.setColor(this.WHITE);
        g.fillRect( ( (cursorPosition % 8) * boardCellSize) + 3 + boardLeft, ( (cursorPosition / 8) * boardCellSize) + 1 + boardTop, boardCellSize - 2, boardCellSize - 2);
    }

 

    /**画一个盘

    * @param g 图形物件 */

    private void drawBoard(Graphics g) {

        /** 清除盘*/
        g.setColor(0xbbbbbb);
        g.fillRect(0, 0, screenWidth, screenHeight);

        /**画盘*/
        g.setColor(BLACK);
        for (int i = 0; i < 9; i++) {
            g.fillRect(boardLeft + 2, boardCellSize * i + boardTop, (boardCellSize * 8) + 1, 1); /*画四条黑色的横线*/
            g.fillRect(boardCellSize * i + boardLeft + 2, boardTop, 1, boardCellSize * 8); /*画四条黑色的竖线*/
        }
    }

}

来自:低调一贱男, 时间:2003-10-28 14:23:00, ID:2257395 | 编辑

package games;


import java.io.IOException;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.io.*;

 

public class control

extends MIDlet {

    private gameCanvas gameScreen;

    /**构造器*/
    public control() {
    }


    /**开始MIDlet*/
    public void startApp() {
        Displayable current = Display.getDisplay(this).getCurrent();
        if (current == null) {
            Image logo = null;
            try {
                logo = Image.createImage("/icons/mine.png");
            }
            catch (IOException e) {
                System.err.println("none image");
            }

            Alert splashScreen = new Alert(null,

            " 乱扫雷 1.0 /n 刘昆制作 /n eMail:liukun966122@hotmail.com", logo,

            AlertType.INFO); //显示一个logo
            splashScreen.setTimeout(4000);

            gameScreen = new gameCanvas(this);

            Display.getDisplay(this).setCurrent(splashScreen, gameScreen);
        }
        else {
            Display.getDisplay(this).setCurrent(current);
        }
    }

 

    /*暂停MIDlet*/

    public void pauseApp() {}

    /**销毁MIDlet中需要销毁的对象

    *@param unconditional 是否被立即销毁 */

    public void destroyApp(boolean unconditional) {}

 

    /**退出MIDlet*/

    public void quit() {
        destroyApp(false);
        notifyDestroyed();
    }

}
 
 

DOS界面下的简易扫雷游戏(C语言项目) 一个DOS界面下的简易C语言扫雷程序,面向初学者。 立即下载

相关推荐

C语言模拟实现简单扫雷游戏

主要为大家详细介绍了C语言模拟实现简单扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

基于STM32单片机扫雷游戏TFT触摸操作单片机小游戏

基于STM32单片机扫雷游戏TFT触摸操作单片机小游戏 功能(更加具体请看功能视频): 硬件组成:STM32F103RCT6最小系统+2.8寸TFT电阻触摸屏+24C02存储器 1.基于STM32F103R系列单片机,2.4寸电阻触摸屏,24c02存储器,。 2.10*10=100个棋盘,带计时功能。时间最短最厉害。 3.有简单模式和困难模式,简单模式10个雷,困难模式35个雷。 4.有排行榜功能,掉电保存。 5.游戏设计合理,操作顺畅,比较耐玩。困难模式很有挑战,烧脑。

2301_81207259的博客 432

扫雷游戏源码,大家都常玩,不用多少了。

扫雷游戏源码,大家都常玩,不用多少了,用到了局部重画

扫雷游戏的设计【转】【收藏】

J2ME伪高手先锋开讲     --扫雷游戏的设计首先我要装得像高手一样,来假装把系统稍微分析一下。一般,按照java得开发模式,这种程序一般是分为三个模块来开发。如下三个:一个程序运作的主文件,也就是一个midlet的继承;一个界面的表示类,也就是一个canvas的继承,界面上应该有些菜单,如new、exit 什么的,那就应该要 implements一个 commandListener消息监听类

1100

秦时明月6.2魔改版_从零到一部署指南_含安卓客户端配置与GM后台直通

本文提供《秦时明月》6.2魔改版从零开始的完整部署指南。详细讲解CentOS服务器环境准备、服务端文件部署与安装、关键端口开放及进程验证。重点涵盖安卓客户端APK的IP修改与重签名方法,并介绍了GM后台工具的访问与使用,助你快速搭建并管理专属游戏服务器。

garlic的博客 310

深入了解torch框架

大国的游戏,政府支持到位,是未来;AI : Artificial Intelligence,旨在研究、开发能够模拟、延伸和扩展人类智能的理论、方法、技术及应用系统,是一种拥有自主学习和推理能力的技术。它模仿人类大脑某些功能,包括感知、学习、理解、决策和问题解决。AI本质​ NLP(说话,听)、CV(眼睛)、自动驾驶、机器人(肢体动作)、大模型三要素:数据、网络、算力① 神经网络:找到合适的数学公式;② 训练:用已有数据训练网络,目标是求最优解;③ 推理:用模型预测新样本;PyTorch,简称Torch

qq_74732076的博客 1136

扫雷游戏的设计与实现

但是,如果我们排查的坐标位于边界处,则会造成越界访问,因此我们需要将原有的棋盘扩大一圈,但并不布置雷,而雷仍然是布置在9*9的棋盘中。这里我们采用一种方案,设置一个数组存放布置的雷的信息(mine数组),再用另外一个数组存放排查出的雷的信息(show数组)。接下来继续分析,棋盘上雷和非雷的信息都是用数字表示,而排查出的坐标周围的雷的信息也用数字表示,这样的话,雷的信息的雷的个数的信息就可能会产生混淆和打印上的困难。在设计游戏的过程中,我们需要考虑到如何去存放所布置的雷的信息和排查的雷的信息。

专注于前后端学习 371

从逻辑门到RISC-V指令:ALU设计实战与Verilog实现

本文详细介绍了从逻辑门到RISC-V指令集的ALU设计实战与Verilog实现方法。通过构建1位ALU并扩展至64位,深入解析了RISC-V指令集的算术逻辑单元实现技巧,包括加法器设计、零标志检测和溢出检测等关键功能。文章还分享了Verilog代码优化、流水线设计和常见问题调试的实用经验,为开发者提供了一套完整的ALU设计解决方案。

weixin_29214335的博客 185

C语言-扫雷游戏程序设计

一.问题要求 本题目要求我们设计一个程序实现扫雷游戏,在游戏时利用最短时间内去点击九宫格的格子显示数字去找出非地雷的格子,并且避免踩到地雷。在完成基本要求的情况下,去提高游戏性,同时增强用户的体验感受和可玩性,以及对数字的反应能力。 2.程序的功能 (1)首先当确定第一个格子里为地雷时,需要将地雷转换到其他格子,防止游戏一开始后就结束。 (2)当我们选择的格子位置周围不存在地雷时,就要以九宫格的形式散步开来,再进行扫雷周围过程,并判断该坐标是否有地雷,同时进行统计地雷的总数,以便减去不必要的过程,从而提升

淡沫初夏zz的博客 7472

IAP板卡远程升级(串口网口都可)+Ymodem+AES对bin文件加密+pc软件+板卡相互烧录

还是决定记录一下,毕竟这个东西我也搞了挺久的,遇到一些棘手的,或者是因为我很粗心遇到的问题也和大家分享一下,。 板卡核心是 STM32F103RCT6,256kflash 前言 开始看这个远程升级的时候,我是一头雾水的,原子哥的教程里面也有说到过iap,有了一个大概的了解,我想不明白的是,通过串口下载的方式把hex文件下载到flash,和把bin文件下载到flash难道不是拉一根线,下载一个文件嘛,(初入职场的我并没有考虑到安全这个问题)如果直接将hex文件给到客户那边,风险是非常大...

那可真是太开心了的博客 5371

CMOS的宽/长比、传输门与三态门、锁存器与触发器、简单版图、竞争与冒险

目录 1.CMOS的宽长比 2.传输门与三态门 传输门 三态门 3.锁存器与触发器 3.1RS锁存器[3] 3.2D锁存器 3.3D触发器 4.简单CMOS器件的版图 5.竞争与冒险 1.CMOS的宽长比 关于COMS原理及结构图可以参考[1]COMS原理及门电路设计. 栅在源漏方向的长度称作栅的长L,垂直方向称为栅的宽W,如图1中NMOS的版图。 图1 以MOS管...

king阿金 1万+

扫雷游戏分析和设计

扫雷游戏是一款经典游戏,是在一个9*9(还有其它大小的)的棋盘上,默认里面有10个雷,每个格子有两种状态,有地雷或者没有地雷,当我们成功避开所有的雷时游戏胜利。游戏。

2402_82667241的博客 2293

Ubuntu20.04安全重启后WiFi图标消失?MT7922网卡驱动修复全攻略

本文详细解析了Ubuntu20.04安全重启后WiFi图标消失的问题,特别是针对MT7922网卡驱动失效的情况。通过诊断命令和分步解决方案,包括禁用PCIe ASPM、更新无线固件和升级HWE内核,帮助用户快速恢复网络连接。文章还提供了进阶排查技巧和系统优化建议,确保WiFi稳定运行。

weixin_29202687的博客 369

J2ME伪高手先锋开讲—扫雷游戏的设计

J2ME伪高手先锋开讲——扫雷游戏的设计      首先我要装得像高手一样,来假装把系统稍微分析一下。    一般,按照java得开发模式,这种程序一般是分为三个模块来开发。如下三个:    一个程序运作的主文件,也就是一个midlet的继承;    一个界面的表示类,也就是一个canvas的继承,界面上应该有些菜单,如new、exit 什么的,那就应该要 implements一个 comma

mobilechannel的专栏 1628

华为OD机考2025C卷 - 精准核酸检测(Java & Python& JS & C++ & C语言)

华为OD机考2025C卷 - 精准核酸检测,提供在线OJ Java & Python& JS & C++ & C

算法大师! 2882

R数据分析:潜在剖面分析LPA的做法与解释

Latent Profile Analysis (LPA) tries to identify clusters of individuals (i.e., latent profiles) based on responses to a series of continuous variables (i.e., indicators). LPA assumes that there are unobserved latent profiles that generate patterns of resp.

Codewar的博客 1万+

扫雷游戏实现超详细讲解——C语言

扫雷是一款很大众的益智类游戏,游戏规则也很简单,在最短时间把所有的所有的雷排查出来即为胜利,如果在期间踩到雷则游戏失败(结束)。这款游戏分为初级(9*9),中级(16*16),高级(16*30),或则自定义大小方块矩阵中随机布置一定量的炸弹(初级为10个,中级为40个,高级为99个炸弹)。由玩家逐个翻开方块,以找出所有炸弹为目的。注意:如果翻开的格子下面没有炸弹,则会显示周围一圈八个格子中炸弹的个数,如下图所示(这里的1指的就是在该格子周围一共有一个雷),所以想要过关就要利用好这些显示出来的数据。

shixiong_的博客 2487

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

扫雷游戏的设计和实现

实际操作中我们发现只创建个大小为9*9大小的数组是不够的因为数组中每个元素的下标实际上比这个元素在数组中的排序要小1,输入坐标会有不方便,而且排查的时候排查该元素的周围八个元素,当排查边界坐标是会发生''越界'',所以我们通常会创建10*10的数组,但是前一行和前一列不会放雷,相当于''无雷区'',顺便显示行和列以便输入坐标。这样一个棋盘需要我们创建两个二维数组来存放信息(一个在后台显示一个在游戏界面),假如后台我们将有雷用字符'1'来表示;printf("--------扫雷游戏-------\n");

2302_79215415的博客 321

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

扫雷游戏的设计(百分百还原电脑操作)

扫雷基础班+改良版,单机拓展,标记雷的位置并对剩余的雷的数量进行计数,一文搞定!

lmbuhuiku的博客 1503

C实现扫雷小游戏(简易版)

本期讲解:C语言实现扫雷简易版

我的主页 2073

STM32+UART HMI的扫雷游戏

STM32+UART HMI的扫雷游戏 最近接到了一个学长的毕设,记录一下开发路程 先上张整体图片 硬件部分 由指南者开发板和4.2英寸的UATRT HMI串口屏组成,其实可以用最小的核心板,单片机外设也就用到了串口而已,但是要求使用开发板只好有点浪费了。 软件部分 刚开始参考了网上一些扫雷代码,但是在单片机上很多地方不一样,大体思路还是一样的,主要要解决的就是与串口屏之间的通信协议,贴一段简单的通信代码 具体完成功能: 1.扫雷基本功能 2.当周围的雷都标记完时,打开剩余未标记的格子 //串口接收中断

weixin_43793057的博客 1707

从零开始实现扫雷

一篇文章带你学会扫雷

2301_79973359的博客 1002

C语言之扫雷游戏

C语言实现扫雷游戏

TU^的博客 637

扫雷游戏设计大致基本框架

扫雷游戏

祯的博客 1471

【Unity知识速递】第7期 Unity 6.6 深度解析:WebGPU 正式版与 WebAssembly64,Web 游戏的内存革命

如何启用 Wasm64?需要注意的是,Wasm64 需要浏览器支持 Memory64 提案,目前主流浏览器的最新版本都已支持。一句话总结: Unity 6.6 用 WebGPU 转正、Wasm64 16GB 内存和渐进式加载三连击,将 Web 游戏的体验天花板又往上推了一大截——Web 平台不再是"妥协版",正在成为真正的一等公民。Unity 6.6 新增了 WebAssembly64(Wasm64) 构建选项,这可能是本次更新中对 Web 游戏开发者影响最大的一项升级。

crystal_WJT的博客 422
上一篇: J2ME游戏开发实例讲解
下一篇: J2ME内存优化
vgame
博客等级 码龄21年 0粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值