excel - 扑克经销商逻辑

标签 excel vba poker

我想使用 Excel 作为我的扑克经销商。这是将生成 20 个介于 1 和 52 之间的随机数(卡片)的代码。前 20 个数字/卡片的输出在 A1:A20 列中。我希望在 A22:A41、第三个 A43:A62 等中生成下一组 20 个数字/卡片。如何修复代码,使其在 A 列中显示 1000 手牌,每组隔开一行?谢谢你。

Sub cards()

Range("A:A").Clear

cardstodraw = 20

For x = 1 To cardstodraw

begL:
    ActiveSheet.Cells(1, 2) = "=Randbetween(1,52)"
    ActiveSheet.Cells(x, 1) = ActiveSheet.Cells(1, 2).Text
    cardvalue = ActiveSheet.Cells(x, 1)
        y = 1
        Count = 0
        Do Until ActiveSheet.Cells(y, 1) = ""
            If ActiveSheet.Cells(y, 1) = cardvalue Then
            Count = Count + 1
            End If:  y = y + 1:  Loop
            If Count > 1 Then GoTo begL

    Next

    Range("B1").Clear


End Sub

最佳答案

您的代码有些复杂(使用 GoTo 通常表示可以改进某些东西)。要从 1-52 获取大小为 20 的样本,请使用修改后的 Fisher-Yates shuffle :

Option Explicit 'you really should be using this

Function deal(n As Long, k As Long) As Variant
    'returns an array of length k
    'consisting of k numbers in the range 1 to n

    Dim deck As Variant
    Dim i As Long, j As Long, temp As Long

    ReDim deck(1 To n)
    For i = 1 To n
        deck(i) = i
    Next i

    With Application.WorksheetFunction
        'do k steps of a Fisher-Yates shuffle on deck
        For i = 1 To .Min(k, n - 1)
            j = .RandBetween(i, n)
            If i < j Then 'swap
                temp = deck(i)
                deck(i) = deck(j)
                deck(j) = temp
            End If
        Next i
    End With

    ReDim Preserve deck(1 To k)
    deal = deck
End Function

如果您想在 A 列中有 1000 手牌:
Sub ManyHands()
    Dim i As Long
    With Application.WorksheetFunction
        For i = 1 To 1000
            Range(Cells(1 + 21 * (i - 1), 1), Cells(21 * i - 1, 1)).Value = .Transpose(deal(52, 20))
        Next i
    End With   
End Sub

在编辑 这是代码的修改版本,它向多个玩家发牌:
Function deal(n As Long, k As Long, players As Long) As Variant
    'returns an array with k rows and players columns
    'consisting of k*players numbers in range 1 to n
    'if players = 1, then the array is 1-dimensional
    'otherwise it is 2-dimensional

    Dim deck As Variant
    Dim i As Long, j As Long, temp As Long
    Dim hands As Variant

    ReDim deck(1 To n)
    For i = 1 To n
        deck(i) = i
    Next i

    With Application.WorksheetFunction
        'do k*players steps of a Fisher-Yates shuffle on deck
        For i = 1 To .Min(k * players, n - 1)
            j = .RandBetween(i, n)
            If i < j Then 'swap
                temp = deck(i)
                deck(i) = deck(j)
                deck(j) = temp
            End If
        Next i
    End With

    ReDim Preserve deck(1 To k * players)
    If players = 1 Then
        deal = deck
        Exit Function
    Else
        ReDim hands(1 To k, 1 To players)
        For i = 1 To k
            For j = 1 To players
                hands(i, j) = deck(players * (i - 1) + j)
            Next j
        Next i
    deal = hands
    End If
End Function

它可以像这样使用:
Sub ManyHands()
    Dim i As Long
    For i = 1 To 1000
        Range(Cells(1 + 11 * (i - 1), 1), Cells(11 * i - 1, 2)).Value = deal(52, 10, 2)
    Next i  
End Sub

关于excel - 扑克经销商逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52946802/

相关文章:

java - Java 中 ArrayList 扑克游戏的比较

java - 最简单的扑克手评估算法

php - preg_replace 多个实例在 1 行?

Excel 比较列表 2(当前)与列表 1(现有)的重复项和新项

excel - Excel 中的 Sigma 或求和函数

arrays - 从 VBA 字符串数组中获取第 n 个元素

vba - 检查值是否可迭代vba

vba - 为什么应用程序级别的 SelectionChange 事件不起作用?

python - 将 Excel 文件导出为 PDF 格式而不丢失超链接功能

excel - 从 Excel 任务 Pane 内的 Angular 应用程序读取 Office 加载项 list 文件的设置