📄 chessrecord.java
字号:
import java.io.*;
import javax.microedition.rms.*;
import javax.microedition.lcdui.*;
public class ChessRecord
{
private RecordStore indexRecord=null,contentRecord=null;
private RecordEnumeration indexEnum=null,contentEnum=null;
private String[] recordName=null;
private int[] indexIds=null;
private int id=0;
public ChessRecord()
{
try
{
indexRecord=RecordStore.openRecordStore("IndexRecord",true);
contentRecord=RecordStore.openRecordStore("ContentRecord",true);
}
catch (RecordStoreNotFoundException e)
{
//errorDeal.push("ChessRecord#1", "没有发现存储文件");
Alert alert=new Alert("Message","没有发现存储文件",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch (RecordStoreFullException e)
{
//errorDeal.push("已经没有够的存储空间,请先删掉其他一些占用存储空间的Java程序");
Alert alert=new Alert("Message","已经没有够的存储空间,请先删掉其他一些占用存储空间的Java程序",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch (RecordStoreException e)
{
//errorDeal.push("ChessRecord#2", "在创建或者打开存储文件时出现了未知错误,请重新启动程序试试");
Alert alert=new Alert("Message","在创建或者打开存储文件时出现了未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
}
public int closeChessRecord()
{
try
{
contentRecord.closeRecordStore();
indexRecord.closeRecordStore();
}
catch(RecordStoreException rse)
{
return 1;
}
return 0;
}
//************************查看记录时,取得初始棋盘
public int[][] getChess(String str)
{
for(int i=0;i<recordName.length;i++)
{
if(str.equals(recordName[i]))
{
id=indexIds[i];
break;
}
}
//System.out.println("取得所选择的index的id:"+id);
byte[] tempRecord=null;
try
{
tempRecord=indexRecord.getRecord(id);
}
catch(RecordStoreNotOpenException e)
{
//System.out.println("获取index数组时出现RecordStoreNotOpenException异常");
//errorDeal.push("ChessRecord#3", "获取初始棋盘时出错,请重新试试");
Alert alert=new Alert("Message","获取初始棋盘时出错",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch(InvalidRecordIDException e)
{
//System.out.println("获取index数组时出现InvalidRecordIDException异常");
//errorDeal.push("ChessRecord#4", "获取初始棋盘时使用的是非法ID");
Alert alert=new Alert("Message","获取初始棋盘时使用的是非法ID",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch(RecordStoreException e)
{
//System.out.println("获取index数组时出现RecordStoreException异常");
//errorDeal.push("ChessRecord#5", "获取初始棋盘时出现未知错误");
Alert alert=new Alert("Message","获取初始棋盘时出现未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
Index index=indexDeserialize(tempRecord);
String initString=index.getInitString();
int n=0;
int[][] chess=new int[10][9];
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
chess[i][j]=initString.charAt(n);
n=n+1;
//System.out.println("成功取得一个棋盘初始化数据:"+chess[i][j]);
}
}
//System.out.println("取得棋盘初始化数据成功");
return chess;
}
//**********************获取走棋的contentId集合
public int[] getChessChance()
{
int[] contentIds=null;
try
{
int firstNumber=0;
int secondNumber=0;
boolean isLast=false;
Index firstIndex=null,secondIndex=null;
firstIndex=indexDeserialize(indexRecord.getRecord(id));
//System.out.println("indexIds的大小是:"+indexIds.length);
//System.out.println("indexRecord的大小是:"+indexRecord.getNumRecords());
try
{
indexEnum=indexRecord.enumerateRecords(null,null,false);
}
catch(Exception e)
{
//System.out.println("这儿出现了异常");
//errorDeal.push("ChessRecord#6", "获取棋盘记录中的初始数据时出错");
Alert alert=new Alert("Message","获取棋盘记录中的初始数据时出错",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
int secondId=0;
//System.out.println("开始进入for循环");
for(int i=0;i<indexIds.length;i++)
{
//System.out.println("一次for循环,i值为:"+i);
if(id==indexIds[i])
{
if(i==0)
{
if(i==indexIds.length-1)
{
isLast=true;
}
else
{
if(indexIds[i+1]>id)
secondId=indexIds[i+1];
else
isLast=true;
}
}
else if(i==indexIds.length-1)
{
if(indexIds[i-1]>id)
secondId=indexIds[i-1];
else
isLast=true;
}
else
{
if(indexIds[i-1]>id)
secondId=indexIds[i-1];
else
secondId=indexIds[i+1];
}
break;
}
}
//System.out.println("走出for循环");
firstNumber=firstIndex.getNumber();
//System.out.println("isLast的值是:"+isLast);
if(!isLast)
{
secondIndex=indexDeserialize(indexRecord.getRecord(secondId));
secondNumber=secondIndex.getNumber();
}
//System.out.println("成功取得firstIndex和secondIndex"+"\n"+firstIndex+"\n"+secondIndex);
//System.out.println("成功取得firstNumber和secondNumber"+"\n"+firstNumber+"\n"+secondNumber);
contentEnum=contentRecord.enumerateRecords(null,null,false);
//System.out.println("成功取得遍历集合");
int tempId=0;
StringBuffer idSb=new StringBuffer();
if(!isLast)
{
//System.out.println("isLast的值是false");
while(contentEnum.hasNextElement())
{
tempId=contentEnum.nextRecordId();
//System.out.println("取得一个tempId:"+tempId);
if(tempId<firstNumber||tempId>=secondNumber)
continue;
else
{
idSb.append((char)tempId);
//System.out.println("成功取得一个ID:"+tempId);
}
}
}
else
{
//System.out.println("isLast的值是true");
while(contentEnum.hasNextElement())
{
tempId=contentEnum.nextRecordId();
if(tempId<firstNumber)
continue;
else
{
idSb.append((char)tempId);
//System.out.println("成功取得一个ID:"+tempId);
}
}
}
int m=idSb.length();
contentIds=new int[m];
for(int i=0;i<m;i++)
{
contentIds[i]=idSb.charAt(i);
//System.out.println(contentIds[i]);
}
}
catch (RecordStoreException e)
{
//System.out.println("出现了异常");
//errorDeal.push("ChessRecord#7", "获取棋盘走步变化ID集合时出现未知错误");
Alert alert=new Alert("Message","获取棋盘走步变化ID集合时出现未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
return contentIds;
}
public int[] getChanceRecord(int cRId)
{
byte[] tempRecord=null;
try
{
tempRecord=contentRecord.getRecord(cRId);
}
catch(RecordStoreNotOpenException e)
{
//errorDeal.push("ChessRecord#8", "获取棋盘走步记录中的走步变化时打开存储文件失败");
Alert alert=new Alert("Message","获取棋盘走步记录中的走步变化时打开存储文件失败",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch(InvalidRecordIDException e)
{
//errorDeal.push("ChessRecord#9", "获取棋盘走步记录中的走步变化时出现了非法ID");
Alert alert=new Alert("Message","获取棋盘走步记录中的走步变化时出现了非法ID",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch(RecordStoreException e)
{
//errorDeal.push("ChessRecord#10", "获取棋盘走步记录中的走步变化时出现了未知错误");
Alert alert=new Alert("Message","获取棋盘走步记录中的走步变化时出现了未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
return contentDeserialize(tempRecord);
}
//************************取得记录列表
public String[] getRecordList()
{
try
{
indexEnum=indexRecord.enumerateRecords(null,null,false);
recordName=new String[indexEnum.numRecords()];
indexIds=new int[indexEnum.numRecords()];
//System.out.println("得到index条数为:"+indexEnum.numRecords());
}
catch(RecordStoreNotOpenException rsno)
{
//errorDeal.push("ChessRecord#11", "获取棋盘走步记录集合列表时打开文件失败");
Alert alert=new Alert("Message","获取棋盘走步记录集合列表时打开文件失败",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
catch (RecordStoreException rse)
{
//这儿应该中断,然后将异常应该上抛,用于在列表显示不出来时,向用户提醒有错误
//errorDeal.push("ChessRecord#12", "获取棋盘走步记录集合列表时出现未知错误");
Alert alert=new Alert("Message","获取棋盘走步记录集合列表时出现未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
byte[] data=null;
int i=0;
try
{
while(indexEnum.hasNextElement())
{
data=indexEnum.nextRecord();
Index index=indexDeserialize(data);
recordName[i]=index.getName();
i=i+1;
//System.out.println("得到一个index名字:"+index.getName());
}
int j=0;
indexEnum.reset();
//索引恢复至刚刚建立之时
while(indexEnum.hasNextElement())
{
//System.out.println("进入遍历循环");
int n=indexEnum.nextRecordId();
indexIds[j]=n;
j=j+1;
}
}
catch (RecordStoreException e)
{
//System.out.println("出现RecordStoreException异常");
//errorDeal.push("ChessRecord#13", "获取棋盘走步记录集合的名字和ID列表时出现未知错误");
Alert alert=new Alert("Message","获取棋盘走步记录集合的名字和ID列表时出现未知错误",null,AlertType.WARNING);
Chess.dis.setCurrent(alert);
Chess.quitApp();
}
return recordName;
}
//*******************添加记录部分的方法
public int addIndex(int chess[][])
{
StringBuffer sb=new StringBuffer(90);
char c=(char)0;
for(int i=0;i<10;i++)
{
for(int j=0;j<9;j++)
{
c=(char)chess[i][j];
sb.append(c);
//System.out.println("初始棋盘数组为:"+chess[i][j]);
}
}
//System.out.println("初始棋盘数组转化为字符串:"+sb);
Index index=new Index();
index.setName();
index.setInitString(sb.toString());
sb=null;
int startNumber=0;
try
{
startNumber=contentRecord.getNumRecords()+1;
//System.out.println("返回开始序列号成功");
}
catch (RecordStoreException e)
{
//System.out.println("返回开始序列号时出现异常");
//errorDeal.push("ChessRecord#14", "添加走棋记录时返回走棋变化开始序列号出现存储文件操作错误");
return 1;
}
index.setNumber(startNumber);
byte[] data=null;
try
{
data=indexSerialize(index);
indexRecord.addRecord(data,0,data.length);
//System.out.println("添加index记录成功");
}
catch (RecordStoreException re)
{
//errorDeal.push("ChessRecord#15", "添加走棋记录时出现存储文件操作错误");
return 2;
}
return 0;
}
public boolean addContent(int[] content)
{
byte[] data=null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -