Guest User

Untitled

a guest
Oct 25th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.86 KB | None | 0 0
  1. Private Sub Dealbtn_Click(sender As Object, e As EventArgs) Handles Dealbtn.Click
  2. Dim Suits() As String = {"S", "D", "C", "H"}
  3. Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}
  4. Dim rand As New Random
  5. Dim rand1 As Integer = rand.Next(12)
  6. Dim rand2 As Integer = rand.Next(3)
  7. Label2.Text() = Faces(rand1) + Suits(rand2)
  8. End Sub
  9.  
  10. Dim Suits() As String = {"S", "D", "C", "H"}
  11. Dim Faces() As String = {"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}
  12.  
  13. Dim cards As New List(Of String)
  14. For Each s As String In Suits
  15. For Each f As String In Faces
  16. cards.Add(s & f)
  17. Next
  18. Next
  19.  
  20. Dim r As New Random
  21. Dim cardsShuffled = cards.OrderBy(Function() r.Next)
  22.  
  23. Dim deck As New Stack(Of String)(cardsShuffled)
  24. For Each lbl As Label in {Label1, Label2, Label3, ...} 'you need to write all
  25. Try
  26. lbl.Text = deck.Pop()
  27. Catch ex As InvalidOperationException
  28. MessageBox.Show("No more cards.")
  29. End Try
  30. Next
  31.  
  32. Dim rand As New Random
  33. Dim rand1 As Integer = rand.Next(12)
  34. Dim rand2 As Integer = rand.Next(3)
  35.  
  36. Public Class Card
  37. Public Property Suit As String
  38. Public Property Rank As Integer
  39.  
  40. ' card images from
  41. ' http://www.jfitz.com/cards/
  42. Public Property Img As Image
  43.  
  44. Private Faces() As String = {"Jack", "Queen", "King", "Ace"}
  45.  
  46. ' for text version of the game
  47. Public Function CardText() As String
  48. Dim tmp As String = Rank.ToString
  49. If Rank = 1 Then
  50. tmp = "Ace"
  51. ElseIf Rank >= 11 Then
  52. tmp = Faces(Rank - 11)
  53. End If
  54. Return String.Format("{0} of {1}", tmp, Suit)
  55.  
  56. End Function
  57.  
  58. ' iDeck class will assign Rank, Suit and img to an "empty" card
  59. Public Sub New(strSuit As String, nRank As Integer, i As Image)
  60. Suit = strSuit
  61. Rank = nRank
  62. Img = i
  63. End Sub
  64.  
  65. Public Overrides Function ToString() As String
  66. Return CardText()
  67. End Function
  68. End Class
  69.  
  70. Public Class Deck
  71. Dim rand As Random
  72.  
  73. ' the deck will be built in the same order a real deck comes in
  74. Private Suits() As String = {"Spades", "Diamonds", "Clubs", "Hearts"}
  75. Private Rank() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
  76.  
  77. ' useful for blackjack
  78. Private Const Ace As Integer = 1
  79.  
  80. ' freshly opened pack where they are in order. this is reused rather
  81. ' than building a new deck each time
  82. Private freshDeck As List(Of Card)
  83.  
  84. ' shuffled deck; Stack prevents any bugs from a botched counter var
  85. Private shoe As Stack(Of Card)
  86.  
  87. ' using an imagelist but My.Resources could work depending on card names
  88. Private imglist As ImageList
  89.  
  90. ' the GAME object passes us the imagelist holding the card pics
  91. Public Sub New(imgs As ImageList) ' ctor
  92. ' new random ONCE
  93. rand = New Random
  94. imglist = imgs
  95. NewDeck()
  96. End Sub
  97.  
  98. ' create a new deck (done ONCE) but could be called again
  99. Private Sub NewDeck()
  100. freshDeck = New List(Of Card) ' new object
  101.  
  102. For Each s As String In Suits
  103. For Each n As Integer In Rank
  104. Dim key As String = CardKey(s, n)
  105.  
  106. freshDeck.Add(New Card(s, n, imglist.Images(key)))
  107. Next
  108. Next
  109. End Sub
  110.  
  111. Private keys() As String = {"J", "Q", "K"}
  112.  
  113. Private Function CardKey(suit As String, rank As Integer) As String
  114. ' convert Suit / Key to the key used in the imglist
  115. ' (e.g C1.JPG for Clubs, Ace)
  116. ' cards come from http://www.jfitz.com/cards/
  117. ' use the windows set (or rename them all)
  118.  
  119. Dim key As String = suit.Substring(0, 1) ' => C, H, D, S
  120. If rank < 11 Then
  121. key &= rank.ToString
  122. Else
  123. key &= keys(rank - 11) ' cvt 11, 12, 13 => J, Q, K
  124. End If
  125.  
  126. Return key & ".png"
  127. End Function
  128.  
  129. ' Shuffle deck using Fisher-Yates; sub optimal here since we "use up"
  130. ' the shoe each hand and are not reshuffling a deck
  131. Public Sub Shuffle()
  132. ' new temp deck preserves the new deck starting point
  133. Dim thisDeck As New List(Of Card)(freshDeck.ToArray)
  134. Dim tmp As Card
  135.  
  136. Dim j As Integer
  137. ' hi to low, so the rand pick result is meaningful
  138. ' lo to hi introduces a definite bias
  139. For i As Integer = thisDeck.Count - 1 To 0 Step -1
  140. j = rand.Next(0, i + 1) ' NB max param is EXCLUSIVE
  141.  
  142. tmp = thisDeck(j)
  143. ' swap Card j and Card i
  144. thisDeck(j) = thisDeck(i)
  145. thisDeck(i) = tmp
  146. Next
  147.  
  148. ' using a stack for the actual deck in use; copy shuffled deck to the Shoe
  149. shoe = New Stack(Of Card)(thisDeck.ToArray)
  150.  
  151. End Sub
  152.  
  153. ' shuffle using random and LINQ (neo's answer)
  154. Public Sub ShuffleLinq()
  155. ' using the same rand per app run may be random enough
  156. ' but would not suffice for most 'serious' games or standards
  157. shoe = New Stack(Of Card)(freshDeck.OrderBy(Function() rand.Next))
  158.  
  159. End Sub
  160.  
  161. ' we run a clean game here, boy
  162. Public Sub DoubleShuffle()
  163. ' to try and avoid any bias, fill shoe as we go
  164. ' picking a random card from a randomized deck
  165. ' combination of the other two
  166.  
  167. Dim thisDeck As New List(Of Card)(freshDeck.OrderBy(Function() rand.Next).ToArray)
  168. Dim j As Integer
  169. shoe = New Stack(Of Card) ' new shoe of shuffled cards
  170.  
  171. ' backwards again
  172. For i As Integer = freshDeck.Count - 1 To 0 Step -1
  173. j = rand.Next(0, i + 1)
  174.  
  175. shoe.Push(thisDeck(j)) ' fill shoe
  176.  
  177. thisDeck.RemoveAt(j) ' remove from shuffled deck
  178. Next
  179.  
  180. End Sub
  181.  
  182. Public Function DealCard() As Card
  183. ' get a card
  184. If shoe.Count = 0 Then
  185. ' ToDo: out of cards
  186. ' happens with 9+ handed, 7 card games and many hi-lo games...
  187. ' usually mucked and burn cards are reshuffled
  188. ' some games use shared cards at the end
  189. ' (muck/burn list not implemented)
  190. End If
  191. Return shoe.Pop
  192. End Function
  193.  
  194. End Class
  195.  
  196. Private poker As Game
  197. ...
  198. New Game(theImgList, 3) ' 3 == the human player
  199.  
  200. poker.ShuffleDeck()
  201. poker.NewHand()
  202. thisRound = Game.Rounds.HoleCards
  203.  
  204. Select Case thisRound
  205. Case Game.Rounds.HoleCards
  206. poker.NewHand() ' clears the display etc
  207. poker.DealRound(thisRound) ' deal cards
  208. thisRound = Game.Rounds.Flop ' change round indicator
  209.  
  210. Case Game.Rounds.Flop ' even this could be internal to Game(poker)
  211. poker.DealRound(thisRound)
  212. thisRound = Game.Rounds.Turn
  213.  
  214. Case Rounds.Flop
  215. myDeck.DealCard() ' burn card
  216. players(0).AddCard(myDeck.DealCard) ' Player(0) is the house or community
  217. players(0).AddCard(myDeck.DealCard)
  218. players(0).AddCard(myDeck.DealCard)
Add Comment
Please, Sign In to add comment