From 2aca12c94d5e88300c3ef4feb7752b7eaf8e1da8 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 28 Feb 2012 22:54:51 +0100 Subject: [PATCH] Implement pgn move dump --- chess/src/main/scala/Move.scala | 13 +++++++++- chess/src/main/scala/Pos.scala | 1 + chess/src/main/scala/Role.scala | 1 + chess/src/main/scala/format/PgnDump.scala | 29 +++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 chess/src/main/scala/format/PgnDump.scala diff --git a/chess/src/main/scala/Move.scala b/chess/src/main/scala/Move.scala index 52a23557fd..f12babef1e 100644 --- a/chess/src/main/scala/Move.scala +++ b/chess/src/main/scala/Move.scala @@ -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 } diff --git a/chess/src/main/scala/Pos.scala b/chess/src/main/scala/Pos.scala index 006eb6a234..161d0800cd 100644 --- a/chess/src/main/scala/Pos.scala +++ b/chess/src/main/scala/Pos.scala @@ -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 diff --git a/chess/src/main/scala/Role.scala b/chess/src/main/scala/Role.scala index 33c37e9c81..b6a57be6d5 100644 --- a/chess/src/main/scala/Role.scala +++ b/chess/src/main/scala/Role.scala @@ -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 diff --git a/chess/src/main/scala/format/PgnDump.scala b/chess/src/main/scala/format/PgnDump.scala new file mode 100644 index 0000000000..0410ed1580 --- /dev/null +++ b/chess/src/main/scala/format/PgnDump.scala @@ -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 + } +}