Hello first of all I am a beginner programmer (student) I am doing the game of the hangman, and my question is the following, I made the code with a method and an "if" condition so that when pressing each button this will be deactivated and will change color in case of coinciding or not the letter of the word in play, the thing is that I have a click event of a button that generates a new game and would like to know how to do so that when executing the new event This game activates me again all the used buttons and return to the default color, so that this clearer I leave the code of the game. Thank you very much and I hope you can help me. Greetings
//DECLARACIONES DE VARIABLES
private int _cantidadErrores = 0;
private bool _iniciarJuego;
private int _cantidadMinimaChar = 3;
private int _cantidadMaximaChar = 50;
//CONSTRUCTOR DEL FORMULARIO
public FormJuego()
{
InitializeComponent();
//INICIALIZAMOS VARIABLES
_iniciarJuego = false;
}
//======================================================================//
//========================== EVENTOS PRIVADOS ==========================//
//======================================================================//
private void FormJuego_Load(object sender, EventArgs e)
{
ClearForm();
}
private void FormJuego_Activated(object sender, EventArgs e)
{
txtPalabraIntroducida.Focus();
}
private void btnComenzar_Click(object sender, EventArgs e)
{
if (ComprobarExtensionPalabra(txtPalabraIntroducida.Text, _cantidadMinimaChar, _cantidadMaximaChar))
{
_iniciarJuego = true;
txtPalabraIntroducida.Enabled = false;
btnComenzar.Enabled = false;
btnNuevoJuego.Enabled = true;
txtProgreso.Text = ReplicarPalabraEnDisplayConGuiones(txtPalabraIntroducida.Text);
}
else
{
string mensajeErrorChar = $"La palabra debe tener entre {_cantidadMinimaChar} y {_cantidadMaximaChar} caracteres.";
MessageBox.Show(mensajeErrorChar);
}
}
private void btnA_Click(object sender, EventArgs e)
{
if (!(sender is Button)) return;//Si no sos un boton return(No sigue ejecutando el codigo)
if (_iniciarJuego)
{
ComprobarYReemplazarLetra(((Button)sender).Text , (Button)sender);
}
else
{
txtPalabraIntroducida.Text += ((Button)sender).Text;
}
}
private void btnNuevoJuego_Click(object sender, EventArgs e)
{
ClearForm();
txtPalabraIntroducida.Focus();
}
private void btnSalir_Click(object sender, EventArgs e)
{
Application.Exit();
}
//======================================================================//
//========================== METODOS PRIVADOS ==========================//
//======================================================================//
/// <summary>
/// Metodo para limpiar los controles del formulario.
/// </summary>
private void ClearForm()
{
txtPalabraIntroducida.Clear();
txtPalabraIntroducida.Enabled = true;
txtProgreso.Clear();
btnComenzar.Enabled = true;
btnNuevoJuego.Enabled = false;
_cantidadErrores = 0;
lblErrores.Text = $"Errores: {_cantidadErrores}";
_iniciarJuego = false;
}
/// <summary>
/// Compruba que la palabra cumpla con la extension establecida.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
private bool ComprobarExtensionPalabra(string palabra, int cantidadMinima, int cantidadMaxima)
{
if (palabra.Length >= cantidadMinima && palabra.Length <= cantidadMaxima) return true;
return false;
}
/// <summary>
/// Replica la palabra introducida con guiones en el display Progreso
/// </summary>
/// <param name="text"></param>
private string ReplicarPalabraEnDisplayConGuiones(string palabra)
{
string palabraIntroducida = string.Empty;
for (int i = 0; i < palabra.Length; i++)
{
palabraIntroducida += "_";
}
return palabraIntroducida;
}
/// <summary>
/// Metodo para comprobar si existe y reemplazar la letra.
/// </summary>
/// <param name="Letra"></param>
private void ComprobarYReemplazarLetra(string Letra, Button botonPresionado)
{
if (VerificarSiExiteLaLetra(txtPalabraIntroducida.Text, Letra))
{
txtProgreso.Text = ReemplazarLetraEnProgreso(txtPalabraIntroducida.Text, txtProgreso.Text, Letra);
botonPresionado.BackColor = Color.Green;
botonPresionado.Enabled = false;
VerificarSiGano(txtPalabraIntroducida.Text, txtProgreso.Text);
}
else
{
_cantidadErrores++;
botonPresionado.BackColor = Color.Red;
botonPresionado.Enabled = false;
lblErrores.Text = $"Errores: {_cantidadErrores}";
MostrarImagenError(_cantidadErrores);
VerificarSiPerdio(_cantidadErrores);
}
}
private void VerificarSiGano(string palabra, string progreso)
{
if (palabra == progreso)
{
MessageBox.Show("Ganaste");
}
}
private void VerificarSiPerdio(int cantidadErrores)
{
if (_cantidadErrores == 6)
{
MessageBox.Show("Perdiste.");
}
}
private void MostrarImagenError(int cantidadErrores)
{
switch (_cantidadErrores)
{
case 1:
imgImagen.Image = Properties.Resources.Error1;
break;
case 2:
imgImagen.Image = Properties.Resources.Error2;
break;
case 3:
imgImagen.Image = Properties.Resources.Error3;
break;
case 4:
imgImagen.Image = Properties.Resources.Error4;
break;
case 5:
imgImagen.Image = Properties.Resources.Error5;
break;
case 6:
imgImagen.Image = Properties.Resources.Error6;
break;
default:
break;
}
}
/// <summary>
/// Reemplaza la letra en la posicion correspondiente a la palabra original.
/// </summary>
/// <param name="text1"></param>
/// <param name="text2"></param>
/// <param name="letra"></param>
private string ReemplazarLetraEnProgreso(string palabraOriginal, string progreso, string Letra)
{
string cadena = string.Empty;
for (int i = 0; i < palabraOriginal.Length; i++)
{
if (palabraOriginal[i] == Letra[0])
{
cadena += Letra;
}
else
{
cadena += progreso[i].ToString();
}
}
return cadena;
}
/// <summary>
/// Verifica si la letra existe en la palabra y devuelve un bool
/// </summary>
/// <param name="text"></param>
/// <param name="letra"></param>
/// <returns></returns>
private bool VerificarSiExiteLaLetra(string palabra, string letra)
{
for (int i = 0; i < palabra.Length; i++)
{
if (palabra[i] == letra[0])
{
return true;
}
}
return false;
}