VBA扑克手

时间:2015-08-22 15:42:15

标签: excel vba excel-vba

我试图编写能产生4个随机扑克牌的代码 imaje http://www.wiseowl.co.uk/files/exercise-images/e1758/i3890.jpg
显然,这些值应该是唯一的。这是我已经尝试过的代码,但是我无法使其正常工作。

{{1}}

2 个答案:

答案 0 :(得分:2)

  1. private boolean prepareMediaRecorder(){ myCamera = getCameraInstance(); mediaRecorder = new MediaRecorder(); // store the quality profile required CamcorderProfile profile = CamcorderProfile.get(mCameraId, CamcorderProfile.QUALITY_HIGH); myCamera.unlock(); mediaRecorder.setCamera(myCamera); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setOutputFormat(profile.fileFormat); mediaRecorder.setVideoEncoder(profile.videoCodec); mediaRecorder.setVideoEncodingBitRate(profile.videoBitRate); mediaRecorder.setVideoFrameRate(profile.videoFrameRate); mediaRecorder.setVideoSize(profile.videoFrameWidth, profile.videoFrameHeight); mediaRecorder.setOutputFile("/sdcard/myvideo.mp4"); mediaRecorder.setMaxDuration(60000 * 20); // Set max duration 60 *20 sec. mediaRecorder.setMaxFileSize(5000000 * 4); // Set max file size 5M * 4 mediaRecorder.setPreviewDisplay(myCameraSurfaceView.getHolder().getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { releaseMediaRecorder(); return false; } catch (IOException e) { releaseMediaRecorder(); return false; } return true; } 更改为键入cs而不是Range来迭代单元格而不是单元格值。

    Variant
  2. 将以下行移动到循环中,以便为每个生成的卡运行。

    Dim cs As Range
    

    将其放在cs = c & "" & s

  3. 之前 顺便说一下,生成卡片的简单方法可能是这样的:

    Next cs

    或者,利用Unicode:

    Const SUITS As String = "CDHS"
    Const RANKS As String = "A23456789TJQK"
    
    Dim s As String, r As String
    s = Mid$(SUITS, Int(Math.Rnd *  4) + 1, 1)
    r = Mid$(RANKS, Int(Math.Rnd * 13) + 1, 1)
    

答案 1 :(得分:2)

这比OP要求的更多,但这里还有一些要看。结果类似于OP提供的图像,因为手被发送到工作表上[B2]开始的范围。

我正在使用Fisher–Yates shuffle

只需运行Deal()例程:

Public Sub Deal()
    Const PLAYERS = 6, CARDS = 5
    Dim i&, j&, k&, deck
    CreateAndShuffle deck
    ReDim hands(1 To CARDS, 1 To PLAYERS)
    For i = 1 To CARDS
        For j = 1 To PLAYERS
            k = k + 1
            hands(i, j) = deck(k)
        Next
    Next
    [b2].Resize(CARDS, PLAYERS) = hands
End Sub
Private Sub CreateAndShuffle(a)
    Dim i&, j&, k&, p&, suit
    ReDim a(1 To 52)
    suit = Array(ChrW$(9829), ChrW$(9830), ChrW$(9827), ChrW$(9824))
    Randomize
    For i = 1 To 13
        For j = 0 To 3
            k = k + 1
            p = Int((k - 1 + 1) * Rnd + 1)
            If j <> k Then a(k) = a(p)
            a(p) = Mid$("A234567890JQK", i, 1): If i = 10 Then a(p) = 10
            a(p) = a(p) & " " & suit(j)
        Next
    Next
End Sub