//------------------------------------------------------------------------------------ public int FindFirst(hash clave) { int low = 0, mid, high = (int)(m_Stream.Length / SIZE_OF_BOOKENTRY) - 1; cLibro.stLineaLibro e = new cLibro.stLineaLibro(); while (low < high) { mid = (low + high) / 2; m_Stream.Seek(mid * SIZE_OF_BOOKENTRY, SeekOrigin.Begin); Read(ref e); if (clave <= e.key) high = mid; else low = mid + 1; } return low; }
//------------------------------------------------------------------------------------ public mov Buscar(cPosicion posicion) { mov move = cMovType.MOV_NAN; if (Open()) { cLibro.stLineaLibro lineaLibro = new cLibro.stLineaLibro(); UInt16 bMejorJugada = 0; uint sum = 0; hash key = PolyglotKey(posicion); m_Stream.Seek(FindFirst(key) * SIZE_OF_BOOKENTRY, SeekOrigin.Begin); while (Read(ref lineaLibro) && lineaLibro.key == key) { bMejorJugada = Math.Max(bMejorJugada, lineaLibro.count); sum += lineaLibro.count; if ((sum != 0 && ((((uint)m_Rand.GetRand()) % sum) < lineaLibro.count)) || (lineaLibro.count == bMejorJugada)) move = (lineaLibro.move); } if (move != 0) { int pt = (move >> 12) & 7; if (pt != 0) move = cTypes.CreaMov(cTypes.GetFromCasilla(move), cTypes.GetToCasilla(move), cMovType.PROMOCION, (pt + 1)); for (cReglas listaMovimientos = new cReglas(posicion, cMovType.LEGAL); listaMovimientos.GetActualMov() != cMovType.MOV_NAN; ++listaMovimientos) if (move == (listaMovimientos.GetActualMov() ^ cTypes.TipoMovimiento(listaMovimientos.GetActualMov()))) { move = listaMovimientos.GetActualMov(); break; } } } return move; }