TranspositionTable { public final void insert(long key, Move sm, int type, int ply, int depth, int evalScore) { if (depth < 0) depth = 0; int idx0 = h0(key); int idx1 = h1(key); TTEntry ent = table[idx0]; byte hashSlot = 0; if (ent.key != key) { ent = table[idx1]; hashSlot = 1; } if (ent.key != key) { if (table[idx1].betterThan(table[idx0], generation)) { ent = table[idx0]; hashSlot = 0; } if (ent.valuable(generation)) { int altEntIdx = (ent.getHashSlot() == 0) ? h1(ent.key) : h0(ent.key); if (ent.betterThan(table[altEntIdx], generation)) { TTEntry altEnt = table[altEntIdx]; altEnt.key = ent.key; altEnt.move = ent.move; altEnt.score = ent.score; altEnt.depthSlot = ent.depthSlot; altEnt.generation = ent.generation; altEnt.type = ent.type; altEnt.setHashSlot(1 - ent.getHashSlot()); altEnt.evalScore = ent.evalScore; } } } boolean doStore = true; if ((ent.key == key) && (ent.getDepth() > depth) && (ent.type == type)) { if (type == TTEntry.T_EXACT) { doStore = false; } else if ((type == TTEntry.T_GE) && (sm.score <= ent.getScore(ply))) { doStore = false; } else if ((type == TTEntry.T_LE) && (sm.score >= ent.getScore(ply))) { doStore = false; } } if (doStore) { if ((ent.key != key) || (sm.from != sm.to)) ent.setMove(sm); ent.key = key; ent.setScore(sm.score, ply); ent.setDepth(depth); ent.generation = generation; ent.type = (byte)type; ent.setHashSlot(hashSlot); ent.evalScore = (short)evalScore; } } TranspositionTable(int log2Size); final void insert(long key, Move sm, int type, int ply, int depth, int evalScore); final TTEntry probe(long key); final void nextGeneration(); final void clear(); final ArrayList<Move> extractPVMoves(Position rootPos, Move m); final String extractPV(Position pos); final void printStats();  }
@Test public void testInsert() throws ChessParseError { System.out.println("insert"); TranspositionTable tt = new TranspositionTable(16); Position pos = TextIO.readFEN(TextIO.startPosFEN); String[] moves = { "e4", "e5", "Nf3", "Nc6", "Bb5", "a6", "Ba4", "b5", "Bb3", "Nf6", "O-O", "Be7", "Re1" }; UndoInfo ui = new UndoInfo(); for (int i = 0; i < moves.length; i++) { Move m = TextIO.stringToMove(pos, moves[i]); pos.makeMove(m, ui); int score = i * 17 + 3; m.score = score; int type = TranspositionTable.TTEntry.T_EXACT; int ply = i + 1; int depth = i * 2 + 5; tt.insert(pos.historyHash(), m, type, ply, depth, score * 2 + 3); } pos = TextIO.readFEN(TextIO.startPosFEN); for (int i = 0; i < moves.length; i++) { Move m = TextIO.stringToMove(pos, moves[i]); pos.makeMove(m, ui); TranspositionTable.TTEntry ent = tt.probe(pos.historyHash()); assertEquals(TranspositionTable.TTEntry.T_EXACT, ent.type); int score = i * 17 + 3; int ply = i + 1; int depth = i * 2 + 5; assertEquals(score, ent.getScore(ply)); assertEquals(depth, ent.getDepth()); assertEquals(score * 2 + 3, ent.evalScore); Move tmpMove = new Move(0,0,0); ent.getMove(tmpMove); assertEquals(m, tmpMove); } }