Avatar of arekbee

arekbee's solution

to Queen Attack in the Scala Track

Published at Jul 03 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)
  }
}
case class Queen(x:Int, y:Int)
object Queen {
    def create(x:Int, y:Int) = (x,y) match {
        case (x,y) if x>=0 && y>=0 && x<8 && y<8=> Some(Queen(x,y))
        case _ => None
    } 
    
    }



object QueenAttack {
    def canAttack(q1: Queen, q2:Queen) = q1 match {
        case Queen(x,y) if x == q2.x => true
        case Queen(x,y) if y == q2.y => true
        case Queen(x,y) if x - q2.x == y-q2.y => true
        case Queen(x,y) if x - q2.x == q2.y -y => true
        case _ => false
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?