Drawing chess board with Linux shell programming

Time:2019-12-9

In the past two days, I learned some knowledge of shell programming. When I got a chess title, I thought it meant something, so I wrote it as a blog.

Let’s see what the chess board looks like first:

After carefully thinking about the distribution rules of the board, there must be many ways to solve this problem. What I have in mind is that the rules are

1. Take the number of rows of chessboard as a big cycle, cycle for 8 times, and determine the order of grid color according to the parity of row number in each cycle;
2. In a single row, cycle 8 times and judge the odd and even number of columns to determine the printing color

The above ideas can print out the simplest chessboard, but the display shape of a space in the shell is not a square, and the height of the space is greater than the width. Therefore, it is necessary to add a layer outside the printing cycle of a single chessboard line to control the number of printing blank lines. The following figure may be helpful for understanding:

As shown in the figure above, three red spaces are not squares printed by echo, and it can be seen that the green cursor in the last line is not a square, which constitutes the minimum printing unit of the whole chessboard. Then I need to control the number of spaces in a single line and how many spaces in a single line constitute a regular square line, as shown in the figure below

In the above figure, the fluorescence part SEQ 2 controls to print two lines, the fluorescence part% 4S controls how many spaces are printed in each line, and the problem of line breaking after one cycle is controlled by \ n \ C.

To sum up, any chessboard can be divided into two loops to control the width and height of line printing, and a judgment statement to control the printing color according to the parity of the number of cycles; on this basis, a large loop to control the printing of the whole chessboard’s 8 lines is added, and a judgment statement is used to control the color of each line and the color stagger of the up and down lines according to the odd and even number of cycles. The printing results are as follows:

On the basis of the above, the user can customize the length and width of chessboard lattice, as well as the custom color.
The basic principles are as follows:

echo -e “\033[1;4$[$COL1]m`printf “%$[$WI]s”`\033[0m\c”

The variable col1 is the first color of the chessboard, and the variable wi is the width of a single chessboard grid. The height is taken in as the number of cycles after the variable is assigned. Note that the minimum width / height cell here is the width and height of a cursor.

The problems of height and width are solved, as well as the problems of color selection. The default colors in Bash are as follows:

Echo – e “\ 033 [40; 37m white on black background \ 033 [0m”
Echo – e “\ 033 [41; 37m white on red background \ 033 [0m”
Echo – e “\ 033 [42; 37m white on green background \ 033 [0m”
Echo – e “\ 033 [43; 37m white on yellow \ 033 [0m”
Echo – e “\ 033 [44; 37m white on blue \ 033 [0m”
Echo – e “\ 033 [45; 37m purple background white character \ 033 [0m”
Echo – e “\ 033 [46; 37m sky blue background white characters \ 033 [0m”
Echo – e “\ 033 [47; 30m black on white background \ 033 [0m”

The numbers 40-47 respectively represent the corresponding background color. There is no need to print the foreground font, so there is no need for 30-37 numbers. Also remove the black background

Select several colors as the selection menu and assign them to the variable col1 col2 through select

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: knowing and doing
#  Mail: [email protected]
#  Date:2018-05-07
#A chess board that knows where to go
#  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 "3[1;4$[$COL1]m`printf "%$[$WI]s"`3[0m\c"
            else
                echo -e "3[1;4$[$COL2]m`printf "%$[$WI]s"`3[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 "3[1;4$[$COL2]m`printf "%$[$WI]s"`3[0m\c"
            else
                echo -e "3[1;4$[$COL1]m`printf "%$[$WI]s"`3[0m\c"
            fi
            let k++
        done
        echo
        let j++
    done
 fi
let i++
done

The above is the whole content of this article. I hope it will help you in your study, and I hope you can support developepaer more.