Jump to content

Deck of Cards


spudw2k
 Share

Recommended Posts

 

Was bored so I started this script to be a starting basis for a card game. My shuffle code doesn't work as well as I had hoped. any community suggestions on how to improve?

Spoiler

Old code, broken since Array UDF rework in AutoIt 3.x.x.x - too lazy to look up

#include <Array.au3>
$newdeck = _ShuffleDeck(_InitializeDeck())

While 1
    $newhand = _DrawCards($newdeck,5)
    _ArrayDisplay($newhand,"Five Cards Drawn")
WEnd

Func _InitializeDeck()
    Dim $deck[52]
    $card = 1
    $suit = 0
    For $x = 0 to 51
        $deck[$x] = $card & "|" & $suit
        $suit += 1
        If $suit >= 4 Then
            $suit = 0
            $card += 1
        EndIf
    Next
    Return $deck
EndFunc

Func _ShuffleDeck($deck,$degree = 1024)
    SRandom(Random(1,64))
    For $x = 1 to $degree
        _ArraySwap($deck[Random(0,51,1)],$deck[Random(0,51,1)])
    Next
    Return $deck
EndFunc

Func _DrawCards(ByRef $deck,$count)
    If Ubound($deck) <= $count Then $deck = _ShuffleDeck(_InitializeDeck())
    Dim $cards[$count]
    For $i = 0 to $count-1
        $cards[$i] = $deck[UBound($deck)-1]
        _ArrayPop($deck)
    Next
    Return $cards
EndFunc

Func _CutDeck($deck,$count=0)
    If $count <= 0 Or $count >= 51 Then $count = Random(13,39,1)
    For $i = 1 to $count
        _ArrayPush($deck,$deck[51],1)
    Next
    Return $deck
EndFunc

 

 

I tried increase the $degree and stacking the shuffle func

_ShuffleDeck(_ShuffleDeck(_InitializeDeck()))

but it doesn't seem to have a great effect.

 

Adding SRandom(Random(0,64)) to the first line of the shuffle func improves it. *added to code.

 

I removed the card value translation. Now the card value is accurate and the suit is zero-base indexed. That way I can still do actual calculation against the values. I'll translate the values to faces in the game (when one's made).

 

edit: fixed broken artifacts from forum upgrades

 

edit: reworking the code for a card game I created and am working on an AutoIt version to work out the game design kinks.

#Region - Includes and Globals
#include <Array.au3>
;Deck of Cards Variables
Global Const $aCardFaces[] = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]
Global Const $aCardSuits[] = ["Diamonds", "Clubs", "Hearts", "Spades"]
Global Const $sJokerCard = "Joker"
Global Const $eCardPropertyFaceDown = 10
Global $aPlayerHand[0]
#EndRegion - Includes and Globals


#Region - Test Code
ConsoleWrite("New Deck Order w/ Jokers" & @CRLF & "================================" & @CRLF)
$aDeck = _NewDeck()
For $iCard In $aDeck
    ConsoleWrite(_GetCardName($iCard) & @CRLF)
Next
ConsoleWrite("================================" & @CRLF & @CRLF)
ConsoleWrite("Shuffled Deck after removing Jokers" & @CRLF & "================================" & @CRLF)
_RemoveCard($aDeck, UBound($aDeck)-1)
_RemoveCard($aDeck, UBound($aDeck)-1)
_ShuffleDeck($aDeck)
_CutDeck($aDeck)
For $iCard In $aDeck
    ConsoleWrite(_GetCardName($iCard) & @CRLF)
Next
ConsoleWrite("================================" & @CRLF)
#EndRegion - Test Code


#Region - Deck of Cards Core Functions
Func _CutDeck(ByRef $aDeck, $iCount = 0)
    If $iCount <= 0 Or $iCount >= UBound($aDeck) - 1 Then $iCount = Random(13, 39, 1)
    For $iX = 1 To $iCount
        _ArrayPush($aDeck, $aDeck[0], 0)
    Next
EndFunc   ;==>_CutDeck

Func _DrawCard(ByRef $aDeck, ByRef $aPlayerHand)
    Local $iDeckCardCount = UBound($aDeck)
    If $iDeckCardCount = 0 Then Return SetError(1, 0, -1)
    Local $iDrawnCard = $aDeck[0]
    _ArrayDelete($aDeck, 0)
    _ArrayAdd($aPlayerHand, $iDrawnCard)
EndFunc   ;==>_DrawCard

Func _GetCardName($dCard)
    If StringRight($dCard, 1) = "F" Then Return $sJokerCard
    Local $iProperty = Dec(StringLeft($dCard, 1))
    Local $iSuit = Mod($iProperty, $eCardPropertyFaceDown)
    Local $iValue = Dec(StringRight($dCard, 1))
    Return $aCardFaces[$iValue] & " of " & $aCardSuits[$iSuit]
EndFunc   ;==>_GetCardName

Func _NewDeck($iJokers = 2)
    $iJokers = Int($iJokers)
    If $iJokers <= 0 Then $iJokers = 0

    Local $iCardFaceCount = UBound($aCardFaces)
    Local $iCardSuitCount = UBound($aCardSuits)
    Local $iCardCount = $iCardFaceCount * $iCardSuitCount + $iJokers
    Local $aDeck[$iCardCount]
    Local $iSuitIndex = 0
    Local $iFaceIndex = 0
    Local $iCardIndex = 0
    Local $iReverseFaceOrder = 0
    Do
        $aDeck[$iCardIndex] = Hex($iSuitIndex + $eCardPropertyFaceDown, 1) & Hex($iFaceIndex, 1)

        If Not $iReverseFaceOrder Then
            $iFaceIndex += 1
            If $iFaceIndex > $iCardFaceCount - 1 Then
                $iFaceIndex = 0
                $iSuitIndex += 1
                If $iSuitIndex = 2 And Not $iReverseFaceOrder Then
                    $iReverseFaceOrder = 1
                    $iFaceIndex = 12
                EndIf
            EndIf
        Else
            $iFaceIndex -= 1
            If $iFaceIndex < 0 Then
                $iFaceIndex = 12
                $iSuitIndex += 1
            EndIf
        EndIf

        $iCardIndex += 1
        If $iCardIndex > $iCardCount - 1 - $iJokers Then
            Do
                $aDeck[$iCardIndex] = "0F"
                $iCardIndex += 1
            Until $iCardIndex > $iCardCount - 1
        EndIf
    Until $iCardIndex > $iCardCount - 1

    Return $aDeck
EndFunc   ;==>_NewDeck

Func _PlayCard(ByRef $aPlayerHand, $iCardIndex)
    Local $iHandCardCount = UBound($aPlayerHand)
    If $iHandCardCount = 0 Then Return SetError(1, 0, -1)
    If $iCardIndex >= $iHandCardCount Or $iCardIndex < 0 Then Return SetError(2, 0, -1)
    Local $iPlayedCard = $aPlayerHand[$iCardIndex]
    _ArrayDelete($aPlayerHand, $iCardIndex)
    Return $iPlayedCard
EndFunc   ;==>_PlayCard

Func _RemoveCard(ByRef $aDeck, $iCardIndex)
    Local $iCard = $aDeck[$iCardIndex]
    _ArrayDelete($aDeck, $iCardIndex)
    Return $iCard
EndFunc   ;==>_RemoveCard

Func _ShuffleDeck(ByRef $aDeck, $iTimes = 10)
    Local $iCardCount = UBound($aDeck) - 1
    For $iRound = 1 To $iTimes
        For $iX = 0 To $iCardCount
            _ArraySwap($aDeck, Random($iX, $iCardCount, 1), $iX)
        Next
        For $iX = $iCardCount To 1 Step -1
            _ArraySwap($aDeck, Random(0, $iX, 1), $iX)
        Next
        For $iX = 0 To $iCardCount
            Local $iJ = Int(Random($iX, $iCardCount-1)) + 1
            Local $iPrev = $aDeck[$iJ]
            For $iK = $iJ To 1 + $iX Step -1
                $aDeck[$iK] = $aDeck[$iK - 1]
            Next
            $aDeck[$iX] = $iPrev
        Next
    Next
EndFunc   ;==>_ShuffleDeck
#EndRegion - Deck of Cards Core Functions

 

Spoiler

Arms & Artifices

#Region - Includes and Globals
#include <Array.au3>
;Deck of Cards Variables
Global Const $aCardFaces[] = ["Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"]
Global Const $aCardSuits[] = ["Diamonds", "Clubs", "Hearts", "Spades"]
Global Const $sJokerCard = "Joker"
Global Const $eCardPropertyFaceDown = 10


;Game Rules  / Mechanics Variables
Global Const $aCallForSupportText[] = ["2) Cowardly Retreat - Choose an enemy's Warrior and flip them face down.  Sacrifice any attached Defender cards to the discard pile.", _
        "3) Transposition - Swap two of your Warriors positions, leaving Defenders attached.  If you do not have two revealed Warriors on the field, do nothing.", _
        "4) Serve your Enemy - Draw one card and add it to your enemy's weakest field position, including unoccupied spaces.", _
        "5) Power Up - Draw one card and immediately add it to your weakest field position.", _
        "6) Traitor - The enemy must hand over a card of their choice from their hand that you will immediately put into play where you see fit.  If the enemy's hand is empty, they must sacrifice a card of their choice from their field.", _
        "7) Call for Backup - Draw one card and add it as a Defender to any revealed Warrior.", _
        "8) Sabotage - Remove any enemy's card from the field and discard it.  They immediately draw a new card and replace the removed card with either the drawn card, or a card from their hand.", _
        "9) Tactical Strike - Collect all your cards on the field, shuffle them and deal them all face down at will, then reveal them all.  Do not include any cards from your hand in the shuffle or deal.", _
        "10) Assassination - The enemy's strongest card on the field is immediatley sacrificed, including Defenders and Jokers.  Only one card should be discarded."]
Global Const $iWinningScore = 10
Global Enum $eCoinHeads, $eCoinTails
Global Enum $eWarriorPosition
Global Enum $ePlayer1Hand, $ePlayer2Hand
Global Enum $ePlayerFieldPosition1, $ePlayerFieldPosition2, $ePlayerFieldPosition3
Global Enum $ePlayingFieldDiscardPile, $ePlayingFieldDeck, $ePlayingFieldPlayer1, $ePlayingFieldPlayer2
Global $aEmptyArray[0]
Global $aPlayerScores[0]
Global $aPlayerHands[0]
Global $aPlayerField[0]
Global $aPlayingField[0]
#EndRegion - Includes and Globals


#Region - Test Code
_InitializePlayingField()
_InitializePlayerScores()
_InitializePlayerHands()
;~ SRandom(@MSEC * @SEC * @MDAY)

;==========Deck of Cards setup==============
$aDeck = _NewDeck()
_ShuffleDeck($aDeck)
_CutDeck($aDeck)
;~ _ArrayDisplay($aDeck)
;===========================================

;==========Display Cards (names) in the Deck==============
;~ For $iCard In $aDeck
;~  ConsoleWrite(_GetCardName($iCard) & @CRLF)
;~ Next
;=========================================================

$iPlayer = 1

;============================Misc Game Play Mechanics=================================
Msgbox(0,"Battle Phase Ready?",_IsPlayerBattlePhaseReady($iPlayer))

For $iY = 1 to 3
    For $iX = 0 To 2
        _DrawCard($aDeck, $aPlayerHands[$iPlayer - 1])
        _PlayerFieldPositionPlaceCard($iPlayer, $iX, _PlayCard($aPlayerHands[$iPlayer - 1], 0))
    Next
Next

Msgbox(0,"Battle Phase Ready?",_IsPlayerBattlePhaseReady($iPlayer))

Local $aCards = _PlayerFieldPositionGetCards($iPlayer, $ePlayerFieldPosition1)
For $dCard In $aCards
    ConsoleWrite(_GetCardName($dCard) & @CRLF)
Next
ConsoleWrite(_PlayerFieldPositionTally($iPlayer, $ePlayerFieldPosition1) & @CRLF)

Local $sCards = ""
For $dCard In ($aPlayingField[$iPlayer + 1])[$ePlayerFieldPosition1]
    $sCards &= _GetCardName($dCard) & @CRLF
Next
MsgBox(0, "", $sCards & _PlayerFieldPositionTally($iPlayer, 0))
;=====================================================================================


;~ Msgbox(0,"Coin Toss", _CoinToss($eCoinHeads))

;~ For $iX = 1 to 10
;~  Local $iD = _DiceRoll(4)
;~  $iD += _DiceRoll(6)
;~  ConsoleWrite($iD & @CRLF)
;~ Next

;==========Display Call for Support Texts based on Dice Roll==============
;~ For $iX = 1 To 5
;~  ConsoleWrite($aCallForSupportText[_CallForSupport() - 2] & @CRLF)
;~  Sleep(1000)
;~ Next
;=========================================================================
#EndRegion - Test Code


#Region - Game Mechanics
Func _CallForSupport()
    Local $iSupportRoll = _DiceRoll(4)
    $iSupportRoll += _DiceRoll(6)
    Return $iSupportRoll
EndFunc   ;==>_CallForSupport

Func _CoinToss($iPrediction)
    If $iPrediction <> $eCoinHeads And $iPrediction <> $eCoinTails Then Return SetError(1, 0, -1)
    Return ($iPrediction = Random(0, 1, 1)) ? True : False
EndFunc   ;==>_CoinToss

Func _DiceRoll($iDieSize)
    Sleep(Random(0, 200))
    Return Random(1, $iDieSize, 1)
EndFunc   ;==>_DiceRoll

Func _FlipCard(ByRef $dCard)
    Local $iProperty = Dec(StringLeft($dCard, 1))
    Local $iSuit = Mod($iProperty, $eCardPropertyFaceDown)
    Local $iValue = StringRight($dCard, 1)
    If _IsCardFaceDown($dCard) Then
        $iProperty = $iSuit
    Else
        $iProperty = Hex($iSuit + $eCardPropertyFaceDown, 1)
    EndIf
    $dCard = $iProperty & $iValue
EndFunc   ;==>_FlipCard

Func _InitializePlayerHands()
    ReDim $aPlayerHands[0]
    ReDim $aPlayerHands[2]
    $aPlayerHands[$ePlayer1Hand] = $aEmptyArray
    $aPlayerHands[$ePlayer2Hand] = $aEmptyArray
EndFunc

Func _InitializePlayerScores()
    ReDim $aPlayerScores[0]
    ReDim $aPlayerScores[2]
EndFunc

Func _InitializePlayingField()
    ReDim $aPlayerField[0]
    ReDim $aPlayerField[3]
    $aPlayerField[$ePlayerFieldPosition1] = $aEmptyArray
    $aPlayerField[$ePlayerFieldPosition2] = $aEmptyArray
    $aPlayerField[$ePlayerFieldPosition3] = $aEmptyArray
    ReDim $aPlayingField[0]
    ReDim $aPlayingField[4]
    $aPlayingField[$ePlayingFieldDiscardPile] = $aEmptyArray
    $aPlayingField[$ePlayingFieldDeck] = $aEmptyArray
    $aPlayingField[$ePlayingFieldPlayer1] = $aPlayerField
    $aPlayingField[$ePlayingFieldPlayer2] = $aPlayerField
EndFunc

Func _IsPlayerBattlePhaseReady($iPlayer)
    $iPlayer = Int($iPlayer)
    If $iPlayer < 1 Or $iPlayer > 2 Then Return SetError(1, 0, -1)
    Return (UBound(($aPlayingField[$iPlayer + 1])[$ePlayerFieldPosition1]) = 0 Or _
            UBound(($aPlayingField[$iPlayer + 1])[$ePlayerFieldPosition2]) = 0 Or _
            UBound(($aPlayingField[$iPlayer + 1])[$ePlayerFieldPosition3]) = 0) ? False : True
EndFunc   ;==>_IsPlayerBattlePhaseReady

Func _IsCardFaceDown(ByRef $dCard)
    Local $bFaceDown = BitAND(Dec(StringLeft($dCard, 1)), $eCardPropertyFaceDown)
    Return ($bFaceDown) ? True : False
EndFunc   ;==>_IsCardFaceDown

Func _PlayerFieldPositionChangeCard($iPlayer, $iPlayerFieldPosition, $iCardIndex, $dNewCard)
    $iPlayer = Int($iPlayer)
    If $iPlayer < 1 Or $iPlayer > 2 Then Return SetError(1, 0, -1)
    Local $aPlayerFields = $aPlayingField[$iPlayer + 1]
    Local $aFieldPosition = $aPlayerFields[$iPlayerFieldPosition]
    $aFieldPosition[$iCardIndex] = $dNewCard
    $aPlayerFields[$iPlayerFieldPosition] = $aFieldPosition
    $aPlayingField[$iPlayer + 1] = $aPlayerFields
EndFunc   ;==>_PlayerFieldPositionChangeCard

Func _PlayerFieldPositionGetCards($iPlayer, $iPlayerFieldPosition)
    Return ($aPlayingField[$iPlayer + 1])[$iPlayerFieldPosition]
EndFunc   ;==>_PlayerFieldPositionGetCards

Func _PlayerFieldPositionPlaceCard($iPlayer, $iPlayerFieldPosition, $dCard)
    $iPlayer = Int($iPlayer)
    If $iPlayer < 1 Or $iPlayer > 2 Then Return SetError(1, 0, -1)
    Local $aPlayerFields = $aPlayingField[$iPlayer + 1]
    Local $aFieldPosition = $aPlayerFields[$iPlayerFieldPosition]
    _ArrayAdd($aFieldPosition, $dCard)
    $aPlayerFields[$iPlayerFieldPosition] = $aFieldPosition
    $aPlayingField[$iPlayer + 1] = $aPlayerFields
EndFunc   ;==>_PlayerFieldPositionPlaceCard

Func _PlayerFieldPositionRemoveCard($iPlayer, $iPlayerFieldPosition, $iCardIndex)
    $iPlayer = Int($iPlayer)
    If $iPlayer < 1 Or $iPlayer > 2 Then Return SetError(1, 0, -1)
    Local $aPlayerFields = $aPlayingField[$iPlayer + 1]
    Local $aFieldPosition = $aPlayerFields[$iPlayerFieldPosition]
    Local $dCard = $aFieldPosition[$iCardIndex]
    _ArrayDelete($aFieldPosition, $iCardIndex)
    $aPlayerFields[$iPlayerFieldPosition] = $aFieldPosition
    $aPlayingField[$iPlayer + 1] = $aPlayerFields
    Return $dCard
EndFunc   ;==>_PlayerFieldPositionRemoveCard

Func _PlayerFieldPositionTally($iPlayer, $iPlayerFieldPosition)
    $iPlayer = Int($iPlayer)
    If $iPlayer < 1 Or $iPlayer > 2 Then Return SetError(1, 0, -1)
    Local $iTally = 0
    Local $aFieldPosition = ($aPlayingField[$iPlayer + 1])[$iPlayerFieldPosition]
    For $dCard In $aFieldPosition
        $iTally += Dec(StringRight($dCard, 1)) + 1
    Next
    Return $iTally
EndFunc   ;==>_PlayerFieldPositionTally
#EndRegion - Game Mechanics


#Region - Deck of Cards Core Functions
Func _CutDeck(ByRef $aDeck, $iCount = 0)
    If $iCount <= 0 Or $iCount >= UBound($aDeck) - 1 Then $iCount = Random(1, UBound($aDeck) - 1, 1)
    For $iX = 1 To $iCount
        _ArrayPush($aDeck, $aDeck[0], 0)
    Next
    Return Null
EndFunc   ;==>_CutDeck

Func _DrawCard(ByRef $aDeck, ByRef $aPlayerHand)
    Local $iDeckCardCount = UBound($aDeck)
    If $iDeckCardCount = 0 Then Return SetError(1, 0, -1)
    Local $iDrawnCard = $aDeck[0]
    _ArrayDelete($aDeck, 0)
    _ArrayAdd($aPlayerHand, $iDrawnCard)
    Return Null
EndFunc   ;==>_DrawCard

Func _GetCardName($dCard)
    If StringRight($dCard, 1) = "F" Then Return $sJokerCard
    Local $iProperty = Dec(StringLeft($dCard, 1))
    Local $iSuit = Mod($iProperty, $eCardPropertyFaceDown)
    Local $iValue = Dec(StringRight($dCard, 1))
    Return $aCardFaces[$iValue] & " of " & $aCardSuits[$iSuit]
EndFunc   ;==>_GetCardName

Func _NewDeck($bJokers = True)
    Local $iJokers = 0
    If $bJokers = True Then $iJokers = 2

    Local $iCardFaceCount = UBound($aCardFaces)
    Local $iCardSuitCount = UBound($aCardSuits)
    Local $iCardCount = $iCardFaceCount * $iCardSuitCount + $iJokers
    Local $aDeck[$iCardCount]
    Local $iSuitIndex = 0
    Local $iFaceIndex = 0
    Local $iCardIndex = 0
    Local $iReverseFaceOrder = 0
    Do
        $aDeck[$iCardIndex] = Hex($iSuitIndex + $eCardPropertyFaceDown, 1) & Hex($iFaceIndex, 1)

        If Not $iReverseFaceOrder Then
            $iFaceIndex += 1
            If $iFaceIndex > $iCardFaceCount - 1 Then
                $iFaceIndex = 0
                $iSuitIndex += 1
                If $iSuitIndex = 2 And Not $iReverseFaceOrder Then
                    $iReverseFaceOrder = 1
                    $iFaceIndex = 12
                EndIf
            EndIf
        Else
            $iFaceIndex -= 1
            If $iFaceIndex < 0 Then
                $iFaceIndex = 12
                $iSuitIndex += 1
            EndIf
        EndIf

        $iCardIndex += 1
        If $iCardIndex > $iCardCount - 1 - $iJokers Then
            Do
                $aDeck[$iCardIndex] = "0F"
                $iCardIndex += 1
            Until $iCardIndex > $iCardCount - 1
        EndIf
    Until $iCardIndex > $iCardCount - 1

    Return $aDeck
EndFunc   ;==>_NewDeck

Func _PlayCard(ByRef $aPlayerHand, $iCardIndex)
    Local $iHandCardCount = UBound($aPlayerHand)
    If $iHandCardCount = 0 Then Return SetError(1, 0, -1)
    If $iCardIndex >= $iHandCardCount Or $iCardIndex < 0 Then Return SetError(2, 0, -1)
    Local $iPlayedCard = $aPlayerHand[$iCardIndex]
    _ArrayDelete($aPlayerHand, $iCardIndex)
    Return $iPlayedCard
EndFunc   ;==>_PlayCard

Func _ShuffleDeck(ByRef $aDeck, $iTimes = 10)
    Local $iCardCount = UBound($aDeck) - 1
    For $iRound = 1 To $iTimes
        For $iX = 0 To $iCardCount
            _ArraySwap($aDeck, Random($iX, $iCardCount, 1), $iX)
        Next
        For $iX = $iCardCount To 1 Step -1
            _ArraySwap($aDeck, Random(0, $iX, 1), $iX)
        Next
        For $iX = 0 To $iCardCount
            Local $iJ = Int(Random($iX, $iCardCount - 1)) + 1
            Local $iPrev = $aDeck[$iJ]
            For $iK = $iJ To 1 + $iX Step -1
                $aDeck[$iK] = $aDeck[$iK - 1]
            Next
            $aDeck[$iX] = $iPrev
        Next
    Next
EndFunc   ;==>_ShuffleDeck
#EndRegion - Deck of Cards Core Functions

 

 

Edited by spudw2k
Link to comment
Share on other sites

You look at the code in my script?

Texas Holdem Trainer

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

You look at the code in my script?

Texas Holdem Trainer

I'm having difficulty finding a "deck" in your script. It appears to just select random cards from what I see.

Very nice looking script though. :)

Edited by spudw2k
Link to comment
Share on other sites

I'm having difficulty finding a "deck" in your script. It appears to just select random cards from what I see.

The hundred number is the suit and the number that follows is the card 124 would be A of spades 112 is the 2 of spades - they match with the gif file of the card file name.

edit added the following function

Func _NewCard() ; taken from another program on texas holdem - credit Rizzet
    Local $Card, $Card1, $Card2
    Do
        $Card1 = Random(1, 4, 1) * 100
        $Card2 = Random(12, 24, 1)
        $Card = $Card1 + $Card2
        _ArraySearch($iCard, $Card)
        For $i = 1 To 5
            If @error = $i Then
                MsgBox(1, "error", "error = " & $i)
                ExitLoop
            EndIf
        Next
    Until @error = 6
    If $TESTING Then
        Return 112
    EndIf
    Return $Card
EndFunc   ;==>_NewCard
Edited by nitekram

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Yes, I understand the whole picture thing. Let me rephrase. From what I can tell, you are picking random cards and putting them in an array rather than starting with an entire deck of cards and drawing from it. Is that correct?

Link to comment
Share on other sites

Yes, I understand the whole picture thing. Let me rephrase. From what I can tell, you are picking random cards and putting them in an array rather than starting with an entire deck of cards and drawing from it. Is that correct?

I reread your code and now I understand your question. You are creating a deck of cards and then using a shuffle funtion and then drawing the cards from there to create your array. I never thought of doing it that way, and I wonder if you get the same type of randomness with my way - yours seem better, may I try and use the same idea for my script?

I of course am doing what you thought. Creating an array from random numbers to create my dealt cards.

All by me:

"Sometimes you have to go back to where you started, to get to where you want to go." 

"Everybody catches up with everyone, eventually" 

"As you teach others, you are really teaching yourself."

From my dad

"Do not worry about yesterday, as the only thing that you can control is tomorrow."

 

WindowsError.gif

WIKI | Tabs; | Arrays; | Strings | Wiki Arrays | How to ask a Question | Forum Search | FAQ | Tutorials | Original FAQ | ONLINE HELP | UDF's Wiki | AutoIt PDF

AutoIt Snippets | Multple Guis | Interrupting a running function | Another Send

StringRegExp | StringRegExp Help | RegEXTester | REG TUTOR | Reg TUTOT 2

AutoItSetOption | Macros | AutoIt Snippets | Wrapper | Autoit  Docs

SCITE | SciteJump | BB | MyTopics | Programming | UDFs | AutoIt 123 | UDFs Form | UDF

Learning to script | Tutorials | Documentation | IE.AU3 | Games? | FreeSoftware | Path_Online | Core Language

Programming Tips

Excel Changes

ControlHover.UDF

GDI_Plus

Draw_On_Screen

GDI Basics

GDI_More_Basics

GDI Rotate

GDI Graph

GDI  CheckExistingItems

GDI Trajectory

Replace $ghGDIPDll with $__g_hGDIPDll

DLL 101?

Array via Object

GDI Swimlane

GDI Plus French 101 Site

GDI Examples UEZ

GDI Basic Clock

GDI Detection

Ternary operator

Link to comment
Share on other sites

Here's what I thought of for my own card type game I thought of:

Func _ShuffleDeck($deck)    
    For $x = 0 To 50 ; Not To 51, as Random(51, 51, 1) would give an error
        $select = Random($x, 51, 1)
        $temp = $deck[$select]
        $deck[$select] = $deck[$x]
        $deck[$x] = $temp
    Next
    For $x = 51 To 1 Step -1 ; Not To 0, as Random(0, 0, 1) would give an error (it'd be 0, which is what we want, but it's still an error)
        $select = Random(0,$x,1)
        $temp = $deck[$select]
        $deck[$select] = $deck[$x]
        $deck[$x] = $temp
    Next
    Return $deck
EndFunc

Worked pretty well for my tastes, see how you like it.

Link to comment
Share on other sites

Worked pretty well for my tastes, see how you like it.

That's pretty good. Thanks for the input. I adapted your method to mine.

Func _ShuffleDeck($deck)
    SRandom(Random(1,128))
    For $x = 0 to 50
        _ArraySwap($deck[Random($x,51,1)],$deck[Random(0,51-$x,1)])
    Next
    Return $deck
EndFunc
Edited by spudw2k
Link to comment
Share on other sites

*Another Function Added. _CutDeck.

Func _CutDeck($deck,$count=0)
    If $count <= 0 Or $count >= 51 Then $count = Random(13,39,1)
    For $i = 1 to $count
        _ArrayPush($deck,$deck[51],1)
    Next
    Return $deck
EndFunc

I'm rethinking the _InitializeDeck function. Ideally i'd like these basic functions to be able to work with any game that uses standard playing cards. As such, not all games use a complete deck and some games use multiple decks. I'll post when I get something laid out.

Edited by spudw2k
Link to comment
Share on other sites

edit: Latest code on POST #20

Updated code. Here's all the latest code. I also changed the _InitializeDeck() func so I can build decks that are not "standard" (52 cards).

I also adjusted the _DrawCards Func to draw from the top of the deck, not the bottom (_ArrayReverse). and Made the shuffle so it is dynamic based on the deck.

edit: Fixed shuffle method based on SkinnyWhiteGuy's recommendations.

edit: Latest code on POST #20

Edited by spudw2k
Link to comment
Share on other sites

Not to burst your bubble, but I did the _ShuffleDeck() function the way I did for a reason. By picking 2 random numbers to be swapped, you end up with more cards left in the same position as before, unshuffled/untouched, whereas if you pick a card at random, move it to the end/beginning, and do it again for the remaining cards, you end up with a more randomized deck. In my tests, after shuffling once, your method resulted in around 16% of the cards staying exactly where they were before being shuffled, while the method I show above only results in around 1%. Now, not knowing how realistic you want the card simulations being, I just thought you'd like to know this information.

Link to comment
Share on other sites

I needed to make a similar code for my bot.

Here is a simple code, It will create a deck with 40 cards, Shuffle, and the deal them 1 by 1 without using the same card twice.

#include <Array.au3>

Global $cards[41] ; 0 Will store the index
_Init_cards()
_ArrayDisplay($cards, "New Deck")
_Shuffle_Deck()
_ArrayDisplay($cards, "Shuffled")
For $x = 1 To 40
    ConsoleWrite(@CRLF & "Deal card #: " & $x & "  " & _deal_card())
Next

Func _Shuffle_Deck()
    Local $Sec_Deck[41], $dice ;The sec_deck is used to shuffle and reshuffle, and the $dice will be a random card
    $Sec_Deck[0] = $cards[0]
    For $i = 1 To 40
        If $cards[0] > 1 Then
            $Dice = Random(1, $cards[0], 1) ; Random a dice
        Else
            $Dice = 1
        EndIf
        $Sec_Deck[$i] = $cards[$dice] ; Attrib a random card to the second array
        For $Swap = $Dice To $cards[0]-1 ;+ 1
            $cards[$Swap] = $cards[$Swap + 1] ; array swap
        Next
        $cards[0] -= 1
    Next
    ;_ArrayDisplay($Sec_Deck,"sec")
    ;now do the same from sec_deck to original deck
    $cards[0] = 0
    For $i = 1 To 40
        If $Sec_Deck[0] > 1 Then
            $Dice = Random(1, $Sec_Deck[0], 1) ; Random a dice
        Else
            $Dice = 1
        EndIf
        $cards[$i] = $Sec_Deck[$dice] ; Attrib a random card to the second array
        For $Swap = $Dice To $Sec_Deck[0]-1 ;+ 1
            $Sec_Deck[$Swap] = $Sec_Deck[$Swap + 1] ; array swap
        Next
        $Sec_Deck[0] -= 1
        $cards[0] +=1 
    Next
EndFunc

Func _deal_card()
    If $cards[0] < 1 Then 
        Return "No cards left"
    Else
        $cards[0] -= 1
        Return $cards[$cards[0]+1]
    EndIf
EndFunc
Func _Init_cards()
    Local $add = 1, $Deck
    $cards[0] = 40
    For $_temp = 0 To 3
        If $_temp = 0 Then $Deck = " s";Spades
        If $_temp = 1 Then $Deck = " h";Hearts
        If $_temp = 2 Then $Deck = " c";Clubs
        If $_temp = 3 Then $Deck = " d";diamonds
        $cards[$add] = "A" & $Deck
        $add += 1
        For $value = 2 To 7
            $cards[$add] = $value & $Deck
            $add += 1
        Next
        $cards[$add] = "J" & $Deck
        $add += 1
        $cards[$add] = "Q" & $Deck
        $add += 1
        $cards[$add] = "K" & $Deck
        $add += 1
    Next
EndFunc   ;==>_Init_cards
Edited by Linux
You can help! Donate to AutoIt! or, visit ClimatePREDICTION.netMy posts:Travian Bot Example (100+ servers) BETAHow to Host you code/app for free! (unlimited team number) (Public or Private)"Sir, we're surrounded!" "Excellent. We can attack in any direction!"
Link to comment
Share on other sites

Look at BlackJack

There is used standard Windows cards.dll for drawing of cards.

Very cool, and useful. I'll prob stick to a no dll scenario if possible....although since this is only for Windows, they (user) should have the card.dll. :)

I needed to make a similar code for my bot.

Here is a simple code, .....

Pretty interesting.

Not to burst your bubble, but I did the _ShuffleDeck() function the way I did for a reason. By picking 2 random numbers to be swapped, you end up with more cards left in the same position as before, unshuffled/untouched, whereas if you pick a card at random, move it to the end/beginning, and do it again for the remaining cards, you end up with a more randomized deck. In my tests, after shuffling once, your method resulted in around 16% of the cards staying exactly where they were before being shuffled, while the method I show above only results in around 1%. Now, not knowing how realistic you want the card simulations being, I just thought you'd like to know this information.

Very useful. I'll do some tests. No bubble to burst friend. I never claimed this to be great code. Just fun. >_<

thanks SKW. I changed the shuffle after comparing the shuffles. Your method works better and I now see why. Thanks. :idiot:

Edited by spudw2k
Link to comment
Share on other sites

I've thought of the next set of functions to write.

_PlayCards()

_Deal()

_SortHand()

edit: I removed the SplitDeck func. I can just deal the deck instead.

edit: Removed Discard func. Same thing as PlayCards essentially.

...There is an edit button and you can reply to several posts in one...

noted. Edited by spudw2k
Link to comment
Share on other sites

SkinnyWhiteGuy

Any idea why running the shuffle func on a deck that has been shuffled doesn't seem to do anything? Maybe how I'm testing is wrong.

edit: Doh! I figured it out. It was returnng the same array twice. Duh. :) Thanks for checking it out.

Edited by spudw2k
Link to comment
Share on other sites

Latest Code edit: removed AddToHand and CombineDecks. Can just use _ArrayConcatenate()

edit: Moved code to latest post.

Func _AddToHand($hand,$cards)

Return _ArrayConcatenate($hand,$cards)

EndFunc

Func _CombineDecks($deck1,$deck2)

Return _ArrayConcatenate($deck1,$deck2)

EndFunc

edit: The shuffle func still isn't the best. I find that deck repeats shuffle patterns. I increase the range of the Ransom Seed which helps but still doesn't prevent duplicate shuffle patterns.

edit: I was looking at nitekram's Texas Holdem Trainer script after a recent update and I noticed that he left a comment in his code reffering to some deck methods I just happen to have created here; also remembering him mention implementing it, I decided to give it a go. I only had to make two slightly-significant (How's that for an oxymoron) changes.

  • Change card structure to meet requirements of nitekram's script. (suit then face, no ~)
  • Alter his _NewCard function to Return _DrawCard instead
Otherwise, just the declare a Global var for the Deck and Customize the deck as appropriate. Edited by spudw2k
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...