2048: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) No edit summary |
m (→{{header|REXX}}: added wording to the REXX section header, order subroutines in alphabetical order, added whitespace, changed comments.) |
||
Line 10,149:
=={{header|REXX}}==
<lang rexx>This REXX version has the features:
::* allows specification of '''N''', the size of the grid (default is '''4''').
::* allows specification of the winning number (default is '''2048''')
Line 10,155:
::* allows abbreviations for the directions (Up, Down, Left, Right).
::* allows the player to quit the game at any time.
::* clears the screen if a legal move is in upper case.
::* does error checking/validation for entered directions (in response to the prompt).
::* keeps track of the number of legal moves made and the score.
Line 10,183 ⟶ 10,184:
if x\=='' then call err "too many arguments entered: " xx
if abbrev('QUIT',a,1) then do; say; say eye "quitting the game".; exit 1; end
good=abbrev('UP',a,1) | abbrev(
if \good then call err "invalid direction: " way
if \ok then iterate; moves= moves + 1; call mov
Line 10,190 ⟶ 10,191:
say translate(eye "Congrats!! You've won the" win 'game!' eye,"═",'─') "score:" score
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/▼
showGrid: do r=0 for N+2; _= '║'; __= '╠'▼
end /*c*/▼
if r==0 then _= '╔'translate( substr(_, 2, length(_) - 2), "╦", '║')"╗"▼
if r >N then _= '╚'translate( substr(_, 2, length(_) - 2), "╩", '║')"╝"▼
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: procedure expose @.; parse arg row,col; return @.row.col
Line 10,210 ⟶ 10,202:
two: do until @.p.q==b; p= random(1,N); q= random(1,N); end; @.p.q= ten(); return
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲/*──────────────────────────────────────────────────────────────────────────────────────*/
mov: move= 0; if d=='R' then call moveLR N, 1, -1 /*move (slide) numbers ► */
if d=='D' then call moveUD N, 1, -1 /* " " " ↓ */
if \move then call err 'moving ' way " doesn't change anything."; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
moveLR: parse arg start, sTo, # /*slide ◄ or ► */
do r=1 for N; old= o_r(); if ! then iterate
do N-1; call packLR /*pack
end /*N-1*/ /* [↓] get new tiles.*/
new= o_r(); move= move | (old\==new) /*indicate tiles moved*/
do c=start for N-1 by # while @.r.c\==b /*slide
if @.r.c\==@(r,c+#) then iterate /*not a duplicate ? */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
c= c + # ; @.r.c= b;
end /*c*/ /* [↑] indicate move.*/
call packLR /*pack
end /*r*/;
/*──────────────────────────────────────────────────────────────────────────────────────*/
moveUD: parse arg start, Sto, # /*slide ↑ or ↓ */
do c=1 for N; old= o_c(); if ! then iterate
do N-1; call packUD /*pack up or down. */
end /*N-1*/ /* [↓] get new tiles.*/
new= o_c(); move= move | (old\==new) /*indicate tiles moved*/
do r=start for N-1 by # while @.r.c\==b /*slide
if @.r.c\==@(r+#,c) then iterate /*not a duplicate ? */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
r= r + # ; @.r.c= b;
end /*r*/ /* [↑] indicate move.*/
call packUD /*pack
end /*c*/;
/*──────────────────────────────────────────────────────────────────────────────────────*/
packLR:
do
end /*s*/;
end
▲ end /*c*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
packUD:
do
end /*s*/;
end
▲ end /*r*/; return</lang>
{{out|output|text= when using the default inputs:}}
<pre style="font-size:84%;height:98ex">
|