C++ Colors

I am making tic tac toe game and i want my char 'X' to be blue and my char 'O' to be green and if 'X' or 'O' win i want that winning combination to be red anyone know how to color a single char ? ex:

char x = 'X'; // And now i want 'X' to be blue and 'O' to be green;
char o = 'O';// and when i output 'X' or 'O' they should be blue and green
Aleksa,

A char in c++ is basically a number between 0 and 255, and each number is traditionally associated with a letter, i.e. 65 is A, 66 is B, ... This allows representing basic text as a string of char, e.g. a succession of numbers. Since this code is fairly universal, devices can exchange basic text messages.

However, c++ doesn't include a way to express color, size, font, etc... and there is not one way to represent them in computers. It can depend on the operating system and on the software or device used to display the text. It is different if it is a web browser, a Windows application, a Linux application, ...

In many cases, colored texts are expressed as a string of char representing a series of tags that say <use this color>, <type this text>, <use this other color>, <type this other text>. For example this is the kind of string you would send to send to a web browser:

<span style="color: #800000;">hello </span><span style="color: #0000ff;">bye bye</span>

and it would write "hello " in red (color #800000) followed by "bye bye" in blue (you can try here: https://html-online.com/editor/)

Give more details on how you plan to display your text and perhaps someone will be able to help you
Last edited on
@Aleksa03

Here's how I would color the text in question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// X and O.cpp : main project file.

#include <iostream>
#include <string>
#include <windows.h>

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

using std::cout;
using std::endl;
using std::cin;
using std::string;

// My text color function. Use it if you wish.
void text(int text_color = 7 , int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

int main()
{
	char X_O;
	int X, O;
	do
	{
		text(white,black);
		cout << "Type a 'X' or an 'O'"<<endl;
		cout << "To end program, type 3 X's or 3 O's, in a row"<<endl;
	do
	{
		cin >> X_O;
		X_O = toupper(X_O);
		if (X_O == 'X')
			X++;
		else
			X=0;
		if (X_O == 'O')
			O++;
		else
			O=0;
	}while(X_O != 'X' && X_O != 'O');
	if(X_O == 'X')
	{
		text(light_blue, white);
			cout << X_O << endl;
	}
	if(X_O == 'O')
	{
		text(light_green, white);
			cout << X_O << endl;
	}
	}while (X!=3 && O != 3);

	for(int i=0;i<3;i++)
	{
		text(light_red, white);
		cout << X_O << " ";
	}
}
Last edited on
Thanks!
Here is my game code :

#include "pch.h"
#include <iostream>
#include <Windows.h>

using namespace std;

char tabla[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char potez = 'X';
int red, kolona;
bool izjednaceno = false;

//Tabla
void prikaz_table() {

system("CLS");
cout << "\t\t\t\t ************************** IKS--OKS IGRA ********************************\n\n" << endl;

cout << "\t\t\t\t\t\t\t ______ _________ _______ \n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| " << tabla[0][0] << " | " << tabla[0][1] << " | " << tabla[0][2] << " |\n";
cout << "\t\t\t\t\t\t\t|______|_________|_______| \n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| " << tabla[1][0] << " | " << tabla[1][1] << " | " << tabla[1][2] << " |\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t|______|_________|_______|\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t| " << tabla[2][0] << " | " << tabla[2][1] << " | " << tabla[2][2] << " |\n";
cout << "\t\t\t\t\t\t\t| | | |\n";
cout << "\t\t\t\t\t\t\t|______|_________|_______| \n";
}

// IGRA U TOKU
void igra_u_toku() {


int polje;

if (potez == 'X') {
cout << "Igrac 1 na potezu [X]: ";
cin >> polje;

}
else if (potez == 'O') {
cout << "Igrac 2 na potezu [O]: ";
cin >> polje;

}

//cin >> polje;

switch (polje) {
case 1: red = 0; kolona = 0; break;
case 2: red = 0; kolona = 1; break;
case 3: red = 0; kolona = 2; break;
case 4: red = 1; kolona = 0; break;
case 5: red = 1; kolona = 1; break;
case 6: red = 1; kolona = 2; break;
case 7: red = 2; kolona = 0; break;
case 8: red = 2; kolona = 1; break;
case 9: red = 2; kolona = 2; break;
}

if (potez == 'X' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
tabla[red][kolona] = 'X';
potez = 'O';
}
else if (potez == 'O' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
tabla[red][kolona] = 'O';
potez = 'X';
}
else {
cout << "To polje je zauzeto.Pokusajte sa drugim poljem.\n";
igra_u_toku();
}
prikaz_table();
}

//KRAJ IGRE
bool kraj_igre() {

for (int i = 0; i < 3; i++) {

if ((tabla[i][0] == tabla[i][1] && tabla[i][0] == tabla[i][2]) || (tabla[0][i] == tabla[1][i] && tabla[0][i] == tabla[2][i]) || (tabla[0][0] == tabla [1][1] && tabla[1][1] == tabla[2][2]) || (tabla[0][2] == tabla[1][1] && tabla[1][1] == tabla[2][0])) {
return false;
}
}

for (int i = 0; i < 3; i++) {

for (int j = 0; j < 3; j++) {

if (tabla[i][j] != 'X' && tabla[i][j] != 'O') {
return true;
}
}
}

izjednaceno = true;
return false;
}


int main() {

igra: while (kraj_igre()) {
prikaz_table();
igra_u_toku();
kraj_igre();
}

if (potez == 'X' && izjednaceno == false) {
system("CLS");
cout << "DRUGI IGRAC JE POBEDIO [O] !!!!!!!!!!!!!!\n";
}
else if (potez == 'O' && izjednaceno == false) {
cout << "PRVI IGRAC JE POBEDIO [X] !!!!!!!!!!!!!!!\n";
}
else {
drugi_pokusaj: cout << "IGRA JE IZJEDNACENA!!!!!!!!!!!!\n";
cout << "Da li zelite da igrate opet [DA] - d ili [NE] - n : ";

char odluka[1];
cin >> odluka[1];

if (odluka[1] == 'd') {
goto igra;
}
else if (odluka[1] == 'n') {
return 0;
}
else {
cout << "Uneli ste nepostojeci simbol. Pokusajte ponovo.";
goto drugi_pokusaj;

}

}

system("PAUSE");
return 0;
}
So what should i do and where to paste your code to get for example blue X green O and final (wining) combination to be red ? I saw what you have done but i dont know how to put that in game.
@Aleksa03

I have the colors for 'X' and 'O', but haven't figured out how to get the winning row, col etc., to show in red, yet.

Of course, this would have been a lot easier for me, IF I understood what was being written to screen. I believe I did get the gist though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <iostream>
#include <Windows.h>

using std::cout;
using std::endl;
using std::cin;
using std::string;

using namespace System;

enum Colors{
	black,          //  0 text color - multiply by 16, for background colors
	dark_blue,      //  1
	dark_green,     //  2
	dark_cyan,      //  3
	dark_red,       //  4
	dark_magenta,   //  5
	dark_yellow,    //  6
	light_gray,     //  7
	dark_gray,      //  8
	light_blue,     //  9
	light_green,    // 10
	light_cyan,     // 11
	light_red,      // 12
	light_magenta,  // 13
	light_yellow,   // 14
	white           // 15
};

void text(int text_color = 7 , int paper_color = 0)
{
	// defaults to light_gray on black
	int color_total = (text_color + (paper_color * 16));
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color_total);
}

void TTT_Piece(char piece)
{
	if(piece == 'X')
	{
		text(light_blue, black);
	}
	else if(piece == 'O')
	{
		text(light_green, black);
	}
	else
		text(light_yellow, black);
}

char tabla[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char potez = 'X';
int red, kolona;
bool izjednaceno = false;

//Tabla
void prikaz_table()
{
	system("CLS");
	char piece;
	text(white, black);
	cout << "\t\t\t\t ************************** IKS--OKS IGRA ********************************\n\n" << endl;

	cout << "\t\t\t\t\t\t\t _____ _____ _____\n";
	cout << "\t\t\t\t\t\t\t|     |     |     |\n";
	cout << "\t\t\t\t\t\t\t|  ";
	piece = tabla[0][0];
	TTT_Piece(piece);
	cout << tabla[0][0];
	text(white, black);
	cout << "  |  ";
	piece = tabla[0][1];
	TTT_Piece(piece);
	cout << tabla[0][1];
	text(white, black);
	cout << "  |  ";
	piece = tabla[0][2];
	TTT_Piece(piece);
	cout << tabla[0][2];
	text(white, black);
	cout << "  |\n";
	cout << "\t\t\t\t\t\t\t|_____|_____|_____| \n";
	cout << "\t\t\t\t\t\t\t|     |     |     |\n";
	cout << "\t\t\t\t\t\t\t|  ";
	piece = tabla[1][0];
	TTT_Piece(piece);
	cout << tabla[1][0];
	text(white, black);
	cout << "  |  ";
	piece = tabla[1][1];
	TTT_Piece(piece);
	cout << tabla[1][1];
	text(white, black);
	cout << "  |  ";
	piece = tabla[1][2];
	TTT_Piece(piece);
	cout << tabla[1][2];
	text(white, black);
	cout << "  |\n";
	cout << "\t\t\t\t\t\t\t|_____|_____|_____|\n";
	cout << "\t\t\t\t\t\t\t|     |     |     |\n";
	cout << "\t\t\t\t\t\t\t|  ";
	piece = tabla[2][0];
	TTT_Piece(piece);
	cout << tabla[2][0];
	text(white, black);
	cout << "  |  ";
	piece = tabla[2][1];
	TTT_Piece(piece);
	cout << tabla[2][1];
	text(white, black);
	cout << "  |  ";
	piece = tabla[2][2];
	TTT_Piece(piece);
	cout << tabla[2][2];
	text(white, black);
	cout << "  |\n";
	cout << "\t\t\t\t\t\t\t|_____|_____|_____| \n";
}

// IGRA U TOKU
void igra_u_toku() {


	int polje;

	if (potez == 'X') {
		cout << "Igrac 1 na potezu [X]: ";
		cin >> polje;

	}
	else if (potez == 'O') {
		cout << "Igrac 2 na potezu [O]: ";
		cin >> polje;

	}

	//cin >> polje;

	switch (polje) {
	case 1: red = 0; kolona = 0; break;
	case 2: red = 0; kolona = 1; break;
	case 3: red = 0; kolona = 2; break;
	case 4: red = 1; kolona = 0; break;
	case 5: red = 1; kolona = 1; break;
	case 6: red = 1; kolona = 2; break;
	case 7: red = 2; kolona = 0; break;
	case 8: red = 2; kolona = 1; break;
	case 9: red = 2; kolona = 2; break;
	}

	if (potez == 'X' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
		tabla[red][kolona] = 'X';
		potez = 'O';
	}
	else if (potez == 'O' && tabla[red][kolona] != 'X' && tabla[red][kolona] != 'O') {
		tabla[red][kolona] = 'O';
		potez = 'X';
	}
	else {
		cout << "To polje je zauzeto.Pokusajte sa drugim poljem.\n";
		igra_u_toku();
	}
	prikaz_table();
}

//KRAJ IGRE
bool kraj_igre() {

	for (int i = 0; i < 3; i++) {

		if ((tabla[i][0] == tabla[i][1] && tabla[i][0] == tabla[i][2]) ||
                (tabla[0][i] == tabla[1][i] && tabla[0][i] == tabla[2][i]) ||
                (tabla[0][0] == tabla [1][1] && tabla[1][1] == tabla[2][2]) ||
                (tabla[0][2] == tabla[1][1] && tabla[1][1] == tabla[2][0]))
                {
			return false;
		}
	}

	for (int i = 0; i < 3; i++) {

		for (int j = 0; j < 3; j++) {

			if (tabla[i][j] != 'X' && tabla[i][j] != 'O') {
				return true;
			}
		}
	}

	izjednaceno = true;
	return false;
}


int main()
{
	char odluka='d';
	bool ok;
	do
	{
		do {
			prikaz_table();
			igra_u_toku();
			ok = kraj_igre();

			if (potez == 'X' && izjednaceno == false)
			{
				//system("CLS");
				cout << "DRUGI IGRAC JE POBEDIO [O] !!!!!!!!!!!!!!\n";
			}
			else if (potez == 'O' && izjednaceno == false) {
				cout << "PRVI IGRAC JE POBEDIO [X] !!!!!!!!!!!!!!!\n";
			}
		}while(ok);

		do
		{
			cout << "IGRA JE IZJEDNACENA!!!!!!!!!!!!\n";
			cout << "Da li zelite da igrate opet [DA] - d ili [NE] - n : ";

			cin >> odluka;

			if ( odluka != 'd' && odluka != 'n')
			{
				cout << "Uneli ste nepostojeci simbol. Pokusajte ponovo.";
			}
		}while( odluka != 'd' && odluka != 'n');
		int x=1;
		for (int i = 0; i < 3; i++) //  New game. Reset the array to original values
		{
			for (int j = 0; j < 3; j++)
			{
				tabla[i][j] = '0'+x;
				x++;
			}
			
		}
	}while(odluka == 'd');

	system("PAUSE");
	return 0;
}
Last edited on
It is working now thank you so much. It has some errors but i fixed them. Errors was on:

using namespace System, and on colors but i write instead of (black, light blue ...) numbers 0,14, 15, 12 .... etc. Also i had to delete enum Colors and it worked. Again, Thank you so much!!
Topic archived. No new replies allowed.