我的目标是什么?
我正在尝试编写一个程序来保持简单的双人游戏得分(52张牌,13个可能的名字中的4个,所以没有笑话)。杰克,女王,国王和王牌都是我的高牌,有了他们,两名球员中的一个可以得分。甲板必须洗牌(所以从卡列表中随机选择)。玩家1首先转动一张卡,然后转动玩家B,依此类推。一旦使用了卡,它就无法重复使用,所以一旦拉出52张牌,游戏就结束了。比赛得分如下:
游戏玩法示例:
到目前为止我有什么代码?
Public Class Form1
'Variable (make a list)
Dim cards As New List(Of String) From {"two", "two", "two", "two", "three", "three", "three", "three", "four", "four", "four", "four", "five", _
"five", "five", "five", "six", "six", "six", "six", "seven", "seven", "seven", "seven", "eight", "eight", _
"eight", "eight", "nine", "nine", "nine", "nine", "ten", "ten", "ten", "ten", "jack", "jack", "jack", _
"jack", "queen", "queen", "queen", "queen", "king", "king", "king", "king", "ace", "ace", "ace", "ace"}
'What Happens when button 1 is clicked
Private Sub btnPlayer1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlayer1.Click
Dim rnd = New Random() 'Set rnd as random generator
If cards.Count > 0 Then 'If all cards have been drawn then no more cards can be drawn
Dim randomcards = cards(rnd.Next(0, cards.Count)) 'Set randomcards as one randomly chosen (using rnd) card from cards list
lstbox1.Items.Add(randomcards) 'Add 1 randomly chosen card from cards list into lstbox1 each time btnPlayer1 is clicked
cards.Remove(randomcards) 'Remove the randomly generated card from cards list to reduce deck size. No card will be picked more than 4 times
End If
lstbox1.TopIndex = lstbox1.Items.Count - 1 'Automatically scroll down lstbox1 each time new item is added so players only see last played card (can still scroll up to see previous cards)
End Sub
'What happens when button 2 is clicked
Private Sub btnPlayer2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlayer2.Click
Dim rnd = New Random() 'Set rnd as random generator
If cards.Count > 0 Then 'If all cards have been drawn then no more cards can be drawn
Dim randomcards = cards(rnd.Next(0, cards.Count)) 'Set randomcards as one randomly chosen (using rnd) card from cards list
lstbox2.Items.Add(randomcards) 'Add 1 randomly chosen card from cards list into lstbox2 each time btnPlayer2 is clicked
cards.Remove(randomcards) 'Remove the randomly generated card from cards list to reduce deck size. No card will be picked more than 4 times
End If
lstbox2.TopIndex = lstbox2.Items.Count - 1 'Automatically scroll down lstbox2 each time new item is added so players only see last played card (can still scroll up to see previous cards)
End Sub
'Button to restart program
Private Sub btnRestart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRestart.Click
Application.Restart()
End Sub
'Button to close program
Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
End
End Sub
End Class
我有什么问题? (删掉了之前我能够解决的问题)
如何从两个列表框中检索卡片的数据/顺序(如果绘图顺序为A B A B等等),然后将我的评分规则应用于 卡订单? (除非它不是从列表框中我应该得到我的数据......?)
刚刚开始 对于您的信息,我刚刚开始编程三周前。我从VB开始,一旦我变得更好,我将继续使用Python。我真的想成为未来的游戏创造者,所以我所制作的所有程序都是这样的小游戏(现在非常简单)。现在我从一个游戏创意开始,绘制外观,设置我希望游戏遵循的规则,然后我编码,尊重我设置的规则。这是我迄今为止最具挑战性的比赛,而且我已经撞墙了。非常感谢帮助:)谢谢!
答案 0 :(得分:0)
我将通过评论更新此答案,以便稍后解释。现在,我在这里开始编写你的游戏。我使用控制台应用程序只是因为它是最快的开始。答案的关键在于Deck
类以及使用Queue(Of T)
确保每张卡片只发放一次这一事实。
Public Module Module1
Public Sub Main()
Dim PlayDeck As New Queue(Of Deck.Card)
Dim Deck As New Deck(True)
Deck.CardList.ForEach(Sub(x) PlayDeck.Enqueue(x))
Dim player1card As Deck.Card = PlayDeck.Dequeue()
Dim player2card As Deck.Card = PlayDeck.Dequeue()
Console.WriteLine(player1card.ToString())
Console.WriteLine(player2card.ToString())
Console.ReadLine()
End Sub
Public Class Deck
Public Structure Card
Public Property Suit As Suits
Public Property Value As CardValue
Public Enum Suits
Hearts = 0
Spades = 1
Clubs = 2
Diamonds = 3
End Enum
Public Enum CardValue
Two = 2
Three = 3
Four = 4
Five = 5
Six = 6
Seven = 7
Eight = 8
Nine = 9
Ten = 10
Jack = 11
Queen = 12
King = 13
Ace = 14
End Enum
Public Overrides Function ToString() As String
Return [Enum].GetName(GetType(CardValue), Me.Value) & " of " & [Enum].GetName(GetType(Suits), Me.Suit)
End Function
End Structure
Public CardList As List(Of Card)
Public Sub New()
CardList = New List(Of Card)
For Each suit As Card.Suits In [Enum].GetValues(GetType(Card.Suits))
For Each val As Card.CardValue In [Enum].GetValues(GetType(Card.CardValue))
CardList.Add(New Card With {.Suit = suit, .Value = val})
Next
Next
End Sub
Public Sub New(Shuffle As Boolean)
Me.New()
If Shuffle Then ShuffleCards()
End Sub
Public Sub ShuffleCards()
Dim csprng As New System.Security.Cryptography.RNGCryptoServiceProvider
Dim c As Integer = Me.CardList.Count
While c > 1
Dim b As Byte() = New Byte(0) {}
Do
csprng.GetBytes(b)
Loop While Not b(0) < c * (Byte.MaxValue / c)
Dim k As Integer = (b(0) Mod c)
c -= 1
Dim v As Card = Me.CardList(k)
Me.CardList(k) = Me.CardList(c)
Me.CardList(c) = v
End While
End Sub
End Class
End Module