Sha256: b60e09204361040f906d98f66686239ebd38cdd442bec1fe348d62ac8b0178e3

Contents?: true

Size: 978 Bytes

Versions: 220

Compression:

Stored size: 978 Bytes

Contents

object Queens {

  def boardString(white: Option[Position], black: Option[Position]): String = {

    def rowString(row: Int): String =
      (0 until 8).flatMap(col => tileString(row, col)).mkString

    def tileString(row: Int, col: Int): String =
      tileChar(Position(row, col)).toString + tileSep(col)

    def tileChar(pos: Position): Char =
      Some(pos) match {
        case `white` => 'W'
        case `black` => 'B'
        case _ => '_'
      }

    def tileSep(col: Int) =
      if (col == 7) '\n'
      else ' '

    (0 until 8).flatMap(row => rowString(row)).mkString
  }

  def canAttack(white: Position, black: Position): Boolean = {
    val canAttackHoriz = white.x == black.x
    val canAttackVert = white.y == black.y
    val deltaRow = Math.abs(white.x - black.x)
    val deltaCol = Math.abs(white.y - black.y)
    val canAttackDiag = deltaRow == deltaCol

    canAttackHoriz || canAttackVert || canAttackDiag
  }
}

case class Position(x: Int, y: Int)

Version data entries

220 entries across 220 versions & 1 rubygems

Version Path
trackler-2.0.6.1 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.6.0 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.18 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.17 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.16 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.15 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.14 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.13 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.12 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.11 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.10 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.9 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.8 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.7 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.6 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.5 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.4 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.3 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.2 tracks/scala/exercises/queen-attack/example.scala
trackler-2.0.5.1 tracks/scala/exercises/queen-attack/example.scala