ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# angelikatyborska's solution

## to Queen Attack in the Erlang Track

Published at Dec 26 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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 tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, `rebar3` is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

``````\$ rebar3 eunit
``````

## Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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

### queen_attack_tests.erl

``````%% Based on canonical data version 2.2.0
%% https://github.com/exercism/problem-specifications/raw/master/exercises/queen-attack/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(queen_attack_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

'1_can_not_attack_test'() ->
?assertNot(queen_attack:can_attack({4, 2}, {6, 6})).

'2_can_attack_on_same_row_test'() ->
?assert(queen_attack:can_attack({4, 2}, {6, 2})).

'3_can_attack_on_same_column_test'() ->
?assert(queen_attack:can_attack({5, 4}, {5, 2})).

'4_can_attack_on_first_diagonal_test'() ->
?assert(queen_attack:can_attack({2, 2}, {4, 0})).

'5_can_attack_on_second_diagonal_test'() ->
?assert(queen_attack:can_attack({2, 2}, {1, 3})).

'6_can_attack_on_third_diagonal_test'() ->
?assert(queen_attack:can_attack({2, 2}, {1, 1})).

'7_can_attack_on_fourth_diagonal_test'() ->
?assert(queen_attack:can_attack({2, 2}, {5, 5})).``````
``````-module(queen_attack).

-export([can_attack/2]).

can_attack({X, _}, {X, _}) -> true;
can_attack({_, Y}, {_, Y}) -> true;
can_attack({WX, WY}, {BX, BY}) -> abs(WX - BX) == abs(WY - BY).``````