-
Notifications
You must be signed in to change notification settings - Fork 5
/
go2048.vim
327 lines (287 loc) · 6.45 KB
/
go2048.vim
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
"a little funny game called go2048(http://go2048.com/), but this implemented in VIM script programming and some perl codes.
"*******************************************
"author: satanson
"email: [email protected]
"date: Thu Aug 14 21:37:54 CST 2014
"*******************************************
"HOW TO PLAY
"1. open go2048.vim in VIM editor.
"2. execute command ":so %"
"3. play the game,OPERATING keys as fellows:
" <F2> -- Reset game
" <F3> -- Quit game
" <Up> -- Move up
" <Down> -- Move down
" <Left> -- Move Left
" <Right> -- Move Right
func! s:str(n,char)
return join(map(range(1,a:n,1),"a:char"),"")
endf
func! s:drawVruler(w,n)
return join(map(range(0,a:n,1),"'|'"),s:str(a:w," "))
endf
func! s:drawBoard(cellw,cellh,celln)
let s:old_tabnr=tabpagenr()
echo "s:old_tabnr=" s:old_tabnr
if !exists("s:go2048_tabnr")
tabnew
sil! file go2048
let s:go2048_tabnr=tabpagenr()
exe "tabn ".s:go2048_tabnr
exe "tabclose".s:old_tabnr
endif
let linenum=1
let maxlinenum=(a:cellh+1)*a:celln+1
while linenum<=maxlinenum
if (linenum-1)%(a:cellh+1)==0
let s = s:str((a:cellw+1)*a:celln+1,"-")
let lst = split(s,'\s*')
for i in range(0,a:celln,1)
let lst[i*(a:cellw+1)]='+'
endfor
let s = join (lst,'')
"call setline(linenum,s:str((a:cellw+1)*a:celln+1,"-"))
call setline(linenum,s)
else
call setline(linenum,s:drawVruler(a:cellw,a:celln))
endif
let linenum+=1
endwhile
endf
func! s:renderRow(row,cellw,celln)
let [lbar,rbar]=[1,1+a:cellw+1]
let s="|"
for i in range(0,a:celln-1,1)
let var=""
if a:row[i]
let var.=a:row[i]
endif
let width=len(var)
let padding=a:cellw-width
let [lpadding,rpadding]=[padding/2,padding-padding/2]
let s.=s:str(lpadding," ").var.s:str(rpadding," ")."|"
endfor
return s
endf
func! s:drawMatrix(matrix,cellw,cellh,celln)
for i in range(0,a:celln-1,1)
let linenum=((a:cellh+1)*(2*i+1)+2)/2
call setline(linenum,s:renderRow(a:matrix[i],a:cellw,a:celln))
endfor
endf
func! s:perlInit()
perl<<DONE
sub sample {
my ($b,$e,$n,$d)=@_;
my (%samp,@samp);
while(@samp<$n){
$r=$b+int(rand($e-$b));
if ($d || !exists $samp{$r}){
$samp{$r}=1;
push @samp,$r;
}
}
return @samp;
}
sub squeeze {
my ($k,$i,$ok)=(0,1,0);
my @array=@_;
while ($i<@array){
if ($array[$i]!=0) {
if ($i!=$k && $array[$i] == $array[$k]) {
$array[$k]+=$array[$i];
++$k;
$array[$i]=0;
$ok=1;
}elsif ($array[$k]==0) {
$array[$k]=$array[$i];
$array[$i]=0;
$ok=1;
}elsif ($k+1!=$i) {
++$k;
$array[$k]=$array[$i];
$array[$i]=0;
$ok=1;
} else {
++$k;
}
}
}continue{++$i}
return ($ok,\@array);
}
sub squeezeTo {
my ($dir,$n,@m) = @_;
foreach my $i (1..$n){
my @i=();
if ($dir eq "left"){
@i=map {($i-1)*$n+$_-1} 1..$n;
}elsif($dir eq "right"){
@i=map {($i-1)*$n+($n-$_)} 1..$n;
}elsif($dir eq "top"){
@i=map {($_-1)*$n+$i-1} 1..$n;
}elsif($dir eq "bottom"){
@i=map {($n-$_)*$n+$i-1} 1..$n;
}
my ($ok,$array)=squeeze(@m[@i]);
if ($ok){
return $ok;
}
}
return 0;
}
sub canSqueeze {
my ($n,@m) = @_;
my $nz=()=grep{not $_} @m;
return 1 if $nz;
return squeezeTo("left",$n,@m) ||
squeezeTo("right",$n,@m) ||
squeezeTo("top",$n,@m) ||
squeezeTo("bottom",$n,@m);
}
sub shiftMatrix {
my ($dir,$n,$mat)=@_;
if (!canSqueeze($n,@{$mat})) {return 0;}
my $squeezed=0;
foreach my $i (1..$n){
my @i=();
if ($dir eq "left"){
@i=map {($i-1)*$n+$_-1} 1..$n;
}elsif($dir eq "right"){
@i=map {($i-1)*$n+($n-$_)} 1..$n;
}elsif($dir eq "top"){
@i=map {($_-1)*$n+$i-1} 1..$n;
}elsif($dir eq "bottom"){
@i=map {($n-$_)*$n+$i-1} 1..$n;
}
my ($ok,$array)=squeeze(@{$mat}[@i]);
if ($ok){
$squeezed=1;
@{$mat}[@i]=@$array;
}
}
my @rest=();
foreach (0..$#{$mat}){push @rest,$_ if $mat->[$_]==0}
if ($squeezed && @rest){
$mat->[$rest[((sample(0,scalar(@rest),1,1))[0])]]=((2,2,2,4)[((sample(0,4,1,1))[0])]);
}
return 1;
}
DONE
endf
call s:perlInit()
func! s:sample(b,e,n,d)
let [@b,@e,@n,@d]=[a:b,a:e,a:n,a:d]
perl<<DONE
($b,$e,$n,$d)=map{(VIM::Eval('@'.$_))[1]} qw(b e n d);
@samp=sample($b,$e,$n,$d);
VIM::SetOption("statusline:".join(",",@samp));
DONE
let samp=split(&statusline,",")
set statusline&
return samp
endf
func! s:xy(n,celln)
return [a:n/a:celln,a:n%a:celln]
endf
func! s:n(x,y,celln)
return a:x*a:celln+a:y
endf
func! s:new2_4()
return [2,2,2,4][s:sample(0,4,1,1)[0]]
endf
func! s:initMatrix(celln)
let row=map(range(1,a:celln,1),0)
let matrix=[]
for r in range(1,a:celln,1)
call add(matrix,deepcopy(row))
endfor
let pos=s:sample(0,a:celln*a:celln,2,0)
for p in pos
let [x,y]=s:xy(p,a:celln)
let matrix[x][y]=s:new2_4()
endfor
return matrix
endf
func! s:flat(matrix)
let array=[]
for row in a:matrix
call extend(array,row)
endfor
return join(array,",")
endf
func! s:deflat(csv,n)
let array=split(a:csv,",")
let matrix=[]
for i in range(1,a:n,1)
call add(matrix,array[(i-1)*a:n : i*a:n-1])
endfor
return matrix
endf
func! s:shiftMatrix(dir,n,matrix)
let m=s:flat(a:matrix)
let [@d,@n,@m]=[a:dir,a:n,m]
perl<<DONE
my ($d,$n,$m)=map{((VIM::Eval('@'.$_))[1])} qw(d n m);
my @m=split /,/,$m;
if (shiftMatrix($d,$n,\@m)){
VIM::SetOption("statusline:".join(",",@m));
}else {
VIM::SetOption("statusline:GameOver");
}
DONE
let rc=&statusline
set statusline&
if rc=="GameOver"
return []
else
return s:deflat(rc,a:n)
endif
endf
func! s:printMatrix(matrix)
echo s:str(10,'*')
for row in a:matrix
echo row
endfor
echo s:str(10,'*')
endf
mapclear
mapclear!
imapclear
omapclear
nmapclear
vmapclear
cmapclear
smapclear
xmapclear
lmapclear
command! -nargs=0 Reset call s:reset()
command! -nargs=0 Quit call s:quit()
command! -nargs=1 Shift call s:shift(<f-args>)
nmap <Up> :Shift top<CR><Esc>
nmap <Down> :Shift bottom<CR><Esc>
nmap <Left> :Shift left<CR><Esc>
nmap <Right> :Shift right<CR><Esc>
nmap <F2> :Reset<CR><Esc>
nmap <F3> :Quit<CR><Esc>
let [s:cellw,s:cellh,s:celln]=[10,5,4]
let s:mat=s:initMatrix(s:celln)
func! s:reset()
set statusline&
let s:mat=s:initMatrix(s:celln)
call s:drawBoard(s:cellw,s:cellh,s:celln)
call s:drawMatrix(s:mat,s:cellw,s:cellh,s:celln)
endf
func! s:quit()
exe "quit!"
endf
func! s:shift(dir)
if !empty(s:mat)
let s:mat=s:shiftMatrix(a:dir,s:celln,s:mat)
endif
if empty(s:mat)
let &statusline="Game Over, Play Again? (<F2>:reset,<F3>:quit"
else
call s:drawMatrix(s:mat,s:cellw,s:cellh,s:celln)
endif
endf
call s:reset()