From 2cbc7cfe34e3bbff8ced959c72d9f122f7a91c88 Mon Sep 17 00:00:00 2001 From: Thibault Duplessis Date: Tue, 21 Feb 2012 22:33:06 +0100 Subject: [PATCH] Rewrite Pos --- lila/src/main/scala/model/Pos.scala | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/lila/src/main/scala/model/Pos.scala b/lila/src/main/scala/model/Pos.scala index 63d4e2c6352..8e114d1c2cd 100644 --- a/lila/src/main/scala/model/Pos.scala +++ b/lila/src/main/scala/model/Pos.scala @@ -5,9 +5,9 @@ import scala.math.{ abs, min, max } object Pos { - val values: Set[Int] = (1 to 8) toSet + private val values: Set[Int] = (1 to 8) toSet - val allKeys: Map[String, Pos] = { + private val allKeys: Map[String, Pos] = { for { x <- 1 to 8 y <- 1 to 8 @@ -51,21 +51,25 @@ object Pos { case class Pos(x: Int, y: Int) { - if (!Pos.values(x) || !Pos.values(y)) + import Pos.values + + if (!values(x) || !values(y)) throw new RuntimeException("Invalid position " + (x, y)) - def ^(n: Int): Option[Pos] = if (Pos.values.contains(y + n)) Some(Pos(y + n, x)) else None - def >(n: Int): Option[Pos] = if (Pos.values.contains(x + n)) Some(Pos(y, x + n)) else None + def ^(n: Int): Option[Pos] = if (values(y + n)) Some(Pos(y + n, x)) else None + def >(n: Int): Option[Pos] = if (values(x + n)) Some(Pos(y, x + n)) else None def v(n: Int): Option[Pos] = this ^ (n * -1) def <(n: Int): Option[Pos] = this > (n * -1) def -?(other: Pos) = y == other.y def |?(other: Pos) = x == other.x def /?(other: Pos) = abs(x - other.x) == abs(y - other.y) - def *?(other: Pos) = this.-?(other) || this.|?(other) || this./?(other) - def ^^(n: Int): List[Pos] = expand(n, List(Some(this)), Pos.^).filter(_.isDefined).map(_.get).reverse - def >>(n: Int): List[Pos] = expand(n, List(Some(this)), Pos.>).filter(_.isDefined).map(_.get).reverse - def vv(n: Int): List[Pos] = expand(n, List(Some(this)), Pos.v).filter(_.isDefined).map(_.get).reverse - def <<(n: Int): List[Pos] = expand(n, List(Some(this)), Pos.<).filter(_.isDefined).map(_.get).reverse + def *?(other: Pos) = (this -? other) || (this |? other) || (this /? other) + def ^^(n: Int): List[Pos] = <>(n, Pos.^) + def >>(n: Int): List[Pos] = <>(n, Pos.>) + def vv(n: Int): List[Pos] = <>(n, Pos.v) + def <<(n: Int): List[Pos] = <>(n, Pos.<) + def <>(n: Int, d: Option[Pos] => Option[Pos]) = + expand(n, List(Some(this)), d).flatten reverse def xToString = Pos xToString x def yToString = y.toString @@ -73,6 +77,8 @@ case class Pos(x: Int, y: Int) { override def toString = xToString + yToString private def expand(i: Int, accumulator: List[Option[Pos]], direct: Option[Pos] ⇒ Option[Pos]): List[Option[Pos]] = { - if (i > 0 && accumulator.head.isDefined) expand(i - 1, direct(accumulator.head) :: accumulator, direct) else accumulator + if (i > 0 && accumulator.head.isDefined) + expand(i - 1, direct(accumulator.head) :: accumulator, direct) + else accumulator } }