 # Tehlyria's solution

## to Queen Attack in the C++ Track

Published at Oct 17 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.

## Getting Started

Make sure you have read the Installing and Running the Tests pages for C++ on exercism.io. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development. Create just enough structure by declaring namespaces, functions, classes, etc., to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, uncomment the next test by moving the following line past the next test.

``````#if defined(EXERCISM_RUN_ALL_TESTS)
``````

This may result in compile errors as new constructs may be invoked that you haven't yet declared or defined. Again, fix the compile errors minimally to get a failing test, then change the code minimally to pass the test, refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C++14 facilities in preference to writing your own low-level algorithms or facilities by hand. CppReference is a wiki reference to the C++ language and standard library. If you are new to C++, but have programmed in C, beware of C traps and pitfalls.

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

``````#include "queen_attack.h"
#include "test/catch.hpp"

TEST_CASE("queens_in_default_positions")
{
const queen_attack::chess_board board;

REQUIRE(std::make_pair(0, 3).first == board.white().first);
REQUIRE(std::make_pair(0, 3).second == board.white().second);
REQUIRE(std::make_pair(7, 3).first == board.black().first);
REQUIRE(std::make_pair(7, 3).second == board.black().second);
}

#if defined(EXERCISM_RUN_ALL_TESTS)
TEST_CASE("initialized_with_specific_positions")
{
const auto white = std::make_pair(3, 7);
const auto black = std::make_pair(6, 1);
const queen_attack::chess_board board{white, black};

REQUIRE(white.first == board.white().first);
REQUIRE(white.second == board.white().second);
REQUIRE(black.first == board.black().first);
REQUIRE(black.second == board.black().second);
}

TEST_CASE("queen_positions_must_be_distinct")
{
const auto pos = std::make_pair(3, 7);
REQUIRE_THROWS_AS((queen_attack::chess_board{pos, pos}), std::domain_error);
}

TEST_CASE("string_representation")
{
const queen_attack::chess_board board{std::make_pair(2, 4), std::make_pair(6, 6)};

const std::string expected{
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ W _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ _ _\n"
"_ _ _ _ _ _ B _\n"
"_ _ _ _ _ _ _ _\n"};
REQUIRE(expected == static_cast<std::string>(board));
}

TEST_CASE("queens_cannot_attack")
{
const queen_attack::chess_board board{std::make_pair(2, 3), std::make_pair(4, 7)};

REQUIRE(!board.can_attack());
}

TEST_CASE("queens_can_attack_when_they_are_on_the_same_row")
{
const queen_attack::chess_board board{std::make_pair(2, 4), std::make_pair(2, 7)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_can_attack_when_they_are_on_the_same_column")
{
const queen_attack::chess_board board{std::make_pair(5, 4), std::make_pair(2, 4)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_can_attack_diagonally")
{
const queen_attack::chess_board board{std::make_pair(1, 1), std::make_pair(6, 6)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_can_attack_another_diagonally")
{
const queen_attack::chess_board board{std::make_pair(0, 6), std::make_pair(1, 7)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_can_attack_yet_another_diagonally")
{
const queen_attack::chess_board board{std::make_pair(4, 1), std::make_pair(6, 3)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_can_attack_on_the_nw_so_diagonal")
{
const queen_attack::chess_board board{std::make_pair(1, 6), std::make_pair(6, 1)};

REQUIRE(board.can_attack());
}

TEST_CASE("queens_cannot_attack_if_not_on_same_row_column_or_diagonal")
{
const queen_attack::chess_board board{std::make_pair(1, 1), std::make_pair(3, 7)};

REQUIRE(!board.can_attack());
}
#endif``````

### queen_attack.cpp

``````#include "queen_attack.h"

namespace queen_attack {
chess_board::chess_board(chess_board::Queen whiteQueen,
chess_board::Queen blackQueen)
: whiteQueen{std::move(whiteQueen)}, blackQueen{std::move(blackQueen)} {
if (whiteQueen == blackQueen) {
throw std::domain_error("Positions must be distinct!");
}
}

bool chess_board::can_attack() const {
const auto deltaX = std::abs(whiteQueen.first - blackQueen.first);
const auto deltaY = std::abs(whiteQueen.second - blackQueen.second);

return deltaX == 0 || deltaY == 0 || deltaX == deltaY;
}

chess_board::operator std::string() const {
std::string result{};

for (auto x = 0; x < 8; ++x) {
for (auto y = 0; y < 8; ++y) {
const auto currentPos = std::make_pair(x, y);
if (whiteQueen == currentPos) {
result += "W";
} else if (blackQueen == currentPos) {
result += "B";
} else {
result += "_";
}

if (y < 7) {
result += " ";
}
}

result += "\n";
}

return result;
}
} // namespace queen_attack``````

### queen_attack.h

``````#if !defined(QUEEN_ATTACK_H)
#define QUEEN_ATTACK_H

#include <cmath>
#include <stdexcept>
#include <string>
#include <utility>

namespace queen_attack {
class chess_board {
using Queen = std::pair<int, int>;

Queen whiteQueen{0, 3};
Queen blackQueen{7, 3};

public:
chess_board() = default;
chess_board(Queen whiteQueen, Queen blackQueen);

const Queen &white() const { return whiteQueen; }
const Queen &black() const { return blackQueen; }
bool can_attack() const;

explicit operator std::string() const;
};
} // namespace queen_attack

#endif // QUEEN_ATTACK_H``````