一个用Shell脚本写的猜丁壳游戏
生活随笔
收集整理的這篇文章主要介紹了
一个用Shell脚本写的猜丁壳游戏
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>>
這幾天在學習Linux的Shell腳本,寫了一個猜丁殼游戲。
游戲規則:每回合玩家和電腦各出石頭、剪子、布中的一個,分別以0、1、2表示,規定石頭可以勝過剪子、剪子可以勝過布、布可以勝過石頭,如果玩家和電腦所出相同,則進入下一回合繼續比試,否則游戲結束,本回合的勝者即為游戲的獲勝方。
#!/bin/sh# generate random numbers function rand() {min=$1max=$(($2-$min+1))num=$(($RANDOM+10000))echo $(($num%$max+$min)) }result=0 echo "Game Start!" while [ true ]; do# Stone win Scissors# Scissors win Cloth# Cloth win Stoneecho "0-Stone, 1-Scissors, 2-Cloth, 3-GiveUp!"read choicernd=$(rand 0 2)case "$choice" in0)if [ $rnd -eq 0 ]; thenecho "Stone vs Stone: Draw"elif [ $rnd -eq 1 ]; thenecho "Stone vs Scissors: Win!"result=1elseecho "Stone vs Cloth: Defeat!"result=-1fi;;1)if [ $rnd -eq 0 ]; thenecho "Scissors vs Stone: Defeat!"result=-1elif [ $rnd -eq 1 ]; thenecho "Scissors vs Scissors: Draw"elseecho "Scissors vs Cloth: Win!"result=1fi;;2)if [ $rnd -eq 0 ]; thenecho "Cloth vs Stone: Win!"result=1elif [ $rnd -eq 1 ]; thenecho "Cloth vs Scissors: Defeat!"result=-1elseecho "Cloth vs Cloth: Draw"fi;;3)result=-1;;*)echo "bad choice";;esac# if draw then continue else breakif [ $result -eq 0 ]; thencontinue elsebreak fidoneecho "End of program" echo " "exit 0運行結果
轉載于:https://my.oschina.net/Tsybius2014/blog/284898
總結
以上是生活随笔為你收集整理的一个用Shell脚本写的猜丁壳游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 虎牙直播app怎么更新(直播个人中心)
- 下一篇: textview多行ellipsize=