From 2f535fe21ad766a3db2b53d74e418496f3f0287a Mon Sep 17 00:00:00 2001 From: "exercism-solutions-syncer[bot]" <211797793+exercism-solutions-syncer[bot]@users.noreply.github.com> Date: Sat, 20 Jun 2026 20:42:32 +0000 Subject: [PATCH] [Sync Iteration] cpp/queen-attack/1 --- solutions/cpp/queen-attack/1/queen_attack.cpp | 40 +++++++++++++++++++ solutions/cpp/queen-attack/1/queen_attack.h | 27 +++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 solutions/cpp/queen-attack/1/queen_attack.cpp create mode 100644 solutions/cpp/queen-attack/1/queen_attack.h diff --git a/solutions/cpp/queen-attack/1/queen_attack.cpp b/solutions/cpp/queen-attack/1/queen_attack.cpp new file mode 100644 index 0000000..a6f4151 --- /dev/null +++ b/solutions/cpp/queen-attack/1/queen_attack.cpp @@ -0,0 +1,40 @@ +#include "queen_attack.h" +#include +#include + +namespace queen_attack { + +// Implementación del validador de límites +bool chess_board::is_valid_position(std::pair pos) const { + return pos.first >= 0 && pos.first < 8 && pos.second >= 0 && pos.second < 8; +} + +// Constructor con lista de inicialización y filtros +chess_board::chess_board(std::pair white, std::pair black) + : m_white(white), m_black(black) { + + // 1. Validar que estén dentro del tablero + if (!is_valid_position(m_white) || !is_valid_position(m_black)) { + throw std::domain_error("Posicion fuera de los limites del tablero"); + } + + // 2. Validar que no compartan la misma casilla + if (m_white == m_black) { + throw std::domain_error("Las reinas no pueden ocupar la misma casilla."); + } +} + +std::pair chess_board::white() const { return m_white; } +std::pair chess_board::black() const { return m_black; } + +// El cálculo +bool chess_board::can_attack() const { + // Extraemos para que el código sea ultra legible + int f1 = m_white.first, c1 = m_white.second; + int f2 = m_black.first, c2 = m_black.second; + + // Misma fila, misma columna, o misma diagonal + return (f1 == f2) || (c1 == c2) || (std::abs(f1 - f2) == std::abs(c1 - c2)); +} + +} \ No newline at end of file diff --git a/solutions/cpp/queen-attack/1/queen_attack.h b/solutions/cpp/queen-attack/1/queen_attack.h new file mode 100644 index 0000000..4cbf077 --- /dev/null +++ b/solutions/cpp/queen-attack/1/queen_attack.h @@ -0,0 +1,27 @@ +#pragma once +#include + +namespace queen_attack { + +class chess_board { +private: + // Atributos privados: coordenadas de cada reina + std::pair m_white; + std::pair m_black; + + // Validar que los índices estén entre 0 y 7 + bool is_valid_position(std::pair pos) const; + +public: + // Constructor que recibe las posiciones de ambas reinas + chess_board(std::pair white, std::pair black); + + // Getters + std::pair white() const; + std::pair black() const; + + // ¿Se pueden atacar? + bool can_attack() const; +}; + +} \ No newline at end of file