2048: Difference between revisions

Content deleted Content added
m →‎{{header|REXX}}: added/changed comments and whitespace, paramatized a command.
Line 9,214: Line 9,214:
<lang rexx>/*REXX program lets a user play the 2048 game on an NxN grid (default is 4x4 grid).*/
<lang rexx>/*REXX program lets a user play the 2048 game on an NxN grid (default is 4x4 grid).*/
parse arg N win seed . /*obtain optional arguments from the CL*/
parse arg N win seed . /*obtain optional arguments from the CL*/
if N=='' | N=="," then N= 4 /*Not specified? Then use the default.*/
if N=='' | N=="," then N= 4 /*Not specified? Then use the default.*/
if win=='' | win=="," then win=2**11 /* " " " " " " */
if win=='' | win=="," then win= 2**11 /* " " " " " " */
if datatype(seed, 'W') then call random ,,seed /*Specified? Then use seed for RANDOM.*/
if datatype(seed, 'W') then call random ,,seed /*Specified? Then use seed for RANDOM.*/
L=length(win) + 2 /*L: used for displaying the grid #'s.*/
L= length(win) + 2 /*L: used for displaying the grid #'s.*/
eye=copies("─", 8); pad=left('', length(eye)+2) /*eye-catchers; and perusable perusing.*/
eye=copies("─", 8); pad=left('', length(eye)+2) /*eye catchers; and perusable perusing.*/
b= ' ' /*comfortable readable name for a blank*/
b= ' ' /*comfortable readable name for a blank*/
@cls= 'CLS' /*hardcoded command to clear the screen*/
prompt= eye "Please enter a direction (Up, Down, Right, Left) ───or─── Quit:"
prompt= eye "Please enter a direction (Up, Down, Right, Left) ───or─── Quit:"
move=1; moves=0; score=0; ok=1 /*simulation that a move was performed.*/
move= 1; moves= 0; score= 0; ok= 1 /*simulation that a move was performed.*/
@.=b /*define all grid elements to a blank. */
@.= b /*define all grid elements to a blank. */
do until any(win); if ok then call put; ok=1; say; call showGrid
do until any(win); if ok then call put; ok= 1; say; call showGrid
say; say prompt; parse pull a x . 1 d 2 1 way xx; upper d a x
say; say prompt; parse pull a x . 1 d 2 1 way xx /*show prompt; obtain answer.*/
if a=='' then do; ok=0 /*the user entered blank(s) or nothing.*/
if datatype(a, 'U') then @cls /*if uppercase, then clear the screen. */
say copies(eye,5) 'moves:' moves eye "score:" score
if a=='' then do; ok= 0 /*the user entered blank(s) or nothing.*/
iterate /* [↑] display # of moves & the score.*/
say copies(eye, 5) 'moves:' moves eye "score:" score
iterate /* [↑] display # of moves & the score.*/
end
end
upper d a x /*uppercase contents of three variables*/
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
end /*until*/
end /*until*/
say
say
Line 9,242: Line 9,245:
do c=1 for N; _=_ || row()'║'; __=__ || copies("═", L)'╬'
do c=1 for N; _=_ || row()'║'; __=__ || copies("═", L)'╬'
end /*c*/
end /*c*/
if r==0 then _= '╔'translate(substr(_, 2, length(_)-2), "╦", '║')"╗"
if r==0 then _= '╔'translate( substr(_, 2, length(_) - 2), "╦", '║')"╗"
if r >N then _= '╚'translate(substr(_, 2, length(_)-2), "╩", '║')"╝"
if r >N then _= '╚'translate( substr(_, 2, length(_) - 2), "╩", '║')"╝"
say pad _
say pad _
if r<N & r>0 then say pad substr(__, 1, length(__) -1)"╣"
if r<N & r>0 then say pad substr(__, 1, length(__) -1)"╣"
Line 9,255: Line 9,258:
put: if \any(b) then call err ,"game over, no more moves."; if move then call two; return
put: if \any(b) then call err ,"game over, no more moves."; if move then call two; return
row: if r==0 | r>N then return copies('═', L); return center(@.r.c, L)
row: if r==0 | r>N then return copies('═', L); return center(@.r.c, L)
ten: if random(9)==4 then return 4; return 2 /*10% of the time, put 4 instead of 2.*/
ten: if random(9)==4 then return 4; return 2 /*10% of the time, use 4 instead of 2.*/
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. */
mov: move= 0; if d=='R' then call moveLR N, 1, -1 /*move (slide) numbers right. */
if d=='L' then call moveLR 1, N, +1 /* " " " left. */
if d=='L' then call moveLR 1, N, +1 /* " " " left. */
if d=='U' then call moveUD 1, N, +1 /* " " " up. */
if d=='U' then call moveUD 1, N, +1 /* " " " up. */
if d=='D' then call moveUD N, 1, -1 /* " " " down. */
if d=='D' then call moveUD N, 1, -1 /* " " " down. */
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, #
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 left or right.*/
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 left or right.*/
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 left or right.*/
end /*r*/; return
end /*r*/; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
moveUD: parse arg start, Sto, #
moveUD: parse arg start, Sto, #
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 up or down. */
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 up or down. */
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 # /*slide left or right.*/
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 left or right.*/
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 # /*slide up or down. */
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 up or down. */
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">