2048: Difference between revisions
Content deleted Content added
m →{{header|Ruby}}: shaved off a few lines in the "case input" |
Thundergnat (talk | contribs) m →{{header|Perl 6}}: minor tweaks, more idiomatic, DRY |
||
Line 1,423: | Line 1,423: | ||
# set flags needed to emulate it manually |
# set flags needed to emulate it manually |
||
$termios.unset_iflags(<BRKINT ICRNL ISTRIP IXON>); |
$termios.unset_iflags(<BRKINT ICRNL ISTRIP IXON>); |
||
$termios.unset_lflags(<ECHO ICANON IEXTEN ISIG>); |
$termios.unset_lflags(< ECHO ICANON IEXTEN ISIG>); |
||
$termios.setattr(:DRAIN); |
$termios.setattr(:DRAIN); |
||
Line 1,457: | Line 1,457: | ||
my $pad = ' ' x ceiling($c/2); |
my $pad = ' ' x ceiling($c/2); |
||
my $tile = sprintf "%{cell}s", "$s$pad"; |
my $tile = sprintf "%{cell}s", "$s$pad"; |
||
my $idx = $s ?? $s.log(2) !! |
my $idx = $s ?? $s.log(2) !! 0; |
||
ansi ?? "\e[{@ANSI[$idx]}m$tile\e[0m" !! $tile; |
ansi ?? "\e[{@ANSI[$idx]}m$tile\e[0m" !! $tile; |
||
} |
} |
||
Line 1,468: | Line 1,468: | ||
Press direction arrows to move. |
Press direction arrows to move. |
||
Press q to quit. |
Press q to quit. |
||
$top |
$top |
||
{ join "\n |
{ join "\n\t$mid\n\t", map { .&row }, @board } |
||
$bot |
$bot |
||
Line 1,479: | Line 1,479: | ||
} |
} |
||
sub squash (@c) { |
sub squash (@c) { |
||
my @t = grep { .chars }, @c; |
my @t = grep { .chars }, @c; |
||
map { combine(@t[$_], @t[$_+1]) if @t[$_] && @t[$_+1] == @t[$_] }, ^@t-1; |
map { combine(@t[$_], @t[$_+1]) if @t[$_] && @t[$_+1] == @t[$_] }, ^@t-1; |
||
Line 1,490: | Line 1,490: | ||
multi sub move('up') { |
multi sub move('up') { |
||
map { @board[* |
map { @board[*;$_] = squash @board[*;$_] }, ^n; |
||
} |
} |
||
multi sub move('down') { |
multi sub move('down') { |
||
map { @board[* |
map { @board[*;$_] = reverse squash reverse @board[*;$_] }, ^n; |
||
} |
} |
||
multi sub move('left') { |
multi sub move('left') { |
||
map { @board[$_] = squash flat @board[$_ |
map { @board[$_] = squash flat @board[$_;*] }, ^n; |
||
} |
} |
||
multi sub move('right') { |
multi sub move('right') { |
||
map { @board[$_] = reverse squash reverse flat @board[$_ |
map { @board[$_] = reverse squash reverse flat @board[$_;*] }, ^n; |
||
} |
} |
||
Line 1,513: | Line 1,513: | ||
@board[$x; $y] = (flat 2 xx 9, 4).roll; |
@board[$x; $y] = (flat 2 xx 9, 4).roll; |
||
} |
} |
||
⚫ | |||
loop { |
loop { |
||
another if $save ne |
another if $save ne save(); |
||
draw-board; |
draw-board; |
||
$save = save(); |
|||
⚫ | |||
# Page navigation keys are 3-4 bytes each. |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
last if $key eq 'q'; # (q)uit |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
last if $char eq 'q'; # (q)uit |
|||
}</lang> |
}</lang> |
||
Sample output: |
Sample output: |