效果如下图:
#!/bin/bash
#设置两个变量,i 和 j,一个代表行,一个代表列,国际象棋为 8*8 棋盘
#i=1 是代表准备打印第一行棋盘,第 1 行棋盘有灰色和蓝色间隔输出,总共为 8 列
#i=1,j=1 代表第 1 行的第 1 列;i=2,j=3 代表第 2 行的第 3 列
#棋盘的规律是 i+j 如果是偶数,就打印蓝色色块,如果是奇数就打印灰色色块
#使用 echo ‐ne 打印色块,并且打印完成色块后不自动换行,在同一行继续输出其他色块
for i in {1..8}
do
for j in {1..8}
do
sum=$[i+j]
if [ $[sum%2] ‐eq 0 ];then
echo ‐ne "\033[46m \033[0m"
else
echo ‐ne "\033[47m \033[0m"
fi
done
echo
done
#!/bin/bash
#set chess cell's width
read -p "Please set the chess cell's width( two space width as unit ):" width
if [[ $width =~ "^[0-9]+$" ]];then
echo "wrong width setting, check your input and try again."
exit
fi
let width=$width*2
#choose player's board cell color
player="player1"
PS3="Which color do you want to set for $player :"
select choice in red green yellow blue purple cyan white;do
case $REPLY in
[1-7])
if [[ $player == player2 ]];then
declare -i color2=$REPLY
break
else
declare -i color1=$REPLY
fi
player="player2"
PS3="Which color do you want to set for $player :"
;;
*)
;;
esac
done
if (( color1==color2 ));then
echo "two player must choose different color, check your choice and try again."
exit
fi
#print the chess board
for (( i=0; i<4; i++ )); do
for (( j=0; j<$width/2; j++ ));do
for (( k=0; k<4; k++ ));do
echo -e "\e[4${color1}m$(printf %${width}s)\e[0m\c"
echo -e "\e[4${color2}m$(printf %${width}s)\e[0m\c"
done
echo
done
for (( j=0; j<$width/2; j++ ));do
for (( k=0; k<4; k++ ));do
echo -e "\e[4${color2}m$(printf %${width}s)\e[0m\c"
echo -e "\e[4${color1}m$(printf %${width}s)\e[0m\c"
done
echo
done
done