Bashシェルスクリプトを使ってヌメロン(Numer0n)を作ロン♪ということで作ってみました。
事前にコンピュータが定める4桁の数字をあてに行くゲームです。
Wikipedia:ヌメロンのルールはWikipediaをご参照ください。
今回作成したものは、一回ごとに継続して挑戦するかを確認する仕様となっています。
もしy/nでの回答を行わずに継続して実行したい場合は「-y」オプションをつけてください。
実行例:
# ./numeron.sh --------------------------------------- Please choose 4 numbers from 0 to 9 and type in as 4 digit number [eg.1234]: 1234 --------------------------------------- 0 eat 1 bite continue?[y/n]: y --------------------------------------- Please choose 4 numbers from 0 to 9 and type in as 4 digit number [eg.1234]: 1238 --------------------------------------- 0 eat 2 bite continue?[y/n]: y
「-y」オプションを利用した場合
# ./numeron.sh -y
---------------------------------------
Please choose 4 numbers from 0 to 9 and type in as 4 digit number [eg.1234]: 1234
---------------------------------------
0 eat 3 bite
----> try again
---------------------------------------
Please choose 4 numbers from 0 to 9 and type in as 4 digit number [eg.1234]: 5234
---------------------------------------
0 eat 2 bite
----> try again
シェルスクリプトの記述は以下のとおり
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #!/bin/bash arr=({0..9}) declare -a kotae for i in `shuf --input-range=0-$(( ${ #arr[*]} - 1 )) | head -4` do kotae=( "${kotae[@]}" "${arr[$i]}" ) done ks=` echo ${kotae[@]} | sed -e 's/ //g' ` # cheat #echo $ks a= "y" while [ $a == "y" ]; do userInput= '' while [ -z ${userInput} ] ; do echo "---------------------------------------" echo -n "Please choose 4 numbers from 0 to 9 and type in as 4 digit number [eg.1234]: " read userInput echo "---------------------------------------" dup=` echo $userInput | grep -o . | sort | uniq -c | grep - v "1 [0-9]" | wc -l` if ! [[ $userInput =~ ^[0-9]{4}$ ]] || ! [ $dup - eq 0 ] ; then echo "not good input... try agin!" userInput= '' fi done u=(` echo $userInput | grep -o .`) if [ $userInput = ${ks} ]; then echo " 4 eat!! GREAT!!! " exit else E=0 B=0 for n in {0..3}; do if [ ${u[$n]} = ${kotae[$n]} ]; then E=$((E+1)) elif [ ` echo ${ks} | grep ${u[$n]}` ]; then B=$((B+1)) fi done echo "$E eat $B bite" fi if [ "$1" = "-y" ]; then echo " ----> try again" else echo -n "continue?[y/n]: " read a fi done <button class= "copy-the-code-button" data-style= "button" title= "コピーさせてもらう" >コピー< /button > |
スクリプトのダウンロードはこちらからどぞ
コメント