在VB.NET中编写洗牌一副牌的方法
我有一副标准的52张牌,用数组表示。每张卡都表示为一个整数。我编写了下面的函数来洗牌。下面的代码看起来正常吗在VB.NET中编写洗牌一副牌的方法,vb.net,Vb.net,我有一副标准的52张牌,用数组表示。每张卡都表示为一个整数。我编写了下面的函数来洗牌。下面的代码看起来正常吗 Module Module3 Sub Main() ' initialize array Dim Cards(52) As Integer ' Unit Test ' Pass array as argument. Console.WriteLine(shuffle(Cards)) End Sub Function shuffle(ByV
Module Module3
Sub Main()
' initialize array
Dim Cards(52) As Integer
' Unit Test
' Pass array as argument.
Console.WriteLine(shuffle(Cards))
End Sub
Function shuffle(ByVal Cards() As Integer)
Dim counter = 1
Dim rand = New Random()
For Each card In Cards
' Grab random number with range of 52
Dim n = rand.Next(52)
' Pick a card
Dim temp = Cards(counter)
' Swap picked card with random card
Cards(counter) = Cards(n)
Cards(n) = temp
counter += 1
Next
Return (Cards)
End Function
End Module
不,代码没有按你说的做
Dim Cards(52) As Integer
这将为53张卡片创建一个数组,而不是52张。使用:
Dim Cards(51) As Integer
洗牌时,将每张牌与牌组中较早的一张牌(或其本身)交换,而不是在牌组中的任何地方。(这就是费希尔·耶茨洗牌的原理。)
不要让计数器与循环分开,而是使用循环计数器
Dim rand = New Random()
For counter = 0 to Cards.Length - 1
Dim n = rand.Next(counter + 1)
Dim temp = Cards(counter)
Cards(counter) = Cards(n)
Cards(n) = temp
Next
看看杰夫·阿特伍德关于这个主题的博客文章
如果这将用于某些游戏,我不会使用数组,我会使用列表,因为可以轻松添加/删除项目
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确认没有任何变化。当您感到满意时,按按钮2,然后按按钮1和3
当你亲自打牌时,很明显牌组并没有不断地被洗牌,但如果是呢?它会改变什么吗?如果我在网上打牌,我是否知道或关心牌组是否经常被洗牌
代码只是为了证明一点,跳出框框思考,不应被视为成品
编辑:
很可能会影响其中一些。这现在正式称为“布里格斯”洗牌
Module module1
Dim cards(51) As String
Dim trues(51) As Boolean
Dim number, Truecheck As Integer
Dim stores, loopy As String
Sub main()
number = 1
cards(0) = "Ace of Spades"
cards(10) = "Jack of Spades"
cards(11) = "Queen of Spades"
cards(12) = "King of Spades"
cards(13) = "Ace of Clubs"
cards(23) = "Jack of Clubs"
cards(24) = "Queen of Clubs"
cards(25) = "King of Clubs"
cards(26) = "Aec of Diamonds"
cards(36) = "Jack of Diamods"
cards(37) = "Queen of Diamonds"
cards(38) = "King of Diamonds"
cards(39) = "Ace of Hearts"
cards(49) = "Jack of Heats"
cards(50) = "Queen of Hearts"
cards(51) = "King of Hearts"
For i = 1 To 9
number = number + 1
cards(i) = number.ToString + " of Spades"
Next
number = 1
For i = 14 To 22
number = number + 1
cards(i) = number.ToString + " of Clubs"
Next
number = 1
For i = 27 To 35
number = number + 1
cards(i) = number.ToString + " of Diamonds"
Next
number = 1
For i = 40 To 48
number = number + 1
cards(i) = number.ToString + " of Hearts"
Next
For i = 0 To 51
Console.WriteLine(cards(i))
Next
Console.WriteLine("")
Console.WriteLine("")
For i = 0 To 51
linetrue:
Randomize()
stores = cards(i)
Truecheck = Int(Rnd() * 51)
If trues(Truecheck) = True Then GoTo linetrue
trues(i) = True
cards(i) = cards(Truecheck)
cards(Truecheck) = stores
Console.WriteLine(cards(i))
Next
End Sub
End Module
可能的副本@布鲁诺-考虑使用一个列表。请参阅我提供的代码。我想知道Intels Bull Mountain体系结构将如何影响所有这些?什么将终止shuffleBkg方法中的无休止循环?应用程序将结束。正如我所说,这是为了证明一点。
Module module1
Dim cards(51) As String
Dim trues(51) As Boolean
Dim number, Truecheck As Integer
Dim stores, loopy As String
Sub main()
number = 1
cards(0) = "Ace of Spades"
cards(10) = "Jack of Spades"
cards(11) = "Queen of Spades"
cards(12) = "King of Spades"
cards(13) = "Ace of Clubs"
cards(23) = "Jack of Clubs"
cards(24) = "Queen of Clubs"
cards(25) = "King of Clubs"
cards(26) = "Aec of Diamonds"
cards(36) = "Jack of Diamods"
cards(37) = "Queen of Diamonds"
cards(38) = "King of Diamonds"
cards(39) = "Ace of Hearts"
cards(49) = "Jack of Heats"
cards(50) = "Queen of Hearts"
cards(51) = "King of Hearts"
For i = 1 To 9
number = number + 1
cards(i) = number.ToString + " of Spades"
Next
number = 1
For i = 14 To 22
number = number + 1
cards(i) = number.ToString + " of Clubs"
Next
number = 1
For i = 27 To 35
number = number + 1
cards(i) = number.ToString + " of Diamonds"
Next
number = 1
For i = 40 To 48
number = number + 1
cards(i) = number.ToString + " of Hearts"
Next
For i = 0 To 51
Console.WriteLine(cards(i))
Next
Console.WriteLine("")
Console.WriteLine("")
For i = 0 To 51
linetrue:
Randomize()
stores = cards(i)
Truecheck = Int(Rnd() * 51)
If trues(Truecheck) = True Then GoTo linetrue
trues(i) = True
cards(i) = cards(Truecheck)
cards(Truecheck) = stores
Console.WriteLine(cards(i))
Next
End Sub
End Module