在Android的内部保存的数据弄乱我的数据 [英] Saving data internally in Android messes up my data
问题描述
我目前工作的一个游戏,我需要创建一个换位表我的AI。我实现了 的Hashtable
使键
来Hashtable中正在考虑一个国家和它对应的值
是最佳的下一步行动。
I'm currently working on a game, where I need to create a transposition table for my AI. I implemented Hashtable
so that the key
to the Hashtable is a state under consideration and its corresponding value
is the optimal next move.
然而,当我保存的哈希表在Android的内部存储,并在下一次恢复它,它似乎有一些保存的数据,但键
(即游戏状态)比键不同
我已保存在previous实例。
However, when I save the Hashtable in Android's internal storage and restore it next time, it seems to have some saved data, but the keys
(i.e. game states) are different than the keys
I have saved in the previous instance.
下面是我如何保存和恢复我的数据:
Here is how I save and restore my data:
private void readTranspositionTableFromFile() {
FileInputStream fis = openFileInput(FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
transpositionTable = (Hashtable<BoardState, NextMove>) ois.readObject();
ois.close();
deleteFile(FILENAME);
}
private void writeTranspositionTableToFile() {
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(transpositionTable);
oos.close();
}
我看不出有什么问题,我的散code()
的实施,因为它保持一遍又一遍为返回相同的状态值相同。
I see no problem in my hashCode()
implementation, since it keeps returning same values for same states over and over.
我怀疑保存/恢复,因为我记录的2运行时和在第一个我得到了散code()
返回置换表弄乱我的数据3964和3029为2规定为要添加到表中。然而,尽管从文件中读取表中,散code()
返回9119的两倍。
I suspect saving/restoring the transposition table messes up my data because I logged the 2 runtimes and on the first one I got hashCode()
to return 3964 and 3029 for the 2 states that were to be added to the table. However, while reading the table from file, the hashCode()
returned 9119 twice.
有没有保存任何问题/恢复数据?
Is there any problem in saving/restoring the data?
推荐答案
我的序列化和散列完全罚款。我的程序去是这样的:
My serializing and hashing was completely fine. My program went something like this:
- 添加新的键值对,
transpositionTable
- 播放下一个动作
- 保存
transpositionTable
的内部存储
- add new key-value pair to
transpositionTable
- play next move
- save
transpositionTable
to internal storage
然而,细致的记录后,我发现我的 transpositionTable
第2步后改变)。这是因为第1步)中,我用
However, after meticulous logging, I found that my transpositionTable
changed after step 2). This was because during step 1), I used
BoardState则newkey = currentData.getBoardState();
并将其保存到我的 transpositionTable
。然后在步骤2),当我的 CURRENTDATA
变了,我的 transpositionTable
相应改变。我试图用
and saved it to my transpositionTable
. Then in step 2), when my currentData
changed, my transpositionTable
changed accordingly. I tried using
BoardState则newkey = currentData.getBoardState()的clone();
但也不能工作。最后,我不得不每场分配 currentData.getBoardState()
到则newkey
手动,它的工作。
but that didn't work either. Finally I had to assign each field of currentData.getBoardState()
to newKey
manually, which worked.
这篇关于在Android的内部保存的数据弄乱我的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!