Afegit position_overflow_ per a EnterName i poder plenar tots els slots de lletres

This commit is contained in:
2025-02-07 12:31:59 +01:00
parent 559210652f
commit 29bc4a64fd
3 changed files with 80 additions and 14 deletions

View File

@@ -2,6 +2,8 @@
#include "utils.h"
#include <stddef.h> // Para size_t
#include <algorithm> // Para max, min
#include <cassert> // Para assert
#include <iostream>
// Constructor
EnterName::EnterName()
@@ -15,12 +17,14 @@ void EnterName::init(const std::string &name)
{
name_ = "A";
position_ = 0;
position_overflow_ = false;
}
// Se pasa un nombre
else
{
name_ = name;
position_ = name_.length();
position_overflow_ = position_ >= NAME_LENGHT ? true : false;
}
// Inicializa el vector de indices con el nombre y espacios
@@ -30,29 +34,85 @@ void EnterName::init(const std::string &name)
// Incrementa la posición
void EnterName::incPosition()
{
++position_;
position_ = std::min(position_, NAME_LENGHT + 1);
if (position_ <= NAME_LENGHT)
if (position_overflow_)
{
character_index_[position_] = character_index_[position_ - 1];
// Si ya estamos en overflow, no incrementamos más.
return;
}
++position_;
if (position_ >= NAME_LENGHT)
{
position_ = NAME_LENGHT; // Mantenemos en el índice máximo válido.
position_overflow_ = true; // Activamos el flag de overflow.
}
else
{
// Copiamos el índice del carácter anterior si es posible.
if (position_ > 0 && position_ < NAME_LENGHT)
{
character_index_[position_] = character_index_[position_ - 1];
}
else
{
// Si position_ es 0, inicializamos el carácter actual.
character_index_[position_] = 0;
}
}
updateNameFromCharacterIndex();
}
// Decrementa la posición
void EnterName::decPosition()
{
--position_;
position_ = std::max(position_, 0);
character_index_[position_ + 1] = 0;
if (position_overflow_)
{
// Si estaba en overflow, lo desactivamos y mantenemos position_ en el máximo.
position_overflow_ = false;
position_ = NAME_LENGHT - 1;
}
else
{
if (position_ > 0)
{
--position_;
// Limpiamos el carácter siguiente si el índice es válido.
if (position_ + 1 < NAME_LENGHT)
{
character_index_[position_ + 1] = 0;
}
}
else
{
// Si position_ es 0, aseguramos que no vaya a ser negativo y limpiamos el carácter actual.
position_ = 0;
character_index_[position_] = 0;
}
// Si position_ es menor que NAME_LENGHT, aseguramos que el overflow esté desactivado.
if (position_ < NAME_LENGHT)
{
position_overflow_ = false;
}
}
updateNameFromCharacterIndex();
}
// Incrementa el índice
void EnterName::incIndex()
{
if (position_overflow_)
{
return;
}
++character_index_[position_];
if (character_index_[position_] >= character_list_.size())
if (character_index_[position_] >= static_cast<int>(character_list_.size()))
{
character_index_[position_] = 0;
}
@@ -62,6 +122,11 @@ void EnterName::incIndex()
// Decrementa el índice
void EnterName::decIndex()
{
if (position_overflow_)
{
return;
}
--character_index_[position_];
if (character_index_[position_] < 0)
{