2048: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl 6}}: refactor a bit, style tweaks)
m (→‎{{header|Perl 6}}: some more refactoring, DRY)
Line 1,805:
 
multi sub squash ('left', @c) {
my @tilest = grep { .chars }, @c;
map { combine(@colt[$x_], @colt[$x_+1]) if @colt[$x_] && @colt[$x_+1] == @colt[$x_] }, ^@t-1;
@tiles.push: '' while @tiles < 4;
@tilest = grep { .chars }, @t;
@tilest.push: '' while @tilest < 4;
}@t;
}
 
multi sub squash ('right', @c) {
my @tilest = reverse grep { .chars }, @c;
map { combine(@rowt[$x_], @rowt[$x_+1]) if @rowt[$x_] && @rowt[$x_+1] == @rowt[$x_] }, ^@t-1;
@tiles.unshift: '' while @tiles < 4;
@tilest = grep { .chars }, @t;
@tilest.unshiftpush: '' while @tilest < 4;
reverse @t;
}
 
Line 1,819 ⟶ 1,823:
 
multi sub move('up') {
map { @board[*]»[$y_] = squash left, @col;board[*]»[$_] }, 0..3
for 0 .. 3 -> $y {
my @col = squash left, @board[*]»[$y];
for 0 .. 2 -> $x {
combine(@col[$x], @col[$x+1]) if @col[$x] && @col[$x+1] == @col[$x]
}
@board[*]»[$y] = squash left, @col;
}
}
 
multi sub move('down') {
map { @board[*]»[$y_] = squash right, @col;board[*]»[$_] }, 0..3
for 0 .. 3 -> $y {
my @col = squash right, @board[*]»[$y];
for 3 ... 1 -> $x {
combine(@col[$x], @col[$x-1]) if @col[$x] && @col[$x-1] == @col[$x]
}
@board[*]»[$y] = squash right, @col;
}
}
 
multi sub move('left') {
map my{ @rowboard[$_] = squash left, flat @board[$y_]»[*]; }, 0..3
for 0 .. 3 -> $y {
my @row = squash left, flat @board[$y]»[*];
for 0 .. 2 -> $x {
combine(@row[$x], @row[$x+1]) if @row[$x] && @row[$x+1] == @row[$x]
}
@board[$y] = squash left, @row;
}
}
 
multi sub move('right') {
map { @board[$y_] = squash leftright, flat @row;board[$_]»[*] }, 0..3
for 0 .. 3 -> $y {
my @row = squash right, flat @board[$y]»[*];
for 3 ... 1 -> $x {
combine(@row[$x], @row[$x-1]) if @row[$x] && @row[$x-1] == @row[$x]
}
@board[$y] = squash right, @row;
}
}