View Full Paper

Owner Consent Verified
Coursework 4.7

A Teddy Bear Picnic Game

2
Pages
Harvard
Style
~ 2 mins
Reading Time
Visual Basic Teddy Bear Picnic Game Recursion Recursive Algorithm VB.NET Event-Driven Programming Boolean Function Algorithm Design GUI Programming Coursework Programming Logic Game Algorithm Software Development User Interface Computer Programming

Cover Page

A Teddy Bear Picnic Game

Name

Institution

Course

Instructor

Date

Visual Basic Program Implementation

Public Function reachGoal(numBears As Integer) As Boolean
    Dim half, lastTwoDigits, minusFortyTwo As Integer

    If numBears = 42 Then
        Return True
    ElseIf numBears < 42 Then
        Return False
    ElseIf (numBears Mod 2 = 0) Or (numBears Mod 3 = 0) Or (numBears Mod 4 = 0) Or (numBears Mod 5 = 0) Then

        If (numBears Mod 2 = 0) Then
            half = reachGoal(numBears / 2)
        End If

        If (numBears Mod 3 = 0) Or (numBears Mod 4 = 0) Then
            Dim gb As Integer
            gb = (numBears Mod 10) * ((numBears Mod 100) / 10)

            If gb <= 0 Then Return False

            lastTwoDigits = reachGoal(numBears - gb)
        End If

        If (numBears Mod 5 = 0) Then
            minusFortyTwo = reachGoal(numBears - 42)
        End If

        If (half Or lastTwoDigits Or minusFortyTwo) Then
            Return True
        Else
            Return False
        End If

    Else
        Return False
    End If
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim numBears As Integer
    numBears = TextBox1.Text

    If reachGoal(numBears) Then
        MsgBox("Your goal is reached!")
    Else
        MsgBox("Your goal cannot be reached!")
    End If

End Sub

Program Logic and Recursive Algorithm

The program uses a recursive function named reachGoal to determine whether the player can successfully reach exactly 42 teddy bears. The function first evaluates the base cases. If the number of bears equals 42, the function immediately returns True, indicating that the objective has been achieved. If the number of bears falls below 42, the function returns False because no further valid moves can produce the target value.

When the number of bears exceeds 42, the function evaluates every legal move according to the game rules. If the number is even, the recursive function attempts to reach the goal after dividing the number of bears by two. If the number is divisible by three or four, the product of the last two digits is calculated and subtracted from the current number before another recursive call is performed. If the number is divisible by five, forty-two bears are subtracted before continuing the recursive search.

Each recursive branch returns either True or False. If any branch successfully reaches exactly 42 bears, the function immediately returns True. Otherwise, after all possible moves have been evaluated, the function returns False.

User Interface Operation

The graphical user interface allows the player to enter the initial number of teddy bears into a text box. When the button is clicked, the program invokes the reachGoal function using the supplied value. A message box then informs the user whether the target of 42 bears can be reached by following the permitted game rules. This design combines recursive problem-solving with a simple event-driven interface to create an interactive solution to the Teddy Bear Picnic Game.

Related Papers
Browse all
You can message us here