Repository: wsdjeg/vim2048 Branch: master Commit: 74f5d8a7f3c3 Files: 6 Total size: 9.3 KB Directory structure: gitextract_3fcveti2/ ├── README.md ├── autoload/ │ └── vim2048/ │ ├── logic.vim │ └── ui.vim ├── ftplugin/ │ └── SPVim2048.vim ├── plugin/ │ └── vim2048.vim └── syntax/ └── SPVim2048.vim ================================================ FILE CONTENTS ================================================ ================================================ FILE: README.md ================================================ # 2048.vim > 2048 in vim [![](https://spacevim.org/img/build-with-SpaceVim.svg)](https://spacevim.org) ## Install this plugin is based of SpaceVim's API, you need to install SpaceVim. 1. load the game layer: ```viml call SpaceVim#layers#load('games') ``` 2. and run the game by pressing ` g 2`. ## screenshot ![2017-05-04-00 29 59](https://cloud.githubusercontent.com/assets/13142418/25671079/66354112-3061-11e7-9e16-676bf891ee1a.png) ================================================ FILE: autoload/vim2048/logic.vim ================================================ let s:score = 0 function! vim2048#logic#up() abort let data = s:merge_up(s:split_list(vim2048#ui#get_data())) call vim2048#ui#update(data, s:score) let s:score = 0 endfunction function! vim2048#logic#down() abort let data = s:merge_down(s:split_list(vim2048#ui#get_data())) call vim2048#ui#update(data, s:score) let s:score = 0 endfunction function! vim2048#logic#right() abort let data = s:merge_right(s:split_list(vim2048#ui#get_data())) call vim2048#ui#update(data, s:score) let s:score = 0 endfunction function! vim2048#logic#left() abort let data = s:merge_left(s:split_list(vim2048#ui#get_data())) call vim2048#ui#update(data, s:score) let s:score = 0 endfunction function! s:split_list(data) abort return [ \ [ \ a:data[0], a:data[1], a:data[2], a:data[3] \ ], \ [ \ a:data[4], a:data[5], a:data[6], a:data[7] \ ], \ [ \ a:data[8], a:data[9], a:data[10], a:data[11] \ ], \ [ \ a:data[12], a:data[13], a:data[14], a:data[15] \ ] \ ] endfunction " a list with 4 items function! s:merge(l) abort let l = filter(a:l, '!empty(v:val)') if len(l) == 0 return ['','','',''] elseif len(l) == 1 return [l[0],'','',''] elseif len(l) == 2 if l[0] == l[1] let s:score += l[0] * 2 return [l[0] * 2, '','',''] else return [l[0], l[1],'',''] endif elseif len(l) == 3 if l[0] == l[1] let s:score += l[0] * 2 return [l[0] * 2, l[2],'',''] elseif l[1] == l[2] let s:score += l[1] * 2 return [l[0], l[1] * 2,'',''] else return [l[0], l[1], l[2],''] endif else if l[0] == l[1] if l[2] == l[3] let s:score += l[0] * 2 + l[2] * 2 return [l[0] * 2, l[2] * 2,'',''] else let s:score += l[0] * 2 return [l[0] * 2, l[2], l[3],''] endif elseif l[1] == l[2] let s:score += l[1] * 2 return [l[0], l[1] * 2, l[3],''] else if l[2] == l[3] let s:score += l[2] * 2 return [l[0], l[1], l[2] * 2,''] else return [l[0], l[1], l[2], l[3]] endif endif endif endfunction function! s:merge_up(data) abort let l0 = s:merge([a:data[0][0], a:data[1][0], a:data[2][0], a:data[3][0]]) let l1 = s:merge([a:data[0][1], a:data[1][1], a:data[2][1], a:data[3][1]]) let l2 = s:merge([a:data[0][2], a:data[1][2], a:data[2][2], a:data[3][2]]) let l3 = s:merge([a:data[0][3], a:data[1][3], a:data[2][3], a:data[3][3]]) return [ \ l0[0],l1[0],l2[0],l3[0], \ l0[1],l1[1],l2[1],l3[1], \ l0[2],l1[2],l2[2],l3[2], \ l0[3],l1[3],l2[2],l3[3], \ ] endfunction function! s:merge_down(data) abort let l0 = s:merge([a:data[3][0], a:data[2][0], a:data[1][0], a:data[0][0]]) let l1 = s:merge([a:data[3][1], a:data[2][1], a:data[1][1], a:data[0][1]]) let l2 = s:merge([a:data[3][2], a:data[2][2], a:data[1][2], a:data[0][2]]) let l3 = s:merge([a:data[3][3], a:data[2][3], a:data[1][3], a:data[0][3]]) return [ \ l0[3],l1[3],l2[3],l3[3], \ l0[2],l1[2],l2[2],l3[2], \ l0[1],l1[1],l2[1],l3[1], \ l0[0],l1[0],l2[0],l3[0], \ ] endfunction function! s:merge_right(data) abort let l0 = s:merge([a:data[0][3], a:data[0][2], a:data[0][1], a:data[0][0]]) let l1 = s:merge([a:data[1][3], a:data[1][2], a:data[1][1], a:data[1][0]]) let l2 = s:merge([a:data[2][3], a:data[2][2], a:data[2][1], a:data[2][0]]) let l3 = s:merge([a:data[3][3], a:data[3][2], a:data[3][1], a:data[3][0]]) return [ \ l0[3],l0[2],l0[1],l0[0], \ l1[3],l1[2],l1[1],l1[0], \ l2[3],l2[2],l2[1],l2[0], \ l2[3],l3[2],l3[1],l3[0], \ ] endfunction function! s:merge_left(data) abort let l0 = s:merge(a:data[0]) let l1 = s:merge(a:data[1]) let l2 = s:merge(a:data[2]) let l3 = s:merge(a:data[3]) return l0 + l1 + l2 + l3 endfunction ================================================ FILE: autoload/vim2048/ui.vim ================================================ let s:drawer = SpaceVim#api#import('unicode#box') let s:number = SpaceVim#api#import('data#number') let s:score = ['score', 0, 'max score', 0] function! vim2048#ui#redraw(...) abort if a:0 == 1 call s:init() else call vim2048#ui#random_add_two() endif let score = s:drawer.drawing_box(s:score, 1, 4, 10) let box = s:drawer.drawing_box(s:data,4,4,6) setl modifiable call setline(1, score) call setline(5, box) setl nomodifiable endfunction function! s:init() abort let s:data = map(range(16), '""') let s:score[1] = 0 let n = s:number.random(0, 16) let s:data[n] = 2 let m = s:number.random(0, 16) while m == n let m = s:number.random(0, 16) endwhile let s:data[m] = 2 endfunction function! vim2048#ui#update(data, score) abort let s:data = a:data let s:score[1] += a:score let s:score[3] = max([s:score[1], s:score[3]]) call vim2048#ui#redraw() endfunction function! vim2048#ui#get_data() abort return s:data endfunction function! vim2048#ui#random_add_two() abort let indexs = [] let idx = 0 for a in s:data if empty(a) call add(indexs, idx) endif let idx += 1 endfor if len(indexs) > 0 let s:data[s:number.random(0, len(indexs))] = 2 endif endfunction ================================================ FILE: ftplugin/SPVim2048.vim ================================================ if exists('g:_SPVim2048_ftplugin') finish else let g:_SPVim2048_ftplugin = 1 endif function! SPVim2048#statusline(...) if &ft ==# 'SPVim2048' call airline#extensions#apply_left_override('Vim2048', '') " Alternatively, set the various w:airline_section variables "let w:airline_section_a = 'SpaceVimPluginManager' "let w:airline_section_b = '' "let w:airline_section_c = '' "let w:airline_render_left = 1 "let w:airline_render_right = 0 endif endfunction try call airline#add_statusline_func('SPVim2048#statusline') catch endtry ================================================ FILE: plugin/vim2048.vim ================================================ function! vim2048#start() abort tabnew call setline(1,['','','','','']) setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap cursorline nomodifiable nospell nocursorline set filetype=SPVim2048 call vim2048#ui#redraw(1) call s:defmappings() endfunction function! s:defmappings() abort nnoremap h :call vim2048#logic#left() nnoremap j :call vim2048#logic#down() nnoremap k :call vim2048#logic#up() nnoremap l :call vim2048#logic#right() nnoremap :call vim2048#logic#left() nnoremap :call vim2048#logic#down() nnoremap :call vim2048#logic#up() nnoremap :call vim2048#logic#right() endfunction ================================================ FILE: syntax/SPVim2048.vim ================================================ syntax clear hi ColNumberSplit gui=bold guifg=Grey ctermfg=DarkGreen hi ColNumber2 gui=bold guibg=DarkGreen ctermfg=DarkGreen hi ColNumber4 gui=bold guibg=DarkBlue ctermfg=DarkBlue hi ColNumber8 gui=bold guibg=DarkCyan ctermfg=DarkCyan hi ColNumber16 gui=bold guibg=DarkRed ctermfg=DarkRed hi ColNumber32 gui=bold guibg=DarkMagenta ctermfg=DarkMagenta hi ColNumber64 gui=bold guibg=Red ctermfg=Red hi ColNumber128 gui=bold guibg=Blue ctermfg=Blue hi ColNumber256 gui=bold guibg=Magenta ctermfg=Magenta hi ColNumber512 gui=bold guibg=White ctermfg=White hi ColNumber1024 gui=bold guibg=Green ctermfg=Green hi ColNumber2048 gui=bold guibg=Yellow ctermfg=Yellow guifg=Black " │ 2 │ 4 │ 8 │ 16 │ 32 │ 64 │ 256 │ 512 │ 1024 │ 2048 │ syn match NumberSplit '│' syn match NumberSplit '╭' syn match NumberSplit '─' syn match NumberSplit '╰' syn match NumberSplit '┬' syn match NumberSplit '┴' syn match NumberSplit '╮' syn match NumberSplit '╯' syn match NumberSplit '┼' syn match NumberSplit '├' syn match NumberSplit '┤' hi link NumberSplit ColNumberSplit syn match Number2 '\s\+2\s\+' hi link Number2 ColNumber2 syn match Number4 '\s\+4\s\+' hi link Number4 ColNumber4 syn match Number8 '\s\+8\s\+' hi link Number8 ColNumber8 syn match Number16 '\s\+16\s\+' hi link Number16 ColNumber16 syn match Number32 '\s\+32\s\+' hi link Number32 ColNumber32 syn match Number64 '\s\+64\s\+' hi link Number64 ColNumber64 syn match Number128 '\s\+128\s\+' hi link Number128 ColNumber128 syn match Number256 '\s\+256\s\+' hi link Number256 ColNumber256 syn match Number512 '\s\+512\s\+' hi link Number512 ColNumber512 syn match Number1024 '\s\+1024\s\+' hi link Number1024 ColNumber1024 syn match Number2048 '\s\+2048\s\+' hi link Number2048 ColNumber2048