本文主要是介绍每日shell脚本之石头剪刀布,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
每日shell脚本之石头剪刀布
#!/bin/bash# 石头剪刀布游戏
function get_computer_choice() {local choices=("石头" "剪刀" "布")echo ${choices[$((RANDOM % 3))]}
}function get_user_choice() {read -p "请输入你的选择(石头、剪刀、布):" user_choiceecho $user_choice
}function judge_result() {local user_choice=$1local computer_choice=$2if [ "$user_choice" == "$computer_choice" ]; thenecho "平局!"elif [ "$user_choice" == "石头" ] && [ "$computer_choice" == "剪刀" ] || \[ "$user_choice" == "剪刀" ] && [ "$computer_choice" == "布" ] || \[ "$user_choice" == "布" ] && [ "$computer_choice" == "石头" ]; thenecho "你赢了!"elseecho "你输了!"fi
}while true; docomputer_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_choiceif [ "$continue_choice" != "y" ]; thenbreakfi
done
这篇关于每日shell脚本之石头剪刀布的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!