sourcegames/2048/play.xtl

1⍝!/usr/bin/env xetal 2⍝# 2048 at the terminal: slide the tiles with a (left), d (right), w (up) 3⍝# or s (down), then Enter; q stops. Equal tiles that meet merge; make a 2048. 4⍝# The board is printed as X_eTaL prints a matrix (0 is an empty 5⍝# square). The rules are those of Twenty48.xtl. 6ᵗ⁼u̲se< "Twenty48" 7ᵖ⁼u̲se< "Play" 8said ← p̲rint! "2048: A LEFT, D RIGHT, W UP, S DOWN" 9 10⍝# A direction (1 left, 2 right, 3 up, 4 down) from the first of a d w s 11⍝# on the line, 0 for q; anything else is asked for again. 12ᵘa̲sk ← { s → 13 k ← "MOVE (A D W S, Q TO STOP) ?" ᵖl̲etter "adwsADWSqQ" 14 k ≤ 8 ? 1 + (k − 1) m̲od 4 15 k ≤ 10 ? 0◆ ᵘa̲sk s 16} 17⍝# A turn: the score and board, a move, until the game ends. 18ᵘt̲urn ← { s → 19 said ← p̲rint! "" 20 said ← p̲rint! @ f̲ormat< "SCORE {t:s_core s}"
f̲ormat< expands to
("SCORE " c̲at (f̲ormat (ᵗs̲core s)))
21 shown ← p̲rint! ᵗb̲oard s 22 1 = ᵗs̲tatus s ? @ f̲ormat< "2048! YOU WIN WITH {t:s_core s}"
f̲ormat< expands to
("2048! YOU WIN WITH " c̲at (f̲ormat (ᵗs̲core s)))
23 2 = ᵗs̲tatus s ? @ f̲ormat< "NO MOVES LEFT. SCORE {t:s_core s}"
f̲ormat< expands to
("NO MOVES LEFT. SCORE " c̲at (f̲ormat (ᵗs̲core s)))
24 d ← ᵘa̲sk s 25 0 = d ? @ f̲ormat< "STOPPED WITH SCORE {t:s_core s}"
f̲ormat< expands to
("STOPPED WITH SCORE " c̲at (f̲ormat (ᵗs̲core s)))
26 next ← s ᵗm̲ove d 27 next m̲atch s ? ᵘt̲urn ᵘs̲tuck s◆ ᵘt̲urn next 28} 29ᵘs̲tuck ← { s → 30 said ← p̲rint! "NOTHING MOVES THAT WAY." 31 s 32} 33ᵘt̲urn ᵗn̲ew 0