programgames/2048/play.xtl

source · imports t: games/2048/Twenty48.xtl; p: lib/Play.xtl

said : Char

value · line 8
said ← p̲rint! "2048: A LEFT, D RIGHT, W UP, S DOWN"

ᵘa̲sk : Any a => a -> Int

function · line 12

A direction (1 left, 2 right, 3 up, 4 down) from the first of a d w s on the line, 0 for q; anything else is asked for again.

ᵘa̲sk ← { s →
  k ← "MOVE (A D W S, Q TO STOP) ?" ᵖl̲etter "adwsADWSqQ"
  k ≤ 8 ? 1 + (k − 1) m̲od 4
  k ≤ 10 ? 0◆ ᵘa̲sk s
}

ᵘt̲urn : (Num a, Truthy a) => a -> Char

function · line 18

A turn: the score and board, a move, until the game ends.

ᵘt̲urn ← { s →
  said ← p̲rint! ""
  said ← p̲rint! @ f̲ormat< "SCORE {t:s_core s}"
  shown ← p̲rint! ᵗb̲oard s
  1 = ᵗs̲tatus s ? @ f̲ormat< "2048! YOU WIN WITH {t:s_core s}"
  2 = ᵗs̲tatus s ? @ f̲ormat< "NO MOVES LEFT. SCORE {t:s_core s}"
  d ← ᵘa̲sk s
  0 = d ? @ f̲ormat< "STOPPED WITH SCORE {t:s_core s}"
  next ← s ᵗm̲ove d
  next m̲atch s ? ᵘt̲urn ᵘs̲tuck s◆ ᵘt̲urn next
}
f̲ormat< expands to
("SCORE " c̲at (f̲ormat (ᵗs̲core s)))
f̲ormat< expands to
("2048! YOU WIN WITH " c̲at (f̲ormat (ᵗs̲core s)))
f̲ormat< expands to
("NO MOVES LEFT. SCORE " c̲at (f̲ormat (ᵗs̲core s)))
f̲ormat< expands to
("STOPPED WITH SCORE " c̲at (f̲ormat (ᵗs̲core s)))

ᵘs̲tuck : (Num a, Truthy a) => a -> a

function · line 29
ᵘs̲tuck ← { s →
  said ← p̲rint! "NOTHING MOVES THAT WAY."
  s
}
Used in: ᵘt̲urn