编写shell脚本,实现打印国际象棋棋盘

代码如下:

#!/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

参考文章:

  1. https://blog.csdn.net/u010134758/article/details/80486748
  2. http://www.runoob.com/linux/linux-shell-printf.html
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值