2
$\begingroup$

The deck consists of 45 cards: one "2", one "A", three "K"s, and four of each other card ranging from "3" to "10", "J", and "Q". The goal is to distribute these cards evenly among three players, with each player receiving 15 cards.

(*Step 1:Create the Deck*)
deck = Join[{"2", "A"},(*One "2" and one "A"*)
   Table["K", {3}],(*Three "K"s*)
   Flatten[Table[
     ToString[i], {i, 3, 
      10}, {4}]],(*Four of each card from "3" to "10"*)
   Table["J", {4}],(*Four "J"s*)
   Table["Q", {4}]                (*Four "Q"s*)];

(*Ensure the deck size is 45 cards*)
deck = Take[Flatten[deck], 45]

(*Step 2:Shuffle the Deck*)
shuffledDeck = RandomSample[deck];

(*Step 3:Distribute the Cards*)
distribution = Partition[shuffledDeck, 15];

(*Verify the distribution*)
distribution

get the result:

{{"Q", "Q", "9", "8", "8", "7", "7", "7", "6", "6", "6", "4", "4", 
  "3", "3"}, {"2", "Q", "Q", "J", "J", "10", "10", "9", "9", "9", "7",
   "6", "5", "5", "3"}, {"A", "K", "K", "K", "J", "J", "10", "10", 
  "8", "8", "5", "5", "4", "4", "3"}}

Can the result of the card dealing be displayed in a more aesthetically pleasing way?

Such as:

Can the process of dealing cards generate them one by one, simulating a real card dealing process? And can the display of the cards resemble real cards?

https://resources.wolframcloud.com/FunctionRepository/resources/PlayingCardGraphic/

This function is good!

$\endgroup$
1
  • 2
    $\begingroup$ I do not understand what you are asking since you already have an answer in the repository function. $\endgroup$ Commented Feb 8 at 10:55

1 Answer 1

8
$\begingroup$
cards = Flatten@
   Join[{1, 2, 13, 13 + 13, 13 + 2*13}, 
    Table[Range[3, 12] + n*13, {n, 0, 3}]];
deal = Partition[RandomSample[cards], 15];
GraphicsColumn[
 ResourceFunction["PlayingCardGraphic"][#, 
    ImageSize -> {1000, 300}] & /@ deal, ImageSize -> 1000]

enter image description here

Sorted by color: deal = Sort /@ Partition[RandomSample[cards], 15]

enter image description here

Sorted by value: deal = SortBy[#, Mod[#, 13, 1] &] & /@ Partition[RandomSample[cards], 15]

enter image description here


{{"Q", "Q", "9", "8", "8", "7", "7", "7", "6", "6", "6", "4", "4", 
    "3", "3"}, {"2", "Q", "Q", "J", "J", "10", "10", "9", "9", "9", 
    "7", "6", "5", "5", "3"}, {"A", "K", "K", "K", "J", "J", "10", 
    "10", "8", "8", "5", "5", "4", "4", "3"}} /. {"A" -> 1, "J" -> 11,
    "Q" -> 12, "K" -> 13} // ToExpression
GraphicsColumn[
 ResourceFunction["PlayingCardGraphic"][#, 
    ImageSize -> {1000, 300}] & /@ %, ImageSize -> 1000]

enter image description here

There are some issues with the resource function and manual choosing of appropriate ImageSize is required to display graphics neatly.

$\endgroup$
6
  • $\begingroup$ ResourceFunction::ffail: Failed to find resource function HoldForm[<|Name->PlayingCardGraphic,ShortName->PlayingCardGraphic,UUID->f225b527-fc83-4eda-9390-d05f08f92b1b,ResourceType->Function,Version->1.0.0,Description->Create graphic displaying standard playing cards,RepositoryLocation->URL[wolframcloud.com/obj/resourcesystem/api/…$5811106fe1e3481e909ba26fb423e263PlayingCardGraphic,FunctionLocation->CloudObject[wolframcloud.com/objects/…. $\endgroup$
    – csn899
    Commented Feb 9 at 12:38
  • $\begingroup$ How does this prompt appear after running the code? $\endgroup$
    – csn899
    Commented Feb 9 at 12:38
  • $\begingroup$ I have no such message. What version of Mathematica do you use? $\endgroup$ Commented Feb 9 at 13:05
  • $\begingroup$ mathematica14.0 $\endgroup$
    – csn899
    Commented Feb 9 at 13:34
  • $\begingroup$ @csn899: Mine is 13.0.1. I have no idea what the problem is if you copied my code correctly. $\endgroup$ Commented Feb 9 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.