謝謝電子發(fā)燒友論壇網(wǎng)
謝謝保定飛凌嵌入式技術(shù)有限公司
這里是一個可以在Shell環(huán)境下運(yùn)行的老虎杠子雞蟲游戲程序:
源代碼:
#!/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 "請輸入數(shù)字選擇你的拳: 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中運(yùn)行 bash tiger_pistol_chicken_worm.sh
即可開始游戲。飛凌嵌入式開發(fā)板ADC連上一篇的游戲遙控,(https://bbs.elecfans.com/jishu_2362224_1_1.html),桿機(jī)械臂會做出相應(yīng)動作并按照屏幕上的提示輸入相應(yīng)的數(shù)字選擇(【飛凌i.MX9352開發(fā)板試用】+機(jī)械臂游戲2游戲操縱桿控制四自由度機(jī)械臂(開源)原創(chuàng)首發(fā) - 飛凌嵌入式 - 電子技術(shù)論壇 ?- 廣受歡迎的專業(yè)電子論壇! https://bbs.elecfans.com/jishu_2364822_1_1.html),程序?qū)⑴袛鄤儇?fù)并詢問是否繼續(xù)游戲。最后,當(dāng)您選擇不再玩時,程序?qū)⒔Y(jié)束并顯示感謝信息。飛凌嵌入式
祝您玩得愉快!
謝謝!
審核編輯 黃宇
-
嵌入式
+關(guān)注
關(guān)注
5082文章
19104瀏覽量
304796 -
開發(fā)板
+關(guān)注
關(guān)注
25文章
5032瀏覽量
97371 -
源代碼
+關(guān)注
關(guān)注
96文章
2945瀏覽量
66730 -
機(jī)械臂
+關(guān)注
關(guān)注
12文章
513瀏覽量
24553
發(fā)布評論請先 登錄
相關(guān)推薦
評論