shell脚本2048小游戏
时间: 2023-08-28 13:04:53 浏览: 128
shell脚本编写的俄罗斯方块游戏代码
以下是一个简单的 shell 脚本 2048 小游戏,你可以在终端中运行它:
```shell
#!/bin/bash
# 初始化游戏
declare -A board
score=0
gameover=false
function init_board {
for i in {0..3}; do
for j in {0..3}; do
board[$i,$j]=0
done
done
add_number
add_number
}
function add_number {
local empty=()
for i in {0..3}; do
for j in {0..3}; do
if [[ ${board[$i,$j]} -eq 0 ]]; then
empty+=($i,$j)
fi
done
done
if [[ ${#empty[@]} -gt 0 ]]; then
local idx=$(( RANDOM % ${#empty[@]} ))
local pos=${empty[$idx]}
local val=$(( RANDOM % 2 * 2 + 2 ))
board[${pos%,*},${pos#*,}]=$val
fi
}
function move_left {
local moved=false
for i in {0..3}; do
for j in {1..3}; do
local k=$j
while [[ $k -gt 0 && ${board[$i,$((k-1))]} -eq 0 ]]; do
board[$i,$((k-1))]=${board[$i,$k]}
board[$i,$k]=0
k=$((k-1))
moved=true
done
if [[ $k -gt 0 && ${board[$i,$((k-1))]} -eq ${board[$i,$k]} ]]; then
board[$i,$((k-1))]=$(( ${board[$i,$k]} * 2 ))
score=$((score + ${board[$i,$((k-1))]}))
board[$i,$k]=0
moved=true
fi
done
done
$moved
}
function move_right {
local moved=false
for i in {0..3}; do
for j in {2..0..-1}; do
local k=$j
while [[ $k -lt 3 && ${board[$i,$((k+1))]} -eq 0 ]]; do
board[$i,$((k+1))]=${board[$i,$k]}
board[$i,$k]=0
k=$((k+1))
moved=true
done
if [[ $k -lt 3 && ${board[$i,$((k+1))]} -eq ${board[$i,$k]} ]]; then
board[$i,$((k+1))]=$(( ${board[$i,$k]} * 2 ))
score=$((score + ${board[$i,$((k+1))]}))
board[$i,$k]=0
moved=true
fi
done
done
$moved
}
function move_up {
local moved=false
for j in {0..3}; do
for i in {1..3}; do
local k=$i
while [[ $k -gt 0 && ${board[$((k-1)),$j]} -eq 0 ]]; do
board[$((k-1)),$j]=${board[$k,$j]}
board[$k,$j]=0
k=$((k-1))
moved=true
done
if [[ $k -gt 0 && ${board[$((k-1)),$j]} -eq ${board[$k,$j]} ]]; then
board[$((k-1)),$j]=$(( ${board[$k,$j]} * 2 ))
score=$((score + ${board[$((k-1)),$j]}))
board[$k,$j]=0
moved=true
fi
done
done
$moved
}
function move_down {
local moved=false
for j in {0..3}; do
for i in {2..0..-1}; do
local k=$i
while [[ $k -lt 3 && ${board[$((k+1)),$j]} -eq 0 ]]; do
board[$((k+1)),$j]=${board[$k,$j]}
board[$k,$j]=0
k=$((k+1))
moved=true
done
if [[ $k -lt 3 && ${board[$((k+1)),$j]} -eq ${board[$k,$j]} ]]; then
board[$((k+1)),$j]=$(( ${board[$k,$j]} * 2 ))
score=$((score + ${board[$((k+1)),$j]}))
board[$k,$j]=0
moved=true
fi
done
done
$moved
}
function print_board {
clear
echo -n "Score: $score"
for i in {0..3}; do
echo
for j in {0..3}; do
printf "%4s" ${board[$i,$j]}
done
done
echo
}
function check_gameover {
for i in {0..3}; do
for j in {0..3}; do
if [[ ${board[$i,$j]} -eq 0 ]]; then
return
fi
if [[ $i -lt 3 && ${board[$i,$j]} -eq ${board[$((i+1)),$j]} ]]; then
return
fi
if [[ $j -lt 3 && ${board[$i,$j]} -eq ${board[$i,$((j+1))]} ]]; then
return
fi
done
done
gameover=true
}
function play_game {
init_board
while ! $gameover; do
print_board
read -s -n1 dir
case $dir in
a) move_left;;
d) move_right;;
w) move_up;;
s) move_down;;
*) continue;;
esac
add_number
check_gameover
done
print_board
echo "Game over! Your score is: $score"
}
play_game
```
运行时,你可以通过输入a、s、d、w来控制方块的移动。注意,脚本需要在支持 Bash 的终端中运行。
阅读全文