The final project utilizes terminal control codes to style and control the output when rendering the chessgame, This will give a deeper explanation of how the starter code is structured and how the colored output works.
§1 ANSI Escape Codes #
Most terminal implementations support control codes which allow for special behavior when printed to the terminal output.
Control codes are used to control formatting, color, and other output options on terminals and consoles. Here are the key uses:
Text Formatting:
- Make text bold, underlined, italic, or strikethrough.
- Reset formatting back to default.
Text or Background Colors:
- Set foreground text color (e.g., red, green, blue, etc.).
- Set background color behind text.
- Use bright/high-intensity variants of colors.
Cursor Movement:
- Move the cursor up, down, left, or right by a specified number of positions.
- Move the cursor to a specific position on the screen.
Clearing the Screen:
- Clear the entire screen or just parts of it (e.g., to the end of the line or the whole line).
Cursor Visibility:
- Show or hide the cursor for dynamic visual effects.
Saving and Restoring Cursor Position:
- Save the current cursor position and restore it later.
Terminal Scrolling:
- Enable scrolling for a specific portion of the terminal.
Bell or Alerts:
- Produce an audible or visual alert (depending on the terminal).
Customizing Terminal Appearance:
- Set terminal title or other meta-properties (varies by terminal support).
Interactive CLI Applications:
- Create menus, interactive prompts, and live updating UIs.
Highlighting Errors or Messages:
- Use colors or bold formatting to highlight important messages or errors.
In the starter code for project 4, I use escape codes to display the board, cursor, and selection highlights.
A control code is just specifically formatted text printed to the terminal. For example, in C/C++ if you run the following line, the background will be changed to green.
printf("\033[42m");
These control codes are used widely in the starter code for project 4 to create the chess game.
In raw form, the output looks like this:
\033[?25l\033[?1049h\033[40m\033[2J\033[HControls: Arrow Keys, Space or Enter to Select ('q' to quit)
\033[100m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m⚔ \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m+ \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m. \033[0m\033[49m\033[40m
In a terminal this becomes:

Here, the color codes are used to darken the background, highlight the “cursor” position, and highlight pieces/positions.
Some ANSI codes used in project 4 starter.cpp:
\033[?1049hAlternate screen buffer:- This saves the current terminal screen and opens a new screen buffer for printing, when closed (with
\033[?1049l) the original terminal contents are put back.
- This saves the current terminal screen and opens a new screen buffer for printing, when closed (with
\033[40mSets the background color to black.\033[41mSets the background color to red.\033[32mSets the text color to green.\033[2JClears the entire screen contents.\033[HMove print cursor to top left of the window.\033[0mReset all color changes.
You can view many more example of ANSI escape codes here: https://en.wikipedia.org/wiki/ANSI_escape_code