#!/bin/bash
declare -a t #定义卡牌数组t
for i in {1..12};do
t[i-1]=" "
done
win=0 #重置获胜条件
RA(){ #定义RA函数,用于生成1个随机1-n不重复数字的数组
unset i ran random test random2
declare -a random
for ((i=0;i<=$(($1-1));i++));do
random[$i]=$i
done
declare -a random2
test=$1
i=0
while [ $test -ge 1 ];do
ran=$(($[RANDOM % $1])) #随机一个0-n的数字
if [[ ${random[$ran]} =~ [0-9]{1,2} ]];then #如果原数组的随机位数有数字
random2[$i]=${random[$ran]}
random[$ran]="used"
test=$(($test-1))
i=$(($i+1))
else
continue
fi
done
echo ${random2[@]}
}
unicode=(○ □ ☆ ● ■ ★)
for i in {0..5};do
k=$(( $i % 6 ))
eval "TYPE_$i[0]=${unicode[$k]}"
done
a=0
time=0
declare -a all
declare -a b
RA12=RA 12
for i in {1..12};do
b[$i-1]=echo $RA12 | awk '{print $'$i'}'
done
for i in ${b[@]};do
if [ $time -eq 0 ];then
time=1
eval "all[$i]=${TYPE_$a[@]}"
else
eval "all[$i]=${TYPE_$a[@]}"
a=$(($a+1))
time=0
fi
done
fanpaitimes=0
while true;do
cat <<EOF
A B C D
1 | ${t[0]} | ${t[1]} | ${t[2]} | ${t[3]} |
|----------------
2 | ${t[4]} | ${t[5]} | ${t[6]} | ${t[7]} |
|----------------
3 | ${t[8]} | ${t[9]} | ${t[10]} | ${t[11]} |
EOF
read -p "请选择翻牌的位置,输入第几列(A-D): " column
read -p "请选择翻牌的位置,输入第几行(1-3): " row
if [[ $fanpaitimes -eq 0 ]];then
for i in {0..11};do
[[ "${t[$i]}" != "✔" ]] && t[$i]=" "
done
fi
if [ $fanpaitimes -eq 0 ];then
if [[ $column != [A-D] ]] || [[ $row -lt 1 || $row -gt 3 ]];then
echo "请输入正确的位置"
continue
else
num=$(( ($(printf "%d" "'$column'")-64)+($row-1)*4 -1 ))
member=${all[$num]}
fi
if [[ "${t[$num]}" != " " ]]; then
echo "该位置已被占用,请选择其他位置。"
continue
else
t[$num]=${member:0:1}
fanpaitimes=1
fi
else
if [[ $column != [A-D] ]] || [[ $row -lt 1 || $row -gt 3 ]];then
echo "请输入正确的位置"
continue
else
num1=$(( ($(printf "%d" "'$column'")-64)+($row-1)*4 -1 ))
member=${all[$num1]}
fi
if [[ "${t[$num1]}" != " " ]]; then
echo "该位置已被占用,请选择其他位置。"
continue
else
t[$num1]=${member:0:1}
fanpaitimes=0
fi
fi
if [[ $fanpaitimes -eq 0 ]];then
for i in {0..10};do
if [[ ${b[$i]} -eq $num ]] && [[ ${b[$i+1]} -eq $num1 ]] && [[ $(($i % 2 )) -eq 0 ]] || ( [[ ${b[$i]} -eq $num1 ]] && [[ ${b[$i+1]} -eq $num ]] && [[ $(($i % 2 )) -eq 0 ]] );then
t[$num]="✔"
t[$num1]="✔"
break
else
continue
fi
done
fi
for i in {0..11};do
[[ "${t[$i]}" = "✔" ]] && win=$(($win+1))
done
echo "已完成:$win"
if [ $win -eq 12 ];then
echo "恭喜你赢了"
break
else
win=0
fi
done