Imports System Imports System.IO Imports System.Text Module Program 'Below are global variables that will be used throughout the program in most subroutines.' Dim Score = 0 Dim Player1Total = 0 Dim Player2Total = 0 Dim RandomNumber As New Random Dim Player1Name As String Dim Player2Name As String Dim FinalResult As String Dim attempt = 0 Sub Main(args As String()) Console.ForegroundColor = ConsoleColor.Green Console.WriteLine("¡Welcome to blaise2g's Dice Game!") 'The welcome message that's diplayed to the players.' Console.WriteLine() 'Blank space.' Console.WriteLine("¡Press Enter to log in the Dice Game!") 'The login prompt.' Console.ReadKey() 'Reads user input.' Call Login() 'Sends player to the Login Subroutine.' For i = 1 To 5 'This loops the game for 5 rounds.' Console.WriteLine("Round " & i) 'Prints the current round onto the console.' Console.WriteLine() 'Blank space.' Console.WriteLine(Player1Name & " press Enter to roll your dice.") 'Commands Player 1 to roll their dice.' Console.ReadKey() 'Collects input from Player 1.' Call P1() 'This calls for the Player 1 Subroutine.' Console.WriteLine() 'Blank space.' Console.WriteLine(Player2Name & " press Enter to roll your dice.") 'Commands Player 2 to roll their dice.' Console.ReadKey() 'Collects input from Player 2.' Call P2() 'This calls for the Player 2 Subroutine.' Next i 'Below works out if there is a winner and prints the winner's name to the console.'my If Player1Total > Player2Total Then 'If Player 1's total is greater than Player 2's.' FinalResult = (Player1Name & "is the winner with " & Player1Total & " over " & Player2Name & "'s " & Player2Total) 'Message printed to the winner.' Console.WriteLine(FinalResult) 'Prints the winner message.' ElseIf Player1Total < Player2Total Then 'If Player 2's total is greater than Player 1's.' FinalResult = (Player2Name & " is the winner with " & Player2Total & " over " & Player1Name & "'s " & Player1Total) 'Message printed to the winner.' Console.WriteLine(FinalResult) 'If Player 2's total is greater than Player 1's.' Else Call Tie() 'If both players have the same score then the Tiebreaker is called.' End If Console.WriteLine() Console.WriteLine("Thanks for playing!") 'Appreciation from myself' Console.WriteLine("Press Enter to close...") 'Commands the player to close the window once the game is finished.' Console.ReadLine() 'Reads the input'. End Sub Sub Login() 'Allows players to enter names.' Console.WriteLine("Enter name for Player 1") 'Commands Player 1 to enter their name.' Player1Name = Console.ReadLine() 'Collects name of Player 1.' Console.WriteLine("Enter name for Player 2") 'Commands Player 2 to enter their name.' Player2Name = Console.ReadLine() 'Collects name of Player 2.' 'Below checks if the password is correct and loops until valid.' Console.WriteLine("Enter the password") 'Tells the user to enter the password.' Console.ForegroundColor = ConsoleColor.Black 'Hides the password.' Dim LoginPassword As String 'Casts the password as a string.' LoginPassword = Console.ReadLine() 'Reads the password input.' Console.ForegroundColor = ConsoleColor.Green 'Sets the foreground green for the rest of the game.' Dim count As Integer count = count + 1 While LoginPassword <> "pass" 'The password.' Console.ForegroundColor = ConsoleColor.Green 'Sets the foreground green so the players can see the message below. ' Console.WriteLine("Retry Password") 'Printed if the password is incorrect.' Console.ForegroundColor = ConsoleColor.Black 'Hides the password again.' LoginPassword = Console.ReadLine 'Reads the password input.' Console.ForegroundColor = ConsoleColor.Green 'Sets the foreground green for the rest of the game.' attempt = attempt + 1 If attempt = 3 Then Call Anti() End If End While End Sub Sub P1() 'Below calls on the roll function which returns the score to add the total of Player 1.' Call Roll() 'Calls on the Roll Subroutine.' Player1Total = Player1Total + Score 'Adds the score onto the Player 1 total.' Console.WriteLine(Player1Name & "'s score is " & Score) 'Personalised message for the score of Player 1.' End Sub Sub P2() 'Below calls on the roll function which returns the score to add the total of Player 2.' Call Roll() 'Calls on the Roll Subroutine.' Player2Total = Player2Total + Score 'Adds the score onto the Player 2 total.' Console.WriteLine(Player2Name & "'s score is " & Score) 'Personalised message for the score of Player 2.' End Sub Function Roll() Dim Dice1 = RandomNumber.Next(1, 7) 'Chooses a random number.' Console.WriteLine("Dice 1 is " & Dice1) 'Prints the value of the dice.' Dim Dice2 = RandomNumber.Next(1, 7) 'Chooses a random number again.' Console.WriteLine("Dice 2 is " & Dice2) 'Prints the value of the dice.' Score = Dice1 + Dice2 'Adds the values of both die.' If Score Mod 2 = 1 Then 'If dice is odd then subtract 5 points.' Score = Score - 5 'Subtracts 5 points.' If Score < 0 Then 'If less than 0, reset to 0.' Score = 0 'Resets the score to 0.' End If Else Score = Score + 10 'Otherwise the value is even, so 10 points are added.' End If 'Below will add the score of another dice if both Dice 1 and Dice 2 are the same.' If Dice1 = Dice2 Then 'If both die are the same value.' Dim D3 = RandomNumber.Next(1, 7) 'Creates a third dice.' Console.WriteLine("Extra dice is " & D3) 'Prints the score from the third dice.' Score = Score + D3 'Adds the score from Dice 3 onto the main score.' End If Return Score 'The resultant score is sent back to the Player Subroutines.' End Function Sub Tie() 'Until there is a winner, below will keep adding a new dice per player.' While Player1Total = Player2Total 'If the total is the same from both players the tiebreaker is called.' Console.WriteLine("Tiebreaker!") 'Prints Tiebreaker to the console.' Dim Player1Tie = RandomNumber.Next(1, 7) 'Chooses a random number.' Console.WriteLine(Player1Name & "'s Dice is " & Player1Tie) 'Prints the value of the Value of Player 1's dice.' Dim Player2Tie = RandomNumber.Next(1, 7) 'Chooses a random number.' Console.WriteLine(Player2Name & "'s Dice is " & Player2Tie) 'Prints the value of the Value of Player 2's dice.' Player1Total = Player1Total + Player1Tie 'Adds scores to Player 1 total.' Player2Total = Player2Total + Player2Tie 'Adds scores to Player 2 total.' 'Below checks if there is a winner and outputs the scores.' If Player1Total > Player2Total Then 'If Player 1 is the winner.' Console.WriteLine(Player1Name & " is the winner with " & Player1Total & " over " & Player2Total) 'Prints if Player 1 is the winner.' ElseIf Player1Total < Player2Total Then 'If Player 2 is the winner.' Console.WriteLine(Player2Name & " is the winner with " & Player2Total & " over " & Player1Total) 'Prints if Player 2 is the winner.' Console.WriteLine() End If End While End Sub Sub Anti() Console.WriteLine("Too many password attempts, closing.") Console.WriteLine() End End Sub End Module
Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net
. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub