每日shell脚本之石头剪刀布
#!/bin/bash
function get_computer_choice() {
local choices=("石头" "剪刀" "布")
echo ${choices[$((RANDOM % 3))]}
}
function get_user_choice() {
read -p "请输入你的选择(石头、剪刀、布):" user_choice
echo $user_choice
}
function judge_result() {
local user_choice=$1
local computer_choice=$2
if [ "$user_choice" == "$computer_choice" ]; then
echo "平局!"
elif [ "$user_choice" == "石头" ] && [ "$computer_choice" == "剪刀" ] || \
[ "$user_choice" == "剪刀" ] && [ "$computer_choice" == "布" ] || \
[ "$user_choice" == "布" ] && [ "$computer_choice" == "石头" ]; then
echo "你赢了!"
else
echo "你输了!"
fi
}
while true; do
computer_choice=$(get_computer_choice)
user_choice=$(get_user_choice)
result=$(judge_result "$user_choice" "$computer_choice")
echo "电脑选择了:$computer_choice,结果:$result"
read -p "是否继续游戏?(y/n):" continue_choice
if [ "$continue_choice" != "y" ]; then
break
fi
done