🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # SergiiVlasiuk's solution

## to Queen Attack in the Scala Track

Published at Sep 01 2019 · 0 comments
Instructions
Test suite
Solution

Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.

In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.

A chessboard can be represented by an 8 by 8 array.

So if you're told the white queen is at (2, 3) and the black queen at (5, 6), then you'd know you've got a set-up like so:

``````_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
``````

You'd also be able to answer whether the queens can attack each other. In this case, that answer would be yes, they can, because both pieces share a diagonal.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

## Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### QueenAttackTest.scala

``````import org.scalatest.{Matchers, FunSuite}

/** @version 2.1.0 */
class QueenAttackTest extends FunSuite with Matchers {

private def create(x: Int, y: Int): Queen = {
Queen.create(x, y) match {
case Some(q) => q
case None => fail("Error creating queen")
}
}

test("queen with a valid position") {
Queen.create(2, 2) should be (Some(Queen(2, 2)))
}

test("queen must have positive row") {
pending
Queen.create(-2, 2) should be (None)
}

test("queen must have row on board") {
pending
Queen.create(8, 4) should be (None)
}

test("queen must have positive column") {
pending
Queen.create(2, -2) should be (None)
}

test("queen must have column on board") {
pending
Queen.create(4, 8) should be (None)
}

test("can not attack") {
pending
QueenAttack.canAttack(create(2, 4), create(6, 6)) should be (false)
}

test("can attack on same row") {
pending
QueenAttack.canAttack(create(2, 4), create(2, 6)) should be (true)
}

test("can attack on same column") {
pending
QueenAttack.canAttack(create(4, 5), create(2, 5)) should be (true)
}

test("can attack on first diagonal") {
pending
QueenAttack.canAttack(create(2, 2), create(0, 4)) should be (true)
}

test("can attack on second diagonal") {
pending
QueenAttack.canAttack(create(2, 2), create(3, 1)) should be (true)
}

test("can attack on third diagonal") {
pending
QueenAttack.canAttack(create(2, 2), create(1, 1)) should be (true)
}

test("can attack on fourth diagonal") {
pending
QueenAttack.canAttack(create(2, 2), create(5, 5)) should be (true)
}
}``````
``````import scala.util.Try

case class Queen(row: Int, col: Int) {
require(0 <= row && row < 8)
require(0 <= col && col < 8)
}

object Queen {
def create(row: Int, col: Int): Option[Queen] = Try[Queen](Queen(row, col)).toOption
}

object QueenAttack {
def canAttack(q1: Queen, q2: Queen): Boolean = q1 match {
case Queen(row, col) if row == q2.row => true
case Queen(row, col) if col == q2.col => true
case Queen(row, col) if math.abs(row - q2.row) == math.abs(col - q2.col) => true
case _ => false
}
}``````