1

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.

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();
    }

I see no problem in my hashCode() implementation, since it keeps returning same values for same states over and over.

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?

sk1ll3r
  • 295
  • 4
  • 15
  • Maybe BoardState isn't correctly serialized? Have you verified that its members are correct after writing and reading to file? – ekholm Jul 31 '12 at 13:02
  • You implemented your Hashtable yourself? Why aren't you using HashMap? I never had serialization issues with it. Please add some code of the serialization process. – Martze Jul 31 '12 at 13:02
  • @ekholm I just added `implements Serializable` to BoardState. Apart from that, my Board state just consists of few instance variables, getters, setters, hashCode() and equals(). @Martze No, I used the [Hashtable](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Hashtable.html) from Java library (java.util.Hashtable). I haven't tried HashMap. – sk1ll3r Jul 31 '12 at 13:12
  • Is that the correct way to serialize? – sk1ll3r Jul 31 '12 at 14:13
  • For debugging, I would try writing a few BoardState objects one by one to an ObjectOutputStream, reading each object back with an ObjectInputStream, and verify that all fields are correct – ekholm Jul 31 '12 at 14:16
  • @ekholm I tried doing what you suggested the returned BoardState objects matched. – sk1ll3r Jul 31 '12 at 21:47
  • @sk1ll3r Ok, so I suppose hashCode() returns the same value before and after serialization? Well, then I'm out of ideas for the moment. Have you tried the same code with Hashtables that have keys and values of other types? – ekholm Aug 01 '12 at 06:55
  • @ekhohlm Yeah, `hashCode()` returns the same value before and after serialization. I also tried to save and restore `Hashtables`, but when restored, it seemed to restore the same `Hashtable` (with same key-value pairs). It is very weird, since it's running the same code, just in 2 different places (while debugging/in game). – sk1ll3r Aug 01 '12 at 10:22

1 Answers1

0

My serializing and hashing was completely fine. My program went something like this:

  1. add new key-value pair to transpositionTable
  2. play next move
  3. save transpositionTable to internal storage

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();

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();

but that didn't work either. Finally I had to assign each field of currentData.getBoardState() to newKey manually, which worked.

sk1ll3r
  • 295
  • 4
  • 15