Implement pgn move dump
This commit is contained in:
parent
ae25d68023
commit
2aca12c94d
|
@ -13,5 +13,16 @@ case class Move(
|
||||||
|
|
||||||
def withHistory(h: History) = copy(after = after withHistory h)
|
def withHistory(h: History) = copy(after = after withHistory h)
|
||||||
|
|
||||||
lazy val isCapture = capture.isDefined
|
def situation = before as piece.color
|
||||||
|
|
||||||
|
// does this move check the opponent?
|
||||||
|
def checks: Boolean = (after as !color).check
|
||||||
|
|
||||||
|
// does this move checkmate the opponent?
|
||||||
|
def checkMates: Boolean = (after as !color).checkMate
|
||||||
|
|
||||||
|
// does this move capture an opponent piece?
|
||||||
|
def captures = capture.isDefined
|
||||||
|
|
||||||
|
def color = piece.color
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,7 @@ sealed case class Pos private (x: Int, y: Int) {
|
||||||
|
|
||||||
def ?<(other: Pos) = x < other.x
|
def ?<(other: Pos) = x < other.x
|
||||||
def ?>(other: Pos) = x > other.x
|
def ?>(other: Pos) = x > other.x
|
||||||
|
def ?|(other: Pos) = x == other.x
|
||||||
|
|
||||||
def <->(other: Pos): Iterable[Pos] =
|
def <->(other: Pos): Iterable[Pos] =
|
||||||
min(x, other.x) to max(x, other.x) map { makePos(_, y) } flatten
|
min(x, other.x) to max(x, other.x) map { makePos(_, y) } flatten
|
||||||
|
|
|
@ -4,6 +4,7 @@ import Pos._
|
||||||
|
|
||||||
sealed trait Role {
|
sealed trait Role {
|
||||||
val forsyth: Char
|
val forsyth: Char
|
||||||
|
lazy val pgn = forsyth.toUpper
|
||||||
def dirs: List[Direction]
|
def dirs: List[Direction]
|
||||||
}
|
}
|
||||||
sealed trait PromotableRole extends Role
|
sealed trait PromotableRole extends Role
|
||||||
|
|
29
chess/src/main/scala/format/PgnDump.scala
Normal file
29
chess/src/main/scala/format/PgnDump.scala
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
package lila.chess
|
||||||
|
package format
|
||||||
|
|
||||||
|
object PgnDump {
|
||||||
|
|
||||||
|
def move(m: Move): String = {
|
||||||
|
import m._
|
||||||
|
|
||||||
|
def disambiguate = {
|
||||||
|
val candidates = situation.actors filter { a ⇒
|
||||||
|
a.pos != orig && a.piece.role == piece.role && (a.destinations contains dest)
|
||||||
|
}
|
||||||
|
if (candidates.isEmpty) ""
|
||||||
|
else if (candidates exists (_.pos ?| orig)) orig.file + orig.rank else orig.file
|
||||||
|
}
|
||||||
|
def capturing = if (captures) "x" else ""
|
||||||
|
def checking = if (checkMates) "#" else if (checks) "+" else ""
|
||||||
|
|
||||||
|
((promotion, piece.role) match {
|
||||||
|
case _ if castle ⇒ if (orig ?> dest) "O-O-O" else "O-O"
|
||||||
|
case _ if enpassant ⇒ orig.file + 'x' + dest.rank
|
||||||
|
case (Some(promotion), _) ⇒ dest.key + promotion.pgn
|
||||||
|
case (_, Pawn) if captures ⇒ orig.file + 'x' + dest.key
|
||||||
|
case (_, Pawn) ⇒ dest.key
|
||||||
|
case (_, role) ⇒ role.pgn + disambiguate + capturing + dest.key
|
||||||
|
case _ ⇒ "?"
|
||||||
|
}) + checking
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue