J2ME 拼图游戏 快速开发 全过程 之代码祥解(5 )——记录集操作类、序列的奇偶性检查类

记录集操作类,代码:

package cn.edu.xtu.tilepuzzle.model;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;


import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;


import cn.edu.xtu.tilepuzzle.GameDB;




public class RecordStoreOperations {
/*
public static void getRecordStoreInfo(String recordStoreString){
RecordStore rStore = getRecord(recordStoreString);
if(rStore!=null){
try {
System.out.println("这里是getRecordStoreInfo:"+rStore.getNumRecords());
} catch (RecordStoreNotOpenException e1) {
e1.printStackTrace();
}
}

}
public static void getRecordStoreList(String recordStoreString) {
RecordStore rStore = getRecord(recordStoreString);
if(rStore!=null){
int num=0;
try {
num = rStore.getNumRecords();
for(int i=1;i<=num;i++){
try {
System.out.println("这里是getRecordStoreList:"+byteToStr(rStore.getRecord(i)));
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
} catch (RecordStoreNotOpenException e1) {
e1.printStackTrace();
}
}
}
*/
public static boolean deleteRecordStroe(String recordStoreString){
try {
RecordStore.deleteRecordStore(recordStoreString);
System.out.println("成功删除记录集:"+recordStoreString);
return true;
} catch (RecordStoreNotFoundException e) {
System.out.println("没有找到你要删除的记录集。");
} catch (RecordStoreException e) {
System.out.println("删除记录集出错。");
}
return false;
}
public static boolean changeRecordStroe(String recordStoreString,String str,int flag){
RecordStore rStore = getRecord(recordStoreString);
// 创建ByteArrayOutputStream对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();


// 创建与baos关联的DataOutputStream对象
DataOutputStream dos = new DataOutputStream(baos);


// 调用dos的writeUTF()方法将字符串写入dos对象中,自动传递给关联的baos对象,转换成ByteArrayOutputStream对象
try {
dos.writeUTF(str);
// 关闭数据流
dos.close();
} catch (IOException e) {
e.printStackTrace();
}


// 调用baos的toByteArray()方法,将str字符串转换成字节数组类型
byte b[] = baos.toByteArray();


try {
rStore.setRecord(flag, b, 0, b.length);
return true;
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (InvalidRecordIDException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
System.out.println("更改记录时出错!!!");
e.printStackTrace();
}finally{
try {
rStore.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
return false;
}

public static ClassPeopleInfo[] ReaderRecordStores(String recordStoreString) {
RecordStore rStore = getRecord(recordStoreString);
if (rStore != null) {
try {
int num = rStore.getNumRecords();
String tempString, str[];
ClassPeopleInfo[] peopleInfo = new ClassPeopleInfo[num];
//System.out.println("人物信息的数量:"+peopleInfo.length);
for (int i = 1; i <= num; i++) {
tempString = byteToStr(rStore.getRecord(i));
// System.out.println("这里是ReaderRecordStores:"+tempString);
str = splitToStr(tempString, ";",3);
//System.out.println("下面进行peopleInfo的设置 :" + i );
peopleInfo[i-1]=new ClassPeopleInfo();
peopleInfo[i-1].setName(str[0]);
peopleInfo[i-1].setFlag(str[1]);
//System.out.println(Long.parseLong(str[2]));
peopleInfo[i-1].setTime(Long.parseLong(str[2]));
//System.out.println("设置 " + i + "完成。");
}
return peopleInfo;
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
return null;
}


public static boolean addStrToRecordStroe(String str,String recordStoreString) {

RecordStore rStore = getRecord(recordStoreString);
// 创建ByteArrayOutputStream对象
ByteArrayOutputStream baos = new ByteArrayOutputStream();


// 创建与baos关联的DataOutputStream对象
DataOutputStream dos = new DataOutputStream(baos);


// 调用dos的writeUTF()方法将字符串写入dos对象中,自动传递给关联的baos对象,转换成ByteArrayOutputStream对象
try {
dos.writeUTF(str);
// 关闭数据流
dos.close();
} catch (IOException e) {
e.printStackTrace();
}


// 调用baos的toByteArray()方法,将str字符串转换成字节数组类型
byte b[] = baos.toByteArray();


// 将已经转换成字节数组类型的数据写入记录存储系统中
try {
//int id=
rStore.addRecord(b, 0, b.length);
//System.out.println("ID:"+id);
//System.out.println(str+":添加成功");
return true;
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}finally{
try {
rStore.closeRecordStore();
} catch (RecordStoreNotOpenException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
}
return false;
}


public static RecordStore getRecord(String recordStoreString) {
try {
return RecordStore.openRecordStore(recordStoreString, true);
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
System.out.println("记录集不存在");
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
}
return null;
}
public static boolean checkIfRecord(String recordStoreString) {
try {
RecordStore.openRecordStore(recordStoreString, false);
System.out.println("记录集正常");
return true;
} catch (RecordStoreFullException e) {
System.out.println("记录集满");
return false;
} catch (RecordStoreNotFoundException e) {
System.out.println("记录集不存在");
return false;
} catch (RecordStoreException e) {
System.out.println("记录集异常");
return false;
}
}


public static String byteToStr(byte[] b) {
String tempString = "";


// 建立与记录内容数组关联的ByteArrayInputStream对象 bs
ByteArrayInputStream bis = new ByteArrayInputStream(b);


// 建立与bs关联的DataInputStream对象ds
DataInputStream dis = new DataInputStream(bis);


// 将读取的记录内容转换成UTF字符串
try {
tempString = dis.readUTF();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return tempString;
}


private static String[] splitToStr(String original, String regex,int num) {


String s = original;
//System.out.println("这里是进入splitToPeopleInfo:"+s);
String[] str = new String[num];
int i = 0;
while (s.indexOf(regex) != -1 && i<num) {
str[i] = s.substring(0, s.indexOf(regex));
//System.out.println("分割中-前面:"+str[i]);
i++;
s = s.substring(s.indexOf(regex) + regex.length());
//System.out.println("分割中-剩下:"+s);
}

//System.out.println(i+","+s);
str[i]=s;
//System.out.println("这里是离开splitToPeopleInfo:"+original);
return str;
}

/*
* str[0]:orgImageString 图片路径默认
* str[1]:反向 boolean true 反向        false
* str[2]:趣味洗牌 boolean true 是 false
* str[3]:标记方格 boolean true 标记 true
* str[4]:困难/简单boolean true 困难 true
* */
public static String[] getGameSetData(String recordStoreGameSetData){
System.out.println("开始获取游戏数据。。。。。。");
//System.out.println("=1 :"+recordStoreGameSetData);
RecordStore rStore = getRecord(recordStoreGameSetData);
if (rStore != null) {
try {
//int num = rStore.getNumRecords();
//System.out.println("共有记录数:"+num);
String tempString="", str[];
//System.out.println("人物信息的数量:"+peopleInfo.length);
tempString = byteToStr(rStore.getRecord(GameDB.GameSetDataFlag));
//for (int i = 1; i <= num; i++)tempString = byteToStr(rStore.getRecord(i));
//System.out.println("记录内容:"+tempString);
str = splitToStr(tempString, ";",GameDB.GameSetDataNum);///***
//System.out.println("=2"+tempString);
return str;
} catch (RecordStoreException e) {
e.printStackTrace();
}
System.out.println("获取游戏数据成功");
}else {
System.out.println("记录为空!!!!");
}
return null;
}
}

序列的奇偶性检查类类,代码:

/*
 * SerialCheck.java
 *
 * Created on 2012年3月21日, 下午6:32
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package cn.edu.xtu.tilepuzzle.model;


/**
 *
 * @author Administrator
 */
public class SerialCheck {


    public static boolean isEven(ClassPiece all[]){
        int len=all.length;
        //System.out.println(len);
        int sum=0;
        for(int i=0;i<len-1;i++){
             for(int j=i+1;j<len;j++){
                 //System.out.println(all[i].serial+","+all[j].serial);
                if(all[i].serial>all[j].serial){
                    sum++;
                  //  System.out.println(all[i].serial+","+all[j].serial);
                }
            }
        }
        if(sum%2==0){
            return true;            
        }
        else{
            return false;            
        }
    }
 /*   
        public static boolean isEven2(int all[]){
        int len=all.length;
        int sum=0;
        for(int i=0;i<all.length;i++){
             for(int j=i+1;j<all.length;j++){
                if(all[i]>all[j])
                    sum++;
            }
        }
        if(sum%2==0)
            return true;
        else
            return false;
    }
  */
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值