From ce63789cb2ca73715727093011dfa2fbf3047d73 Mon Sep 17 00:00:00 2001 From: Dmitry Kokorin Date: Thu, 12 May 2016 17:48:09 +0300 Subject: [PATCH] queen_attack: iteration 1 --- cpp/queen-attack/queen_attack.cpp | 82 +++++++++++++++++++++++++++++++ cpp/queen-attack/queen_attack.h | 28 +++++++++++ 2 files changed, 110 insertions(+) create mode 100644 cpp/queen-attack/queen_attack.cpp create mode 100644 cpp/queen-attack/queen_attack.h diff --git a/cpp/queen-attack/queen_attack.cpp b/cpp/queen-attack/queen_attack.cpp new file mode 100644 index 0000000..b8bd55e --- /dev/null +++ b/cpp/queen-attack/queen_attack.cpp @@ -0,0 +1,82 @@ +#include "queen_attack.h" + +#include + +namespace queen_attack { + + +namespace { + +const int board_width = 8; +const int board_height = 8; + +inline int row(const position_t &pos) +{ + return pos.first; +} + +inline int col(const position_t &pos) +{ + return pos.second; +} + +} + + +chess_board::chess_board(position_t white, position_t black) + : white_{std::move(white)} + , black_{std::move(black)} +{ + if ( row(white_) < 0 || row(white_) >= board_height + || col(white_) < 0 || col(white_) >= board_width + || row(black_) < 0 || row(black_) >= board_height + || col(black_) < 0 || col(black_) >= board_width) { + + throw std::domain_error("invalid queen position"); + } + + if (white_ == black_) + throw std::domain_error("queen positions must be distinct"); +} + +const position_t &chess_board::white() const +{ + return white_; +} + +const position_t &chess_board::black() const +{ + return black_; +} + +bool chess_board::can_attack() const +{ + return col(white_) == col(black_) + || row(white_) == row(black_) + || std::abs(col(white_) - col(black_)) == std::abs(row(white_) - row(black_)); +} + +chess_board::operator std::string() const +{ + static const int stride = 2*board_width; + static const int size = stride*board_height; + + std::string result(size, ' '); + + for (int i = 0; i < size; i += 2) { + + result[i] = '_'; + } + + for (int i = stride - 1; i < size; i += stride) { + + result[i] = '\n'; + } + + result[row(white_)*stride + col(white_)*2] = 'W'; + result[row(black_)*stride + col(black_)*2] = 'B'; + + return result; +} + +} diff --git a/cpp/queen-attack/queen_attack.h b/cpp/queen-attack/queen_attack.h new file mode 100644 index 0000000..fd9bc9d --- /dev/null +++ b/cpp/queen-attack/queen_attack.h @@ -0,0 +1,28 @@ +#pragma once + +#define EXERCISM_RUN_ALL_TESTS + +#include +#include + +namespace queen_attack { + +typedef std::pair position_t; + +class chess_board +{ + position_t white_, black_; + +public: + + chess_board(position_t white = std::make_pair(0, 3), + position_t black = std::make_pair(7, 3)); + + const position_t &black() const; + const position_t &white() const; + + bool can_attack() const; + operator std::string() const; +}; + +}