lose() { clear echo "You lost" echo "Score: $score" stty -echo read stty echo exit } tput smcup printf "\033]0;2048\007" score="0" declare -a matrix for item in {0..15} do matrix+=("0") done echo "Use WASD to move." echo "Combine numbers and try to get a 2048!" echo "Press ENTER to start game" stty -echo read stty echo while true do printf "\e[8;19;33t" clear echo " Score: $score ┌───────┬───────┬───────┬───────┐ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ├───────┼───────┼───────┼───────┤ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ├───────┼───────┼───────┼───────┤ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ├───────┼───────┼───────┼───────┤ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ └───────┴───────┴───────┴───────┘" found=0 for item in {0..15} do if [ ${matrix[$item]} == "0" ] then found=1 fi done if [ $found == "0" ] then lose fi item=$(($RANDOM % 16)) while [ ${matrix[$item]} != "0" ] do item=$(($RANDOM % 16)) done matrix[$item]="1" for row in {0..3} do for col in {0..3} do tput cup $((4 * $row + 3)) $((8 * $col + 2)) item=$((4 * $row + $col)) if [ ${matrix[$item]} != 0 ] then printf ${matrix[$item]} fi done done tput cup 19 0 read -n 1 key if [ $key == "w" ] then for n in {1..3} do for row in {1..3} do for col in {0..3} do item=$((4 * $row + $col)) if [ ${matrix[$(($item - 4))]} == "0" ] then matrix[$(($item - 4))]=${matrix[$item]} matrix[$item]="0" fi if [ ${matrix[$item]} == ${matrix[$(($item - 4))]} ] then matrix[$(($item - 4))]=$((2 * ${matrix[$item]})) matrix[$item]="0" fi done done done elif [ $key == "a" ] then for n in {1..3} do for col in {1..3} do for row in {0..3} do item=$((4 * $row + $col)) if [ ${matrix[$(($item - 1))]} == "0" ] then matrix[$(($item - 1))]=${matrix[$item]} matrix[$item]="0" fi if [ ${matrix[$item]} == ${matrix[$(($item - 1))]} ] then matrix[$(($item - 1))]=$((2 * ${matrix[$item]})) matrix[$item]="0" fi done done done elif [ $key == "s" ] then for n in {1..3} do for row in {0..2} do for col in {0..3} do item=$((4 * $row + $col)) if [ ${matrix[$(($item + 4))]} == "0" ] then matrix[$(($item + 4))]=${matrix[$item]} matrix[$item]="0" fi if [ ${matrix[$item]} == ${matrix[$(($item + 4))]} ] then matrix[$(($item + 4))]=$((2 * ${matrix[$item]})) matrix[$item]="0" fi done done done elif [ $key == "d" ] then for n in {1..3} do for col in {0..2} do for row in {0..3} do item=$((4 * $row + $col)) if [ ${matrix[$(($item + 1))]} == "0" ] then matrix[$(($item + 1))]=${matrix[$item]} matrix[$item]="0" fi if [ ${matrix[$item]} == ${matrix[$(($item + 1))]} ] then matrix[$(($item + 1))]=$((2 * ${matrix[$item]})) matrix[$item]="0" fi done done done else lose fi ((score++)) done