[Level 3] 1A2B shell game
Another game.
Wish this helps.
regards,
Stanley Huang
#!/usr/bin/bash
showUsage() {
cat <<EOF
Usage:
$0
Ex.
$0
EOF
}
getNum() {
#set -vx
sNum="0|1|2|3|4|5|6|7|8|9"
sAns=""
declare -i n1=$RANDOM*10/32767+1
declare -i n2=$RANDOM*9/32767+1
declare -i n3=$RANDOM*8/32767+1
declare -i n4=$RANDOM*7/32767+1
s=`echo $sNum | cut -d'|' -f$n1` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n2` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n3` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n4` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
echo $sAns
}
# check $sAns $sGuess
checkNum() {
sAns=$1
sGuess=$2
declare -i A=0
declare -i B=0
sA1=`echo ${sAns:0:1}`
sA2=`echo ${sAns:1:1}`
sA3=`echo ${sAns:2:1}`
sA4=`echo ${sAns:3:1}`
sG1=`echo ${sGuess:0:1}`
sG2=`echo ${sGuess:1:1}`
sG3=`echo ${sGuess:2:1}`
sG4=`echo ${sGuess:3:1}`
if [ $sAns == $sGuess ]
then
echo "Bingo...($sOAns:$sGuess)" && return 0
else
[ "$sA1" == "$sG1" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA1//"`
[ "$sA2" == "$sG2" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA2//"`
[ "$sA3" == "$sG3" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA3//"`
[ "$sA4" == "$sG4" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA4//"`
echo $sAns | grep $sG1 >/dev/null && B=$B+1
echo $sAns | grep $sG2 >/dev/null && B=$B+1
echo $sAns | grep $sG3 >/dev/null && B=$B+1
echo $sAns | grep $sG4 >/dev/null && B=$B+1
echo $sGuess: $A"A"$B"B ..." >> $0.log
cat $0.log
return 1
fi
}
######################################################## main
sOAns=`getNum`
#echo "sOAns=$sOAns"
declare -i nCount=0
cat /dev/null > $0.log
while true
do
read -p "Please enter 4 digit number or quit (you have guess $nCount times): " sGuess
[ `echo $sGuess | awk '{print(length($1))}'` -ne 4 ] && echo "shorter then 4 digits..." && continue
[ $sGuess == "quit" ] && echo "The answer is: $sOAns" && break
checkNum $sOAns $sGuess && break
nCount=$nCount+1
done
rm $0.log
cat <<EOF
Usage:
$0
Ex.
$0
EOF
}
getNum() {
#set -vx
sNum="0|1|2|3|4|5|6|7|8|9"
sAns=""
declare -i n1=$RANDOM*10/32767+1
declare -i n2=$RANDOM*9/32767+1
declare -i n3=$RANDOM*8/32767+1
declare -i n4=$RANDOM*7/32767+1
s=`echo $sNum | cut -d'|' -f$n1` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n2` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n3` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
s=`echo $sNum | cut -d'|' -f$n4` && sAns=$sAns$s && sNum=`echo $sNum | sed -e "s/$s//" | sed -e 's/||/|/g' | sed -e 's/^|//g' | sed -e s'/|$//'`
echo $sAns
}
# check $sAns $sGuess
checkNum() {
sAns=$1
sGuess=$2
declare -i A=0
declare -i B=0
sA1=`echo ${sAns:0:1}`
sA2=`echo ${sAns:1:1}`
sA3=`echo ${sAns:2:1}`
sA4=`echo ${sAns:3:1}`
sG1=`echo ${sGuess:0:1}`
sG2=`echo ${sGuess:1:1}`
sG3=`echo ${sGuess:2:1}`
sG4=`echo ${sGuess:3:1}`
if [ $sAns == $sGuess ]
then
echo "Bingo...($sOAns:$sGuess)" && return 0
else
[ "$sA1" == "$sG1" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA1//"`
[ "$sA2" == "$sG2" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA2//"`
[ "$sA3" == "$sG3" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA3//"`
[ "$sA4" == "$sG4" ] && A=$A+1 && sAns=`echo $sAns | sed -e "s/$sA4//"`
echo $sAns | grep $sG1 >/dev/null && B=$B+1
echo $sAns | grep $sG2 >/dev/null && B=$B+1
echo $sAns | grep $sG3 >/dev/null && B=$B+1
echo $sAns | grep $sG4 >/dev/null && B=$B+1
echo $sGuess: $A"A"$B"B ..." >> $0.log
cat $0.log
return 1
fi
}
######################################################## main
sOAns=`getNum`
#echo "sOAns=$sOAns"
declare -i nCount=0
cat /dev/null > $0.log
while true
do
read -p "Please enter 4 digit number or quit (you have guess $nCount times): " sGuess
[ `echo $sGuess | awk '{print(length($1))}'` -ne 4 ] && echo "shorter then 4 digits..." && continue
[ $sGuess == "quit" ] && echo "The answer is: $sOAns" && break
checkNum $sOAns $sGuess && break
nCount=$nCount+1
done
rm $0.log
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment