Skip to content

Commit

Permalink
Create NumNivelOpcional
Browse files Browse the repository at this point in the history
  • Loading branch information
autosquash committed Jan 16, 2024
1 parent b1a2971 commit bf7c5ec
Show file tree
Hide file tree
Showing 19 changed files with 93 additions and 52 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ add_executable(
src/tests/presentador/test_enlace_vista.cpp
src/tests/presentador/test_presentador.cpp
src/tests/presentador/test_activacion_botones.cpp
src/tests/test_num_nivel.cpp
src/tests/test_tiempo.cpp
src/tests/test_timer.cpp
src/tests/test_usecases.cpp


# archivos del proyecto
src/controlador_clicks.cpp
src/datos_niveles.cpp
Expand Down
8 changes: 4 additions & 4 deletions src/demos/demo_nivel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#include <cassert>
#include <iostream>

int demo_nivel(NumNivel numero_nivel) {
int demo_nivel(NumNivelOpcional numero_nivel) {

std::cout << "DEMO NIVEL " << numero_nivel.valor << std::endl;
int indice_nivel = numero_nivel.valor - 1;
std::cout << "DEMO NIVEL " << numero_nivel.to_string() << std::endl;
int indice_nivel = numero_nivel.get_valor() - 1;
const auto globales = std::make_shared<Globales>();
const auto grid = std::make_shared<Grid>();
bool resultado_setup = setup_juego(globales);
Expand All @@ -23,7 +23,7 @@ int demo_nivel(NumNivel numero_nivel) {
while (true) {
Nivel nivel(
globales, std::make_shared<DatosNivel>(datos_niveles[indice_nivel]),
std::optional<NumNivel>(numero_nivel), grid, true
numero_nivel, grid, true
);
AccionGeneral res = nivel.ejecutar();
if (res == AccionGeneral::Reiniciar) {
Expand Down
2 changes: 1 addition & 1 deletion src/demos/demo_nivel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
#include "../shared.h"
#include <memory>

int demo_nivel(NumNivel numero_nivel);
int demo_nivel(NumNivelOpcional numero_nivel);
4 changes: 2 additions & 2 deletions src/juego.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ int juego() {
bool reiniciar = false;
for (int i = 0; i < NUM_DATOS_NIVELES; i++) {
bool es_el_ultimo = (i == NUM_DATOS_NIVELES - 1);
auto num_nivel = NumNivel(i + 1);
auto num_nivel = NumNivelOpcional(i + 1);
auto datos_nivel = std::make_shared<DatosNivel>(datos_niveles[i]);
assert(datos_nivel);
std::cout << "a punto de crear Nivel " << num_nivel.valor
std::cout << "a punto de crear Nivel " << num_nivel.to_string()
<< std::endl;
Nivel nivel(globales, datos_nivel, num_nivel, grid, es_el_ultimo);

Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ int _main(int argc, char *argv[]) {
write_line("El numero de nivel debe ser mayor que 0");
return USER_ERROR;
}
return demo_nivel(NumNivel(num_nivel_introducido));
return demo_nivel(NumNivelOpcional(num_nivel_introducido));
}
break;
case AppNombrada::Visual:
Expand Down
11 changes: 3 additions & 8 deletions src/nivel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,16 @@ std::optional<AccionGeneral> Nivel::_procesa_cambio_de_fase(
Nivel::Nivel(
std::shared_ptr<Globales> globales, //
std::shared_ptr<DatosNivel> datos_nivel, //
std::optional<NumNivel> num_nivel, //
const NumNivelOpcional num_nivel, //
std::shared_ptr<Grid> grid, //
bool es_el_ultimo //
)
: globales(globales), datos_nivel(datos_nivel), num_nivel(num_nivel),
grid(grid), es_el_ultimo(es_el_ultimo) {

controlador_clicks = std::shared_ptr<ControladorClicks>();
std::string repr_num_nivel;
if (!num_nivel) {
repr_num_nivel = "-";
} else {
repr_num_nivel = std::to_string(num_nivel.value().valor);
}
LOG(info) << "Creando nivel " << repr_num_nivel << std::endl;

LOG(info) << "Creando nivel " << num_nivel.to_string() << std::endl;
if (datos_nivel) {
modelo_amplio.emplace(datos_nivel->datos_modelo_interno);
} else {
Expand Down
12 changes: 6 additions & 6 deletions src/nivel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ struct Nivel {
public:
const std::shared_ptr<Globales> globales;
const std::shared_ptr<DatosNivel> datos_nivel;
const std::optional<NumNivel> num_nivel;
const NumNivelOpcional &num_nivel;
const std::shared_ptr<Grid> grid;

const bool es_el_ultimo;
std::shared_ptr<ControladorClicks> controlador_clicks;
std::shared_ptr<EnlaceVista> enlace_vista;

Nivel(
std::shared_ptr<Globales> globales = nullptr, //
std::shared_ptr<DatosNivel> datos_nivel = nullptr, //
std::optional<NumNivel> num_nivel = std::nullopt, //
std::shared_ptr<Grid> grid = nullptr, //
bool es_el_ultimo = false //
std::shared_ptr<Globales> globales = nullptr, //
std::shared_ptr<DatosNivel> datos_nivel = nullptr, //
const NumNivelOpcional num_nivel = NumNivelOpcional(), //
std::shared_ptr<Grid> grid = nullptr, //
bool es_el_ultimo = false //
);
AccionGeneral ejecutar();
void establecer_fase(FaseNivel);
Expand Down
10 changes: 1 addition & 9 deletions src/shared.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "shared_num_nivel.h"
#include <cassert>
#include <memory>

Expand All @@ -19,15 +20,6 @@ class OptionalFont {

constexpr bool MODO_DESARROLLO = true;

/* NewType para el numero de nivel del juego */
struct NumNivel {
const int valor;
explicit NumNivel(int v) : valor(v) { //
assert(valor > 0);
}
NumNivel &operator=(const NumNivel &) = delete;
};

class ObjetoConFont {
protected:
const OptionalFont &font;
Expand Down
25 changes: 25 additions & 0 deletions src/shared_num_nivel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <cassert>
#include <optional>
#include <string>

/* NewType para el numero de nivel del juego */
class NumNivelOpcional {
private:
const std::optional<int> valor;

public:
NumNivelOpcional() {}
explicit NumNivelOpcional(int v) : valor(v) { //
assert(valor > 0);
}
std::string to_string() const { //
if (!valor)
return "-";
return std::to_string(valor.value());
}
int get_valor() const { //
return valor.value();
}
};
37 changes: 37 additions & 0 deletions src/tests/test_num_nivel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../shared_num_nivel.h"
#include <gtest/gtest.h>

TEST(NumNivelOpcional, SePuedeCrearSinArgumentos) { //
const NumNivelOpcional num_nivel;
}

TEST(NumNivelOpcional, SePuedeCrearConArgumentos) { //
const NumNivelOpcional num_nivel(1);
}

TEST(NumNivelOpcional, ToStringSinValorNumerico) { //
const NumNivelOpcional num_nivel;
ASSERT_EQ(num_nivel.to_string(), "-");
}

TEST(NumNivelOpcional, ToStringConValorNumerico) { //
const NumNivelOpcional num_nivel(1);
ASSERT_EQ(num_nivel.to_string(), "1");
}

TEST(NumNivelOpcional, ObtenerValorSinValorNumericoGeneraExcepcion) { //
const NumNivelOpcional num_nivel;
ASSERT_THROW(num_nivel.get_valor(), std::bad_optional_access);
}

TEST(NumNivelOpcional, ObtenerValorConValorNumerico) { //
const NumNivelOpcional num_nivel(1);
ASSERT_EQ(num_nivel.get_valor(), 1);
}

TEST(NumNivelOpcional, ManualTest) { //
NumNivelOpcional num_nivel(1);
NumNivelOpcional num_nivel_2(2);
// SHOULD NOT COMPILE
// num_nivel = num_nivel_2;
}
1 change: 0 additions & 1 deletion src/tests/test_usecases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

TEST(Usecases, EnModoInstruccionesNoSeMuestranLosPaneles) { //
Nivel nivel;
std::cout << "Nivel creado" << std::endl;
nivel.establecer_fase(FaseNivel::MostrandoInstrucciones);
auto paneles = nivel.get_vista()->get_paneles();
ASSERT_EQ(paneles->get_visibilidad(), false);
Expand Down
13 changes: 3 additions & 10 deletions src/textos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@
"%!Enhorabuena! Todos los clientes est%an satisfechos."

std::string construir_texto_instrucciones(
const std::string &plantilla, //
const std::optional<NumNivel> num_nivel //
const std::string &plantilla, //
const NumNivelOpcional &num_nivel //
) {
CadenaJuego cadena = interpolar_unicode(plantilla);
std::string repr_num_nivel;
if (num_nivel == std::nullopt) {
repr_num_nivel = "-";
} else {
repr_num_nivel = std::to_string(num_nivel.value().valor);
}
LOG(debug) << "repr_num_nivel= " << repr_num_nivel;
return cadena.interpolar_por_clave("num_nivel", repr_num_nivel);
return cadena.interpolar_por_clave("num_nivel", num_nivel.to_string());
}

std::string construir_resultado() { //
Expand Down
2 changes: 1 addition & 1 deletion src/textos.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

std::string construir_texto_instrucciones(
const std::string &plantilla, //
const std::optional<NumNivel> //
const NumNivelOpcional& //
);
std::string construir_resultado();

Expand Down
2 changes: 1 addition & 1 deletion src/vista/etiquetas/etiquetas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ EtiquetasGenerales::~EtiquetasGenerales() = default;

void EtiquetasGenerales::setup(
const std::string &instr, //
const std::optional<NumNivel> num_nivel, //
const NumNivelOpcional& num_nivel, //
const dominio::TiposDePizza &tp_disponibles //
) {
info->setup(instr, num_nivel);
Expand Down
2 changes: 1 addition & 1 deletion src/vista/etiquetas/etiquetas.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct EtiquetasGenerales : public ObjetoConFont {

void setup(
const std::string &instr, //
const std::optional<NumNivel>, //
const NumNivelOpcional&, //
const dominio::TiposDePizza &tp_disponibles //
);

Expand Down
4 changes: 2 additions & 2 deletions src/vista/etiquetas/etiquetas_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct FabricaEtiquetasInfo : public ObjetoConFont {
FabricaEtiquetasInfo(const OptionalFont &font) : ObjetoConFont(font) {}
sf::Text crear_etiqueta_instrucciones(
const std::string &plantilla, //
const std::optional<NumNivel> num_nivel //
const NumNivelOpcional& num_nivel //
) const {
const auto estilo = estilos::INSTRUCCIONES;
const auto texto = construir_texto_instrucciones(plantilla, num_nivel);
Expand All @@ -64,7 +64,7 @@ EtiquetasInfo::EtiquetasInfo(const OptionalFont &font) : ObjetoConFont(font) {}

void EtiquetasInfo::setup(
const std::string &instr, //
const std::optional<NumNivel> num_nivel //
const NumNivelOpcional& num_nivel //
) {
const auto fabrica = FabricaEtiquetasInfo(font);
LOG(info) << "Construyendo instrucciones" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/vista/etiquetas/etiquetas_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct EtiquetasInfo : public ObjetoConFont, public sf::Drawable {
EtiquetasInfo(const OptionalFont &);
void setup(
const std::string &instr, //
const std::optional<NumNivel> //
const NumNivelOpcional& //

);
void set_presentacion_vista(
Expand Down
4 changes: 2 additions & 2 deletions src/vista/vista.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ Vista::Vista(
}

void Vista::setup(
const std::string &instrucciones, //
const std::optional<NumNivel> num_nivel //
const std::string &instrucciones, //
const NumNivelOpcional &num_nivel //
) {
LOG(info) << "Inicializando etiquetas" << std::endl;
etiquetas.setup(
Expand Down
2 changes: 1 addition & 1 deletion src/vista/vista.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Vista : public ObjetoConFont,

void setup(
const std::string &instrucciones, //
const std::optional<NumNivel>
const NumNivelOpcional&
);
void set_presentacion_vista( //
std::shared_ptr<PresentacionVista> //
Expand Down

0 comments on commit bf7c5ec

Please sign in to comment.