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.2.0.6 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.5 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.4 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.3 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.2 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.1 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.0.0 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.55 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.54 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.53 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.52 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.51 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.50 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.49 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.48 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.47 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.46 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.45 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.44 tracks/scala/exercises/queen-attack/example.scala
trackler-2.1.0.43 tracks/scala/exercises/queen-attack/example.scala