Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions solutions/cpp/queen-attack/1/queen_attack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "queen_attack.h"
#include <stdexcept>
#include <cmath>

namespace queen_attack {

// Implementación del validador de límites
bool chess_board::is_valid_position(std::pair<int, int> 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<int, int> white, std::pair<int, int> 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<int, int> chess_board::white() const { return m_white; }
std::pair<int, int> 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));
}

}
27 changes: 27 additions & 0 deletions solutions/cpp/queen-attack/1/queen_attack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once
#include <utility>

namespace queen_attack {

class chess_board {
private:
// Atributos privados: coordenadas de cada reina
std::pair<int, int> m_white;
std::pair<int, int> m_black;

// Validar que los índices estén entre 0 y 7
bool is_valid_position(std::pair<int, int> pos) const;

public:
// Constructor que recibe las posiciones de ambas reinas
chess_board(std::pair<int, int> white, std::pair<int, int> black);

// Getters
std::pair<int, int> white() const;
std::pair<int, int> black() const;

// ¿Se pueden atacar?
bool can_attack() const;
};

}