Write an APL function called Dist that returns the 4 hands of a random Bridge cards distribution.
You should make use of the following global variables:
pips←'SHDC' ⍝ S=Spades, D=Diamonds, H=Hearts, C=Clubs
crds←'AKQJX98765432' ⍝ A=Ace, K=King, Q=Queen, J=Jack, X=10, 9=9, ...
and you can use the following constants in your code:
52 ⍝ total number of cards
13 ⍝ number of cards per player
4 ⍝ number of players
The result must be a 4-element nested vector, representing the 4 hands, such as:
⎕←aaa←Dist
DJ S8 CK DX S3 S6 HQ S2 HA C2 D6 C9 SJ D8 D3 C5 CJ DQ C6 S4 C8 H7 D9 H8 H2 CA HK H6 C7 SK SQ S7 D5 CQ HX DK HJ S5 C3 SA H9 D2 D7 H5 C4 D4 H3 SX S9 CX DA H4
⍝ which you can look at in matrix form
⍝ as follows:
⊃aaa
DJ S8 CK DX S3 S6 HQ S2 HA C2 D6 C9 SJ
D8 D3 C5 CJ DQ C6 S4 C8 H7 D9 H8 H2 CA
HK H6 C7 SK SQ S7 D5 CQ HX DK HJ S5 C3
SA H9 D2 D7 H5 C4 D4 H3 SX S9 CX DA H4
⍴aaa
4
⍴¨aaa
13 13 13 13
⍴¨¨aaa
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
Each card in the result is represented by the pip character (S,H,D or C) followed by the card letter (A,K,Q,J,X,9,8,7,6,5,4,3 or 2)
One solution is:
∇ r←Dist
[1] ⍝∇ r←Dist
[2] ⍝∇ r ←→ 4-element nested vector
[3] ⍝∇ each element contains 13 cards,
[4] ⍝∇ each represented by 2 letters
[5] ⍝∇ Example:
[6] ⍝∇ ⊃Dist
[7] ⍝∇ DJ S8 CK DX S3 S6 HQ S2 HA C2 D6 C9 SJ
[8] ⍝∇ D8 D3 C5 CJ DQ C6 S4 C8 H7 D9 H8 H2 CA
[9] ⍝∇ HK H6 C7 SK SQ S7 D5 CQ HX DK HJ S5 C3
[10] ⍝∇ SA H9 D2 D7 H5 C4 D4 H3 SX S9 CX DA H4
[11]
[12] r←⊂[2](,pips∘.,crds)[4 13⍴52?52]
∇
Explanations
⍝ The following expression:
pips∘.,crds
⍝ catenates each pip character
⍝ to each card character, returning
⍝ a 4x13 nested matrix of all
⍝ existing cards:
SA SK SQ SJ SX S9 S8 S7 S6 S5 S4 S3 S2
HA HK HQ HJ HX H9 H8 H7 H6 H5 H4 H3 H2
DA DK DQ DJ DX D9 D8 D7 D6 D5 D4 D3 D2
CA CK CQ CJ CX C9 C8 C7 C6 C5 C4 C3 C2
⍝ We now need to randomize the cards
⍝ First, we ravel the previous result
⍝ with the ravel (,) primitive:
,pips∘.,crds
SA SK SQ SJ SX S9 S8 S7 S6 S5 S4 S3 S2 HA HK HQ HJ HX H9 H8 H7 H6 H5 H4 H3 H2 DA DK DQ DJ DX D9 D8 D7 D6 D5 D4 D3 D2 CA CK CQ CJ CX C9 C8 C7 C6 C5 C4 C3 C2
⍝ This gives us a nested vector of
⍝ the 52 cards in the game
⍝ Next we create a permutation of
⍝ the 52 first integers with the APL
⍝ Deal (?) primitive:
52?52
51 34 47 23 25 52 35 26 16 30 42 2 36 27 41 48 31 1 40 32 38 7 24 39 21 44 20 18 11 3 4 50 9 5 33 49 22 12 46 10 8 15 17 14 37 29 13 6 28 19 45 43
⍝ So we can create a matrix of indices:
4 13⍴52?52
51 34 47 23 25 52 35 26 16 30 42 2 36
27 41 48 31 1 40 32 38 7 24 39 21 44
20 18 11 3 4 50 9 5 33 49 22 12 46
10 8 15 17 14 37 29 13 6 28 19 45 43
⍝ APL is array oriented and allows
⍝ us to index a vector by a matrix.
⍝ so we can index our cards by this
⍝ matrix:
(,pips∘.,crds)[4 13⍴52?52]
C3 D7 C7 H5 H3 C2 D6 H2 HQ DJ CQ SK D5
DA CK C6 DX SA CA D9 D3 S8 H4 D2 H7 CX
H8 HX S4 SQ SJ C4 S6 SX D8 C5 H6 S3 C8
S5 S7 HK HJ HA D4 DQ S2 S9 DK H9 C9 CJ
⍝ Finally we use the ⎕split APL System
⍝ function or ⊂[2] to return the result
⍝ as a 4-element nested vector (with
⍝ one row of the above matrix per
⍝ element)
⊂[2](,pips∘.,crds)[4 13⍴52?52]
C3 D7 C7 H5 H3 C2 D6 H2 HQ DJ CQ SK D5 DA CK C6 DX SA CA D9 D3 S8 H4 D2 H7 CX H8 HX S4 SQ SJ C4 S6 SX D8 C5 H6 S3 C8 S5 S7 HK HJ HA D4 DQ S2 S9 DK H9 C9 CJ