Dealing Cards

 

Problem

Four persons play poker with a pack of cards without big and little jokers. Shuffle the remaining 52 cards and deal out four hands of 13 cards each. The four suits of poker are indicated with letters A, B, C, and D. For example, A8 indicates Spade 8, BK indicates Heart K. Write a program to implement the dealing process and print out four sequences.

Tip

General steps: Arrange the numbers 1-52 randomly and divide them into 4 groups. Call a subroutine to which the current number is passed as an argument to compute the card corresponding to this number, and return a card indicated by a letter for the suit and this number. This is the dealing process.

  1. Randomize the number 1-52.

  2. Divide the 52 numbers into 4 groups.

  3. Sort the numbers in each group. Repeatedly call the subroutine into which the number is passed and return a card indicated by a letter for the suit and this number.

Code

A B
1 =52.sort(rand()) Shuffle the cards.
2 =A1.group((#-1)\13) Divide the cards into four groups.
3 =A2.(~.sort().(func(A5,~))) Call the subroutine to return a result.
4
5 func =mid("KA234567890JQ",1+A5%13,1) Get the number for the card.
6 =mid("ABCD", (A5-1)\13+1,1) Get the suit for the card.
7 return B6+B5 Return a card indicated by this number and the suit letter

Result

imagepng