J2ME 拼图游戏 快速开发 全过程 之代码祥解(5 )——记录集操作类、序列的奇偶性检查类
2012-04-14 14:09
375 查看
记录集操作类,代码:
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;
}
*/
}
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;
}
*/
}
相关文章推荐
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(4 )——图片操作类、图片预览后处理类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(1 )——程序入口与控制器开发
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(2 )——游戏数据库与游戏模型开发
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(3 )——拼图的方格类、个人信息类、选择背景拼图的列表类
- J2ME 拼图游戏 快速开发 全过程 之代码祥解(6 )——欢迎界面、游戏结束后的等待界面与增加用户名字界面
- J2ME 拼图游戏 快速开发 全过程 (五)——游戏帮助界面
- J2ME 拼图游戏 快速开发 全过程 (六)——图片设置界面设计
- J2ME 拼图游戏 快速开发 全过程 (一)——开发环境及游戏菜单
- J2ME 拼图游戏 快速开发 全过程 (七)——设置图片后的图片预览界面
- J2ME 拼图游戏 快速开发 全过程 (二)——游戏开始界面设计
- J2ME 拼图游戏 快速开发 全过程 (三)——新游戏界面
- J2ME 拼图游戏 快速开发 全过程 (一)——最佳成绩显示界面
- J2ME 拼图游戏 快速开发 全过程 (四)——游戏选项设置界面
- [Android开发] 在项目中快速实现 列表字母排序滑动索引 功能原理以及过程代码
- Android 开发过程中 Java 代码中属性 设置 底层实现
- ATECC508A芯片开发笔记(五):Provision执行过程及代码分析
- 微信快速开发框架(七)--发送客服信息,版本更新至V2.2 代码已更新至github
- asp.net导出excel-一行代码实现excel、xml、pdf、word、html、csv等7种格式文件导出功能而且美观-SNF快速开发平台
- iOS开发UI篇—以微博界面为例使用纯代码自定义cell程序编码全过程(一)
- 自动生成导出前端页面的快速开发工具,让代码开发变得简单