0
My goal is to play a hangman’s game.
Basically my problem is this: I have a word divided into substrings for each letter of the word, all stored in an array. For example:
string[] letras = new string[numletras];
for (int i = 0; i < numletras; i++)
{
letras[i] = palavra.Substring(i, 1);
}
With the word divided in its respective letters, I wanted to associate each letter to each label.
As exemplified, I have a word "shirt" and wanted to divide the letters for each label. Because every word has a different letter number, I need to find a cycle that does that for me, which means I can’t do it one by one.
Here is the whole code of my program. There are parts I have to optimize I know, but my main problem was what I mentioned above.
public partial class Form1 : Form
{
string palavra;
int tentativas;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Enabled = true;
textBox2.Enabled = true;
button2.Enabled = true;
button3.Enabled = true;
tentativas = 5;
label14.Text = Convert.ToString(tentativas);
var linhas = File.ReadAllLines(Application.StartupPath + "Roupas.txt").Length;
Random rnd = new Random();
int randomlinhanum = rnd.Next(linhas);
int indicator = 0;
using (var reader = File.OpenText(Application.StartupPath + "Roupas.txt"))
{
while (reader.ReadLine() != null)
{
if (indicator == randomlinhanum)
{
palavra = File.ReadLines(Application.StartupPath + "Roupas.txt").Skip(indicator - 1).Take(1).First();
MessageBox.Show(palavra);
break;
}
indicator++;
}
}
int numletras = palavra.Length;
MessageBox.Show(Convert.ToString(numletras));
//iNDICAÇÃO DE QUANTAS LABELS O TABULEIRO IRÁ TER
if (numletras == 4)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
}
if (numletras == 5)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
}
if (numletras == 6)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
letra6.Show();
}
if (numletras == 7)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
letra6.Show();
letra7.Show();
}
if (numletras == 8)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
letra6.Show();
letra7.Show();
letra8.Show();
}
if (numletras == 9)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
letra6.Show();
letra7.Show();
letra8.Show();
letra9.Show();
}
if (numletras == 10)
{
letra1.Show();
letra2.Show();
letra3.Show();
letra4.Show();
letra5.Show();
letra6.Show();
letra7.Show();
letra8.Show();
letra9.Show();
letra10.Show();
}
string[] letras = new string[numletras];
for (int i = 0; i < numletras; i++)
{
letras[i] = palavra.Substring(i, 1);
}
//Onde preciso de ajuda
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Enabled = false;
textBox2.Enabled = false;
button2.Enabled = false;
button3.Enabled = false;
letra1.Hide();
letra2.Hide();
letra3.Hide();
letra4.Hide();
letra5.Hide();
letra6.Hide();
letra7.Hide();
letra8.Hide();
letra9.Hide();
letra10.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
}
Where are the Labels?
– Maniero
They are the squares in the middle of the form! They are only visually edited.
– Alexandre Neves
but I want to know in your code, which I imagine is where you want help.
– Maniero
I already added the code to the question!
– Alexandre Neves
No, you added a lot of images, it doesn’t help us at all, put the code even.
– Maniero
As Niero said, enter the code, and it makes no sense to create a string array to save a word. a string is already a char array =]. Other than that after putting the code, I think I should put the Tags on the screen at runtime, according to the amount of letters and, using the label tag to save the correct letter is an option
– Rovann Linhalis
I am a beginner in programming, I do not understand much of these terms... But I added the code and I hope that solves the situation!
– Alexandre Neves
@Alexandreneves Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero