謝謝電子發(fā)燒友論壇網(wǎng)
謝謝保定飛凌嵌入式技術(shù)有限公司
這里是一個(gè)可以在Shell環(huán)境下運(yùn)行的老虎杠子雞蟲(chóng)游戲程序:
源代碼:
#!/bin/bash
# Function to generate random choices
getComputerChoice() {
choices=("杠子" "蟲(chóng)" "雞" "老虎")
computerChoice=$((RANDOM % 4))
echo ${choices[$computerChoice]}
}
#游戲開(kāi)始
# Function to determine the winner
getWinner() {
case "$1-$2" in
"杠子-蟲(chóng)" | "蟲(chóng)-雞" | "雞-老虎" | "老虎-杠子")
echo "你贏了!"
;;
"蟲(chóng)-杠子" | "雞-蟲(chóng)" | "老虎-雞" | "杠子-老虎")
echo "電腦贏了!"
;;
*)
echo "平局!"
;;
esac
}
echo "歡迎來(lái)玩杠子-蟲(chóng)-雞-老虎游戲!"
echo "請(qǐng)輸入數(shù)字選擇你的拳: 1-杠子, 2-蟲(chóng), 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 "請(qǐng)輸入你的拳: " 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="蟲(chóng)"
;;
3)
playerChoice="雞"
;;
4)
playerChoice="老虎"
;;
*)
echo "無(wú)效的選擇,請(qǐng)重新輸入."
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 "謝謝參與游戲!"
將上述代碼保存為一個(gè)名為 tiger_pistol_chicken_worm.sh
的文件,然后在Shell中運(yùn)行 bash tiger_pistol_chicken_worm.sh
即可開(kāi)始游戲。飛凌嵌入式開(kāi)發(fā)板ADC連上一篇的游戲遙控,(https://bbs.elecfans.com/jishu_2362224_1_1.html),桿機(jī)械臂會(huì)做出相應(yīng)動(dòng)作并按照屏幕上的提示輸入相應(yīng)的數(shù)字選擇(【飛凌i.MX9352開(kāi)發(fā)板試用】+機(jī)械臂游戲2游戲操縱桿控制四自由度機(jī)械臂(開(kāi)源)原創(chuàng)首發(fā) - 飛凌嵌入式 - 電子技術(shù)論壇 ?- 廣受歡迎的專(zhuān)業(yè)電子論壇! https://bbs.elecfans.com/jishu_2364822_1_1.html),程序?qū)⑴袛鄤儇?fù)并詢(xún)問(wèn)是否繼續(xù)游戲。最后,當(dāng)您選擇不再玩時(shí),程序?qū)⒔Y(jié)束并顯示感謝信息。飛凌嵌入式
祝您玩得愉快!
謝謝!
審核編輯 黃宇
-
嵌入式
+關(guān)注
關(guān)注
5035文章
18762瀏覽量
297260 -
開(kāi)發(fā)板
+關(guān)注
關(guān)注
25文章
4729瀏覽量
95938 -
源代碼
+關(guān)注
關(guān)注
96文章
2942瀏覽量
66399 -
機(jī)械臂
+關(guān)注
關(guān)注
12文章
500瀏覽量
24236
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論