Guessing game
Have several build errors for a guessing game i made in a form app
using Guessing_Game;
using System;
using System.Windows.Forms;
namespace GuessingGame
{
public partial class Form1 : Form
{
private int numberToGuess;
private int attempts;
public Form1()
{
InitializeComponent();
StartNewGame();
}
private void StartNewGame()
{
numberToGuess = new Random().Next(1, 11); // Random number between 1 and 10
attempts = 0;
lblFeedback.Text = "I'm thinking of a number between 1 and 10. Can you guess it?";
txtGuess.Clear();
txtGuess.Enabled = true;
btnCheckGuess.Enabled = true;
btnPlayAgain.Visible = false;
}
private void btnCheckGuess_Click(object sender, EventArgs e)
{
if (int.TryParse(txtGuess.Text, out int guess))
{
attempts++;
if (guess < 1 || guess > 10)
{
lblFeedback.Text = "Please guess a number between 1 and 10.";
}
else if (guess < numberToGuess)
{
lblFeedback.Text = "Your guess is too low. Try again!";
}
else if (guess > numberToGuess)
{
lblFeedback.Text = "Your guess is too high. Try again!";
}
else
{
lblFeedback.Text = $"Congratulations! You've guessed the number {numberToGuess} correctly in {attempts} attempts.";
txtGuess.Enabled = false;
btnCheckGuess.Enabled = false;
btnPlayAgain.Visible = true;
}
}
else
{
lblFeedback.Text = "Invalid input! Please enter a number.";
}
}
private void btnPlayAgain_Click(object sender, EventArgs e)
{
StartNewGame();
}
}
}
2 Replies