Hello. I write the application "guess the word." I found video tutorials and make them. Code next
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Net; namespace finalHome { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string word = ""; List<Label> labels = new List<Label>(); int t = 10; private void timer1_Tick(object sender, EventArgs e) { t--; timer.Text = t.ToString(); if (t == 0) { timer1.Stop(); MessageBox.Show("Your time is up the word was "+ word,"LOST"); Reset(); } } private void startButton_Click(object sender, EventArgs e) { Reset(); timer1.Enabled = true; } public void MakeLabels() { word = GetRandomWord(); char[] chars = word.ToCharArray(); int between = 330 / chars.Length; for (int i = 0; i < chars.Length; i++) { labels.Add(new Label()); labels[i].Location = new Point((i * between) + 10, 80); labels[i].Text = "_"; labels[i].Parent = groupBox1; labels[i].BringToFront(); labels[i].CreateControl(); } attemptLabel2.Text = (chars.Length).ToString(); } void Mistake() { for (int i = word.Length; i > 0; i--) { attemptLabel2.Text = (word.Length-1).ToString(); } } string GetRandomWord() { WebClient wc = new WebClient(); string wordList = wc.DownloadString("http://www-cs-faculty.stanford.edu/~knuth/sgb-words.txt"); string[] words = wordList.Split('\n'); Random ran = new Random(); return words[ran.Next(0, words.Length )]; } public void submitButton_Click(object sender, EventArgs e) { char letter = SubmitTextox.Text.ToLower().ToCharArray()[0]; if(!char.IsLetter(letter)) { MessageBox.Show("you can submit only letters","Error", MessageBoxButtons.OK,MessageBoxIcon.Error); return; } if (word.Contains(letter)) { char[] letters = word.ToCharArray(); for (int i = 0; i < letters.Length; i++) { if (letters[i] == letter) labels[i].Text = letter.ToString(); } foreach (Label l in labels) if (l.Text == "_") return; MessageBox.Show("You won!", "Nice"); Reset(); } else { MessageBox.Show("there is not this letter in the word!","Error"); label1.Text += " " + letter.ToString() + ","; Mistake(); } } void Reset() { GetRandomWord(); MakeLabels(); label1.Text = "Missed"; SubmitTextox.Text = ""; timer1.Enabled = false; timer1.Enabled = true; } private void resetButton_Click(object sender, EventArgs e) { Reset(); } private void submitWord_Click(object sender, EventArgs e) { if (WordTextBox.Text == word) { MessageBox.Show("You have won! The word is " + word); Reset(); } else { MessageBox.Show("Wrong word! Try again"); } } } } 1 If the timer value is 0, a message is displayed that the user has lost. When you click on reset, the timer continues to count to -1 -2 -3 and so on. How to make it so that when you click on the Reset timer starts the report with an initial value? 2. How to make that when entering incorrect letters or words in SubmitTextOx and WordTextBox respectively, the value of attemptLabel2 is decreased by one. and when this value reaches 0, a message was also issued that the user lost. Thank you very much
timer1? - VladD