我一直在开发基于视觉基础的二十一点游戏,但一直坚持一项特定的任务:
"创建游戏循环以播放单人游戏版本。游戏应以玩家破产或持有22岁以下的有效分数结束。"
我已经开始编写代码,直到那时用子程序管理" shuffling"堆栈的比喻牌,交易牌,检查玩家是否已经破坏,以及处理玩家转牌的牌。
(注释掉的部分是我在游戏循环中的尝试,它不起作用,因为我输入S或T它继续询问我是否想要坚持或扭曲一遍又一遍) 这是我的完整代码:
Module Module1
Sub Main()
'task 1
Dim deck() As String = {"AH", "2H", "3H", "4H", "5H", "6H", "7H", "8H",
"9H", "10H", "JH", "QH", "KH",
"AD", "2D", "3D", "4D", "5D", "6D", "7D", "8D", "9D", "10D", "JD", "QD", "KD",
"AS", "2S", "3S", "4S", "5S", "6S", "7S", "8S", "9S", "10S", "JS", "QS", "KS",
"AC", "2C", "3C", "4C", "5C", "6C", "7C", "8C", "9C", "10C", "JC", "QC", "KC"}
Dim hand As New List(Of String)
Dim cardPile As New Stack(Of String)
cardPile = Shuffle(deck)
Dealing(cardPile, hand)
Dealing(cardPile, hand)
Dim BustOrNot As String
BustOrNot = Left(Bust(hand), 2)
Dim total As Integer
total = Mid(Bust(hand), 3, Bust(hand).Length)
If total = 21 Then
BlackJack(total)
End If
Console.WriteLine("Your total so far is: " & total)
'Dim stickOrTwist As Boolean
'stickOrTwist = PlayerTurn(hand)
'While BustOrNot = "NB" Or stickOrTwist = False Or total = 21
'Dealing(cardPile, hand)
'BustOrNot = Left(Bust(hand), 2)
'stickOrTwist = PlayerTurn(hand)
'End While
'If stickOrTwist = False Then
'Win(total)
'ElseIf BustOrNot = "BB" Then
'Lose(total)
'ElseIf total = 21 Then
'BlackJack(total)
'End If
Console.ReadKey()
End Sub
Function Shuffle(ByVal deck As Array)
'task 2
Dim rand As New Random()
Dim card As String
Dim card2 As String
Dim indexToShuffle As Integer
Dim indexToShuffle2 As Integer
Dim cardStack As New Stack(Of String)
For i = 1 To 1000
indexToShuffle = rand.Next(0, 51)
card = deck(indexToShuffle)
indexToShuffle2 = rand.Next(0, 51)
card2 = deck(indexToShuffle2)
deck(indexToShuffle2) = card
deck(indexToShuffle) = card2
Next
For j = 0 To 51
cardStack.Push(deck(j))
Next
Return cardStack
End Function
Sub Dealing(ByRef cardPile As Stack(Of String), hand As List(Of String))
'task 3
hand.Add(cardPile.Pop)
End Sub
Function Bust(ByVal hand As List(Of String))
'task 4
Dim total As Integer = 0
For Each item In hand
If IsNumeric(Left(item, 1)) Then
total = total + Left(item, 1)
ElseIf Left(item, 1) = "A" Then
total = total + 11
ElseIf Left(item, 1) = "J" Or Left(item, 1) = "Q" Or Left(item, 1) = "K" Then
total = total + 10
End If
Next
If total > 21 Then
Return ("BB" & total)
Else
Return ("NB" & total)
End If
End Function
Function PlayerTurn(ByRef hand As List(Of String))
'task 5
Console.WriteLine("Would you like to stick or twist? (Input S or T)")
Dim stickOrTwist As String = UCase(Console.ReadLine())
If stickOrTwist = "S" Then
Return False
ElseIf stickOrTwist = "T" Then
Return True
Else
Console.WriteLine("That isn't a valid input. Try again")
Console.WriteLine("...")
PlayerTurn(hand)
End If
End Function
Sub Lose(ByVal total As Integer)
Console.WriteLine("Oh no! you lose.")
Console.WriteLine("Your final score was: " & total)
Console.ReadKey()
End Sub
Sub Win(ByVal total As Integer)
Console.WriteLine("Well done! your score is: " & total)
Console.ReadKey()
End Sub
Sub BlackJack(ByVal total As Integer)
Console.WriteLine("Well done you scored blackjack! Your score is: " & total)
Console.ReadKey()
End Sub
End Module