這里是一個可以在Shell環境下運行的老虎杠子雞蟲游戲程序:
源代碼:
#!/bin/bash
# Function to generate random choices
getComputerChoice() {
choices=("杠子" "蟲" "雞" "老虎")
computerChoice=$((RANDOM % 4))
echo ${choices[$computerChoice]}
}
#游戲開始
# Function to determine the winner
getWinner() {
case "$1-$2" in
"杠子-蟲" | "蟲-雞" | "雞-老虎" | "老虎-杠子")
echo "你贏了!"
;;
"蟲-杠子" | "雞-蟲" | "老虎-雞" | "杠子-老虎")
echo "電腦贏了!"
;;
*)
echo "平局!"
;;
esac
}
echo "歡迎來玩杠子-蟲-雞-老虎游戲!"
echo "請輸入數字選擇你的拳: 1-杠子, 2-蟲, 3-雞, 4-老虎."
while true; do
adc_value2=$(cat /sys/bus/iio/devices/iio:device0/in_voltage0_raw)
echo "ADC value2: $adc_value2"
#掃描按鍵2
if (( $(echo "$adc_value2 < 500" | bc -l) )); then
echo "C" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value2 > 3900" | bc -l) )); then
echo "d" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
read -p "請輸入你的拳: " playerChoice
adc_value=$(cat /sys/bus/iio/devices/iio:device0/in_voltage1_raw)
echo "ADC value: $adc_value"
#掃描按鍵1
if (( $(echo "$adc_value < 500" | bc -l) )); then
echo "A" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value > 3900" | bc -l) )); then
echo "b" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
case $playerChoice in
1)
playerChoice="杠子"
;;
2)
playerChoice="蟲"
;;
3)
playerChoice="雞"
;;
4)
playerChoice="老虎"
;;
*)
echo "無效的選擇,請重新輸入."
continue
;;
esac
computerChoice=$(getComputerChoice)
echo "你的選擇: $playerChoice"
echo "電腦的選擇: $computerChoice"
getWinner $playerChoice $computerChoice
adc_value3=$(cat /sys/bus/iio/devices/iio:device0/in_voltage2_raw)
echo "ADC value3: $adc_value3"
#掃描按鍵3
if (( $(echo "$adc_value3 < 500" | bc -l) )); then
echo "e" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value3 > 3900" | bc -l) )); then
echo "f" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
adc_value4=$(cat /sys/bus/iio/devices/iio:device0/in_voltage3_raw)
echo "ADC value4: $adc_value4"
#掃描按鍵4
if (( $(echo "$adc_value4 < 500" | bc -l) )); then
echo "g" > "$port"
# echo 1 > /sys/class/leds/led1/brightness
elif(( $(echo "$adc_value4 > 3900" | bc -l) )); then
echo "h" > "$port"
#echo 0 > /sys/class/leds/led1/brightness
fi
sleep 0.018
read -p "再玩一次?(y/n): " playAgain
if [[ $playAgain != "y" && $playAgain != "Y" ]]; then
break
fi
done
echo "謝謝參與游戲!"
將上述代碼保存為一個名為 tiger_pistol_chicken_worm.sh
的文件,然后在Shell中運行 bash tiger_pistol_chicken_worm.sh
即可開始游戲。飛凌嵌入式開發板ADC連上一篇的游戲遙控,(https://bbs.elecfans.com/jishu_2362224_1_1.html),桿機械臂會做出相應動作并按照屏幕上的提示輸入相應的數字選擇(【飛凌i.MX9352開發板試用】+機械臂游戲2游戲操縱桿控制四自由度機械臂(開源)原創首發 - 飛凌嵌入式 - 電子技術論壇 ?- 廣受歡迎的專業電子論壇! https://bbs.elecfans.com/jishu_2364822_1_1.html),程序將判斷勝負并詢問是否繼續游戲。最后,當您選擇不再玩時,程序將結束并顯示感謝信息。飛凌嵌入式
祝您玩得愉快!
謝謝!
審核編輯 黃宇
-
嵌入式
+關注
關注
5082文章
19104瀏覽量
304816 -
開發板
+關注
關注
25文章
5032瀏覽量
97375 -
源代碼
+關注
關注
96文章
2945瀏覽量
66730 -
機械臂
+關注
關注
12文章
513瀏覽量
24555
發布評論請先 登錄
相關推薦
評論