Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show characters of a string in a row and make a timer for labels c#

Tags:

c#

I'm trying to make hangman in windows form with c#.

I want to show points or something instead of letters.
Example: "hello" would be:

. . . . . or _ _ _ _ _ 

Can I also show the letters what is good on that points?

Example:

 word: Hello.
 player try: e.
 Output: Correct.
 Show: . e . . . or _ e _ _ _. 

When a player gives a correct letter there shows up a label with "correct", can I make the label disappear after a few seconds? I got this already:

if (woord.Contains(letter))
{
      labelJuist.Text = "Juist!";
}
else
{
      labelFout.Text = "Fout!";
}
like image 292
Issam. H Avatar asked Aug 14 '18 16:08

Issam. H


Video Answer


1 Answers

If you have a string called WordToGuess that represents the actual word, and you have a BindingList<char> called guesses, then to show just the characters of the word that they've guessed, you can use something like:

lblWord.Text = string.Join(" ", 
    WordToGuess.Select(chr => guesses.Contains(chr) ? chr : '_'));

This will display only the characters from WordToGuess that exist in guesses, and will show underscore characters for the others. By using string.Join, we can also insert a space between each character so the underscores stand out better.

Also, in order to store the characters in a single location, you might want to put them in a BindingList<char> guesses, and then set the DataBinding of the ListBox to the guesses list. Then, each time the user submits a new guess, add it to the list. The binding list should be a class-level scoped field, so that it retains it's contents. This way the list box shows the letters automatically, and you still have the private list to work with (which is slightly easier than the Items property of the list box).

Here's a copy/pastable sample that you can put into a new WinForms project (with "StackOverflow" as the hard-coded word):

public partial class Form1 : Form
{
    private Button btnSubmit;
    private Label lblGuess;
    private TextBox txtGuess;
    private Label lblWord;
    private Label lblGuessedLetters;
    private ListBox lstGuessedLetters;
    private readonly BindingList<char> guesses = new BindingList<char>();

    private const string WordToGuess = "StackOverflow";

    private const int controlHeight = 20;
    private const int controlWidth = 200;
    private const int controlPadding = 10;

    public Form1()
    {
        InitializeComponent();

        lblWord = new Label
        {
            Left = controlPadding,
            Top = controlPadding,
            Height = controlHeight,
            Width = controlWidth,
            Text = new string('_', WordToGuess.Length)
        };
        Controls.Add(lblWord);

        lblGuessedLetters = new Label
        {
            Left = controlPadding,
            Top = lblWord.Bottom + controlPadding,
            Height = controlHeight,
            Width = controlWidth,
            Text = "Guessed Letters:"
        };
        Controls.Add(lblGuessedLetters);

        lstGuessedLetters = new ListBox
        {
            Left = controlPadding,
            Top = lblGuessedLetters.Bottom + controlPadding,
            Height = controlHeight * 5,
            Width = controlWidth,
            Enabled = false,
            DataSource = guesses,
            SelectionMode = SelectionMode.None
        };
        Controls.Add(lstGuessedLetters);

        lblGuess = new Label
        {
            Left = controlPadding,
            Top = lstGuessedLetters.Bottom + controlPadding,
            Height = controlHeight,
            Width = controlWidth,
            Text = "Enter your guess below"
        };
        Controls.Add(lblGuess);

        txtGuess = new TextBox
        {
            Left = controlPadding,
            Top = lblGuess.Bottom + controlPadding,
            Height = controlHeight,
            Width = controlWidth
        };
        Controls.Add(txtGuess);

        btnSubmit = new Button
        {
            Left = controlPadding,
            Top = txtGuess.Bottom + controlPadding,
            Height = controlHeight,
            Width = controlWidth,
            Text = "Submit Guess"
        };
        btnSubmit.Click += BtnSubmit_Click;
        AcceptButton = btnSubmit; // Make this button default - just press Enter
        Controls.Add(btnSubmit);
    }

    private void BtnSubmit_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrWhiteSpace(txtGuess.Text)) 
        {
            MessageBox.Show("enter a guess in the textbox");
        }
        else
        {
            // Grab the first letter in case they typed more than one
            var letter = txtGuess.Text[0];

            if (guesses.Contains(letter)) 
            {
                MessageBox.Show("you already guessed that letter!");
            }
            else
            {
                guesses.Add(letter);
                lblWord.Text = string.Join(" ", 
                    WordToGuess.Select(chr => guesses.Contains(chr) ? chr : '_'));
            }
        }

        txtGuess.Focus();
        txtGuess.Text = "";
    }
}

Output

enter image description here

like image 193
Rufus L Avatar answered Sep 17 '22 15:09

Rufus L