C++ Tic Tac Toe help

how would you make tic tac toe game using classes and algorithms
I wrote a code but theres only one class which has got if and else statemnets. But I want to make 3 classes and algorithms can some one tell me how that would work
can I edit the following code that I made, below is the header file and the code
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include<iostream>
#include<string>
#include"110ct.h"
using namespace std;

class TicTacToe 
{ 
private: 
	ColourController ct;
    ColourController cls; 
    CursorController crs;
    char Board[3][3]; 
    int play(); 
    void playermove(); 
    int turn(int); 
    int check (int, int); 
    void boardProgress(char x[][3]); 
    int Winner(char x[][3]); 
    int player1, player2, draw, player,winner,done; 
    int row, col; 
 
public: 
    int y, z, t, s; 
    void intBoard(); 
    int Statistics(int); 
 
    char playAgain(char); 
}playBall; 
 
int main() 
{ 
    playBall.y = 0,playBall.z = 0,playBall.t = 0,playBall.s = 0; 
    int Winner = 0; 
	ColourController ct;
    ColourController cls; 
    cls.setForeground(Aqua);
        cout << " welcome to TIC TAC TOE" <<endl; 
 
    char done = false; 
 
    while (!done) 
        { 
        ColourController cls; 
        cls.setForeground(Aqua);
        cout << " Good Luck!"<< endl;
                 
 
        
        playBall.intBoard(); 
 
    
    done = playBall.playAgain(done); 
} 
 

cout << "Thank you for my playing my game"<< endl; 
system ("pause"); 
return 0; 
} 
void TicTacToe::intBoard() 
{ 
    done = false; 
    winner = 0; 
    player = 2; 
    player1 = 1; 
    player2 = 2; 
    draw = 3; 
    int i,j; 
    for (j=0;j<3;j++) 
    { 
        for (i=0;i<3;i++) 
        { 
            Board[j][i]=0; 
        } 
    } 
    play(); 
} 
 
int TicTacToe::play() 
{ 
    int done = false; 
    boardProgress(Board); 
    while(!done) 
    { 
        playermove(); 
        boardProgress(Board); 
        done = Winner(Board); 
    } 
    Statistics(winner); 
    return 0; 
} 
void TicTacToe::playermove() 
{ 
    int answer = false; 
    while (answer == false) 
    { 
        cout <<"Row: "; 
        cin >> row; 
        row--; 
        cout <<"Column: "; 
        cin >> col; 
        col--; 
        answer = check(row,col); 
    } 
    player = turn(player); 
    if (player == 1) 
        Board[row][col] = 'X'; 
    else if (player == 2) 
        Board[row][col] = 'O'; 
    else 
        cout<< "Failed."; 
} 
int TicTacToe::check(int row, int col) 
{ 
    if(Board[row][col] == 0) 
    { 
        return true; 
    } 
    else if (Board[row][col] != 0) 
    { 
        cout <<"You can't go there"<< endl; 
        return false; 
    } 
    else 
    { 
        cout<< "Going through Check"; 
        Winner(Board); 
    } 
    return false; 
} 
 
int TicTacToe::turn(int player) 
{ 
    switch(player) 
    { 
    case 1: player = 2; 
        { 
        cout<<"It's Player 1's turn"<< endl; 
        break; 
        } 
    case 2: player = 1; 
        { 
        cout<<"It's Player 2's turn"<< endl; 
        break; 
        } 
    } 
    return player; 
} 
 
int TicTacToe::Winner(char x[][3]) 
{ 
    winner = 0; 
    int count = 0; 
    int a = row; 
    int b = col; 
    for (a=0;a<3;a++) 
    { 
        for (b=0;b<3;b++) 
        { 
            if (Board[a][b]==0) 
            { 
                count++; 
            } 
        } 
    } 
    if (count > 0) 
    { 
        int row, col, r, c, d, r1, c1, d1; 
        for (row=0; row<3; row++) 
        { 
            r=0; 
            r1=0; 
 
            for (col=0; col<3; col++) 
            { 
			
                if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {  
                    winner=2; 
                } 
                if (r==3) 
                {    
                    winner=1; 
                } 
            } 
        } 
        for (col=0; col<3; col++) 
        { 
            r=0; 
            r1=0; 
            for (row=0; row<3; row++) 
            { 
                    if(x[row][col]=='X') 
                    r++; 
                if(x[row][col]=='O') 
                    r1++; 
                if (r1==3) 
                {    
                    winner=2; 
                } 
                if (r==3) 
                { 
                    winner=1; 
                    } 
            } 
        } 
        if (x[0][0]=='X' && x[1][1]=='X' && x[2][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[0][0]=='O' && x[1][1]=='O' && x[2][2]=='O') 
        { 
            winner=2; 
        } 
        else if (x[2][0]=='X' && x[1][1]=='X' && x[0][2]=='X') 
        { 
            winner=1; 
        } 
        else if (x[2][0]=='O' && x[1][1]=='O' && x[0][2]=='O') 
        { 
            winner=2; 
        } 
    } 
    else if (count == 9) 
    { 
        cout << "Its a draw" << endl; 
        winner = 3; 
    }    
    else 
    { 
        cout<< "next player's turn"<< endl; 
    } 
    if (winner > 0) 
    { 
        done = true; 
    } 
return done; 
} 
 
int TicTacToe::Statistics(int winner)     
{ 
    playBall.y++; 
    switch(winner) 
    { 
    case 1: z++; 
        { 
        break; 
        } 
    case 2: t++; 
        { 
        break; 
        } 
    case 3: s++; 
        { 
        break; 
        } 
    } 
    cout<<" Number of games you have played: " << y; 
    cout<<" Number of games Player 1 has won: " << z << " games.";  
    cout<<" Number of games Player 2 has won: " << t << " games."; 
    cout<<" Number have draws you had: " << s << " draws."; 
    return 0; 
} 
 
char TicTacToe::playAgain(char done) 
{ 
    cout <<"Would you like to play again"<< endl; 
    cout <<"Y/N: " << endl; 
    cin >> done; 
    if(done == 'Y' || done == 'y') 
    { 
         done = false; 
    } 
    else 
    { 
        done = true; 
    } 
    return done; 
} 
void TicTacToe::boardProgress(char x[][3]) 

{
	cls.setForeground(Purple);

    cout << "      |     |     \n"; 
    cout << "   " << x[0][0] << "  |  " << x[0][1] << "  |  " << x[0][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[1][0] << "  |  " << x[1][1] << "  |  " << x[1][2] <<"  \n"; 
    cout << " _____|_____|_____\n"; 
    cout << "      |     |     \n"; 
    cout << "   " << x[2][0] << "  |  " << x[2][1] << "  | " << x[2][2] <<"  \n"; 
    cout << "      |     |     \n"; 
} 

this is header file:
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
#ifndef QUICKIO_H
#define QUICKIO_H

#include <conio.h>
#include <time.h>
#include <windows.h>
#include <cstdlib>

class QuickIO
{
	bool echo, extended;
	public:
	QuickIO(){echo = false;}
	void Echo() {echo = true;}
	void DontEcho() {echo = false;}
	bool isEchoing(){return echo;}
	void get(){if(echo) getche(); else getch();}
	QuickIO& operator >> (int& var);
	QuickIO& operator >> (char& var);
};

QuickIO& QuickIO::operator >> (int& var)
{
	do
		if(echo)
			var = getche();
		else
			var = getch();
	while(var==0||var==224);
	return *this;
}

QuickIO& QuickIO::operator >> (char& var)
{
	int p;
	do
		if(echo)
			p = getche();
		else
			p = getch();
	while(p==0||p==224);
	var = (char) p;
	return *this;
}

QuickIO qin;

class Stopwatch
{
	clock_t proctime, procelapsed;
	bool running;
public:
	Stopwatch(); //called automatically when a stopwatch object is created
	void start(); //starts the stopwatch
	float stop(); //stops the watch and returns the time
	void reset(); //resets the stopwatch to zero
	float read(); //returns the current time (without stopping the watch)
};


//implementation code...

Stopwatch::Stopwatch()
{
	procelapsed = 0;
	running = false;
}

void Stopwatch::start()
{
	proctime = clock();
	running = true;
}

float Stopwatch::stop()
{
	running = false;
	clock_t oldproc = proctime;
	proctime = clock(); 
	procelapsed = proctime - oldproc;
	return float(procelapsed)/CLOCKS_PER_SEC;
}

void Stopwatch::reset()
{
if(!running)
	procelapsed = 0;
}
 
float Stopwatch::read()
{
	if(running)
		return float(clock() - proctime + procelapsed)/CLOCKS_PER_SEC;
	else 
		return float(procelapsed)/CLOCKS_PER_SEC;
}

enum colour {Black, Blue, Green, Aqua, Red, Purple, Yellow, White};

class ColourController
{
	HANDLE hout;
	int foreground, background;
	void setConsole(){SetConsoleTextAttribute(hout, foreground + background*16);}
public:
	ColourController(): foreground(15), background(0) {hout = GetStdHandle(STD_OUTPUT_HANDLE);}
	void setForeground(colour c);
	void setBackground(colour c);
	void setForeBright(bool b);
	void setBackBright(bool b);
	void invert();
	~ColourController(){setForeground(White); setForeBright(true); setBackground(Black); setBackBright(false);}
};

void ColourController::setForeground(colour c)
{
if(c < 8)
foreground = c + (foreground >= 8) * 8;
setConsole();
}

void ColourController::setBackground(colour c)
{
if(c < 8)
background = c + (background >= 8) * 8;
setConsole();
}

void ColourController::setBackBright(bool b)
{
if(b && background < 8)
background += 8;
else if(!b && background >= 8)
background -= 8;
setConsole();
}

void ColourController::setForeBright(bool b)
{
if(b && foreground < 8)
foreground += 8;
else if(!b && foreground >= 8)
foreground -= 8;
setConsole();
}

void ColourController::invert()
{
foreground = foreground ^ background;
background = background ^ foreground;
foreground = foreground ^ background;
setConsole();
}

class CursorController
{
	HANDLE hout;
	CONSOLE_SCREEN_BUFFER_INFO csbi;
	COORD c;
	void setConsole(){SetConsoleCursorPosition(hout, c);}
public:
	CursorController(){hout = GetStdHandle (STD_OUTPUT_HANDLE);GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;}
	void setPosition(short x, short y){c.X = x; c.Y = y; setConsole();}
	short getX(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.X;}
	short getY(){GetConsoleScreenBufferInfo(hout,&csbi);c=csbi.dwCursorPosition;return c.Y;}
	void clearAll();
};

void CursorController::clearAll()
{
	COORD origin = {0,0};
	if(!GetConsoleScreenBufferInfo(hout,&csbi))return;
	DWORD consize = csbi.dwSize.X * csbi.dwSize.Y;
	DWORD charsWritten;
	if(!FillConsoleOutputCharacter( hout,(TCHAR) ' ', consize, origin, &charsWritten ))return;
	if(!FillConsoleOutputAttribute( hout, csbi.wAttributes, consize, origin, &charsWritten ))return;
	SetConsoleCursorPosition(hout, origin);
}

class Random
{
	int mlower, mupper;
protected:
	int value;
public:
	Random():mlower(0),mupper(RAND_MAX){time_t seconds;time(&seconds);srand((unsigned int) seconds);}
	void setLimits(double lower, double upper){mlower=lower; mupper=upper;}
	int get();	
	int getLast(){return value;}
};
	
int Random::get()
{
	value = rand()%(mupper-mlower+1)+mlower;
	return value;
}

#endif 
closed account (10oTURfi)
Be more specific...
I want to know how to make classes, liek how to change this code , so that I have class for computer , human and grid, I have done what I could do, but I just don't kniow how to make class for computer , grid and human. all I have done in this one is I have put everything together, and also what algorithms do I write? I just don't know how to write algorithms
closed account (10oTURfi)
This is how: http://cplusplus.com/doc/tutorial/classes/

There is no need to create class for computer, human and grid. This code is unnecessarily complex; You should simplify it. Tic tac toe can be written in less than 100 lines of code.

Algorithms? What for?
Last edited on
Oh so what I have done is right? but how can I get rid of player 2 and have computer playing instead?
closed account (10oTURfi)
Add computer move function which will check if it is possible to block players victory. If not possible, place X (or O) on random avaliable place on board.
idhjs
Last edited on
Topic archived. No new replies allowed.