Public Class AceyDucey ''' ''' Create a single instance of the Random class to be used ''' throughout the program. ''' Private ReadOnly Property Rnd As New Random() ''' ''' Define a varaible to store the the player balance.
''' Defaults to 0 '''
''' ''' Since is a value type, and no value ''' has been explicitly set, the default value of the type is used. ''' Private _balance As Integer Public Sub New() DisplayIntroduction() End Sub ''' ''' Play multiple games of Acey Ducey until the player chooses to quit. ''' Public Sub Play() Do PlayGame() Loop While TryAgain() 'Loop (play again) based on the Boolean value returned by TryAgain Console.WriteLine("O.K., HOPE YOU HAD FUN!") End Sub ''' ''' Play a game of Acey Ducey, which ends when the player balance reaches 0 ''' Private Sub PlayGame() _balance = 100 'At the start of the game, set the player balance to 100 Console.WriteLine() Console.WriteLine($"YOU NOW HAVE {_balance} DOLLARS.") Do PlayTurn() Loop While _balance > 0 'Continue playing while the user has a balance Console.WriteLine() Console.WriteLine("SORRY, FRIEND, BUT YOU BLEW YOUR WAD.") End Sub ''' ''' Play one turn of Acey Ducey ''' ''' ''' A turn consists of displaying to cards, making a wager ''' and determining the result (win/lose) ''' Private Sub PlayTurn() Console.WriteLine() Console.WriteLine("HERE ARE YOUR NEXT TWO CARDS: ") Dim cards = GetOrderedCards() For Each card In cards DisplayCard(card) Next Dim wager As Integer = GetWager() Dim finalCard As Integer = GetCard() If wager = 0 Then Console.WriteLine("CHICKEN!!") Return End If DisplayCard(finalCard) Console.WriteLine() '''Check if the value of the final card is between the first and second cards. ''' '''The use of AndAlso is used to short-circuit the evaluation of the IF condition. '''Short-circuiting means that both sides of the condition do not need to be '''evaluated. In this case, if the left criteria returns FALSE, the right criteria '''is ignored and the evaluation result is returned as FALSE. ''' '''This works because AndAlso requires both condition to return TRUE in order to be '''evaluated as TRUE. If the first condition is FALSE we already know the evaluation result. If finalCard >= cards.First() AndAlso finalCard <= cards.Last() Then Console.WriteLine("YOU WIN!!!") _balance += wager 'Condensed version of _balance = _balance + wager Else Console.WriteLine("SORRY, YOU LOSE.") _balance -= wager 'Condensed version of _balance = _balance - wager End If End Sub ''' ''' Get two cards in ascending order ''' ''' ''' The original version generates two cards (A and B) ''' If A is greater than or equal to B, both cards are regenerated. '''

''' This version generates the two cards, but only regenerates A ''' if A is equal to B. The cards are then returned is ascending order, ''' ensuring that A is less than B (maintaining the original end result) '''
Private Function GetOrderedCards() As Integer() '''When declaring fixed size arrays in VB.NET you declare the MAX INDEX of the array '''and NOT the SIZE (number of elements) of the array. '''As such, card(1) gives you and array with index 0 and index 1, which means '''the array stores two elements and not one Dim cards(1) As Integer cards(0) = GetCard() cards(1) = GetCard() 'Perform this action as long as the first card is equal to the second card While cards(0) = cards(1) cards(0) = GetCard() End While Array.Sort(cards) 'Sort the values in ascending order Return cards End Function ''' ''' Get a random number (card) ranked 2 to 14 ''' Private Function GetCard() As Integer Return Rnd.Next(2, 15) End Function ''' ''' Display the face value of the card ''' Private Sub DisplayCard(card As Integer) Dim output As String Select Case card Case 2 To 10 output = card.ToString() Case 11 output = "JACK" Case 12 output = "QUEEN" Case 13 output = "KING" Case 14 output = "ACE" Case Else Throw New ArgumentOutOfRangeException(NameOf(card), "Value must be between 2 and 14") End Select Console.WriteLine(output) End Sub ''' ''' Prompt the user to make a bet ''' ''' ''' The function will not return until a valid bet is made.
''' is used to validate that the user input is a valid '''
Private Function GetWager() As Integer Dim wager As Integer Do Console.WriteLine() Console.Write("WHAT IS YOUR BET? ") Dim input As String = Console.ReadLine() '''Determine if the user input is an Integer '''If it is an Integer, store the value in the variable wager If Not Integer.TryParse(input, wager) Then Console.WriteLine("SORRY, I DID'T QUITE GET THAT.") Continue Do 'restart the loop End If 'Prevent the user from betting more than their current balance If _balance < wager Then Console.WriteLine("SORRY, MY FRIEND, BUT YOU BET TOO MUCH.") Console.WriteLine($"YOU HAVE ONLY {_balance} DOLLARS TO BET.") Continue Do 'restart the loop End If 'Prevent the user from betting negative values If wager < 0 Then Console.WriteLine("FUNNY GUY! YOU CANNOT MAKE A NEGATIVE BET.") Continue Do 'restart the loop End If Exit Do 'If we get to this line, exit the loop as all above validations passed Loop Return wager End Function ''' ''' Prompt the user to try again ''' ''' ''' This function will not return until a valid reponse is given ''' Private Function TryAgain() As Boolean Dim response As String Do Console.Write("TRY AGAIN (YES OR NO) ") response = Console.ReadLine() If response.Equals("YES", StringComparison.OrdinalIgnoreCase) Then Return True If response.Equals("NO", StringComparison.OrdinalIgnoreCase) Then Return False Console.WriteLine("SORRY, I DID'T QUITE GET THAT.") Loop End Function ''' ''' Display the opening title and instructions ''' ''' ''' Refer to ''' ''' Interpolated Strings ''' documentation for the use of $ and { } with strings ''' Private Sub DisplayIntroduction() Console.WriteLine($"{Space((Console.WindowWidth \ 2) - 10)}ACEY DUCEY CARD GAME") Console.WriteLine($"{Space((Console.WindowWidth \ 2) - 21)}CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") Console.WriteLine("") Console.WriteLine("") Console.WriteLine("ACEY-DUCEY IS PLAYED IN THE FOLLOWING MANNER") Console.WriteLine("THE DEALER (COMPUTER) DEALS TWO CARDS FACE UP") Console.WriteLine("YOU HAVE AN OPTION TO BET OR NOT BET DEPENDING") Console.WriteLine("ON WHETHER OR NOT YOU FEEL THE CARD WILL HAVE") Console.WriteLine("A VALUE BETWEEN THE FIRST TWO.") Console.WriteLine("IF YOU DO NOT WANT TO BET, INPUT A 0") Console.WriteLine("") End Sub End Class