-
Notifications
You must be signed in to change notification settings - Fork 3
/
Raku 2048.raku
115 lines (87 loc) · 2.61 KB
/
Raku 2048.raku
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use Term::termios;
constant $saved = Term::termios.new(fd => 1).getattr;
constant $termios = Term::termios.new(fd => 1).getattr;
# raw mode interferes with carriage returns, so
# set flags needed to emulate it manually
$termios.unset_iflags(<BRKINT ICRNL ISTRIP IXON>);
$termios.unset_lflags(< ECHO ICANON IEXTEN ISIG>);
$termios.setattr(:DRAIN);
# reset terminal to original setting on exit
END { $saved.setattr(:NOW) }
constant n = 4; # board size
constant cell = 6; # cell width
constant ansi = True; # color!
my @board = ( ['' xx n] xx n );
my $save = '';
my $score = 0;
constant $top = join '─' x cell, '┌', '┬' xx n-1, '┐';
constant $mid = join '─' x cell, '├', '┼' xx n-1, '┤';
constant $bot = join '─' x cell, '└', '┴' xx n-1, '┘';
my %dir = (
"\e[A" => 'up',
"\e[B" => 'down',
"\e[C" => 'right',
"\e[D" => 'left',
);
my @ANSI = <0 1;97 1;93 1;92 1;96 1;91 1;95 1;94 1;30;47 1;43
1;42 1;46 1;41 1;45 1;44 1;33;43 1;33;42 1;33;41 1;33;44>;
sub row (@row) { '│' ~ (join '│', @row».¢er) ~ '│' }
sub center ($s){
my $c = cell - $s.chars;
my $pad = ' ' x ceiling($c/2);
my $tile = sprintf "%{cell}s", "$s$pad";
my $idx = $s ?? $s.log(2) !! 0;
ansi ?? "\e[{@ANSI[$idx]}m$tile\e[0m" !! $tile;
}
sub draw-board {
run('clear');
print qq:to/END/;
Press direction arrows to move.
Press q to quit.
$top
{ join "\n\t$mid\n\t", map { .&row }, @board }
$bot
Score: $score
END
}
sub squash (@c) {
my @t = grep { .chars }, @c;
map { combine(@t[$_], @t[$_+1]) if @t[$_] && @t[$_+1] == @t[$_] }, ^@t-1;
@t = grep { .chars }, @t;
@t.push: '' while @t < n;
@t;
}
sub combine ($v is rw, $w is rw) { $v += $w; $w = ''; $score += $v; }
proto sub move (|) {*};
multi move('up') {
map { @board[*;$_] = squash @board[*;$_] }, ^n;
}
multi move('down') {
map { @board[*;$_] = reverse squash reverse @board[*;$_] }, ^n;
}
multi move('left') {
map { @board[$_] = squash @board[$_] }, ^n;
}
multi move('right') {
map { @board[$_;*] = reverse squash reverse @board[$_] }, ^n;
}
sub another {
my @empties;
for @board.kv -> $r, @row {
@empties.push(($r, $_)) for @row.grep(:k, '');
}
my ( $x, $y ) = @empties.roll;
@board[$x; $y] = (flat 2 xx 9, 4).roll;
}
sub save () { join '|', flat @board».list }
loop {
another if $save ne save();
draw-board;
$save = save();
# Read up to 4 bytes from keyboard buffer.
# Page navigation keys are 3-4 bytes each.
# Specifically, arrow keys are 3.
my $key = $*IN.read(4).decode;
move %dir{$key} if so %dir{$key};
last if $key eq 'q'; # (q)uit
}