如果这将用于某些游戏,我不会使用数组,我会使用列表,因为可以添加/删除轻松项目。
Module Module1
Dim deck As New List(Of Integer)
Dim prng As New Random
Sub Main()
newDeck()
showDeck()
shuffle()
showDeck()
Dim s As String
Do
s = Console.ReadLine()
Select Case s
Case "s"
showDeck()
Case "c"
If deck.Count = 0 Then
Console.WriteLine("No cards")
Else
'take top card
Dim foo As Integer = deck(0)
deck.RemoveAt(0)
Console.WriteLine(foo.ToString)
End If
Case "n"
newDeck()
shuffle()
End Select
Loop Until s.ToLower = "x"
End Sub
Sub newDeck()
deck = Enumerable.Range(1, 52).ToList 'create deck
End Sub
Sub shuffle()
deck = deck.OrderBy(Function(r) prng.Next).ToList
End Sub
Sub showDeck()
Dim ctr As Integer = 0
Console.WriteLine()
For Each card As Integer In deck
Console.Write("{0}", card.ToString.PadLeft(4, " "c))
ctr += 1
If ctr Mod 10 = 0 Then Console.WriteLine()
Next
Console.WriteLine()
End Sub
End Module
一段时间以来,我一直认为这是计算机不应该完全模仿现实世界的情况。首先,这是一个程序,它显示了知道随机数生成器的种子的问题。
'create a form with three buttons and a richtextbox
Dim deckIdx As New List(Of Integer)
Dim cards As New List(Of card)
Dim prng As New Random
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
newDeck()
shuffle()
showDeck()
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
'show next five cards
Dim sb As New System.Text.StringBuilder
sb.AppendLine()
For x As Integer = 1 To 5
Dim foo As card = getTopCard()
If Not foo Is Nothing Then sb.AppendFormat("{0}", foo.theCard.ToString.PadLeft(4, " "c))
Next
RichTextBox1.AppendText(sb.ToString)
RichTextBox1.ScrollToCaret()
End Sub
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
Button1.PerformClick()
End Sub
Class card
Public theCard As Integer
Public count As Integer
End Class
Sub newDeck()
prng = New Random(42) '<<<<<<<<<<<<<<<<<<<<<<<< OOPS!!!
deckIdx = Enumerable.Range(0, 51).ToList 'create deck indicies
cards = New List(Of card)
For Each cIDX As Integer In deckIdx
Dim foo As New card
foo.theCard = cIDX
foo.count = 0
cards.Add(foo)
Next
End Sub
Sub shuffle()
deckIdx = deckIdx.OrderBy(Function(r) prng.Next).ToList
End Sub
Function getTopCard() As card
If deckIdx.Count > 0 Then
Dim foo As New card
foo.theCard = cards(deckIdx(0)).theCard
foo.count = cards(deckIdx(0)).count
deckIdx.RemoveAt(0)
Return foo
Else
Return Nothing
End If
End Function
Sub showDeck()
Dim ctr As Integer = 0
Dim sb As New System.Text.StringBuilder
For Each card As Integer In deckIdx
sb.AppendFormat("{0}", cards(card).theCard.ToString.PadLeft(4, " "c))
ctr += 1
If ctr Mod 10 = 0 Then sb.AppendLine()
Next
RichTextBox1.Text = sb.ToString
End Sub
运行程序并反复按下按钮 1 / 3 将产生相同的结果,一遍又一遍。如果我们要坚持计算机必须完全模仿现实世界的想法,那么明显的解决方法是不要重新播种 PRNG。但是,如果我们采用不同的方法会怎样。
添加此代码
Dim bkgShuffle As Threading.Thread
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Button2.Enabled = False
bkgShuffle = New Threading.Thread(AddressOf shuffleBkg)
bkgShuffle.IsBackground = True
bkgShuffle.Start()
End Sub
Sub shuffleBkg()
Do
Threading.Thread.Sleep(50)
shuffle()
Loop
End Sub
像以前一样运行程序,按下按钮 1 和 3 以验证没有任何变化。满意后按按钮 2,然后按按钮 1 和 3。
当你亲自打牌时,很明显牌组并没有不断地被洗牌,但如果是这样呢?它会改变什么吗?如果我在网上打牌,我是否知道或关心牌组是否不断被洗牌?
代码只是为了说明一个观点,跳出框框思考,不应被视为成品。
编辑:
Bull Mountain 可能会影响其中的一部分。