Basic functional vector-based piece movement implementation
parent
0d885713bd
commit
5e9e63a70d
|
@ -1,14 +1,27 @@
|
|||
package lila
|
||||
package model
|
||||
|
||||
import scala.annotation.tailrec
|
||||
|
||||
case class Piece(color: Color, role: Role) {
|
||||
|
||||
type Vector = Pos ⇒ Option[Pos]
|
||||
|
||||
def basicMoves(pos: Pos, board: Board): Set[Pos] = {
|
||||
|
||||
role match {
|
||||
case Rook ⇒ {
|
||||
val vectors: List[Pos => Option[Pos]] = List(_.up, _.down, _.left, _.right)
|
||||
Set.empty
|
||||
val occupation = board occupation !color
|
||||
val vectors: List[Vector] = List(_.up, _.down, _.left, _.right)
|
||||
|
||||
//@tailrec
|
||||
def fwd(p: Pos, v: Vector): List[Pos] = v(p) map { next ⇒
|
||||
next :: fwd(next, v)
|
||||
} getOrElse Nil
|
||||
|
||||
vectors flatMap { vector =>
|
||||
fwd(pos, vector)
|
||||
} toSet
|
||||
}
|
||||
case _ ⇒ Set.empty
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ package model
|
|||
|
||||
import scala.math.{ abs, min, max }
|
||||
|
||||
case class Pos private(x: Int, y: Int) {
|
||||
case class Pos private(x: Int, y: Int) extends Ordered[Pos] {
|
||||
|
||||
import Pos.pos
|
||||
|
||||
|
@ -31,6 +31,8 @@ case class Pos private(x: Int, y: Int) {
|
|||
|
||||
override def toString = xToString + yToString
|
||||
|
||||
def compare(other: Pos) = toString compare other.toString
|
||||
|
||||
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)
|
||||
|
|
|
@ -10,7 +10,7 @@ trait LilaSpec
|
|||
extends Specification
|
||||
with OrnicarValidationMatchers {
|
||||
|
||||
def bePoss(poss: Pos*): Matcher[Valid[Set[Pos]]] = beSuccess.like {
|
||||
case p ⇒ p.toSet must_== poss
|
||||
def bePoss(poss: Pos*): Matcher[Valid[Iterable[Pos]]] = beSuccess.like {
|
||||
case p ⇒ p.toList.sorted must_== poss.toList.sorted
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue