Jump to content

Concept Check / Best Method for randomizing cards


Recommended Posts

I've seen some of these around before but I wanted to create something for myself (Just how I like to do things) but I wanted to check with others to make sure its 100% random.

 

To me it seems 100% random but I wanted some opinions. To test it you would need to remove the section for creating images or download the image pack below. Just make sure to keep the folder structure intact. I know there's probably a better way of doing the randomization by adding in a ArrayShuffle but is that really necessary with the Random's I have in there? Thanks!

 

Card Images Download

 

#include <Array.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

Dim $iPic[21]


Dim $Cards = ["52", "Ace of Spades", "Ace of Clubs", "Ace of Hearts", "Ace of Diamonds", "Two of Spades", "Two of Clubs", "Two of Hearts", "Two of Diamonds", "Three of Spades", "Three of Clubs", "Three of Hearts", "Three of Diamonds", _
        "Four of Spades", "Four of Clubs", "Four of Hearts", "Four of Diamonds", "Five of Spades", "Five of Clubs", "Five of Hearts", "Five of Diamonds", "Six of Spades", "Six of Clubs", "Six of Hearts", "Six of Diamonds", _
        "Seven of Spades", "Seven of Clubs", "Seven of Hearts", "Seven of Diamonds", "Eight of Spades", "Eight of Clubs", "Eight of Hearts", "Eight of Diamonds", "Nine of Spades", "Nine of Clubs", "Nine of Hearts", "Nine of Diamonds", _
        "Ten of Spades", "Ten of Clubs", "Ten of Hearts", "Ten of Diamonds", "Jack of Spades", "Jack of Clubs", "Jack of Hearts", "Jack of Diamonds", "Queen of Spades", "Queen of Clubs", "Queen of Hearts", "Queen of Diamonds", _
        "King of Spades", "King of Clubs", "King of Hearts", "King of Diamonds", "Joker"]


$CardWidth = 100
$CardHeight = 150

_CreateGui()

Func _CreateGui()
    $MainGUI = GUICreate("Display Cards", 600, 600)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit")
    GUISetState()

    $DealButton = GUICtrlCreateButton("New Hand", 200, 550, 200, 40)
    GUICtrlSetOnEvent(-1, "_DealNewHands")
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Player 1's Hand", 400, 40, 200, 40)
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Player 2's Hand", 400, 160, 200, 40)
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Player 3's Hand", 400, 280, 200, 40)
    GUICtrlSetFont(-1, 16)

    GUICtrlCreateLabel("Player 4's Hand", 400, 400, 200, 40)
    GUICtrlSetFont(-1, 16)

EndFunc   ;==>_CreateGui

Func _DealNewHands()

    Dim $Hand[5][6]
    Dim $PlayerHand[5]
    Dim $iPic[21]
    Local $HandCount = 0


    For $i = 1 To 20
        GUICtrlDelete($iPic[$i])
    Next


    Dim $Deck = ["54", "Ace of Spades", "Ace of Clubs", "Ace of Hearts", "Ace of Diamonds", "Two of Spades", "Two of Clubs", "Two of Hearts", "Two of Diamonds", "Three of Spades", "Three of Clubs", "Three of Hearts", "Three of Diamonds", _
            "Four of Spades", "Four of Clubs", "Four of Hearts", "Four of Diamonds", "Five of Spades", "Five of Clubs", "Five of Hearts", "Five of Diamonds", "Six of Spades", "Six of Clubs", "Six of Hearts", "Six of Diamonds", _
            "Seven of Spades", "Seven of Clubs", "Seven of Hearts", "Seven of Diamonds", "Eight of Spades", "Eight of Clubs", "Eight of Hearts", "Eight of Diamonds", "Nine of Spades", "Nine of Clubs", "Nine of Hearts", "Nine of Diamonds", _
            "Ten of Spades", "Ten of Clubs", "Ten of Hearts", "Ten of Diamonds", "Jack of Spades", "Jack of Clubs", "Jack of Hearts", "Jack of Diamonds", "Queen of Spades", "Queen of Clubs", "Queen of Hearts", "Queen of Diamonds", _
            "King of Spades", "King of Clubs", "King of Hearts", "King of Diamonds", "Joker", "Joker"]

    While $HandCount < 5

        Local $Count = 1
        While $Count < 6

            $RandomizeNumber = Random(1, 14, 1)
            $RandomizeSuit = Random(1, 4, 1)

            If $RandomizeNumber = 14 Then
                $Card = 53
            Else
                $Card = $RandomizeNumber * $RandomizeSuit
            EndIf

            $CardStatus = _ArraySearch($Deck, $Cards[$Card])
            If @error Then
                Sleep(10)
            Else
                $Hand[$HandCount][$Count] = $Cards[$Card]
                _ArrayDelete($Deck, $CardStatus)
                $Count += 1
            EndIf
        WEnd

        $HandCount += 1
    WEnd


    For $a = 1 To 4
        For $b = 1 To 5
            $PlayerHand[$a] = $PlayerHand[$a] & $Hand[$a][$b] & "-"
        Next
        $PlayerHand[$a] = StringTrimRight($PlayerHand[$a], 1)
    Next

    $ControlCount = 1
    $X = 10
    $Y = 10
    _GDIPlus_Startup()

    For $a = 1 To 4

        $DisplayHand = StringSplit($PlayerHand[$a], "-")

        For $i = 1 To 5


            $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "/Cards/" & $DisplayHand[$i] & ".png") ;<-- your image
            $hGDIBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
            _GDIPlus_ImageDispose($hImage)

            $iPic[$ControlCount] = GUICtrlCreatePic("", $X, $Y, $CardWidth, $CardHeight)

            _WinAPI_DeleteObject(GUICtrlSendMsg($iPic[$ControlCount], 0x0172, $IMAGE_BITMAP, $hGDIBitmap)) ;$STM_SETIMAGE = 0x0172
            _WinAPI_DeleteObject($hGDIBitmap)

            $X += 80
            $ControlCount += 1

        Next

        $X = 10
        $Y += 120

    Next

    _GDIPlus_Shutdown()


EndFunc   ;==>_DealNewHands

Func _Exit()
    Exit
EndFunc   ;==>_Exit

While 1
    Sleep(10)
WEnd

 

Edited by Damein

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

But is there any argument against what I did? Is it a truly random method?

MCR.jpg?t=1286371579

Most recent sig. I made

Quick Launcher W/ Profiles Topic Movie Database Topic & Website | LiveStreamer Pro Website | YouTube Stand-Alone Playlist Manager: Topic | Weather Desktop Widget: Topic | Flash Memory Game: Topic | Volume Control With Mouse / iTunes Hotkeys: Topic | Weather program: Topic | Paws & Tales radio drama podcast mini-player: Topic | Quick Math Calculations: Topic

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...