Chess Board in Terminal

LinuxLinuxIntermediate
Practice Now

Introduction

In this project, you will learn how to create a chess board using Bash scripting. By completing this project, you will gain experience in using nested loops, color printing, and shell scripting.

🎯 Tasks

In this project, you will learn:

  • How to set up the environment for creating the chess board
  • How to prompt the user for the size of the chess board
  • How to use nested loops to print the chess board with alternating black and white cells

🏆 Achievements

After completing this project, you will be able to:

  • Create a visual chess board using Bash scripting
  • Utilize nested loops to generate a grid-like structure
  • Implement color printing to enhance the visual appeal of the output
  • Prompt the user for input and use that input to generate the desired output

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/col("`Line Feed Filtering`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-299820{{"`Chess Board in Terminal`"}} linux/echo -.-> lab-299820{{"`Chess Board in Terminal`"}} linux/redirect -.-> lab-299820{{"`Chess Board in Terminal`"}} linux/read -.-> lab-299820{{"`Chess Board in Terminal`"}} linux/col -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/shebang -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/comments -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/quoting -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/variables_decl -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/variables_usage -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/for_loops -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/cond_expr -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/arith_expansion -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/read_input -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/cmd_substitution -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/subshells -.-> lab-299820{{"`Chess Board in Terminal`"}} shell/globbing_expansion -.-> lab-299820{{"`Chess Board in Terminal`"}} end

Set up the Environment

In this step, you will set up the environment for creating the chess board.

  1. Open a text editor and create a new file named chess_board.sh.
  2. Add the following shebang line at the beginning of the file:
#!/bin/bash

This line tells the system to use the Bash shell to execute the script.

Prompt the User for the Board Size

In this step, you will prompt the user to enter the size of the chess board.

  1. Add the following code to your script:
echo -e "\e[44m[Input]\e[0m Enter the size of the chess board: "
read value

This code will display a prompt for the user to enter the size of the chess board, and store the user's input in the value variable.

Print the Chess Board

In this step, you will use nested loops to print the chess board.

  1. Add the following code to your script:
echo -e "\n\n\e[42m[OUTPUT]\e[0m REQUESTED CHESS-BOARD \e[42m[OUTPUT]\e[0m"

for ((row = 1; row <= value; row++)); do
  for ((col = 1; col <= value; col++)); do
    sumOfRowAndCol=$(($(($row + $col)) % 2))
    if [ $sumOfRowAndCol -eq 0 ]; then
      echo -e -n "\033[47m" " "
    else
      echo -e -n "\033[40m" " "
    fi
  done
  echo -ne "\033[0m" " "
  echo
done

echo -n -e "\033[0m"

This code uses nested loops to iterate through the rows and columns of the chess board. For each cell, it calculates the sum of the row and column indices, and uses this value to determine whether the cell should be black or white. The echo commands are used to print the appropriate color for each cell, and to reset the color after each row.

Run the Script

In this step, you will run the script to generate the chess board.

  1. Save the chess_board.sh file.
  2. Open a terminal and navigate to the directory where you saved the script.
  3. Make the script executable by running the following command:
chmod +x chess_board.sh
  1. Run the script by executing the following command:
./chess_board.sh

The script will prompt you to enter the size of the chess board, and then it will print the chess board to the terminal.

Congratulations! You have successfully created a chess board using Bash scripting.

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like