J2ME 拼图游戏 快速开发 全过程 之代码祥解(2 )——游戏数据库与游戏模型开发

游戏模型开发,代码:

package cn.edu.xtu.tilepuzzle.model;



import cn.edu.xtu.tilepuzzle.GameDB;
import java.io.IOException;
import java.util.Random;


import javax.microedition.lcdui.Image;


import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.ui.GameSetUI;




public class BoardModel{
public String [] gameSetData;
    public String orgImageString ;
    public Image orgImage;
    public int rows ;// 行大小
    public int columns ;// 列大小
 //   Image images[][] ;
    
    public ClassPiece blankp; // 空方格类
    public ClassPiece[] all;// 所有方格类
    public ClassPiece[][] grid;// 二维方格类
    public Random rand;// 随机类
    
    // grid origin in pixels
    public int gridWidth;// 所有方格的宽
    public int gridHeight;// 所有方格的高
    
    // cell geometry in pixels
    public int cellWidth;// 方格的宽
    public int cellHeight;// 方格的高
    //Command[] cmd;// 命令数组
    public int gameState;// 游戏状态
    public  boolean cheated;
    
   
    public long starTime=0;
    public long sumTime=0;
    
    public MainUIController mainUIController;
    
    public BoardModel(MainUIController mainUIController) {
        this.mainUIController=mainUIController;    
        gridHeight = mainUIController.welcomeUI.getHeight();
        gridWidth = mainUIController.welcomeUI.getWidth();
       // System.out.println("屏幕的宽,高:" + gridWidth + "," + gridHeight);
    }
    
    public void initGameData(){
    gameSetData=RecordStoreOperations.getGameSetData(GameDB.recordStoreGameSetData); 
    //System.out.println("==1");
    for(int i=0;i<gameSetData.length;i++)
        System.out.println(gameSetData[i]);  
    orgImageString=gameSetData[GameDB.IndexInGameSetDatat_orgImageString];        
        rows=Integer.parseInt(gameSetData[GameDB.IndexInGameSetDatat_rows]);
        columns=Integer.parseInt(gameSetData[GameDB.IndexInGameSetDatat_columns]);
        
        System.out.println("columns:"+columns+",rows:"+rows);
    }
    
    /*
* gameSetData[0]:orgImageString 图片路径 默认   DataBase.orgImageString
* gameSetData[1]:反向 boolean true 反向        false
* gameSetData[2]:趣味洗牌 boolean true 是 false
* gameSetData[3]:标记方格 boolean true 标记 true
* gameSetData[4]:困难/简单 boolean true 困难 true
* gameSetData[5]:行 大小 int rows 4
* gameSetData[6]:列 大小 int columns 5
* */
    public void updateGameData(GameSetUI gameSetUI){
    System.out.println("从手机设置菜单更新游戏数据。");
    /*
    * gameSetData[0]:orgImageString 图片路径 默认   DataBase.orgImageString
    * gameSetData[1]:反向 boolean true 反向        false reversed
    * gameSetData[2]:趣味洗牌 boolean true 是 false funny
    * gameSetData[3]:标记方格 boolean true 标记 true addString
    * gameSetData[4]:困难/简单 boolean true 困难 true hard
    * gameSetData[5]:行 大小 int rows 4 rows
    * gameSetData[6]:列 大小 int columns 5 columns
    * */
    if(gameSetUI.reversed){
    gameSetData[GameDB.IndexInGameSetDatat_reversed]="true";
    }else {
    gameSetData[GameDB.IndexInGameSetDatat_reversed]="false";
}
    if(gameSetUI.funny)
    gameSetData[GameDB.IndexInGameSetDatat_funny]="true";    
    else {
    gameSetData[GameDB.IndexInGameSetDatat_funny]="false";
}
    if(gameSetUI.addString)
    gameSetData[GameDB.IndexInGameSetDatat_addString]="true";
    else {
    gameSetData[GameDB.IndexInGameSetDatat_addString]="false";;
}
    if(gameSetUI.hard)
    gameSetData[GameDB.IndexInGameSetDatat_hard]="true";
    else {
    gameSetData[GameDB.IndexInGameSetDatat_hard]="false";;
}
    if(gameSetUI.rowColumnIs_4x3){
    // 3 行 4 列
    gameSetData[GameDB.IndexInGameSetDatat_rows]="3";
    gameSetData[GameDB.IndexInGameSetDatat_columns]="4";    
    }
    else {
    // 4 行 5 列
    gameSetData[GameDB.IndexInGameSetDatat_rows]="4";
    gameSetData[GameDB.IndexInGameSetDatat_columns]="5";
      
}
    stroeGameData();
    //initGameData();
    //initPhotoData();
    }
    
    public void stroeGameData(){
    mainUIController.isChangeGameData=true;
    System.out.println("更改记录集游戏数据。");
    String str="";
for(int i=0;i<gameSetData.length-1;i++)
str+=gameSetData[i]+";";
str+=gameSetData[gameSetData.length-1];
System.out.println("修改图片后:"+str);
RecordStoreOperations.changeRecordStroe(GameDB.recordStoreGameSetData, str, GameDB.GameSetDataFlag); 
    }
    
    public void initData(){    
    initGameData();
    System.out.println("初始化拼图信息......");      
        
        cheated = false;
        rand = new Random();
        
        // create the grid arrays
        grid = new ClassPiece[columns][rows];
        all = new ClassPiece[rows * columns];
        initPhotoData();
       // all[(rows * columns) - 1] = blankp;
        this.setGameState(GameDB.INITIALIZED);
    }
    public void initPhotoData(){
    try {
            orgImage = Image.createImage(orgImageString);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        cellWidth=gridWidth/rows;
        cellHeight=gridHeight/columns;
        Image temp;
        int i;
        //System.out.println("开始获取原图......");
        /**
         * @x 横坐标
         * @y 列坐标
         * */
        int x = 0, y = 0,j=0;
        for ( i = 0; i < columns ; ) {
            x = 0;
            for ( j = 0; j < rows; ) {
                //grid[i][j].img ;
                 temp= PhotoOperations.effect_cut(orgImage, x, y, cellWidth, cellHeight); 
                 grid[i][j] = all[i*rows+j] = new ClassPiece(temp, i*rows+j, i, j,cellWidth,cellHeight);
                x =(++j) * cellWidth;
            }
            y = (++i) * cellHeight;
        }      
        // make the special blank piece
        Image blankImage=PhotoOperations.effect_hoodwink(grid[columns-1][rows-1].img);
        blankp = new ClassPiece(blankImage, (rows * columns) - 1,columns  - 1, rows - 1, cellWidth,cellHeight);
        grid[columns - 1][ rows- 1] = blankp;
   
    }
    public void addString(){
        int j=0,i=0;
        for ( i = 0; i < columns ; i++) {
            for ( j = 0; j < rows; j++) {
                grid[i][j].addString=true;
            }
        }
        
        for (i = 0; i <rows * columns; i++) {
            all[i].addString=true;
        }
    }
    public void removeString(){
        int j=0,i=0;
        for ( i = 0; i < columns ; i++) {
            for ( j = 0; j < rows; j++) {
                grid[i][j].addString=false;
            }
        }
        
        for (i = 0; i <rows * columns; i++) {
            all[i].addString=false;
        }
    }
    
   
    //把P类赋值给(x,y),更改坐标为(x,y)
    void setGrid(ClassPiece p, int x, int y) {
        grid[x][y] = p;
        grid[x][y].setLocation(x, y);
    }
    
    // swap the piece at sx, sy with the blank piece
    // assumes that this is a legal move
    public void moveBlank(int swapx, int swapy) {
        // System.out.println("黑块移动时输出: "+(blankp.x+1)+","+ (blankp.y+1)+","+(swapx+1)+","+(swapy+1));
        swap(swapx,swapy, blankp.x, blankp.y);
        // setGrid(grid[swapx][swapy], blankp.x, blankp.y);
        //setGrid(blankp, swapx, swapy);
    }
    
    // swaps the pieces at (x1, y1) and (x2, y2)
    // no parity checking is done!
    void swap(int x1, int y1, int x2, int y2) {
        // System.out.println("交换时输出:"+(x1+1)+","+(y1+1)+","+(x2+1)+","+(y2+1));
        ClassPiece t = grid[x1][y1];
        setGrid(grid[x2][y2], x1, y1);
        setGrid(t, x2, y2);
    }
    
    public boolean isSolved() {
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                if (!grid[i][j].isHome()) {
                    return false;
                }
            }
        }
        
        return true;
    }
    
    // return a random integer in the range [0..n)
    int randRange(int n) {
        int r = rand.nextInt() % n;
        
        if (r < 0) {
            r += n;
        }
        return r;
    }
    
    // randomize by making random moves
    void randomize_by_moving() {
        int dx;
        int dy;
        int v;
        
        for (int i = 0; i < 100; i++) {
            dx = dy = 0;
            v = (rand.nextInt() & 2) - 1; // 1 or -1
            
            if ((rand.nextInt() & 1) == 0) {
                dx = v;
            } else {
                dy = v;
            }
            
            if ((blankp.x + dx) < 0) {
                dx = 1;
            }
            
            if ((blankp.x + dx) == rows) {
                dx = -1;
            }
            
            if ((blankp.y + dy) < 0) {
                dy = 1;
            }
            
            if ((blankp.y + dy) == columns) {
                dy = -1;
            }
            // System.out.println("randomize_by_moving:"+blankp.x+blankp.y);
            moveBlank(blankp.x + dx, blankp.y + dy);
        }
        
        // now move the blank tile to the lower right corner
        while (blankp.x != (rows - 1))
            moveBlank(blankp.x + 1, blankp.y);
        
        while (blankp.y != (columns - 1))
            moveBlank(blankp.x, blankp.y + 1);
    }
    
    // shuffle the tiles randomly and place the blank at the bottom right
    void shuffle() {
        int limit = (rows * columns) - 1;
        ClassPiece[] ta = new ClassPiece[limit];
        ClassPiece temp;
        
        System.arraycopy(all, 0, ta, 0, limit);
        // System.out.println("下面开始判断");
        //添加奇偶性判断
        //直到为偶
        while(true){
            for (int i = 0; i < limit; i++) {
                int j = randRange(limit);
                temp = ta[j];
                ta[j] = ta[i];
                ta[i] = temp;
            }
            
            for (int i = 0; i < limit; i++) {
                setGrid(ta[i], i / rows, i % rows);
            }
            
            setGrid(blankp, columns - 1, rows - 1);
            //如果判定为偶,跳出
            if(SerialCheck.isEven(ta)){
                //  System.out.println("判断为 偶 !");
                break;
            } else {
                // System.out.println("判断为 奇 !重新洗牌。。。。。。");
                resetGrid();
                continue;
            }
        }
        
    }
    
    public void randomize(boolean hard) {
        
        shuffle();
        int ra;
        int rb;
        int x;
        int y;
        
        if (hard) {
            ra = 7;
            rb = 0;
        } else {
            ra = 0;
            rb = 7;
        }
        
        x = rand.nextInt() & 1;
        y = rand.nextInt() & 1;
        
        if ((x == 1) && (y == 1)) {
            x = 2;
            y = 0;
        }
        swap(x, y, all[ra].x, all[ra].y);
        swap((rand.nextInt() & 1) + 1, 2, all[rb].x, all[rb].y);
        
        if ((displacement() & 1) == 1) {
            swap(1, rows-1, rows-1, rows-1);
        }
        
    }
    
    // Compute and return the displacement, that is, the number of
    // pairs of tiles that are out of order.  The blank tile *must*
    // be in the lower right corner.
    int displacement() {
        boolean[] temp = new boolean[(rows * columns) - 1]; // all false
        int n = 0;
        
        for (int i = 0; i < columns; i++) {
            for (int j = 0; j < rows; j++) {
                ClassPiece p = grid[i][j];
                
                if (p == blankp) {
                    continue;
                }
                
                temp[p.serial] = true;
                
                for (int k = 0; k < p.serial; k++) {
                    if (!temp[k]) {
                        n++;
                    }
                }
            }
        }
        
        return n;
    }
    
    public void resetGrid() {
        ClassPiece[] temp = new ClassPiece[rows * columns];
        int k = 0;
        
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                temp[k++] = grid[j][i];
            }
        }
        
        for (k = 0; k < temp.length; k++) {
            temp[k].goHome();
        }
    }
    //有趣的洗牌
    public void rearrangeFunnily(boolean hard) {
        resetGrid();
        
        if (hard) {
            //System.out.println("困难");
            // RATE YOUR MIDP LAN
            swap(0, 0, columns-1, 1);
            swap(columns-1, rows-2, columns-1, 2);
            swap(columns-1, rows-1, 0, rows-1);
            swap(0, rows-1, rows-1, rows-1);
        } else {
            System.out.println("容易");
            // RATE YOUR MIDP NAL
            swap(0, 0, columns-1, rows-1);
            swap(columns-1, 0, 0, rows-1);
        }
    }
    
    public static String getTimeStringByS(long sum){
//long sumTime=sum;
//System.out.println("getTimeString:"+sum);
//1小时=60分=3600秒
String timeString=String.valueOf(sum/1000)+" 秒";
/*while(sumTime>0){
if(sumTime>=3600*1000){
timeString+=String.valueOf((sumTime/1000)/3600)+" 时 ";
sumTime/=3600;
}else if (sumTime>60*1000) {
timeString+=String.valueOf((sumTime/1000)/60)+" 分 ";
sumTime/=60;
}else {
timeString+=String.valueOf(sumTime/1000)+".";
sumTime%=1000;
timeString+=String.valueOf(sumTime/100);
sumTime%=100;
timeString+=String.valueOf(sumTime/10)+" 秒";
sumTime=0;
}
}
*/ /*if(sum>=3600*1000){
timeString+=String.valueOf((sumTime/1000)/3600)+" 时 ";
sumTime/=3600;
}else if (sumTime>60*1000) {
timeString+=String.valueOf((sumTime/1000)/60)+" 分 ";
sumTime/=60;
}else if(sumTime>1000){
timeString+=String.valueOf((float)sumTime/1000)+" 秒 ";
sumTime=0;
}else {
}
*/
//System.out.println("getTimeString:"+timeString);
return timeString;
}


    
    public ClassPiece[][] getGrid() {
        return grid;
    }
    
    public void setGrid(ClassPiece[][] grid) {
        this.grid = grid;
    }
    
    public int getGridWidth() {
        return gridWidth;
    }
    
    public void setGridWidth(int gridWidth) {
        this.gridWidth = gridWidth;
    }
    
    public int getGridHeight() {
        return gridHeight;
    }
    
    public void setGridHeight(int gridHeight) {
        this.gridHeight = gridHeight;
    }
    
    public int getCellWidth() {
        return cellWidth;
    }
    
    public void setCellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
    }
    
    public int getCellHeight() {
        return cellHeight;
    }
    
    public void setCellHeight(int cellHeight) {
        this.cellHeight = cellHeight;
    }
    
    public int getGameState() {
        return gameState;
    }
    
    public void setGameState(int gameState) {
        this.gameState = gameState;
        if(gameState == GameDB.WON){
    //System.out.println("胜利了。。。");
    mainUIController.handleCOMMAND_ID(GameDB.WON);
    }
    }

}


游戏数据库,代码:

package cn.edu.xtu.tilepuzzle;


import javax.microedition.lcdui.Font;




public class GameDB {
    public static final String author="作者:刘要华";
    public static final String school="湖南湘潭大学";
    
    //手机开始界面选择菜单
    public static final String[] menu = { "开始游戏", "设置图片", "最佳成绩", "选项", "帮助","游戏重置"};
    //public static final String[] menu = { "开始游戏", "设置图片", "最佳成绩", "选项", "帮助","游戏重置","作弊" };
    // 行大小
    public static final int rows=4;
    // 列大小
    public static final int columns = 5;
    
        //个人成绩 记录集 名字
    public static final String recordStorePeopleResult="peopleResult";
    //游戏设置 记录集 名字
    public static final String recordStoreGameSetData="GameSetData";
    
    // 图片的根目录
    public static final String rootPhotoPath = "/img/";
    public static final String rootMMPath = rootPhotoPath + "mm/";
    public static final String rootDONGMANPath = rootPhotoPath + "dongman/";
    public static final String rootCHUANGYIPath = rootPhotoPath + "chuangyi/";
    public static final String rootZIHUAPath = rootPhotoPath + "zihua/";
    public static final String rootOTHERPath = rootPhotoPath + "other/";
    
    public static final String imageDefalut_1Path = rootPhotoPath+"default_1.jpg";
    public static final String imageDefalut_2Path = rootPhotoPath+"default_2.jpg";
    
    public static final String orgImageString = rootPhotoPath+"default.jpg";
    
    // 控制器命令
    public static final int MM_NUMBERS = 4;
    public static final int DONGMAN_NUMBERS = 3;
    public static final int ZHIHUA_NUMBERS = 1;
    public static final int CHUANGYI_NUMBERS = 4;
    public static final int OTHER_NUMBERS = 4;
    


    //游戏设置信息在记录集中保存的ID
    public static final int GameSetDataFlag=1;
    
        /*
         * gameSetData[0]:orgImageString 图片路径 默认   DataBase.orgImageString
         * gameSetData[1]:反向 boolean true 反向        false reversed
         * gameSetData[2]:趣味洗牌 boolean true 是 false funny
         * gameSetData[3]:标记方格  boolean true 标记 true addString
         * gameSetData[4]:困难/简单 boolean true 困难 true hard
         * gameSetData[5]:行 大小 int  rows 4 rows
         * gameSetData[6]:列 大小 int  columns 5 columns
         * */
    public static final String gameSetData[]={
        orgImageString, //0
        "false", //1
        "false", //2
        "false", //3
        "false", //4
        String.valueOf(rows), //5
        String.valueOf(columns),//6
    };
    //游戏设置信息数组的长度
    public static final int GameSetDataNum=gameSetData.length;
    // 游戏设置固定编号
    public static final int IndexInGameSetDatat_orgImageString = 0;
    public static final int IndexInGameSetDatat_reversed = 1;
    public static final int IndexInGameSetDatat_funny = 2;
    public static final int IndexInGameSetDatat_addString = 3;
    public static final int IndexInGameSetDatat_hard = 4;
    public static final int IndexInGameSetDatat_rows = 5;
    public static final int IndexInGameSetDatat_columns = 6;
    
    // 控制器命令
    public static final int COMMAND_ID_NEW_GAME = 0;
    public static final int COMMAND_ID_SHOW_PHOTO = 1;
    public static final int COMMAND_ID_BEST = 2;
    public static final int COMMAND_ID_OPTIONS = 3;
    public static final int COMMAND_ID_HELP = 4;
    public static final int COMMAND_ID_RESET = 5;
    public static final int COMMAND_ID_TEST = 6;
    public static final int COMMAND_ID_EXIT = 7;
    static final int MENU_ITEM_COUNT = 8;
    
    // 游戏状态变量
    public static final int INITIALIZED = 10;
    public static final int PLAYING = 11;
    public static final int WON = 12;
    
    // 游戏图片组编号
    public static final int IndexInItemList_MM = 0;
    public static final int IndexInItemList_DONGMAN = 1;
    public static final int IndexInItemList_CHUANGYI = 2;
    public static final int IndexInItemList_ZIHUA = 3;
    public static final int IndexInItemList_OTHER = 4;
    
    //字体
    public static final Font MONOSPACE_PLAIN_SMALL_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_SMALL);
    public static final Font MONOSPACE_BOLD_SMALL_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_SMALL);
    
    public static final Font MONOSPACE_PLAIN_MEDIUM_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    public static final Font MONOSPACE_BOLD_MEDIUM_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM);
    
    public static final Font MONOSPACE_PLAIN_LARGE_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_PLAIN, Font.SIZE_LARGE);
    public static final Font MONOSPACE_BOLD_LARGE_Font = Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值