Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Instructions: Using Visual Studio, choose an ASP.NET C# Empty Website to create

ID: 3913327 • Letter: I

Question

Instructions: Using Visual Studio, choose an ASP.NET C# Empty Website to create a web application for the guessing game. The application will create a randomly generated hidden value and the player will try to guess this hidden value in the fewest guesses possible. Make sure all the items listed below are included: • The randomly generated hidden value is between 1 and 10 • Player can enter the guess using the appropriate server control • Player will click a submit button to check the entered guess • The app will count the player’s number of guesses and display it • After every guess, the player will receive a feedback message indicating if the guess is too high, too low, or if the guess matches the hidden number • When the player finds the hidden value, a congratulation message with the total number of guesses should be displayed. Submit all code files, solution files, folders, images, etc as a zip file in the Assignments section of D2L.

Explanation / Answer


WebForm1.aspx Source:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="GuessingGame.WebForm1" ViewStateMode="Enabled" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title></title>

</head>

<body>

<form id="form1" runat="server" >

<div>

<asp:Label ID="lblGuess" runat="server" Text="Enter your Guess"></asp:Label>

<asp:TextBox ID="txtGuess" runat="server"></asp:TextBox>

<br />

<asp:Label ID="lblDisplay" runat="server" Text="Label"></asp:Label>

</div>

<asp:Button ID="btnGuess" runat="server" Text="Enter" />

</form>

</body>

</html>

webform1.aspx.vb code

Public Class WebForm1

Inherits System.Web.UI.Page

Dim guessCount As Integer 'Stores the guess count

Dim guessVal As Integer 'Stores the guessed value

Dim randomNumber As Integer = CInt(Math.Ceiling(Rnd() * 10)) + 1 'Random Number generation between 1 and 10


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

lblDisplay.Visible = False 'Used to store the guessvalue and hold using viewstate

End Sub

Protected Sub btnGuess_Click(sender As Object, e As EventArgs) Handles btnGuess.Click

If (Page.IsPostBack = True) Then

'This If logic checks if the View State already has a value of guess count and moves it to the variable guesscount

'guesscount is later incremented as usual for the clicks

If ViewState("vguesscount") <> 0 Then

guessCount = ViewState("vguesscount")

guessCount = guessCount + 1

lblDisplay.Text = guessCount

ViewState("vguesscount") = lblDisplay.Text

Else

guessCount = guessCount + 1

lblDisplay.Text = guessCount

ViewState("vguesscount") = lblDisplay.Text

End If


guessVal = Integer.Parse(txtGuess.Text)

'Following If logic compares the guessed value with Random number and displays message based on the difference around number 5

If (guessVal = randomNumber) Then

MsgBox("Congratulation Your guess is correct!!!!")

MsgBox("Number of guesses =" + guessCount.ToString())

ElseIf (guessVal < randomNumber) Then

guessVal = randomNumber - guessVal

If (guessVal < 5) Then

MsgBox("Your guess is low")

ElseIf (guessVal >= 5) Then

MsgBox("Your guess is too low")

End If

ElseIf (guessVal > randomNumber) Then

guessVal = randomNumber - guessVal

If (guessVal < 5) Then

MsgBox("Your guess is high")

ElseIf (guessVal >= 5) Then

MsgBox("Your guess is too High")

End If

End If

End If

End Sub


End Class