Background Color In Console Help

Hey all. I am just finishing up a program to play Tic-Tac-Toe, and everything is working fine except for a couple of small issues.

1. I need only the Xs on the grid to be displayed in blue

2. I need only the Os on the grid to be displayed in red

3. The Background of the winning line must change to the color of the winning player. (with the rest remaining yellow)

4. I need a way to ensure the user is playing a proper space. (ie if the user tries to input 9, the program stops and asks player for a proper play)

The code I have posted below changes the color with the turn, but in doing so, changed the color of the entire board. Is the some conditional statement in order to do this?

There are no errors when the game is compiled, it is simply a logic issue with changing the colors. I would appreciate any help. My code is below:

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
244
245
246
247
248
249
250
// Include the following preprocessor libraries
#include "stdafx.h"
#include <iostream>
#include <string>
#include <conio.h>
#include <ctime>
#include <cstdlib>
#include <stdio.h>
#include <Windows.h>

//Using the Standard Namespace
using namespace std;


// Declare the following functions
void ShowBoard ();
int IsOver();
int IsRandom();


//Assign values to the playing board in order to allow user to make their selection easier within the array
string Board[9] = {"0","1","2","3","4","5","6","7","8"};



int main ()
{

//Declare the Following local variables:
string Player1;
string Player2;
int Whose_Turn = 1;
int Move; 

//Commands Used To Introduce Color into The Game
HANDLE hConsole;

hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

// Get player names
   cout << "Player 1: Please enter your name." << endl;
   cin >> Player1;
   cout << "Please enter a name for your computer opponent" << endl;
   cin >> Player2;

   //Initialize the following variables
	int win=1;

	//Utilizing Repetition Loop Stating While There Is No Winner, Do The Following
	while(win==1)
{

//Call the Following Functions Within Main In Order To Show the Board and Determine If There Is A Winner
   ShowBoard ();
   win=IsOver();



   // Tell Which Player To Move
   if (Whose_Turn == 1 && win!=0)
   {



      cout << Player1 << ": It's your turn." << endl;
         cout << "Enter the number of the space where you'd like to play." << endl;
		 cout << "Failure To Select A Valid Space Will Result in Forfeiture of Your Turn. "<<endl;

		//Input the Move From The User
		cin >> Move;
   }

   //Or Assign The Computer To Make A Move By Calling the Function IsRandom
   else if(Whose_Turn == 2 && win!=0)
   {
	    cout << Player2 << ": It's your turn." << endl;
        Move=IsRandom();
   }

   // Change whose turn it is
   if(win==1)
   {
   switch (Whose_Turn)
		{
	
			case (1):
			{
		//Set The Color to Light Blue with Yellow Background If Player's Turn
		 SetConsoleTextAttribute (hConsole, _rotl(14, 4) | 9);
         Board[Move] = 'X';
         Whose_Turn = 2;
         break;
			 }
			case (2):
			{

		//Set the Color to Light Red with Yellow Background If Computer's Turn
		 SetConsoleTextAttribute (hConsole, _rotl(14, 4) | 12);
         Board[Move] = 'O';
         Whose_Turn = 1;
			}
		}
  }
//End of While Loop
}

// Calls the Function ShowBoard Within Function Main to Display the Board
ShowBoard ();

//Instruct The Console To Remain Open So You Can Read The Results Of The Game
_getch();
}

//This Function Will Draw the Physical Board To The Console For Use By The Game Player
void ShowBoard ()
{

//These Outputs draw the bounds and label the array within the board
   cout << endl;
   cout << (char) (218) << (char) (196) << (char) (196) << (char) (196) << (char) (194) << (char) (196) << (char) (196) << (char) (196) << (char) (194) << (char) (196) << (char) (196) << (char) (196) << (char) (191)<<endl;
   cout << (char) (179) << " " << Board[0] << " " << (char) (179) <<" " << Board[1] << " " << (char) (179) << " " << Board[2] << " "<< (char) (179)<< endl;
   cout << (char) (195) << (char) (196) << (char) (196) << (char) (196) << (char) (197) << (char) (196) << (char) (196) << (char) (196) << (char) (197) << (char) (196) << (char) (196) << (char) (196) << (char) (180)<<endl;
   cout << (char) (179) << " " << Board[3] << " " << (char) (179) <<" " << Board[4] << " " << (char) (179) << " " << Board[5] << " "<< (char) (179)<< endl;
   cout << (char) (195) << (char) (196) << (char) (196) << (char) (196) << (char) (197) << (char) (196) << (char) (196) << (char) (196) << (char) (197) << (char) (196) << (char) (196) << (char) (196) << (char) (180)<<endl;
   cout << (char) (179) << " " << Board[6] << " " << (char) (179) <<" " << Board[7] << " " << (char) (179) << " " << Board[8] << " "<< (char) (179)<< endl;
   cout << (char) (192) << (char) (196) << (char) (196) << (char) (196) << (char) (193) << (char) (196) << (char) (196) << (char) (196) << (char) (193) << (char) (196) << (char) (196) << (char) (196) << (char) (217)<<endl;
   cout << endl;

}

//This function Utilizes a Random Number Modified With A Modulus To Post A Move For The Computer
int IsRandom() 
{
    string block = "no";
    int num;
  srand(time(0));
while(block=="no")
{
      num=rand()%8;
      if(Board[num]!="X"&&Board[num]!="O")
      {
         block="go";
      }
}
return num;
}

//This function determines whether a winner has been determined
int IsOver() 
{
	//Initialize The Following Variable of Type Int
    int end=0;

//Required To Change The Color of the Outputs
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

 //Display Winning Message If You Have Won The Game

 if(Board[0]=="X" && Board[1]=="X" && Board[2]=="X")
 {
	  SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
 else if(Board[3]=="X"&&Board[4]=="X" && Board[5]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
else if(Board[6]=="X"&&Board[7]=="X" && Board[8]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
else if(Board[0]=="X"&&Board[3]=="X" && Board[6]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
else if(Board[1]=="X"&&Board[4]=="X" && Board[7]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!" <<endl;
 }
else if(Board[2]=="X"&&Board[5]=="X" && Board[8]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
else if(Board[0]=="X"&&Board[4]=="X" && Board[8]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }
else if(Board[2]=="X"&&Board[4]=="X" && Board[6]=="X")
 {
	 SetConsoleTextAttribute (hConsole, 9 | 14);
     cout<<"You have won the game!"<<endl;
 }

//Display Losing Message If You Have Lost The Game

 else if(Board[0]=="O"&&Board[1]=="O" && Board[2]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
 else if(Board[3]=="O"&&Board[4]=="O" && Board[5]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[6]=="O"&&Board[7]=="O" && Board[8]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[0]=="O"&&Board[3]=="O" && Board[6]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[1]=="O"&&Board[4]=="O" && Board[7]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[2]=="O"&&Board[5]=="O" && Board[8]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[0]=="O"&&Board[4]=="O" && Board[8]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
else if(Board[2]=="O"&&Board[4]=="O" && Board[6]=="O")
 {
	 SetConsoleTextAttribute (hConsole, 12 | 14);
     cout<<"Sorry. You have lost."<<endl;
 }
  else
  {
      end=1;

  }

return end;
}
that's a ton of if - else statements, that can sometimes get pretty messy! I searched through my files and found a tic tac toe i had started on awhile ago, it is very different from the code you've written. If you'd like to compare or just see it reply, about 164 lines so i wouldnt want to fill up a page if not needed.
Yeah I should change that into a switch to make the code a bit simpler. Was going to try and clean up the code a bit at the end once I have the constraints satisfied. Yeah I'd love to see your code. Could really help me with my placement conditional.
Here it is, the only thing i havnt finished is finding a winner. I plan to make preset arrays in a seperate file of all the possible ways someone could win a game of tic tac toe. After everyturn a function would run that compares the current board to each array of bools, if it is equal to any of the bool maps, then the player wins!


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
#include <iostream>
#include <windows.h>
#define Rows 3
#define Cols 3
#include "Grids.h" //unfinished file that will hold all possible winning matches.
/* Layout of game(when i run in my console)
GAME        CONTROLS        ****TICTACTOE****
*|*|*        1  |  2  |  3
-------     --------------
*|*|*       11  | 22  | 33
-------     --------------
*|*|*       111 | 222 | 333
    PLAYER ONE'S TURN:

*/
void printMap(char Map[][3]);
void Movep(int Input, char Map[][3], char Player); //move player
void cls(); //windows function to clear screen
void Controls(int r, int c); //takes rows and columns as params
void CheckThree(char Map[][3], char Player); //check for 3 in a row(unfinished)
int main()
{
    int Input; //input to decide where to go
    bool Playerone, PlayerTwo, Game; //used for while loops
    Playerone  = Game = true; //set playerone to go first and the game to run
    PlayerTwo = false; //player two starts out false
    char Map[Rows][Cols];
    //this compressed for loop runs through and sets everything to '*' in the array
    for(int r = 0; r < Rows; r++){for(int c = 0; c < Cols; c++){Map[r][c] = '*';}}
    //print the map:
    printMap(Map);
    while(Game) //used so i can later add in a play again button
    {
        while(Playerone) //PLAYER ONE LOOP
        {
            std::cout << "\n PLAYER ONE'S TURN: ";
            std::cin >> Input; //ask where he wants to go
            Movep(Input, Map, 'X');
            cls();
            printMap(Map);
            CheckThree(Map, 'X'); //check for win? UNFINISHED
            PlayerTwo = true; //playertwo's turn now
            Playerone = false; //playerone's turn ends
        }

        while(PlayerTwo) //PLAYER TWO LOOP
        {
            std::cout << "\n PLAYER TWO'S TURN: ";
            std::cin >> Input;
            Movep(Input, Map, 'O');
            cls();
            printMap(Map);
            CheckThree(Map,'O');
            Playerone = true; //restart the cycle again.
            PlayerTwo = false;
        }
    }
}

void printMap(char Map[][3])
{
    std::cout << "GAME\t\t   CONTROLS\t\t****TICTACTOE****\n"; //title of console
    for(int r = 0; r < Rows; r++){
        for(int c = 0; c < Cols; c++){
            std::cout << Map[r][c];
            if(c<2){std::cout << "|";}
            Controls(r,c);
        }
        if(r>1){continue;}
        std::cout << "\n-------\t\t--------------\n"; //the dashes separating rows for contols + game
    }
}

void Movep(int x, char Map[][3], char Player)
{
    switch(x) //cumbersome and repetitve, only way to do this?
    //im taking the input and setting the square he inputed to his player
    //i use a case to check every option.
    {

        case 1:
            if(Map[0][0] == '*') // used to make sure you arent overlapping squares
                Map[0][0] = Player;
            break;
        case 2:
            if(Map[0][1] == '*')
                Map[0][1] = Player;
            break;
        case 3:
            if(Map[0][2] == '*')
                Map[0][2] = Player;
            break;
        case 11:
            if(Map[1][0] == '*')
                Map[1][0] = Player;
            break;
        case 22:
            if(Map[1][1] == '*')
                Map[1][1] = Player;
            break;
        case 33:
            if(Map[1][2] == '*')
                Map[1][2] = Player;
            break;
        case 111:
            if(Map[2][0] == '*')
                Map[2][0] = Player;
            break;
        case 222:
            if(Map[2][1] == '*')
                Map[2][1] = Player;
            break;
        case 333:
            if(Map[2][2] == '*')
                Map[2][2] = Player;
            break;
    }
}

void cls() //windows h function to replace screen with nulls
{
  DWORD n;
  DWORD size;
  COORD coord = {0};
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
  GetConsoleScreenBufferInfo ( h, &csbi );
  size = csbi.dwSize.X * csbi.dwSize.Y;
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
  SetConsoleCursorPosition ( h, coord );
}

void Controls(int r, int c) //PRINT OUT CONTROLS
{
    if(c < 2){return;} //end function aftet columns > 2
    switch(r){
    case 0:
    std::cout << "\t\t" << "  1 |   2 |   3";
    break;
    case 1:
    std::cout << "\t\t" << " 11 |  22 |  33";
    break;
    case 2:
    std::cout << "\t\t" << "111 | 222 | 333";
    break;
    }
}

void CheckThree(char Map[][3], char Player) //on clue how to go about this
//THIS FUNCTION IS UNFINISHED DUE TO OTHER PROJECTS IVE MOVED ONTO, i plan to fix this later on.
//my plan is to make premade arrays of bools that show the paths that result in a win, then compare the current array to each bool array.
{
    for(int r = 0; r < Rows;r++){
        for(int c = 0; c< Cols; c++){
            if(Map[r][c] == Player)
                if(Map[r+1][c] == Player)
                    if(Map[r+2][c] == Player)
                        std::cout << "\n\n" << Player << " WINS THE GAME!";
        }
    }
}
Topic archived. No new replies allowed.