PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Poker hand ideas?-VBForums
Results 1 to 7 of 7

Thread: Poker hand ideas?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    393

    Poker hand ideas?

    First let me say that I am not a student.
    I have working code.
    I would like to know how you would code the card shuffle, and hand evaluation for five card stud with one hand. It seems as though others have problems and a lot of code.
    If this is of interest to you, I will gladly post my code.
    I am doing this for the fun and challenge. I am not very concerned about the finished product.
    Thanks for reading
    George

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,760

    Re: Poker hand ideas?

    Shuffling is easy. Assuming that you have collection containing your card objects, you can just do something like this:
    vb.net Code:
    1. Dim rng As New Random
    2.  
    3. cardList = cardList.OrderBy(Function(c) rng.NextDouble()).ToList()
    As for evaluating the hands, there's really no magic to it. You just have to test for each type of hand individually and how you test depends on exactly what you're testing for, e.g. for a flush you could get the suit of the first card and compare that to the suits of the other cards to see whether they match.

    I would suggest that you ask a specific question and post the code relevant to that specific question.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,580

    Re: Poker hand ideas?

    That's a really good one-liner, but an overall inefficient shuffle and might even have a bias, since the same card won't consistently compare twice. The go-to shuffle in any person's toolbox should be Knuth-Fisher-Yates.

    But there's not exactly a cute method of hand evaluation, that part's right. You can do some clever things with equality operators but since you need many different contexts of comparison, it'd probably be better to avoid those and write your own comparison functions.

    So, for example, you could test a straight:
    Code:
    Public Function IsStraight(hand As Hand) As Boolean
        Dim sortedHand = hand.OrderByValue()
    
        Dim expectedValue = sortedHand.First().Value + 1
        For Each card In hand.Skip(1)
            If card.Value <> expectedValue Then Return False
            expectedValue += 1
        Next
    
        Return False
    End Function
    If you don't care about efficiency, you could do a Straight Flush like:
    Code:
    Public Function IsStraightFlush(hand As Hand) As Boolean
        Dim firstSuit = hand.First().Suit
        If IsStraight(hand) Then
            Return hand.All(Function(card) card.Suit = firstSuit)
        End If
    
        Return False
    End Function
    That assumes a Card type that has a value and a suit, and a Hand type that presents itself as an IEnumerable(Of Card). You could simplify it with convenience methods on Hand to sort by various facts, and/or provide information like what suits are in the hand. That'd make testing for, say, Full House easier.

    So Hand could:
    Code:
    Public Function GetValues() As IEnumerable(Of Integer)
        Return _innerCollection.Select(Function(card) card.Value).Distinct()
    End Function
    So then:
    Code:
    Function IsFullHouse(hand As Hand) As Boolean
        Dim values = hand.GetValues().ToArray()
    
        If values.Length <> 2 Then Return False
    
        Dim countValues = Function(hand, value)
                            Return hand.Where(Function(card) card.Value = value)
                        End Function
    
        Dim counts = { countValue(values(0)), countValue(values(1)) }
        
        Return (counts(0) = 2 AndAlso counts(1) = 3) OrElse (counts(0) = 3 AndAlso counts(1) = 2)
    End Function
    So it's not hard, but it's tedious, and there are lots of ways to be clever about it. I rejected 4 different ideas for IsFullHouse() that were still good.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  4. #4
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,217

    Re: Poker hand ideas?

    Hi,

    well we play in Germany every week 'Lotto'
    you have to pick 6 Numbers plus 1 (SuperZahl) to win the Jackpot.
    the numbers 1 - 49 you have to pick

    here a shuffle for the 6 Numbers and the Superzahl
    Code:
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim r As New Random
            Dim a(48) As Integer
            txtZahlen.Clear()
            txtSuperzahl.Clear()
            For i As Integer = 1 To 49
                a(i - 1) = i
            Next
            Dim b() As Integer = (From i As Integer In a Order By r.NextDouble Select i Take 7).ToArray
            For j = 0 To 5 Step 1
                txtZahlen.Text = txtZahlen.Text & " " & b(j).ToString
            Next
            txtSuperzahl.Text = b(6).ToString
        End Sub
    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,760

    Re: Poker hand ideas?

    Quote Originally Posted by Sitten Spynne View Post
    That's a really good one-liner, but an overall inefficient shuffle and might even have a bias, since the same card won't consistently compare twice.
    Actually, that's not true. This came up at Stack Overflow in the past and I did some testing and this method does generate just one random value per item. An issue can arise if you provide a Comparison(Of T) delegate or an IComparer(Of T) object to methods like Array.Sort or List(Of T).Sort because then you will almost certainly be generating different random values for the same item on different comparisons.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,760

    Re: Poker hand ideas?

    Quote Originally Posted by jmcilhinney View Post
    Actually, that's not true. This came up at Stack Overflow in the past and I did some testing and this method does generate just one random value per item.
    You can prove it to yourself using this code:
    vb.net Code:
    1. Module Module1
    2.  
    3.     Sub Main()
    4.         Dim numbers1 As New List(Of Integer) From {3, 7, 9, 4, 1, 6, 8, 2, 5, 0}
    5.         Dim numbers2 As New List(Of Integer) From {3, 7, 9, 4, 1, 6, 8, 2, 5, 0}
    6.         Dim rng1 As New RandomEx
    7.         Dim rng2 As New RandomEx
    8.  
    9.         numbers1 = numbers1.OrderBy(Function(n) rng1.NextDouble()).ToList()
    10.         numbers2.Sort(Function(n1, n2) rng2.NextDouble().CompareTo(rng2.NextDouble()))
    11.  
    12.         Console.WriteLine("Random numbers generated using OrderBy: " & rng1.GenerationCount)
    13.         Console.WriteLine("Random numbers generated using Sort: " & rng2.GenerationCount)
    14.  
    15.         Console.ReadLine()
    16.     End Sub
    17.  
    18. End Module
    19.  
    20.  
    21. Public Class RandomEx
    22.     Inherits Random
    23.  
    24.     Private _generationCount As Integer
    25.  
    26.     Public ReadOnly Property GenerationCount As Integer
    27.         Get
    28.             Return _generationCount
    29.         End Get
    30.     End Property
    31.  
    32.     Public Overrides Function NextDouble() As Double
    33.         _generationCount += 1
    34.  
    35.         Return MyBase.NextDouble()
    36.     End Function
    37.  
    38. End Class
    Run that code and you'll see that, using OrderBy, ten random numbers are generated every time, i.e. one for each item in the list. Using Sort, on the other hand, will generate a different number of random numbers each time. I ran it about six times and saw counts from 26 up to 40.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    393

    Re: Poker hand ideas?

    Here is my code
    Code:
    Option Explicit On
    Option Strict On
    Public Class Form1
        Private rnd As New Random
        Private watch As New Stopwatch
        Private deck As New List(Of Integer)
        Private newdeck As New List(Of Integer)
        Private hand As New List(Of Integer)
        Private locate As Integer
        Private flag As Integer
        Private occur(10) As Integer
    
        Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click
            For y = 1 To 4
                For x = 2 To 14
                    newdeck.Add(x * 10 + y)
                Next
            Next
            For y = 1 To 10
                flag = 0
                TextBox2.Text = ""
                TextBox1.Text = ""
                TextBox3.Text = ""
                deck.Clear()
                For x = 0 To newdeck.Count - 1
                    locate = rnd.Next(newdeck.Count)
                    deck.Add(newdeck(locate))
                    hand.Add(locate)
                    newdeck.RemoveAt(locate)
                Next
                newdeck.AddRange(deck)
                hand = deck.Take(5).ToList
                hand.Sort()
                For w = 0 To 4
                    TextBox1.Text += hand(w).ToString + ", "
                Next
                TextBox2.Text = ""
                For Each card As Integer In deck
                    TextBox2.Text += card.ToString + ", "
                Next
                For x = 0 To 8
                    TextBox3.Text += (x.ToString + ") " + occur(x).ToString + "   ")
                Next
                run_Click(sender, e)
            Next
        End Sub
    
        Private Sub run_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles run.Click
            Dim count As Integer = 0
            Dim hold As Integer = 0
            For x = 0 To 3
                If hand(x) \ 10 = hand(x + 1) \ 10 Then
                    count += 1
                    hold = x
                End If
            Next
            Select Case count
                Case 0
                    flush()
                    straight()
                    Select Case flag
                        Case 0
                            MsgBox("no pairs")
                            occur(0) += 1
                        Case 1
                            MsgBox("Flush")
                            occur(5) += 1
                        Case 2
                            MsgBox("straight")
                            occur(4) += 1
                        Case 3
                            MsgBox("straight flush")
                            occur(8) += 1
                    End Select
                Case 1
                    MsgBox("A pair")
                    occur(1) += 1
                Case 2
                    If hand(hold) \ 10 = hand(hold - 1) \ 10 Then
                        MsgBox("three of a kind")
                        occur(3) += 1
                    Else
                        MsgBox("two pair")
                        occur(2) += 1
                    End If
                Case 3
                    If hand(hold) \ 10 = hand(hold - 2) \ 10 Then
                        MsgBox("Four a kind")
                        occur(7) += 1
                    Else
                        MsgBox("full house")
                        occur(6) += 1
                    End If
            End Select
        End Sub
      
        Private Sub flush()
            For y = 0 To 3
                If hand(y) Mod 10 <> hand(y + 1) Mod 10 Then
                    Exit Sub
                End If
            Next
            flag = 1
        End Sub
    
        Private Sub straight()
            Select Case hand(4) \ 10
                Case 14
                    For x = 0 To 3
                        If hand(x) \ 10 <> x + 2 Then
                            Exit Sub
                        End If
                    Next
                    flag += 2
                Case Else
                    For x = 0 To 3
                        If ((hand(x) \ 10) + 1) <> (hand(x + 1) \ 10) Then
                            Exit Sub
                        End If
                    Next
                    flag += 2
            End Select
        End Sub
    End Class
    It is far from done, but it does a multiple shuffle, deal, evaluate the hand and record the number of each type of hand.
    Constructive criticism is welcome.
    Thanks to everyone who replied
    George

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width