How to write the 7 Boom game. If a number is divided by 7 or includes the digit 7, the user writes Boom in VB.NET

1 Answer

0 votes
Imports System

' 7 Boom game. The user enters a number he wants to start the game, 
' (-1 to end the game, or lost), then the user enters numbers, from the number he entered
' If the current number is divisible by 7, or one of its digits is 7, 
' And the user did not write the word 'Boom', the user loses. 

Module Program

    Sub Main()
        RunGame()
    End Sub

    ' -----------------------------
    ' Game Controller
    ' -----------------------------
    Sub RunGame()
        Console.Write("Enter a number from which you want to start the game (-1 to exit): ")
        Dim startInput As String = Console.ReadLine()

        Dim currentNum As Integer
        If Not Integer.TryParse(startInput, currentNum) OrElse currentNum = -1 Then
            Return
        End If

        While True
            Dim userInput As String = GetUserInput()

            If userInput = "-1" Then
                Console.WriteLine("Game Over!")
                Exit While
            End If

            If Not ProcessTurn(currentNum, userInput) Then
                Exit While
            End If

            currentNum += 1
        End While
    End Sub

    ' -----------------------------
    ' Input Handling
    ' -----------------------------
    Function GetUserInput() As String
        Console.Write("Enter a number (or Boom): ")
        Return Console.ReadLine()
    End Function

    ' -----------------------------
    ' Turn Logic
    ' -----------------------------
    Function ProcessTurn(expectedNumber As Integer, userInput As String) As Boolean
        Dim shouldBoom As Boolean = IsBoom(expectedNumber)
        Dim lower As String = userInput.ToLower()

        If shouldBoom Then
            If lower <> "boom" Then
                Console.WriteLine($"{userInput} (should be Boom)")
                Console.WriteLine("YOU LOSE!")
                Return False
            End If
        Else
            Dim value As Integer
            If lower = "boom" OrElse Not Integer.TryParse(userInput, value) OrElse value <> expectedNumber Then
                Console.WriteLine($"Wrong move! It was {expectedNumber}")
                Console.WriteLine("YOU LOSE!")
                Return False
            End If
        End If

        Return True
    End Function

    ' -----------------------------
    ' Boom Logic
    ' -----------------------------
    Function ContainsSeven(num As Integer) As Boolean
        Return num.ToString().Contains("7"c)
    End Function

    Function IsBoom(num As Integer) As Boolean
        Return (num Mod 7 = 0) OrElse ContainsSeven(num)
    End Function

End Module



' run:
'
' Enter a number from which you want to start the game (-1 to exit): 6
' Enter a number (Or Boom): 6
' Enter a number (Or Boom): Boom
' Enter a number (Or Boom): 8
' Enter a number (Or Boom): 9
' Enter a number (Or Boom): 10
' Enter a number (Or Boom): 11
' Enter a number (Or Boom): 12
' Enter a number (Or Boom): 13
' Enter a number (Or Boom): Boom
' Enter a number (Or Boom): 15
' Enter a number (Or Boom): 16
' Enter a number (Or Boom): Boom
' Enter a number (Or Boom): 18
' Enter a number (Or Boom): 19
' Enter a number (Or Boom): 20
' Enter a number (Or Boom): 21
' 21 (should be Boom)
' YOU LOSE!
'

 



answered Mar 14 by avibootz
edited Mar 14 by avibootz
...