These two days in learning some shell programming knowledge, when you do a chess topic, feel a bit of meaning, it is written as a blog.
Let's see what the chess board looks like:
Think carefully about the rules of the distribution of the chessboard, this problem must have a lot of ideas, I think of the rule is
1. The number of checkerboard rows as a large cycle, cycle 8 times, each cycle according to the parity of the line number to determine the order of the lattice color;
2. In a single row, loop 8 times and determine the odd even number of columns to identify the print color
The idea is to print out the simplest chessboard, but the shape of a space in the shell is not square, and the height of the space is greater than the width. Therefore, it is necessary to control the loop of printing the number of lines of space in the print cycle of the row of a single chessboard, in order to be helpful to the understanding:
By using Echo to print 3 red spaces is not a square, and you can see the last line of the green cursor is not a square, it constitutes the entire board of the smallest printing unit, then I need to control the number of single-row hollow lattice and how many lines of space to form a regular square row, such as,
, the fluorescent part SEQ 2 controls the printing of two lines, the fluorescent%4s section controls how many spaces are printed per line, and \n\c to control the issue of line wrapping after a loop.
All in all, how a chessboard is divided into when the line is printed by two loops to control the width and height, by a judgment statement according to the number of cycles of the odd couple to control the printing color; On this basis, plus a control to print the entire board 8 lines of the large cycle, And a judgment statement is used to control the color of each row and the color of the upper and lower rows according to the odd even number of cycles. The printing results are as follows:
Based on the above, the user can customize the length and width of the checkerboard, as well as the custom color.
The basic principles are:
echo -e "\033[1;4$[$COL1]m`printf "%$[$WI]s"`\033[0m\c"
Where the variable COL1 is the first color of the chessboard, the variable wi is the width of the checkerboard's individual lattice. The height is taken as the number of times the loop is assigned to the variable. Notice here that the width/height of the minimum cell is the width and height of a cursor.
The problem of height, width, and control of color selection is solved. There are several default colors in bash:
Echo-e "\033[40;37m Black Bottom White \033[0m"
Echo-e "\033[41;37m Red bottom White \033[0m"
Echo-e "\033[42;37m Green bottom White \033[0m"
Echo-e "\033[43;37m Yellow \033[0m"
Echo-e "\033[44;37m Blue bottom White \033[0m"
Echo-e "\033[45;37m Purple \033[0m"
Echo-e "\033[46;37m sky blue Bottom White \033[0m"
Echo-e "\033[47;30m black character \033[0m on white background"
Where the number 40-47 represents the corresponding background color, there is no need to print the foreground font, so there is no need for 30-37 numbers. Black background also excluded
Choose from several colors as a selection menu, select and assign to variable COL1 COL2
The complete effect is as follows:
The following is the complete code, please follow the GPL:
#!/bin/bash
#==================================================================
# Copyright (C) 2018Westos All rights reserved.
#
# FileName:chess.sh
# Author: 知行至之
# Mail: [email protected]
# Date:2018-05-07
# Description: 知行至之的国际象棋棋盘
# Blog: http://blog.51cto.com/4081735
# Lisense: GPL
#
#=================================================================
read -p "Please input cell‘s height: " HI
echo
read -p "Please input cell‘s width: " WI
echo
[[ "$HI" =~ ^[0-9]+$ ]] && [[ "$WI" =~ ^[0-9]+$ ]] || { echo "Invalid argu,Please input Numeric"; exit; }
echo "====================="
PS3="Please choose the Player1‘s Board Color(1-7): "
select COL1 in red green yellow blue purple skyblue white;do
case $COL1 in
red)
echo Player\‘s color is $COL1
let COL1=1
break
;;
green)
echo Player\‘s color is $COL1
let COL1=2
break
;;
yellow)
echo Player\‘s color is $COL1
let COL1=3
break
;;
blue)
echo Player\‘s color is $COL1
let COL1=4
break
;;
purple)
echo Player\‘s color is $COL1
let COL1=5
break
;;
skyblue)
echo Player\‘s color is $COL1
let COL1=6
break
;;
white)
echo Player\‘s color is $COL1
let COL1=7
break
;;
esac
done
echo
echo "====================="
echo
PS3="Please choose the Player2‘s Board Color(1-7): "
select COL2 in red green yellow blue purple skyblue white;do
case $COL2 in
red)
echo Player2\‘s color is $COL2
let COL2=1
break
;;
green)
echo Player2\‘s color is $COL2
let COL2=2
break
;;
yellow)
echo Player2\‘s color is $COL2
let COL2=3
break
;;
blue)
echo Player2\‘s color is $COL2
let COL2=4
break
;;
purple)
echo Player2\‘s color is $COL2
let COL2=5
break
;;
skyblue)
echo Player2\‘s color is $COL2
let COL2=6
break
;;
white)
echo Player2\‘s color is $COL2
let COL2=7
break
;;
esac
done
echo
echo "====================="
echo
for i in {1..8};do
if [ $[i%2] -eq 1 ];then
for j in `seq $HI`;do
for k in {1..8};do
if [ $[k%2] -eq 1 ];then
echo -e "\033[1;4$[$COL1]m`printf "%$[$WI]s"`\033[0m\c"
else
echo -e "\033[1;4$[$COL2]m`printf "%$[$WI]s"`\033[0m\c"
fi
let k++
done
echo
let j++
done
else
for j in `seq $HI`;do
for k in {1..8};do
if [ $[k%2] -eq 1 ];then
echo -e "\033[1;4$[$COL2]m`printf "%$[$WI]s"`\033[0m\c"
else
echo -e "\033[1;4$[$COL1]m`printf "%$[$WI]s"`\033[0m\c"
fi
let k++
done
echo
let j++
done
fi
let i++
done
Linux Shell Programming Basics---chess board