程序入口,代码:
/*
* TilePuzzle.java
*
* Created on 2012年3月14日, 下午3:19
*/
package cn.edu.xtu.tilepuzzle;
import javax.microedition.midlet.*;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.RecordStoreOperations;
/**
*
* @author Administrator
* @version
*/
public class ClassMain extends MIDlet {
private MainUIController mainUIController;
public void startApp() {
gameCheck();
//testStroe();
try {
mainUIController = new MainUIController(ClassMain.this);
mainUIController.init();
} catch (Exception e) {
System.out.println("Error when initial the record Store");
}
}
private void gameCheck(){
//RecordStoreOperations.deleteRecordStroe(GameDB.recordStorePeopleResult);
//RecordStoreOperations.deleteRecordStroe(GameDB.recordStoreGameSetData);
if(!RecordStoreOperations.checkIfRecord(GameDB.recordStorePeopleResult)){
RecordStoreOperations.getRecord(GameDB.recordStorePeopleResult);
}
if(!RecordStoreOperations.checkIfRecord(GameDB.recordStoreGameSetData)){
/*
* str[0]:orgImageString 图片路径 默认 DataBase.orgImageString
* str[1]:反向 boolean true 反向 false
* str[2]:趣味洗牌 boolean true 是 false
* str[3]:标记方格 boolean true 标记 true
* str[4]:困难/简单 boolean true 困难 true
* str[5]:行 大小 int rows 4
* str[6]:列 大小 int columns 5
* */
String str="";
for(int i=0;i<GameDB.gameSetData.length-1;i++)
str+=GameDB.gameSetData[i]+";";
str+=GameDB.gameSetData[GameDB.gameSetData.length-1];
//System.out.println(str);
RecordStoreOperations.addStrToRecordStroe(str, GameDB.recordStoreGameSetData);
//RecordStoreOperations.changeRecordStroe( DataBase.recordStoreGameSetData,str,DataBase.GameSetDataFlag);
}
}
public void testStroe(){
String string="刘华戴晴;4x5;1000";
RecordStoreOperations.addStrToRecordStroe(string, GameDB.recordStorePeopleResult);
string="戴晴;4x5;500";
RecordStoreOperations.addStrToRecordStroe(string, GameDB.recordStorePeopleResult);
string="刘要华;4x5;100";
RecordStoreOperations.addStrToRecordStroe(string, GameDB.recordStorePeopleResult);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
控制器开发,代码:
package cn.edu.xtu.tilepuzzle.contorller;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.ClassMain;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
import cn.edu.xtu.tilepuzzle.model.ClassSetPhoto;
import cn.edu.xtu.tilepuzzle.ui.AddUserNameUI;
import cn.edu.xtu.tilepuzzle.ui.BoardUI;
import cn.edu.xtu.tilepuzzle.ui.GameSetUI;
import cn.edu.xtu.tilepuzzle.ui.HelpUI;
import cn.edu.xtu.tilepuzzle.ui.MainMenuUI;
import cn.edu.xtu.tilepuzzle.ui.ShowPeopleInfoListUI;
import cn.edu.xtu.tilepuzzle.ui.WaitCanvasUI;
import cn.edu.xtu.tilepuzzle.ui.WelcomeUI;
public class MainUIController {
public boolean isChangeGameData=false;
public ClassMain classMain;
public Displayable preDisplayable;
private BoardModel boardModel;
boolean cheated;
private Display display; // 屏幕
public GameSetUI gameSetUI;// 操作类
int gameState;// 游戏状态
public WelcomeUI welcomeUI;
public MainMenuUI mainMenuUI;
public WaitCanvasUI waitCanvasUI;
public BoardUI boardUI;
public AddUserNameUI addUserNameUI;
public ShowPeopleInfoListUI showPeopleInfoListUI;
public ClassSetPhoto classSetPhoto;
public MainUIController(ClassMain classMain) {
this.classMain = classMain;
}
public void init() {
this.display=Display.getDisplay(this.classMain);
this.welcomeUI = new WelcomeUI();
this.boardModel=new BoardModel(MainUIController.this);
this.boardModel.initData();
this.mainMenuUI=new MainMenuUI(MainUIController.this);
this.boardUI=new BoardUI(MainUIController.this,this.boardModel,this.display,this.display.getCurrent());
this.boardUI.init();
this.addUserNameUI=new AddUserNameUI(MainUIController.this, this.boardModel, this.display, this.display.getCurrent());
this.addUserNameUI.init();
this.waitCanvasUI=new WaitCanvasUI(MainUIController.this, this.boardModel, this.display, this.display.getCurrent());
this.showPeopleInfoListUI=new ShowPeopleInfoListUI(MainUIController.this,this.display,this.display.getCurrent());
this.showPeopleInfoListUI.init();
this.classSetPhoto=new ClassSetPhoto(MainUIController.this,this.boardModel,this.display,this.display.getCurrent());
//GameSetUI设置在下面 setOPtions();
displayWelcome();
}
//这里设置GameSetUI
public void setOPtions(Displayable db){
this.gameSetUI=new GameSetUI(this.boardModel,display,db);
}
public void setCurrent(Displayable disp) {
display.setCurrent(disp);
}
public void displayWelcome() {
setCurrent(welcomeUI);
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
}
setCurrent(mainMenuUI);
}
public void handleCOMMAND_ID(int eventID) {
switch (eventID) {
case GameDB.COMMAND_ID_NEW_GAME: {
// 新游戏
if (boardModel.getGameState() != GameDB.PLAYING) {
cheated = false;
if (isChangeGameData) {
boardModel.initData();
isChangeGameData = false;
}
// 给图片添加/去掉文字
if (gameSetUI.addString) {
boardModel.addString();
} else {
boardModel.removeString();
}
// System.out.println("开始游戏......");
// boardModel.randomize(options.hard);
if (gameSetUI.funny) {
boardModel.rearrangeFunnily(gameSetUI.hard);
} else {
boardModel.randomize(gameSetUI.hard);
}
boardModel.starTime = System.currentTimeMillis();
boardModel.sumTime = 0;
} else {// 恢复游戏
// 暂停计时
// boardModel.sumtTime+=System.currentTimeMillis()-boardModel.starTime;
// System.out.println(boardModel.sumTime/1000);
// 给图片添加/去掉文字
if (gameSetUI.addString) {
boardModel.addString();
} else {
boardModel.removeString();
}
boardModel.starTime = System.currentTimeMillis();
}
boardUI.setBoardModel(boardModel);
boardModel.setGameState(GameDB.PLAYING);
boardUI.repaint();
display.setCurrent(boardUI);
break;
}
case GameDB.COMMAND_ID_SHOW_PHOTO: {
cheated = false;
// 新游戏;如果游戏正在玩。第二个选项是“新游戏”
if (boardModel.getGameState() == GameDB.PLAYING) {
if (isChangeGameData) {
boardModel.initData();
isChangeGameData = false;
}
// 给图片添加/去掉文字
if (gameSetUI.addString) {
boardModel.addString();
} else {
boardModel.removeString();
}
cheated = false;
// System.out.println("开始游戏......");
boardModel.randomize(gameSetUI.hard);
if (gameSetUI.funny) {
boardModel.rearrangeFunnily(gameSetUI.hard);
} else {
boardModel.randomize(gameSetUI.hard);
}
boardModel.starTime = System.currentTimeMillis();
boardModel.sumTime = 0;
boardUI.setBoardModel(boardModel);
boardModel.setGameState(GameDB.PLAYING);
boardUI.repaint();
display.setCurrent(boardUI);
} else {// 设置图片
boardModel.resetGrid();
boardModel.setGameState(GameDB.INITIALIZED);
classSetPhoto.showList();
// display.setCurrent(setPhotoUI);
}
break;
}
case GameDB.COMMAND_ID_BEST: {
display.setCurrent(showPeopleInfoListUI);
// display.setCurrent(indexFunctionUI);
// boardUI.repaint();
break;
}
case GameDB.COMMAND_ID_OPTIONS: {
display.setCurrent(this.gameSetUI);
// display.setCurrent(indexFunctionUI);
boardUI.repaint();
break;
}
case GameDB.COMMAND_ID_HELP: {
new HelpUI(this.display,this.display.getCurrent());
// display.setCurrent(indexFunctionUI);
break;
}
case GameDB.COMMAND_ID_RESET: {
this.isChangeGameData=false;
System.out.println("游戏重置。");
this.boardModel.initData();
setCurrent(this.mainMenuUI);
break;
}
case GameDB.COMMAND_ID_TEST: {
System.out.println("进入测试。");
setCurrent(waitCanvasUI);
waitCanvasUI.setFlag(true);
break;
}
case GameDB.COMMAND_ID_EXIT: {
classMain.notifyDestroyed();
// display.setCurrent(listPhoneUI);
break;
}
case GameDB.INITIALIZED: {
// newPhoneUI.clear();
// display.setCurrent(newPhoneUI);
break;
}
case GameDB.PLAYING: {
break;
}
case GameDB.WON: {
setCurrent(waitCanvasUI);
waitCanvasUI.setFlag(true);
break;
}
default:
break;
}
}
public BoardModel getBoardModel() {
return boardModel;
}
public void setBoardModel(BoardModel boardModel) {
this.boardModel = boardModel;
}
}