Stefan Valentine
Stefan Valentine

Reputation: 9

My chessboard has no color, how do I add color to the boxes or spaces of it?

I am making a Chess game, atm I am making a chessboard and was about to finish when I realized that it has no color and was like...

"How do I add color?"

Heres the code, where and how do I add color to this chessboard?

#include <iostream>
using namespace std;

int main() {
    cout << "Chess V.1" << endl;

    int size = 8;

    for (int i = 0; i < size; i++) {
        for (int j = 0; j < size; j++) {
            if ((i + j) % 2 == 0)
                cout << "[  ]";
            Represents black squares
            else cout << "|  |";
            Represents white squares
        }
        cout << "\n";
    }

return 0;
}

I tried to add this in the cout boxes...

cout << "\033[40m\033[37m[  ]\033[0m";

it well of course didnt work, I know for sure Im lacking something...

Upvotes: 0

Views: 103

Answers (2)

Li Xin Yuan
Li Xin Yuan

Reputation: 11

You can try something like this:

int main() {

int size = 8;

for (int i = 0; i < size; i++) {
    for (int j = 0; j < size; j++) {
        if ((i + j) % 2 == 0) {
            // Black square
            cout << "\033[40m\033[37m[  ]\033[0m";  //this makes the background black and text in the background white.
        } else {
            // White square
            cout << "\033[47m\033[30m[  ]\033[0m";  // opposite of the previous one
        }
    }
    cout << "\n";
}

return 0;}

Upvotes: -3

Brendan Lynn
Brendan Lynn

Reputation: 468

Use:

if ((i + j) % 2 == 0)
    cout << "\033[47m\033[30m  \033[0m";
else
    cout << "  ";

See Colorizing text in the console with C++.

Upvotes: -2

Related Questions