RanXerox
Master
Posts: 550
Likes Given: 1
Likes Received: 12 in 9 posts
Joined: Dec 2010
Reputation: 19
|
RE: I need a more efficient function
I like that idea of having an array of card properties... Here is what I did instead:
Code:
[DEFNAME poker_card_properties]
//Cards Symbol,Suit,Rank,Name,Nickname
POKER.Card.As As,s,0,Ace of Spades,Ace of Death
POKER.Card.Ah Ah,h,0,Ace of Hearts
POKER.Card.Ad Ad,d,0,Ace of Diamonds
POKER.Card.Ac Ac,c,0,Ace of Clubs
POKER.Card.Ks Ks,s,1,King of Spades
POKER.Card.Kh Kh,h,1,King of Hearts
POKER.Card.Kd Kd,d,1,King of Diamonds,One Eyed King
POKER.Card.Kc Kc,c,1,King of Clubs
POKER.Card.Qs Qs,s,2,Queen of Spades
POKER.Card.Qh Qh,h,2,Queen of Hearts,Helen of Troy
POKER.Card.Qd Qd,d,2,Queen of Diamonds
POKER.Card.Qc Qc,c,2,Queen of Clubs
POKER.Card.Js Js,s,3,Jack of Spades
POKER.Card.Jh Jh,h,3,Jack of Hearts,One Eyed Jack of Hearts
POKER.Card.Jd Jd,d,3,Jack of Diamonds
POKER.Card.Jc Jc,c,3,Jack of Clubs
POKER.Card.Ts Ts,s,4,Ten of Spades
POKER.Card.Th Th,h,4,Ten of Hearts
POKER.Card.Td Td,d,4,Ten of Diamonds,Diamond Dime
POKER.Card.Tc Tc,c,4,Ten of Clubs
POKER.Card.9s 9s,s,5,Nine of Spades
POKER.Card.9h 9h,h,5,Nine of Hearts
POKER.Card.9d 9d,d,5,Nine of Diamonds,The Curse of Scotland
POKER.Card.9c 9c,c,5,Nine of Clubs
POKER.Card.8s 8s,s,6,Eight of Spades
POKER.Card.8h 8h,h,6,Eight of Hearts
POKER.Card.8d 8d,d,6,Eight of Diamonds
POKER.Card.8c 8c,c,6,Eight of Clubs
POKER.Card.7s 7s,s,7,Seven of Spades
POKER.Card.7h 7h,h,7,Seven of Hearts
POKER.Card.7d 7d,d,7,Seven of Diamonds
POKER.Card.7c 7c,c,7,Seven of Clubs,Clubbed Walking Stick
POKER.Card.6s 6s,s,8,Six of Spades
POKER.Card.6h 6h,h,8,Six of Hearts,Loyalty at the Risk of Death
POKER.Card.6d 6d,d,8,Six of Diamonds
POKER.Card.6c 6c,c,8,Six of Clubs
POKER.Card.5s 5s,s,9,Five of Spades,Nickle Spade
POKER.Card.5h 5h,h,9,Five of Hearts
POKER.Card.5d 5d,d,9,Five of Diamonds
POKER.Card.5c 5c,c,9,Five of Clubs
POKER.Card.4s 4s,s,10,Four of Spades
POKER.Card.4h 4h,h,10,Four of Hearts
POKER.Card.4d 4d,d,10,Four of Diamonds
POKER.Card.4c 4c,c,10,Four of Clubs
POKER.Card.3s 3s,s,11,Three of Spades
POKER.Card.3h 3h,h,11,Three of Hearts
POKER.Card.3d 3d,d,11,Three of Diamonds
POKER.Card.3c 3c,c,11,Three of Clubs
POKER.Card.2s 2s,s,12,Two of Spades
POKER.Card.2h 2h,h,12,Two of Hearts
POKER.Card.2d 2d,d,12,Two of Diamonds
POKER.Card.2c 2c,c,12,Two of Clubs
[FUNCTION f_Poker_RankCards]
//"
// Usage: f_Poker_RankCards CardA,CardB
//
// Compares CardA to CardB
//
// Returns: -1 if A is less than B
// Returns: 0 if A is equal to B
// Returns: 1 if A is greater than B
//"
IF (<ARGV>!=2)
// f_pokerlog 1,"Poker_RankCards: needs two cards as arguments"
RETURN 1
ENDIF
LOCAL.CardA.Rank=<getargv 2 <DEF.POKER.Card.<ARGV[0]>>>
LOCAL.CardB.Rank=<getargv 2 <DEF.POKER.Card.<ARGV[1]>>>
f_pokerlog 1,".CardA Rank=<dLOCAL.CardA.Rank> CardB Rank=<dLOCAL.CardB.Rank>"
IF (<LOCAL.CardA> > <LOCAL.CardB>)
// f_pokerlog 1,"A is ranked Greater than B"
RETURN 1
ELSEIF (<LOCAL.CardA> < <LOCAL.CardB>)
// f_pokerlog 1,"A is ranked Less than B"
RETURN -1
ELSE
// f_pokerlog 1,"A is equal to B"
RETURN 0
ENDIF
I alluded to a more complicated optimization.... here is a card sorting function (a horrifying CompSci 201 bubble sort that leverages the previous f_poker_RankCards function)... It works but its not ideal. Can anyone make me a quicksort or merge-sort version while I carry on with other functions?
Code:
[FUNCTION f_Poker_SortCards]
//"
// Usage: f_Poker_SortCards DeckUID
//
// Sorts a Global LIST object containing an array of abbreviated Poker card names.
//"
IF (<ARGV>==0)
f_pokerlog 1,"Poker_SortCards: needs a DeckUID"
RETURN 1
ENDIF
LOCAL.Input=<ARGS>
LOCAL.InputSize=<LIST.<LOCAL.Input>.COUNT>
LOCAL.Swapped=1
WHILE (<dLOCAL.Swapped>==1)
LOCAL.Swapped=0
FOR LoopNumber 1 <EVAL <LOCAL.InputSize>-1>
// f_pokerlog 2,"Loop <dLOCAL.LoopNumber>"
LOCAL.CardA="<STRSUB 1 2 <LIST.<LOCAL.Input>.<EVAL <LOCAL.LoopNumber>-1>>>"
LOCAL.CardB="<STRSUB 1 2 <LIST.<LOCAL.Input>.<dLOCAL.LoopNumber>>>"
// f_pokerlog 2,".Is <LOCAL.CardA> greater than <LOCAL.CardB>? STRCMP(<LOCAL.CardA>,<LOCAL.CardB>)"
IF (<f_Poker_RankCards <LOCAL.CardA>,<LOCAL.CardB>>==-1) //CardA is Less than CardB
// f_pokerlog 3,"..Yes, swap them"
LIST.<LOCAL.Input>.<EVAL <LOCAL.LoopNumber>-1>=<LOCAL.CardB>
LIST.<LOCAL.Input>.<dLOCAL.LoopNumber>=<LOCAL.CardA>
LOCAL.Swapped=1
ELSE
// f_pokerlog 3,"..No, skipping"
ENDIF
ENDFOR
LOCAL.InputSize=<EVAL <LOCAL.InputSize>-1>
ENDWHILE
f_pokerlog 1,"Poker_SortCards: Complete... size=<LIST.<LOCAL.Input>.COUNT>"
In order to make a new deck of cards (as a global LIST object containing an array of abbreviated Poker card names)... and then shuffle it... these functions will come in handy:
Code:
[DEFNAME poker_card_and_suit_properties]//Card and Suit Symbols:
POKER.Card.Symbols=A,K,Q,J,T,9,8,7,6,5,4,3,2
POKER.Suit.Symbols=s,h,d,c
[FUNCTION f_Poker_NewDeck]
//"
// Usage: f_Poker_NewDeck DeckUID
//
// Creates a Global LIST object named DeckUID and load it with a full deck of abbreviated Poker card names.
//"
IF (<ARGV>==0)
f_pokerlog 1,"Poker_NewDeck: needs a DeckUID"
RETURN 1
ENDIF
LOCAL.Input=<ARGS>
FOR Cards 0 <EVAL <f_sizeof <DEF.POKER.Card.Symbols>>-1>
FOR Suits 0 <EVAL <f_sizeof <DEF.POKER.Suit.Symbols>>-1>
LOCAL.TheCard=<getargv <dLOCAL.Cards> <DEF.POKER.Card.Symbols>><getargv <dLOCAL.Suits> <DEF.POKER.Suit.Symbols>>
LIST.<LOCAL.Input>.ADD <LOCAL.TheCard>
ENDFOR
ENDFOR
f_pokerlog 1,"Poker_NewDeck: size=<LIST.<LOCAL.Input>.COUNT>"
[FUNCTION f_Poker_Shuffle]
//"
// Usage: f_Poker_Shuffle DeckUID
//
// Shuffles a Global LIST object containing an array of abbreviated Poker card names.
//"
IF (<ARGV>==0)
f_pokerlog 1,"Poker_Shuffle: needs a DeckUID"
RETURN 1
ENDIF
LOCAL.Input=<ARGS>
FOR LoopNumber 0 <EVAL <LIST.<LOCAL.Input>.COUNT>-1>
LOCAL.Rand=<R0,<EVAL <LIST.<LOCAL.Input>.COUNT>-1>>
LOCAL.CardA=<STRSUB 1 2 <LIST.<LOCAL.Input>.<dLOCAL.LoopNumber>>>
LOCAL.CardB=<STRSUB 1 2 <LIST.<LOCAL.Input>.<dLOCAL.Rand>>>
LIST.<LOCAL.Input>.<dLOCAL.LoopNumber>=<LOCAL.CardB>
LIST.<LOCAL.Input>.<dLOCAL.Rand>=<LOCAL.CardA>
ENDFOR
f_pokerlog 1,"Poker_Shuffle: size=<LIST.<LOCAL.Input>.COUNT>"
As always... please point out suggestions for optimization!
(This post was last modified: 05-16-2012 07:09 AM by RanXerox.)
|
|