package de.nowchess.bot

import de.nowchess.api.move.Move

enum TTFlag:
  case Exact   // Score is exact
  case Lower   // Score is a lower bound
  case Upper   // Score is an upper bound

final case class TTEntry(
  hash: Long,
  depth: Int,
  score: Int,
  flag: TTFlag,
  bestMove: Option[Move]
)

final class TranspositionTable(val sizePow2: Int = 20):
  private val size = 1 << sizePow2
  private val mask = size - 1L
  private var table: Array[Option[TTEntry]] = Array.fill(size)(None)

  def probe(hash: Long): Option[TTEntry] =
    val index = (hash & mask).toInt
    table(index).filter(_.hash == hash)

  def store(entry: TTEntry): Unit =
    val index = (entry.hash & mask).toInt
    table(index) = Some(entry)

  def clear(): Unit =
    table = Array.fill(size)(None)
