Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I am a beginner of the shell script programming. I tried to print out a 4 *4 (black and white) chessboard on my screen, but it doesn't work. my ...
  1. #1
    Just Joined!
    Join Date
    Sep 2007
    Location
    new york
    Posts
    1

    Smile what's wrong with my programming?

    Hi,
    I am a beginner of the shell script programming. I tried to print out a 4 *4 (black and white) chessboard on my screen, but it doesn't work. my programming is :

    ************************************************** *********
    $ vi chessboard.sh
    # using for-loop to print out a chessboard on screen
    for((i=1;i<5;i++))
    do
    for((j=1;j<5;j++))
    do
    sum=`expr $i + $j`
    index=`expr $sum % 2`
    if [ $index -eq 0 ]; then
    echo -e -n "\033[47m"
    else
    echo -e -n "\033[40m"
    fi
    done
    echo -e -n "\033[47m" #set background to grey#
    echo " "
    done
    ************************************************** *********

    my screen background is white, but it seems there is no command to set the white color ( i used "echo -e
    "\033[47m", but it is grey background).

    I don't know what's wrong with the above programming, it just doesn't give me a normal chessboard! : (

    can anyone help me to fix it? thanks in advance!

  2. #2
    Just Joined!
    Join Date
    Aug 2007
    Posts
    37
    echo -n " " - your screen is white by default so this line should print two white spaces.
    echo -e -n "\033[40m \033[0m" - this line sets the background to black, prints two black spaces, then resets all atributes to their default (white background)

    Code:
    #! /bin/bash
    
    for((i=1;i<5;i++)); do
        for((j=1;j<5;j++)); do
            sum=$(expr $i + $j)
            index=$(expr $sum &#37; 2)
            if [ $index -eq 0 ]; then
                echo -n "  "
            else
                echo -e -n "\033[40m  \033[0m"
            fi
        done
        echo
    done
    And if you wanted to put a grey border around the board you could do it like this:

    Code:
    #! /bin/bash
    
    GREY_LN="\033[47m                    \033[0m"
    GREY_SQ="\033[47m  \033[0m"
    BLACK_SQ="\033[40m  \033[0m"
    WHITE_SQ="  "
    
    echo -e "$GREY_LN"
    for((i=1;i<9;i++)); do
        echo -e -n "$GREY_SQ"
        for((j=1;j<9;j++)); do
            sum=$(expr $i + $j)
            index=$(expr $sum % 2)
            if [ $index -eq 0 ]; then
                echo -n "$WHITE_SQ"
            else
                echo -e -n "$BLACK_SQ"
            fi
        done
        echo -e "$GREY_SQ"
    done
    echo -e "$GREY_LN"
    Or you could mark out the grid like so:

    Code:
    #! /bin/bash
    
    number=8
    GREY_LN="\033[30;47m  a b c d e f g h   \033[0m"
    BLACK_SQ="\033[40m  \033[0m"
    WHITE_SQ="  "
    
    echo -e "$GREY_LN"
    for((i=1;i<9;i++)); do
        echo -e -n "\033[47m${number} \033[0m"
        for((j=1;j<9;j++)); do
            sum=$(expr $i + $j)
            index=$(expr $sum % 2)
            if [ $index -eq 0 ]; then
                echo -n "$WHITE_SQ"
            else
                echo -e -n "$BLACK_SQ"
            fi
        done
        echo -e "\033[47m ${number}\033[0m"
        let "number-=1"
    done
    echo -e "$GREY_LN"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...