Implement forsyth export

pull/1/merge
Thibault Duplessis 2012-03-05 02:34:37 +01:00
parent 0601a1184a
commit 5a3a443c18
2 changed files with 47 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package lila.chess
import format.PgnDump
import scala.math.max
case class Game(
board: Board = Board(),
@ -33,9 +34,23 @@ case class Game(
orig: Pos,
dest: Pos,
promotion: PromotableRole = Queen): Valid[Game] =
apply(orig, dest, promotion) map (_._1)
apply(orig, dest, promotion) map (_._1)
lazy val situation = Situation(board, player)
def pgnMovesList = pgnMoves.split(' ').toList
/**
* Halfmove clock: This is the number of halfmoves
* since the last pawn advance or capture.
* This is used to determine if a draw
* can be claimed under the fifty-move rule.
*/
def halfMoveClock: Int = board.history.positionHashes.size
/**
* Fullmove number: The number of the full move.
* It starts at 1, and is incremented after Black's move.
*/
def fullMoveNumber: Int = 1 + turns / 2
}

View File

@ -13,5 +13,35 @@ object Forsyth extends Format[Game] {
Game()
}
def >>(game: Game): String = ""
def >>(game: Game): String = List(
exportBoard(game.board),
game.player.letter,
game.board.history.castleNotation,
((for {
lastMove game.board.history.lastMove
(orig, dest) = lastMove
piece game board dest
if piece is Pawn
pos if (orig.y == 2 && dest.y == 4) dest.down
else if (orig.y == 7 && dest.y == 5) dest.up
else None
} yield pos.toString) getOrElse "-"),
game.halfMoveClock,
game.fullMoveNumber
) mkString " "
private def exportBoard(board: Board) = {
{
for (y 8 to 1 by -1) yield {
(1 to 8).map(board(_, y)).foldLeft(("", 0)) {
case ((out, empty), None) (out, empty + 1)
case ((out, 0), Some(piece)) (out + piece.forsyth.toString, 0)
case ((out, empty), Some(piece)) (out + empty.toString + piece.forsyth, 0)
} match {
case (out, 0) out
case (out, empty) out + empty
}
} mkString
} mkString "/"
} mkString
}