0

I'm writing a playing card simulator of sorts, and everything works fine, but some Modules use a disgusting amount of variables. For Example:

randomHand[x_] := 
Module[{hand, kings, cards, suits, value, suitsK, valueK, cardList},
hand = PadLeft[IntegerDigits[RandomInteger[{1, 52}, x], 13]];
kings = Cases[hand, {___, 0}, 2];
cards = DeleteCases[hand, {___, 0}, 2];
suits = First /@ cards;
value = Last /@ cards;
suitsK = First /@ kings;
valueK = ReplaceAll[Last /@ kings, 0 -> "King"];
suits = suits /. {0 -> "Hearts", 1 -> "Diamonds", 2 -> "Clubs", 
 3 -> "Spades"};
suitsK = suitsK /. {1 -> "Hearts", 2 -> "Diamonds", 3 -> "Clubs", 
 4 -> "Spades"};
value = value /. {11 -> "Jack", 12 -> "Queen"};
cardList = Partition[Riffle[value, suits], 2];
Map[ToString[cardList[[#]][[1]]] <> " of " <> cardList[[#]][[2]] &, Range[Length[cardList]]]]

My Mathematica knowledge is not as fluent as I'd like it to be, so I'm wondering if it is bad to use so many variables, and if it is, how would I go about cleaning up the above excerpt?

Smith W.
  • 141
  • 5

3 Answers3

5
suits = {" of Hearts", " of Diamonds", " of Clubs", " of Spades"};
deck = Join @@ Outer[StringJoin, Join[ToString /@ Range[2, 10], 
                  {"Jack", "Queen", "King", "Ace"}], suits];
RandomSample[deck, 5]

(* {"King of Diamonds", "2 of Spades", "6 of Hearts", "5 of Clubs", "8 of Diamonds"} *)
ciao
  • 25,774
  • 2
  • 58
  • 139
  • @smith: Thanks for accept - please note - minor boo-boo, changed RandomChoice to RandomSample: the former is with replacement, the latter is without (which I'm sure is what you want.) – ciao Mar 27 '14 at 03:28
1

Nothing special here, except it uses your idea of encoding the card using a base-13 representation:

card[n_Integer /; 1 <= n <= 52] := 
 With[{suits = {"Hearts", "Diamonds", "Clubs", "Spades"}, 
   values = Join[{"Ace"},  ToString /@ Range[2, 10], {"Jack", "Queen", "King"}]}, 
  IntegerDigits[n - 1, 13, 2] /. {s_, v_} :> 
    values[[v + 1]] <> " of " <> suits[[s + 1]]]

I don't play cards, so I don't know if there's a natural order to the values(? if that's what they're called) but I've arranged them in the order Ace, 2, 3 ... 10, Jack, Queen, King.

card[1] is "Ace of Hearts" and card[52] is "King of Spades"

Aky
  • 2,719
  • 12
  • 19
0
randomHand[x_] := Module[{cards, suits, value},
  cards = DeleteCases[#, {___, 0}, 2] &@
       PadLeft[IntegerDigits[RandomInteger[{1, 52}, x], 13]];
  suits = First /@ cards /. {0 -> "Hearts", 1 -> "Diamonds", 2 -> "Clubs", 3 -> "Spades"};
  value = Last /@ cards /. {11 -> "Jack", 12 -> "Queen"};
  StringJoin @@ Insert[ToString /@ #, " of ", 2] & /@ 
  Transpose[{value, suits}]
  ]
randomHand[5]
Apple
  • 3,663
  • 16
  • 25
  • Yes, but this fails to account for the kings. In base 13, kings are multiples of 10 (10,20,30,40). So 1 is the leading digit for diamonds until 20, which is the king of diamonds. From my test, this is also slower than the original. – Smith W. Mar 27 '14 at 03:00
  • You code is not include kings.I just rewrite your code for reduce variables.And you need to randomHand[10^5] or more? – Apple Mar 27 '14 at 03:05