#/bin/bash
read -p "来一场紧张刺激的五子棋游戏吧!按任意键继续" contin
for i in {1..225};do
a[$i-1]=" "
done
a[112]='*'
a[48]='*'
a[56]='*'
a[168]='*'
a[176]='*'
count=0 #重置计数,用于判断轮到哪方落子
blackwin=0 #重置黑棋获胜参数
whitewin=0 #重置白棋获胜参数
#:s/\v(\d+)/\=printf('%d', str2nr(submatch(1)) + 15)/g 这条命令用于快捷修改某一行所有数字加一个特定值
cat <<EOF
A B C D E F G H I J K L M N O
1| ${a[0]} | ${a[1]} | ${a[2]} | ${a[3]} | ${a[4]} | ${a[5]} | ${a[6]} | ${a[7]} | ${a[8]} | ${a[9]} | ${a[10]} | ${a[11]} | ${a[12]} | ${a[13]} | ${a[14]} |
2| ${a[15]} | ${a[16]} | ${a[17]} | ${a[18]} | ${a[19]} | ${a[20]} | ${a[21]} | ${a[22]} | ${a[23]} | ${a[24]} | ${a[25]} | ${a[26]} | ${a[27]} | ${a[28]} | ${a[29]} |
3| ${a[30]} | ${a[31]} | ${a[32]} | ${a[33]} | ${a[34]} | ${a[35]} | ${a[36]} | ${a[37]} | ${a[38]} | ${a[39]} | ${a[40]} | ${a[41]} | ${a[42]} | ${a[43]} | ${a[44]} |
4| ${a[45]} | ${a[46]} | ${a[47]} | ${a[48]} | ${a[49]} | ${a[50]} | ${a[51]} | ${a[52]} | ${a[53]} | ${a[54]} | ${a[55]} | ${a[56]} | ${a[57]} | ${a[58]} | ${a[59]} |
5| ${a[60]} | ${a[61]} | ${a[62]} | ${a[63]} | ${a[64]} | ${a[65]} | ${a[66]} | ${a[67]} | ${a[68]} | ${a[69]} | ${a[70]} | ${a[71]} | ${a[72]} | ${a[73]} | ${a[74]} |
6| ${a[75]} | ${a[76]} | ${a[77]} | ${a[78]} | ${a[79]} | ${a[80]} | ${a[81]} | ${a[82]} | ${a[83]} | ${a[84]} | ${a[85]} | ${a[86]} | ${a[87]} | ${a[88]} | ${a[89]} |
7| ${a[90]} | ${a[91]} | ${a[92]} | ${a[93]} | ${a[94]} | ${a[95]} | ${a[96]} | ${a[97]} | ${a[98]} | ${a[99]} | ${a[100]} | ${a[101]} | ${a[102]} | ${a[103]} | ${a[104]} |
8| ${a[105]} | ${a[106]} | ${a[107]} | ${a[108]} | ${a[109]} | ${a[110]} | ${a[111]} | ${a[112]} | ${a[113]} | ${a[114]} | ${a[115]} | ${a[116]} | ${a[117]} | ${a[118]} | ${a[119]} |
9| ${a[120]} | ${a[121]} | ${a[122]} | ${a[123]} | ${a[124]} | ${a[125]} | ${a[126]} | ${a[127]} | ${a[128]} | ${a[129]} | ${a[130]} | ${a[131]} | ${a[132]} | ${a[133]} | ${a[134]} |
10| ${a[135]} | ${a[136]} | ${a[137]} | ${a[138]} | ${a[139]} | ${a[140]} | ${a[141]} | ${a[142]} | ${a[143]} | ${a[144]} | ${a[145]} | ${a[146]} | ${a[147]} | ${a[148]} | ${a[149]} |
11| ${a[150]} | ${a[151]} | ${a[152]} | ${a[153]} | ${a[154]} | ${a[155]} | ${a[156]} | ${a[157]} | ${a[158]} | ${a[159]} | ${a[160]} | ${a[161]} | ${a[162]} | ${a[163]} | ${a[164]} |
12| ${a[165]} | ${a[166]} | ${a[167]} | ${a[168]} | ${a[169]} | ${a[170]} | ${a[171]} | ${a[172]} | ${a[173]} | ${a[174]} | ${a[175]} | ${a[176]} | ${a[177]} | ${a[178]} | ${a[179]} |
13| ${a[180]} | ${a[181]} | ${a[182]} | ${a[183]} | ${a[184]} | ${a[185]} | ${a[186]} | ${a[187]} | ${a[188]} | ${a[189]} | ${a[190]} | ${a[191]} | ${a[192]} | ${a[193]} | ${a[194]} |
14| ${a[195]} | ${a[196]} | ${a[197]} | ${a[198]} | ${a[199]} | ${a[200]} | ${a[201]} | ${a[202]} | ${a[203]} | ${a[204]} | ${a[205]} | ${a[206]} | ${a[207]} | ${a[208]} | ${a[209]} |
15| ${a[210]} | ${a[211]} | ${a[212]} | ${a[213]} | ${a[214]} | ${a[215]} | ${a[216]} | ${a[217]} | ${a[218]} | ${a[219]} | ${a[220]} | ${a[221]} | ${a[222]} | ${a[223]} | ${a[224]} |
EOF
for i in {1..225};do
if [ $((count % 2)) -ne 0 ];then
player='O'
else
player='@'
fi
read -p "请选择要下的位置,输入第几列(A-O): " column
read -p "请选择要下的位置,输入第几行(1-15): " row
if [[ $column != [A-O] ]] || [[ $row -lt 1 || $row -gt 15 ]];then
echo "请输入正确的位置"
continue
else
num=$(( ($(printf "%d" "'$column'")-64)+($row-1)*15 -1 ))
fi
if [ "${a[$num]}" == "O" ] || [ "${a[$num]}" == "@" ]; then
echo "该位置已被占用,请选择其他位置。"
continue
else
a[$num]=$player
fi
cat <<EOF
A B C D E F G H I J K L M N O
1| ${a[0]} | ${a[1]} | ${a[2]} | ${a[3]} | ${a[4]} | ${a[5]} | ${a[6]} | ${a[7]} | ${a[8]} | ${a[9]} | ${a[10]} | ${a[11]} | ${a[12]} | ${a[13]} | ${a[14]} |
2| ${a[15]} | ${a[16]} | ${a[17]} | ${a[18]} | ${a[19]} | ${a[20]} | ${a[21]} | ${a[22]} | ${a[23]} | ${a[24]} | ${a[25]} | ${a[26]} | ${a[27]} | ${a[28]} | ${a[29]} |
3| ${a[30]} | ${a[31]} | ${a[32]} | ${a[33]} | ${a[34]} | ${a[35]} | ${a[36]} | ${a[37]} | ${a[38]} | ${a[39]} | ${a[40]} | ${a[41]} | ${a[42]} | ${a[43]} | ${a[44]} |
4| ${a[45]} | ${a[46]} | ${a[47]} | ${a[48]} | ${a[49]} | ${a[50]} | ${a[51]} | ${a[52]} | ${a[53]} | ${a[54]} | ${a[55]} | ${a[56]} | ${a[57]} | ${a[58]} | ${a[59]} |
5| ${a[60]} | ${a[61]} | ${a[62]} | ${a[63]} | ${a[64]} | ${a[65]} | ${a[66]} | ${a[67]} | ${a[68]} | ${a[69]} | ${a[70]} | ${a[71]} | ${a[72]} | ${a[73]} | ${a[74]} |
6| ${a[75]} | ${a[76]} | ${a[77]} | ${a[78]} | ${a[79]} | ${a[80]} | ${a[81]} | ${a[82]} | ${a[83]} | ${a[84]} | ${a[85]} | ${a[86]} | ${a[87]} | ${a[88]} | ${a[89]} |
7| ${a[90]} | ${a[91]} | ${a[92]} | ${a[93]} | ${a[94]} | ${a[95]} | ${a[96]} | ${a[97]} | ${a[98]} | ${a[99]} | ${a[100]} | ${a[101]} | ${a[102]} | ${a[103]} | ${a[104]} |
8| ${a[105]} | ${a[106]} | ${a[107]} | ${a[108]} | ${a[109]} | ${a[110]} | ${a[111]} | ${a[112]} | ${a[113]} | ${a[114]} | ${a[115]} | ${a[116]} | ${a[117]} | ${a[118]} | ${a[119]} |
9| ${a[120]} | ${a[121]} | ${a[122]} | ${a[123]} | ${a[124]} | ${a[125]} | ${a[126]} | ${a[127]} | ${a[128]} | ${a[129]} | ${a[130]} | ${a[131]} | ${a[132]} | ${a[133]} | ${a[134]} |
10| ${a[135]} | ${a[136]} | ${a[137]} | ${a[138]} | ${a[139]} | ${a[140]} | ${a[141]} | ${a[142]} | ${a[143]} | ${a[144]} | ${a[145]} | ${a[146]} | ${a[147]} | ${a[148]} | ${a[149]} |
11| ${a[150]} | ${a[151]} | ${a[152]} | ${a[153]} | ${a[154]} | ${a[155]} | ${a[156]} | ${a[157]} | ${a[158]} | ${a[159]} | ${a[160]} | ${a[161]} | ${a[162]} | ${a[163]} | ${a[164]} |
12| ${a[165]} | ${a[166]} | ${a[167]} | ${a[168]} | ${a[169]} | ${a[170]} | ${a[171]} | ${a[172]} | ${a[173]} | ${a[174]} | ${a[175]} | ${a[176]} | ${a[177]} | ${a[178]} | ${a[179]} |
13| ${a[180]} | ${a[181]} | ${a[182]} | ${a[183]} | ${a[184]} | ${a[185]} | ${a[186]} | ${a[187]} | ${a[188]} | ${a[189]} | ${a[190]} | ${a[191]} | ${a[192]} | ${a[193]} | ${a[194]} |
14| ${a[195]} | ${a[196]} | ${a[197]} | ${a[198]} | ${a[199]} | ${a[200]} | ${a[201]} | ${a[202]} | ${a[203]} | ${a[204]} | ${a[205]} | ${a[206]} | ${a[207]} | ${a[208]} | ${a[209]} |
15| ${a[210]} | ${a[211]} | ${a[212]} | ${a[213]} | ${a[214]} | ${a[215]} | ${a[216]} | ${a[217]} | ${a[218]} | ${a[219]} | ${a[220]} | ${a[221]} | ${a[222]} | ${a[223]} | ${a[224]} |
EOF
#判断胜利条件(行条件)
for ((i=0;i<=210;i+=15));do
for ((j=0;j<=10;j++));do
if [ "${a[$i+$j]}" == "${a[$i+$j+1]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+2]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+3]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+4]}" ] && [ "${a[$i+$j]}" == "O" ]; then
whitewin=1
break
elif [ "${a[$i+$j]}" == "${a[$i+$j+1]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+2]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+3]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+4]}" ] && [ "${a[$i+$j]}" == "@" ]; then
blackwin=1
break
fi
done
done
#判断胜利条件(列条件)
for ((i=0;i<=14;i++));do
for ((j=0;j<=180;j+=15));do
if [ "${a[$i+$j]}" == "${a[$i+$j+15]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+30]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+45]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+60]}" ] && [ "${a[$i+$j]}" == "O" ]; then
whitewin=1
break
elif [ "${a[$i+$j]}" == "${a[$i+$j+15]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+30]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+45]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+60]}" ] && [ "${a[$i+$j]}" == "@" ]; then
blackwin=1
break
fi
done
done
#判断胜利条件(反斜条件)
for ((i=0;i<=160;i+=16));do
for ((j=0;j<=10;j++));do
if [ "${a[$i+$j]}" == "${a[$i+$j+16]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+32]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+48]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+64]}" ] && [ "${a[$i+$j]}" == "O" ]; then
whitewin=1
break
elif [ "${a[$i+$j]}" == "${a[$i+$j+16]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+32]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+48]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+64]}" ] && [ "${a[$i+$j]}" == "@" ]; then
blackwin=1
break
fi
done
done
#判断胜利条件(正斜条件)
for ((i=0;i<=160;i+=16));do
for ((j=4;j<=14;j++));do
if [ "${a[$i+$j]}" == "${a[$i+$j+14]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+28]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+42]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+56]}" ] && [ "${a[$i+$j]}" == "O" ]; then
whitewin=1
break
elif [ "${a[$i+$j]}" == "${a[$i+$j+14]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+28]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+42]}" ] && [ "${a[$i+$j]}" == "${a[$i+$j+56]}" ] && [ "${a[$i+$j]}" == "@" ]; then
blackwin=1
break
fi
done
done
if [ "$blackwin" == "1" ];then
echo "恭喜黑棋获得胜利"
break
elif [ "$whitewin" == "1" ];then
echo "恭喜白棋获得胜利"
break
fi
count=$count+1
done
趣味bash小游戏(五子棋)
于 2024-08-26 15:54:54 首次发布