Skip to content

Commit

Permalink
more resilient research of anchor point for explorer insertion
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Sep 19, 2017
1 parent e708a27 commit 8ddf770
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
19 changes: 9 additions & 10 deletions modules/study/src/main/ExplorerGame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,19 @@ private final class ExplorerGame(
}

private def merge(fromNode: RootOrNode, fromPath: Path, game: Node.Root, comment: Comment): Option[(Node, Path)] = {
val gameNodes = game.mainline.take(lila.explorer.maxPlies).reverse
val fromNodes = fromNode.mainline.take(lila.explorer.maxPlies - fromNode.ply)
val fromEndPath = fromNodes.drop(1).foldLeft(fromPath)(_ + _)
val (path, foundGameNode) = fromNodes.reverse.foldLeft((fromEndPath, none[Node])) {
case ((path, None), fromNode) =>
gameNodes.find(_.fen == fromNode.fen) match {
case Some(gameNode) => path -> gameNode.some
case None => path.init -> none
val gameNodes = game.mainline.dropWhile(_.fen != fromNode.fen) drop 1
val (path, foundGameNode) = gameNodes.foldLeft((Path.root, none[Node])) {
case ((path, None), gameNode) =>
val nextPath = path + gameNode
fromNode.children.nodeAt(nextPath) match {
case Some(child) => (nextPath, none)
case None => (path, gameNode.some)
}
case (found, _) => found
}
foundGameNode.flatMap(_.children.first).map { nextGameNode =>
foundGameNode.map { nextGameNode =>
val commentedNode = nextGameNode setComment comment
commentedNode -> path
commentedNode -> fromPath.+(path)
}
}

Expand Down
3 changes: 2 additions & 1 deletion modules/study/src/main/Path.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ case class Path(ids: List[UciCharPair]) extends AnyVal {

def isEmpty = ids.isEmpty

def +(node: Node) = Path(ids :+ node.id)
def +(node: Node): Path = Path(ids :+ node.id)
def +(more: Path): Path = Path(ids ::: more.ids)

override def toString = ids.mkString
}
Expand Down

0 comments on commit 8ddf770

Please sign in to comment.