Implement pgn move dump

pull/1/merge
Thibault Duplessis 2012-02-28 22:54:51 +01:00
parent ae25d68023
commit 2aca12c94d
4 changed files with 43 additions and 1 deletions

View File

@ -13,5 +13,16 @@ case class Move(
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
}

View File

@ -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): Iterable[Pos] =
min(x, other.x) to max(x, other.x) map { makePos(_, y) } flatten

View File

@ -4,6 +4,7 @@ import Pos._
sealed trait Role {
val forsyth: Char
lazy val pgn = forsyth.toUpper
def dirs: List[Direction]
}
sealed trait PromotableRole extends Role

View 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
}
}