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

## to Queen Attack in the F# Track

Published at Nov 05 2020 · 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.

## Running the tests

To run the tests, run the command `dotnet test` from within the exercise directory.

## Autoformatting the code

F# source code can be formatted with the Fantomas tool.

After installing it with `dotnet tool restore`, run `dotnet fantomas .` to format code within the current directory.

## Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

## Source

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

### QueenAttackTests.fs

``````// This file was auto-generated based on version 2.3.0 of the canonical data.

module QueenAttackTests

open FsUnit.Xunit
open Xunit

open QueenAttack

[<Fact>]
let ``Queen with a valid position`` () =
create (2, 2) |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Queen must have positive row`` () =
create (-2, 2) |> should equal false

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Queen must have row on board`` () =
create (8, 4) |> should equal false

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Queen must have positive column`` () =
create (2, -2) |> should equal false

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Queen must have column on board`` () =
create (4, 8) |> should equal false

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can not attack`` () =
let whiteQueen = (2, 4)
let blackQueen = (6, 6)
canAttack blackQueen whiteQueen |> should equal false

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on same row`` () =
let whiteQueen = (2, 4)
let blackQueen = (2, 6)
canAttack blackQueen whiteQueen |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on same column`` () =
let whiteQueen = (4, 5)
let blackQueen = (2, 5)
canAttack blackQueen whiteQueen |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on first diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (0, 4)
canAttack blackQueen whiteQueen |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on second diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (3, 1)
canAttack blackQueen whiteQueen |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on third diagonal`` () =
let whiteQueen = (2, 2)
let blackQueen = (1, 1)
canAttack blackQueen whiteQueen |> should equal true

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Can attack on fourth diagonal`` () =
let whiteQueen = (1, 7)
let blackQueen = (0, 6)
canAttack blackQueen whiteQueen |> should equal true``````
``````module QueenAttack

let create (x, y) = x >= 0 && x < 8 && y >= 0 && y < 8

let canAttack (x1: int, y1: int) (x2: int, y2: int) =
if x1 = x2 && y1 = y2
then invalidOp "Queens cannot occupy the same space"
else x1 = x2 || y1 = y2 || (x2 - x1 |> abs) = (y2 - y1 |> abs)``````

## 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?