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.1.39 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.38 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.37 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.36 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.35 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.34 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.33 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.32 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.31 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.30 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.29 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.28 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.27 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.26 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.25 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.24 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.23 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.22 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.21 tracks/scala/exercises/queen-attack/example.scala
trackler-2.2.1.20 tracks/scala/exercises/queen-attack/example.scala