AN APL Scrabble Bag
Vector 9:4, page 41
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 41
This simple game is a nice little exercise in APL. 100 Scrabble tiles (with letters in various proportions) are mixed up and drawn in groups of five. You take the five tiles presented, plus two optional letters of your own choosing, and try to make an anagram. If you succeed, you score the value of the tiles drawn. The computer keeps the score.
[0] BAG;ALPH;COUNT;LETTERS;ORDER;PLAYERB;SUM;BLOCK;I;NUMS;PLAYERA;SCORE;⎕IO
[1] LETTERS←'ABCDEFGHIJKLMNOPQRSTUVWXYZ⎕'
[2] SCORE←1 3 3 2 1 4 2 4 1 8 5 1 3 1 1 3 10 1 1 1 1 4 4 8 4 10 0
[3] NUMS←9 2 2 4 12 2 3 2 9 1 1 4 2 6 8 2 1 6 4 6 4 2 2 1 2 1 2
[4] ALPH←NUMS/LETTERS
[5] PLAYERA←0
[6] PLAYERB←0
[7] SUM←0
[8] I←0
[9] ⎕IO←0
[10] ORDER←100?100
[11] BLOCK←20 5⍴(ALPH[ORDER])
[12] LINE:⎕←BLOCK[I;]
[13] COUNT←LETTERS⍳BLOCK[I;]
[14] SUM←+/SCORE[COUNT]
[15] →(2|(I,I+1))/EVEN,ODD
[16] ODD:'PLAYER A. DID YOU MAKE AN ANAGRAM? (Y/N)'
[17] →('YN'=1↑⍞)/AA,BB
[18] AA:PLAYERA←PLAYERA+SUM
[19] BB:I←I+1
[20] →LINE
[21] EVEN:'PLAYER B. DID YOU MAKE AN ANAGRAM? (Y/N)'
[22] →('YN'=1↑⍞)/CC,DD
[23] CC:PLAYERB←PLAYERB+SUM
[24] DD:I←I+1
[25] →(I<20)/LINE
[26] 'PLAYER A: SCORE = ',⍕PLAYERA
[27] 'PLAYER B: SCORE = ',⍕PLAYERB
The scores for the different letters are set up in line 2, the numbers of each letter in line 3, and the random ordering is set up in line 10, and arranged in rows of five in line 11. Inside the loop (lines 12-25) the score is calculated in lines 13 and 14. Line 15 determines whose turn it is. The scores are only added if an anagram is claimed.
A typical "game" goes something like this:
BAG
IGEIQ
PLAYER A. DID YOU MAKE AN ANAGRAM? (Y/N)
N
EOFOV
PLAYER B. DID YOU MAKE AN ANAGRAM? (Y/N)
N
TIEPT
PLAYER A. DID YOU MAKE AN ANAGRAM? (Y/N)
Y
SRENS
PLAYER B. DID YOU MAKE AN ANAGRAM? (Y/N)
Y ...
and so on, until you have had ten goes each…
… and you can start all over again!