Remove unsafe positions

This commit is contained in:
Thibault Duplessis 2012-02-29 20:01:21 +01:00
parent 7ede900287
commit 0f0e8d90e7
5 changed files with 24 additions and 18 deletions

View file

@ -1,6 +1,6 @@
package lila.chess package lila.chess
import Pos.makePos import Pos.posAt
case class Actor(piece: Piece, pos: Pos, board: Board) { case class Actor(piece: Piece, pos: Pos, board: Board) {
@ -94,10 +94,10 @@ case class Actor(piece: Piece, pos: Pos, board: Board) {
tripToRook = side.tripToRook(kingPos, board) tripToRook = side.tripToRook(kingPos, board)
rookPos tripToRook.lastOption rookPos tripToRook.lastOption
if board(rookPos) == Some(color.rook) if board(rookPos) == Some(color.rook)
newKingPos makePos(side.castledKingX, kingPos.y) newKingPos posAt(side.castledKingX, kingPos.y)
securedPoss = kingPos <-> newKingPos securedPoss = kingPos <-> newKingPos
if (enemyThreats & securedPoss.toSet).isEmpty if (enemyThreats & securedPoss.toSet).isEmpty
newRookPos makePos(side.castledRookX, rookPos.y) newRookPos posAt(side.castledRookX, rookPos.y)
b1 board take rookPos b1 board take rookPos
b2 b1.move(kingPos, newKingPos) b2 b1.move(kingPos, newKingPos)
b3 b2.place(color.rook, newRookPos) b3 b2.place(color.rook, newRookPos)

View file

@ -1,10 +1,10 @@
package lila.chess package lila.chess
import scala.math.{ abs, min, max } import scala.math.{ min, max }
sealed case class Pos private (x: Int, y: Int) { sealed case class Pos private (x: Int, y: Int) {
import Pos.makePos import Pos.posAt
lazy val up: Option[Pos] = this ^ 1 lazy val up: Option[Pos] = this ^ 1
lazy val down: Option[Pos] = this v 1 lazy val down: Option[Pos] = this v 1
@ -15,10 +15,10 @@ sealed case class Pos private (x: Int, y: Int) {
lazy val downLeft: Option[Pos] = down flatMap (_ left) lazy val downLeft: Option[Pos] = down flatMap (_ left)
lazy val downRight: Option[Pos] = down flatMap (_ right) lazy val downRight: Option[Pos] = down flatMap (_ right)
def ^(n: Int): Option[Pos] = makePos(x, y + n) def ^(n: Int): Option[Pos] = posAt(x, y + n)
def v(n: Int): Option[Pos] = makePos(x, y - n) def v(n: Int): Option[Pos] = posAt(x, y - n)
def >(n: Int): Option[Pos] = makePos(x + n, y) def >(n: Int): Option[Pos] = posAt(x + n, y)
def <(n: Int): Option[Pos] = makePos(x - n, y) def <(n: Int): Option[Pos] = posAt(x - n, y)
def >|(stop: Pos Boolean): List[Pos] = |<>|(stop, _.right) def >|(stop: Pos Boolean): List[Pos] = |<>|(stop, _.right)
def |<(stop: Pos Boolean): List[Pos] = |<>|(stop, _.left) def |<(stop: Pos Boolean): List[Pos] = |<>|(stop, _.left)
def |<>|(stop: Pos Boolean, dir: Direction): List[Pos] = dir(this) map { p def |<>|(stop: Pos Boolean, dir: Direction): List[Pos] = dir(this) map { p
@ -30,7 +30,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): 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 { posAt(_, y) } flatten
lazy val file = Pos xToString x lazy val file = Pos xToString x
lazy val rank = y.toString lazy val rank = y.toString
@ -41,9 +41,9 @@ sealed case class Pos private (x: Int, y: Int) {
object Pos { object Pos {
def makePos(x: Int, y: Int): Option[Pos] = allCoords get (x, y) def posAt(x: Int, y: Int): Option[Pos] = allCoords get (x, y)
def unsafe(x: Int, y: Int): Pos = allCoords((x, y)) def posAt(key: String): Option[Pos] = allKeys get key
def xToString(x: Int) = (96 + x).toChar.toString def xToString(x: Int) = (96 + x).toChar.toString

View file

@ -3,7 +3,7 @@ package format
object PgnDump { object PgnDump {
def move(situation: Situation, data: Move, nextSituation: Situation): String = { def move(situation: Situation, data: Move, next: Situation): String = {
import data._ import data._
((promotion, piece.role) match { ((promotion, piece.role) match {
case _ if castle if (orig ?> dest) "O-O-O" else "O-O" case _ if castle if (orig ?> dest) "O-O-O" else "O-O"
@ -18,6 +18,6 @@ object PgnDump {
else if (candidates exists (_.pos ?| orig)) orig.file + orig.rank else orig.file else if (candidates exists (_.pos ?| orig)) orig.file + orig.rank else orig.file
} + (if (captures) "x" else "") + dest.key } + (if (captures) "x" else "") + dest.key
case _ "?" case _ "?"
}) + (if (nextSituation.check) if (nextSituation.checkMate) "#" else "+" else "") }) + (if (next.check) if (next.checkMate) "#" else "+" else "")
} }
} }

View file

@ -1,6 +1,8 @@
package lila.chess package lila.chess
package format package format
import Pos.posAt
/** /**
* r bqkb r * r bqkb r
* p ppp pp * p ppp pp
@ -23,13 +25,17 @@ object Visual extends Format[Board] {
case n (List.fill(8 - n)("")) ::: lines case n (List.fill(8 - n)("")) ::: lines
} }
Board( Board(
for { (for {
line (filtered.zipWithIndex) line (filtered.zipWithIndex)
(l, y) = line (l, y) = line
char (l zipWithIndex) char (l zipWithIndex)
(c, x) = char (c, x) = char
if pieces.keySet(c toLower) if pieces.keySet(c toLower)
} yield Pos.unsafe(x + 1, 8 - y) -> (Color(c isUpper) - pieces(c toLower)) } yield {
posAt(x + 1, 8 - y) map { pos
pos -> (Color(c isUpper) - pieces(c toLower))
}
}) flatten
) withHistory History.noCastle ) withHistory History.noCastle
} }
@ -41,7 +47,7 @@ object Visual extends Format[Board] {
} }
for (y 8 to 1 by -1) yield { for (y 8 to 1 by -1) yield {
for (x 1 to 8) yield { for (x 1 to 8) yield {
markedPoss get Pos.unsafe(x, y) getOrElse board(x, y).fold(_ forsyth, ' ') posAt(x, y) flatMap markedPoss.get getOrElse board(x, y).fold(_ forsyth, ' ')
} }
} mkString } mkString
} map { """\s*$""".r.replaceFirstIn(_, "") } mkString "\n" } map { """\s*$""".r.replaceFirstIn(_, "") } mkString "\n"

View file

@ -20,7 +20,7 @@ trait Dependencies {
val slf4jNop = "org.slf4j" % "slf4j-nop" % "1.6.4" val slf4jNop = "org.slf4j" % "slf4j-nop" % "1.6.4"
val instrumenter = "com.google.code.java-allocation-instrumenter" % "java-allocation-instrumenter" % "2.0" val instrumenter = "com.google.code.java-allocation-instrumenter" % "java-allocation-instrumenter" % "2.0"
val gson = "com.google.code.gson" % "gson" % "1.7.1" val gson = "com.google.code.gson" % "gson" % "1.7.1"
val scalalib = "com.github.ornicar" %% "scalalib" % "1.12" val scalalib = "com.github.ornicar" %% "scalalib" % "1.15"
} }
object ApplicationBuild extends Build with Resolvers with Dependencies { object ApplicationBuild extends Build with Resolvers with Dependencies {