public void ButtonTry_Click(object sender, EventArgs e) { char text; try { text = Char.Parse(this.InputTry.Text); } catch (Exception) { LabelInputFeedback.Text = "You have to insert a letter!"; return; } this.InputTry.Text = String.Empty; bool result = false; try { result = CurrentGame.Try(text); } catch { LabelInputFeedback.Text = "You have already tried that letter!"; return; } InputTry.Focus(); LabelInputFeedback.Text = String.Empty; if (result) { LabelCurrent.Text = CurrentGame.CurrentState; if (CurrentGame.LettersLeft() <= 0) { LabelWon.Show(); InputTry.Hide(); ButtonTry.Hide(); TimerTimeSpent.Stop(); } } else { UpdateCurrentErrors(); if (CurrentGame.Errors >= CurrentGame.MaxErrors) { LabelWon.Text = "Oh no! You have lost."; LabelWon.ForeColor = Color.Red; LabelWon.Show(); InputTry.Hide(); ButtonTry.Hide(); TimerTimeSpent.Stop(); LabelCurrent.Text = String.Empty; foreach (var letter in CurrentGame.Word) { LabelCurrent.Text += $"{letter} "; } } } }
public Game() { InitializeComponent(); StartedAt = DateTime.Now; CurrentGame = new GuessTheWord.Game(); LabelCurrent.Text = CurrentGame.CurrentState; UpdateCurrentErrors(); UpdateTried(); LabelWon.Hide(); }