feat: Refactor move ordering logic to improve gain calculation and simplify exchange handling
Build & Test (NowChessSystems) TeamCity build failed
Build & Test (NowChessSystems) TeamCity build failed
This commit is contained in:
@@ -108,7 +108,7 @@ object MoveOrdering:
|
|||||||
val initialGain = victimValue(context, move)
|
val initialGain = victimValue(context, move)
|
||||||
movedPieceAfterMove(context, move).fold(initialGain) { moved =>
|
movedPieceAfterMove(context, move).fold(initialGain) { moved =>
|
||||||
val boardAfterMove = applySeeMove(context.board, move, moved)
|
val boardAfterMove = applySeeMove(context.board, move, moved)
|
||||||
exchangeGain(boardAfterMove, target, context.turn.opposite, pieceValue(moved), Vector(initialGain))
|
initialGain - seeGain(boardAfterMove, target, context.turn.opposite, pieceValue(moved))
|
||||||
}
|
}
|
||||||
|
|
||||||
private def movedPieceAfterMove(context: GameContext, move: Move): Option[Piece] =
|
private def movedPieceAfterMove(context: GameContext, move: Move): Option[Piece] =
|
||||||
@@ -116,21 +116,13 @@ object MoveOrdering:
|
|||||||
case MoveType.Promotion(pp) => Some(Piece(context.turn, promotionPieceType(pp)))
|
case MoveType.Promotion(pp) => Some(Piece(context.turn, promotionPieceType(pp)))
|
||||||
case _ => context.board.pieceAt(move.from)
|
case _ => context.board.pieceAt(move.from)
|
||||||
|
|
||||||
@tailrec
|
private def seeGain(board: Board, target: Square, side: Color, currentValue: Int): Int =
|
||||||
private def exchangeGain(board: Board, target: Square, side: Color, capturedValue: Int, gains: Vector[Int]): Int =
|
|
||||||
leastValuableAttacker(board, target, side) match
|
leastValuableAttacker(board, target, side) match
|
||||||
case None => resolveGain(gains)
|
case None => 0
|
||||||
case Some((from, attacker)) =>
|
case Some((from, attacker)) =>
|
||||||
val nextGain = capturedValue - gains.last
|
|
||||||
val nextBoard = board.removed(from).updated(target, attacker)
|
val nextBoard = board.removed(from).updated(target, attacker)
|
||||||
exchangeGain(nextBoard, target, side.opposite, pieceValue(attacker), gains :+ nextGain)
|
val replyGain = seeGain(nextBoard, target, side.opposite, pieceValue(attacker))
|
||||||
|
math.max(0, currentValue - replyGain)
|
||||||
private def resolveGain(gains: Vector[Int]): Int =
|
|
||||||
(gains.length - 2 to 0 by -1)
|
|
||||||
.foldLeft(gains) { (acc, idx) =>
|
|
||||||
acc.updated(idx, math.max(acc(idx), -acc(idx + 1)))
|
|
||||||
}
|
|
||||||
.head
|
|
||||||
|
|
||||||
private def applySeeMove(board: Board, move: Move, moved: Piece): Board =
|
private def applySeeMove(board: Board, move: Move, moved: Piece): Board =
|
||||||
move.moveType match
|
move.moveType match
|
||||||
|
|||||||
Reference in New Issue
Block a user