# artemkorsakov's solution

## to Queen Attack in the C# Track

Published at Apr 21 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 the tests

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

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the `Skip` property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using `exercism submit QueenAttack.cs`

## Further information

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

## Source

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

### QueenAttackTest.cs

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

using System;
using Xunit;

public class QueenAttackTest
{
[Fact]
public void Queen_with_a_valid_position()
{
var actual = QueenAttack.Create(2, 2);
}

[Fact(Skip = "Remove to run test")]
public void Queen_must_have_positive_row()
{
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(-2, 2));
}

[Fact(Skip = "Remove to run test")]
public void Queen_must_have_row_on_board()
{
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(8, 4));
}

[Fact(Skip = "Remove to run test")]
public void Queen_must_have_positive_column()
{
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(2, -2));
}

[Fact(Skip = "Remove to run test")]
public void Queen_must_have_column_on_board()
{
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(4, 8));
}

[Fact(Skip = "Remove to run test")]
public void Can_not_attack()
{
var whiteQueen = QueenAttack.Create(2,4);
var blackQueen = QueenAttack.Create(6,6);
Assert.False(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_same_row()
{
var whiteQueen = QueenAttack.Create(2,4);
var blackQueen = QueenAttack.Create(2,6);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_same_column()
{
var whiteQueen = QueenAttack.Create(4,5);
var blackQueen = QueenAttack.Create(2,5);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_first_diagonal()
{
var whiteQueen = QueenAttack.Create(2,2);
var blackQueen = QueenAttack.Create(0,4);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_second_diagonal()
{
var whiteQueen = QueenAttack.Create(2,2);
var blackQueen = QueenAttack.Create(3,1);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_third_diagonal()
{
var whiteQueen = QueenAttack.Create(2,2);
var blackQueen = QueenAttack.Create(1,1);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}

[Fact(Skip = "Remove to run test")]
public void Can_attack_on_fourth_diagonal()
{
var whiteQueen = QueenAttack.Create(2,2);
var blackQueen = QueenAttack.Create(5,5);
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
}
}``````
``````﻿using System;

public class Queen
{
public Queen(int row, int column)
{
if (row < 0)
{
throw new ArgumentOutOfRangeException("Queen position must have positive row.");
}
if (row > 7)
{
throw new ArgumentOutOfRangeException("Queen position must have row <= 7.");
}
if (column < 0)
{
throw new ArgumentOutOfRangeException("Queen position must have positive column.");
}
if (column > 7)
{
throw new ArgumentOutOfRangeException("Queen position must have column <= 7.");
}
Row = row;
Column = column;
}

public int Row { get; }
public int Column { get; }

public override bool Equals(object obj) => obj is Queen queen &&
Row == queen.Row &&
Column == queen.Column;

public override int GetHashCode() => HashCode.Combine(Row, Column);
}

public static class QueenAttack
{
public static Queen Create(int row, int column) => new Queen(row, column);

public static bool CanAttack(Queen white, Queen black) => IsSameRow(white, black) ||
IsSameColumn(white, black) ||
IsSameDiagonalTopLeftBottomRight(white, black) ||
IsSameDiagonalBottomLeftTopRight(white, black);

private static bool IsSameRow(Queen white, Queen black) => white.Row == black.Row;
private static bool IsSameColumn(Queen white, Queen black) => white.Column == black.Column;
private static bool IsSameDiagonalTopLeftBottomRight(Queen white, Queen black) =>
(white.Row - white.Column) == (black.Row - black.Column);
private static bool IsSameDiagonalBottomLeftTopRight(Queen white, Queen black) =>
(white.Row + white.Column) == (black.Row + black.Column);
}``````