Skip to content

Commit

Permalink
Convierte num_nivel en opcional
Browse files Browse the repository at this point in the history
  • Loading branch information
autosquash committed Jan 16, 2024
1 parent 0423019 commit 0b4480a
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 55 deletions.
10 changes: 5 additions & 5 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(std::shared_ptr<NumNivel> numero_nivel) {
assert(numero_nivel);
std::cout << "DEMO NIVEL " << numero_nivel->valor << std::endl;
int indice_nivel = numero_nivel->valor - 1;
int demo_nivel(NumNivel numero_nivel) {

std::cout << "DEMO NIVEL " << numero_nivel.valor << std::endl;
int indice_nivel = numero_nivel.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(std::shared_ptr<NumNivel> numero_nivel) {
while (true) {
Nivel nivel(
globales, std::make_shared<DatosNivel>(datos_niveles[indice_nivel]),
numero_nivel, grid, true
std::optional<NumNivel>(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(std::shared_ptr<NumNivel> numero_nivel);
int demo_nivel(NumNivel 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 = std::make_shared<NumNivel>(i + 1);
auto num_nivel = NumNivel(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.valor
<< std::endl;
Nivel nivel(globales, datos_nivel, num_nivel, grid, es_el_ultimo);

Expand Down
2 changes: 2 additions & 0 deletions src/log_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ namespace keywords = boost::log::keywords;
void init_log() {
auto core = logging::core::get();
#ifdef _DEBUG
std::cout << "Habilitando logging" << std::endl;
core->set_logging_enabled(true);
#else
std::cout << "Deshabilitando logging" << std::endl;
core->set_logging_enabled(false);
#endif
std::filesystem::path logFileFolder =
Expand Down
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +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(
std::make_shared<NumNivel>(num_nivel_introducido)
);
return demo_nivel(NumNivel(num_nivel_introducido));
}
break;
case AppNombrada::Visual:
Expand Down
31 changes: 16 additions & 15 deletions src/nivel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ std::shared_ptr<EnlaceVista> Nivel::_crear_enlace_vista( //
assert(globales->font);
font.set_pointer(globales->font);
}
std::cout << "Creando vista" << std::endl;
LOG(info) << "Creando vista" << std::endl;
const auto vista_ptr = std::make_shared<Vista>(
font, //
grid, //
control_pizzas.get_tipos_disponibles() //
);
std::cout << "Vista creada" << std::endl;
LOG(info) << "Vista creada" << std::endl;
std::string instrucciones;
if (datos_nivel == nullptr) {
instrucciones = "No hay instrucciones";
} else {
instrucciones = datos_nivel->instrucciones;
}
vista_ptr->setup(instrucciones, num_nivel);
std::cout << "Vista inicializada" << std::endl;
LOG(info) << "Vista inicializada" << std::endl;
auto enlace_vista = std::make_shared<EnlaceVista>();
enlace_vista->set_vista(vista_ptr);
return enlace_vista;
Expand Down Expand Up @@ -177,34 +177,34 @@ std::optional<AccionGeneral> Nivel::_procesa_cambio_de_fase(
Nivel::Nivel(
std::shared_ptr<Globales> globales, //
std::shared_ptr<DatosNivel> datos_nivel, //
std::shared_ptr<NumNivel> num_nivel, //
std::optional<NumNivel> 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>();
int valor_num_nivel;
if (num_nivel == nullptr) {
valor_num_nivel = 0;
std::string repr_num_nivel;
if (!num_nivel) {
repr_num_nivel = "-";
} else {
valor_num_nivel = num_nivel->valor;
repr_num_nivel = std::to_string(num_nivel.value().valor);
}
std::cout << "Creando nivel " << valor_num_nivel << std::endl;
LOG(info) << "Creando nivel " << repr_num_nivel << std::endl;
if (datos_nivel) {
modelo_amplio.emplace(datos_nivel->datos_modelo_interno);
} else {
modelo_amplio.emplace();
};
std::cout << "modelo amplio creado" << std::endl;
assert(modelo_amplio);
LOG(info) << "modelo amplio creado" << std::endl;
assert(modelo_amplio->establecido);
std::cout << "modelo amplio establecido" << std::endl;
LOG(info) << "modelo amplio establecido" << std::endl;
auto &control_pizzas = modelo_amplio->modelo_interno.control_pizzas;
std::cout << "tenemos control piezas" << std::endl;
LOG(info) << "tenemos control piezas" << std::endl;
enlace_vista = _crear_enlace_vista(control_pizzas);
std::cout << "enlace_vista creado" << std::endl;
LOG(info) << "enlace_vista creado" << std::endl;
modelo_amplio->observadores_fase.push_back(enlace_vista);
}

Expand All @@ -213,7 +213,7 @@ AccionGeneral Nivel::ejecutar() {
auto &contadores = control_pizzas.contadores;
establecer_fase(FaseNivel::MostrandoInstrucciones);
assert(!contadores.empty());
std::cout << "modelo amplio iniciado" << std::endl;
LOG(info) << "modelo amplio iniciado" << std::endl;
auto &gestor_tiempo_juego = modelo_amplio->modelo_interno.gestor_tiempo;

Timer timer_espera_antes_de_resultado;
Expand All @@ -224,8 +224,9 @@ AccionGeneral Nivel::ejecutar() {
};
sf::Sound sound;
sf::Time previo = tiempo::obtener_tiempo_actual();
std::cout << "empezando bucle de juego" << std::endl;
LOG(info) << "Empezando bucle de juego" << std::endl;
while (globales->window.isOpen()) {
LOG(info) << "Nueva iteracion del bucle de juego" << std::endl;
sf::Event event;
while (globales->window.pollEvent(event)) {
LOG(info) << "Antes de procesar evento" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/nivel.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct Nivel {
public:
const std::shared_ptr<Globales> globales;
const std::shared_ptr<DatosNivel> datos_nivel;
const std::shared_ptr<NumNivel> num_nivel;
const std::optional<NumNivel> num_nivel;
const std::shared_ptr<Grid> grid;

const bool es_el_ultimo;
Expand All @@ -57,7 +57,7 @@ struct Nivel {
Nivel(
std::shared_ptr<Globales> globales = nullptr, //
std::shared_ptr<DatosNivel> datos_nivel = nullptr, //
std::shared_ptr<NumNivel> num_nivel = nullptr, //
std::optional<NumNivel> num_nivel = std::nullopt, //
std::shared_ptr<Grid> grid = nullptr, //
bool es_el_ultimo = false //
);
Expand Down
1 change: 0 additions & 1 deletion src/shared.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ struct NumNivel {
explicit NumNivel(int v) : valor(v) { //
assert(valor > 0);
}
NumNivel(const NumNivel &) = delete;
NumNivel &operator=(const NumNivel &) = delete;
};

Expand Down
2 changes: 2 additions & 0 deletions src/tests/test_main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "../log_init.h"
#include <gtest/gtest.h>

int main(int argc, char **argv) {
init_log();
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
19 changes: 9 additions & 10 deletions src/textos.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
#include "textos.h"
#include "log_init.h"
#include "vista/cadenas.h"
#include <iostream>

#define RESULTADO_NIVEL_1 \
"%!Enhorabuena! Todos los clientes est%an satisfechos."

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

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::shared_ptr<NumNivel> //
const std::optional<NumNivel> //
);
std::string construir_resultado();

Expand Down
1 change: 0 additions & 1 deletion src/vista/componentes/varios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,5 @@ sf::Text crearEtiqueta(
const sf::Vector2f &posicion //

) {
std::cout << "Creando etiqueta con EstiloTexto y mas" << std::endl;
return crearEtiqueta(texto, estilo.tamano, estilo.color, font, posicion);
}
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::shared_ptr<NumNivel> num_nivel, //
const std::optional<NumNivel> 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::shared_ptr<NumNivel>, //
const std::optional<NumNivel>, //
const dominio::TiposDePizza &tp_disponibles //
);

Expand Down
16 changes: 8 additions & 8 deletions src/vista/etiquetas/etiquetas_info.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "etiquetas_info.h"
#include "../../log_init.h"
#include "../../textos.h"
#include "../basicos_vista.h"
#include "../componentes/varios.h"
Expand Down Expand Up @@ -37,13 +38,12 @@ struct FabricaEtiquetasInfo : public ObjetoConFont {
public:
FabricaEtiquetasInfo(const OptionalFont &font) : ObjetoConFont(font) {}
sf::Text crear_etiqueta_instrucciones(
const std::string &plantilla, //
const std::shared_ptr<NumNivel> num_nivel //
const std::string &plantilla, //
const std::optional<NumNivel> num_nivel //
) const {
std::cout << "en crear_etiqueta_instrucciones" << std::endl;
const auto estilo = estilos::INSTRUCCIONES;
const auto texto = construir_texto_instrucciones(plantilla, num_nivel);
std::cout << "Instrucciones construidas" << std::endl;
LOG(info) << "Instrucciones construidas" << std::endl;
const auto etiqueta = crear_etiqueta_info(texto, estilo);
return etiqueta;
}
Expand All @@ -63,13 +63,13 @@ struct FabricaEtiquetasInfo : public ObjetoConFont {
EtiquetasInfo::EtiquetasInfo(const OptionalFont &font) : ObjetoConFont(font) {}

void EtiquetasInfo::setup(
const std::string &instr, //
const std::shared_ptr<NumNivel> num_nivel //
const std::string &instr, //
const std::optional<NumNivel> num_nivel //
) {
const auto fabrica = FabricaEtiquetasInfo(font);
std::cout << "Construyendo instrucciones" << std::endl;
LOG(info) << "Construyendo instrucciones" << std::endl;
instrucciones = fabrica.crear_etiqueta_instrucciones(instr, num_nivel);
std::cout << "Construyendo etiqueta resultado" << std::endl;
LOG(info) << "Construyendo etiqueta resultado" << std::endl;
resultado = fabrica.crear_etiqueta_resultado();
}

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::shared_ptr<NumNivel> //
const std::optional<NumNivel> //

);
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 @@ -101,8 +101,8 @@ Vista::Vista(
}

void Vista::setup(
const std::string &instrucciones, //
const std::shared_ptr<NumNivel> num_nivel //
const std::string &instrucciones, //
const std::optional<NumNivel> num_nivel //
) {
std::cout << "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::shared_ptr<NumNivel>
const std::optional<NumNivel>
);
void set_presentacion_vista( //
std::shared_ptr<PresentacionVista> //
Expand Down

0 comments on commit 0b4480a

Please sign in to comment.