bash 脚本小游戏 2048
通过方向键玩游戏 q键退出 不支持撤销操作 不判断游戏是否game over (玩家自觉按q键退出)
默认4x4矩阵 可通过命令行参数(第1个参数)设置为NxN矩阵 (4<=N && N<=9)
#!/bin/bashset -eN=${1:-4}(( 3 < N && N < 10 )) || exitdo_exit() { echo -ne "\e[?1049l" exit}init_draw() { echo -ne "\e[?1049h" stty -echo echo -ne "\e[?25l" trap 'stty echo; echo -ne "\e[?25h"' exit trap do_exit INT}skip() { local a=($*) local j=0 local i for ((i=0; i0; j--)); do echo 0 done}move() { local a=($*) local i for ((i=0; i =0; i--)); do for ((j=0; j =0; j--)); do echo ${a[N*j+i]} done done}flip() { local a=($*) local i local j for ((i=0; i =0; j--)); do echo ${a[N*i+j]} done done}add() { local a=($*) local j=0 local i for ((i=0; i <3; k++)); do for ((j=0; j 99)); then printf " %-5s\e[0m" $v else printf " %-4s\e[0m" $v fi else printf " \e[0m" fi done printf "\n" done printf "\n" done}draw2() { draw $* sleep 0.1 matrix=$(add $*) draw $matrix}left() { draw2 $(move $matrix)}right() { draw2 $(flip $(move $(flip $matrix)))}up() { draw2 $(rotate_right $(move $(rotate_left $matrix)))}down() { draw2 $(rotate_left $(move $(rotate_right $matrix)))}init_matrixinit_drawdraw $matrixwhile true; do read -n1 C case $C in A) up; ;; B) down; ;; C) right; ;; D) left; ;; q) do_exit; ;; esacdone