2048: Difference between revisions

Content added Content deleted
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: Line 10,149:


=={{header|REXX}}==
=={{header|REXX}}==
This REXX version has the features:
<lang rexx>This REXX version has the features:
::* &nbsp; allows specification of '''N''', &nbsp; the size of the grid &nbsp; (default is '''4''').
::* &nbsp; allows specification of '''N''', &nbsp; the size of the grid &nbsp; (default is '''4''').
::* &nbsp; allows specification of the winning number &nbsp; (default is '''2048''')
::* &nbsp; allows specification of the winning number &nbsp; (default is '''2048''')
Line 10,155: Line 10,155:
::* &nbsp; allows abbreviations for the directions &nbsp; (Up, Down, Left, Right).
::* &nbsp; allows abbreviations for the directions &nbsp; (Up, Down, Left, Right).
::* &nbsp; allows the player to quit the game at any time.
::* &nbsp; allows the player to quit the game at any time.
::* &nbsp; clears the screen if a legal move is in upper case.
::* &nbsp; does error checking/validation for entered directions &nbsp; (in response to the prompt).
::* &nbsp; does error checking/validation for entered directions &nbsp; (in response to the prompt).
::* &nbsp; keeps track of the number of legal moves made and the score.
::* &nbsp; keeps track of the number of legal moves made and the score.
Line 10,183: Line 10,184:
if x\=='' then call err "too many arguments entered: " xx
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
if abbrev('QUIT',a,1) then do; say; say eye "quitting the game".; exit 1; end
good=abbrev('UP',a,1) | abbrev('DOWN',a,1) | abbrev('RIGHT',a,1) | abbrev('LEFT',a,1)
good=abbrev('UP',a,1) | abbrev("DOWN",a,1) | abbrev('RIGHT',a,1) | abbrev("LEFT",a,1)
if \good then call err "invalid direction: " way
if \good then call err "invalid direction: " way
if \ok then iterate; moves= moves + 1; call mov
if \ok then iterate; moves= moves + 1; call mov
Line 10,190: Line 10,191:
say translate(eye "Congrats!! You've won the" win 'game!' eye,"═",'─') "score:" score
say translate(eye "Congrats!! You've won the" win 'game!' eye,"═",'─') "score:" score
exit 0 /*stick a fork in it, we're all done. */
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
showGrid: do r=0 for N+2; _= '║'; __= '╠'
do c=1 for N; _=_ || row()'║'; __=__ || copies("═", L)'╬'
end /*c*/
if r==0 then _= ''translate( substr(_, 2, length(_) - 2), "", '║')""
if r >N then _= '╚'translate( substr(_, 2, length(_) - 2), "╩", '║')""
say pad _
if r<N & r>0 then say pad substr(__, 1, length(__) -1)"╣"
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
@: procedure expose @.; parse arg row,col; return @.row.col
@: procedure expose @.; parse arg row,col; return @.row.col
Line 10,210: Line 10,202:
two: do until @.p.q==b; p= random(1,N); q= random(1,N); end; @.p.q= ten(); return
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 right. */
showGrid: do r=0 for N+2; _= ''; __= "╠"
if d=='L' then call moveLR 1, N, +1 /* " " " left. */
do c=1 for N; _= _ || row()'║'; __= __ || copies("═", L)'╬'
if d=='U' then call moveUD 1, N, +1 /* " " " up. */
end /*c*/
if d=='D' then call moveUD N, 1, -1 /* " " " down. */
if r==0 then _= '╔'translate( substr(_, 2, length(_) - 2), "", '║')"╗"
if r >N then _= ''translate( substr(_, 2, length(_) - 2), "", '║')""
say pad _
if r<N & r>0 then say pad substr(__, 1, length(__) - 1)""
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
mov: move= 0; if d=='R' then call moveLR N, 1, -1 /*move (slide) numbers ► */
if d=='L' then call moveLR 1, N, +1 /* " " " */
if d=='U' then call moveUD 1, N, +1 /* " " " */
if d=='D' then call moveUD N, 1, -1 /* " " " ↓ */
if \move then call err 'moving ' way " doesn't change anything."; return
if \move then call err 'moving ' way " doesn't change anything."; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
moveLR: parse arg start, sTo, #
moveLR: parse arg start, sTo, # /*slide ◄ or ► */
do r=1 for N; old= o_r(); if ! then iterate /*is this row blank? */
do r=1 for N; old= o_r(); if ! then iterate /*is this row blank? */
do N-1; call packLR /*pack left or right.*/
do N-1; call packLR /*pack or */
end /*N-1*/ /* [↓] get new tiles.*/
end /*N-1*/ /* [↓] get new tiles.*/
new= o_r(); move= move | (old\==new) /*indicate tiles moved*/
new= o_r(); move= move | (old\==new) /*indicate tiles moved*/
do c=start for N-1 by # while @.r.c\==b /*slide left or right.*/
do c=start for N-1 by # while @.r.c\==b /*slide or */
if @.r.c\==@(r,c+#) then iterate /*not a duplicate ? */
if @.r.c\==@(r,c+#) then iterate /*not a duplicate ? */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
c= c + # ; @.r.c= b; move= 1 /*bump C; blank dup 2.*/
c= c + # ; @.r.c= b; move= 1 /*bump C; blank dup 2.*/
end /*c*/ /* [↑] indicate move.*/
end /*c*/ /* [↑] indicate move.*/
call packLR /*pack left or right.*/
call packLR /*pack or */
end /*r*/; return
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
moveUD: parse arg start, Sto, #
moveUD: parse arg start, Sto, # /*slide ↑ or ↓ */
do c=1 for N; old= o_c(); if ! then iterate /*is this col blank? */
do c=1 for N; old= o_c(); if ! then iterate /*is this col blank? */
do N-1; call packUD /*pack up or down. */
do N-1; call packUD /*pack up or down. */
end /*N-1*/ /* [↓] get new tiles.*/
end /*N-1*/ /* [↓] get new tiles.*/
new= o_c(); move= move | (old\==new) /*indicate tiles moved*/
new= o_c(); move= move | (old\==new) /*indicate tiles moved*/
do r=start for N-1 by # while @.r.c\==b /*slide up or down. */
do r=start for N-1 by # while @.r.c\==b /*slide or */
if @.r.c\==@(r+#,c) then iterate /*not a duplicate ? */
if @.r.c\==@(r+#,c) then iterate /*not a duplicate ? */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
@.r.c= @.r.c * 2; score= score + @.r.c /*double; bump score */
r= r + # ; @.r.c= b; move= 1 /*bump R; blank dup 2.*/
r= r + # ; @.r.c= b; move= 1 /*bump R; blank dup 2.*/
end /*r*/ /* [↑] indicate move.*/
end /*r*/ /* [↑] indicate move.*/
call packUD /*pack up or down. */
call packUD /*pack or */
end /*c*/; return
end /*c*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
packLR: do c=start for N-1 by # /*slide left or right.*/
packLR: do c=start for N-1 by #; if @.r.c\==b then iterate /*Not a blank? Skip. */
if @.r.c\==b then iterate /*Not a blank? Skip. */
do s=c to sTo by #; @.r.s= @(r, s + #) /*slide or */
do s=c to sTo by #; @.r.s= @(r, s+#) /*slide left or right.*/
end /*s*/; @.r.sTo= b /*handle the last one.*/
end /*s*/; @.r.sTo= b /*handle the last one.*/
end /*c*/; return
end /*c*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
packUD: do r=start for N-1 by # /*slide up or down. */
packUD: do r=start for N-1 by #; if @.r.c\==b then iterate /*Not a blank? Skip. */
if @.r.c\==b then iterate /*Not a blank? Skip. */
do s=r to sTo by #; @.s.c= @(s + #, c) /*slide or */
do s=r to sTo by #; @.s.c= @(s+#, c) /*slide up or down. */
end /*s*/; @.sTo.c= b /*handle the last one.*/
end /*s*/; @.sTo.c= b /*handle the last one.*/
end /*r*/; return</lang>
end /*r*/; return</lang>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre style="font-size:84%;height:98ex">
<pre style="font-size:84%;height:98ex">