Basic Console Tic Tac Toe game

So I started learning C++ a few years ago and got a little bit past console programming but then I stopped and just recently started up again. I did some basic stuff like a calculator and fraction calculator and I want to start working on things a little bit more complicated. So I took a stab at writing a basic tic tac toe program. After working for about a day I finished and as far as I've tested it works except that it won't say who wins or loses, the game will just end. It's also a really inefficient code so I'm wondering if someone can give me some tips on it. Here it is:
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
#include <iostream>
#include <string>

using namespace std;

const int ROWsnum = 3;
const int COLsnum = 3;
int row;
int col;
int winner = 0;
bool gameover = false;
char board[COLsnum][ROWsnum] = { {' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '} };
string playername1;
string playername2;

void printboard()
{
	cout << board[0][2] << " | " << board[1][2] << " | " << board[2][2] << endl;
	cout << "__________" << endl;
	cout << board[0][1] << " | " << board[1][1] << " | " << board[2][1] << endl;
	cout << "__________" << endl;
	cout << board[0][0] << " | " << board[1][0] << " | " << board[2][0] << endl;
}

void movex()
{
	cout << "Column:";
	cin >> col;
	cout << endl << "Row:";
	cin >> row;
	board[col][row] = 'X';
}

void moveo()
{
	cout << "Column:";
	cin >> col;
	cout << endl << "Row:";
	cin >> row;
	board[col][row] = 'O';
}

void checkwinner()
{
	if (gameover == false)
	{
		winner = -1;
	}
	else if (board[0][2] == board[1][2] == board[2][2])
	{
		if(board[0][2] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[0][1] == board[1][1] == board[2][1])
	{
		if(board[0][1] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[0][0] == board[1][0] == board[2][0])
	{
		if(board[0][0] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[0][0] == board[0][1] == board[0][2])
	{
		if(board[0][0] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[1][0] == board[1][1] == board[1][2])
	{
		if(board[1][0] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[2][0] == board[2][1] == board[2][2])
	{
		if(board[2][0] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[0][2] == board[1][1] == board[2][0])
	{
		if(board[0][2] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (board[0][0] == board[1][1] == board[2][2])
	{
		if(board[0][0] == 'X')
		{
			winner = 1;
		}
		else
		{
			winner = 2;
		}
	}
	else if (gameover == true)
	{
		winner = 5;
	}
	system("PAUSE");
}

void checkgameover()
{
	int otherc = 5;
	for(int c = 0; c <= 2; c++)
	{
		for(int r = 0; r <= 2; r++)
		{
			if(board[c][r] == ' ')
			{
				gameover = false;
				c = 5;
				r = 5;
				otherc = 10;
			}
		}
	}
	if(otherc == 5)
	{
		gameover == true;
	}
	else if(otherc == 10)
	{

	}
}

void main()
{
	cout << "Welcome to the game!" << endl << endl;
	cout << "Player 1, enter your name:";
	cin >> playername1;
	cout << "Hello " << playername1 << ", you are X." << endl;
	cout << "Player 2, enter your name:";
	cin >> playername2;
	cout << "Hello " << playername2 << ", you are O." << endl;
	system("PAUSE>nul");
	cout << "So " << playername1 << ", its you're turn." << endl;
	cout << "To move, enter the column you would like to choose and press return." << endl << "Then enter the row you would like to choose and press return." << endl;
	cout << "The bottom left is Col:0 and Row:0, the middle is Col:1 and Row:1, and the top right is Col:2 and Row:2" << endl;
	printboard();
	movex();
	printboard();
	cout << endl << "Now its " << playername2 << "'s turn." << endl;
	moveo();
	printboard();
	for(int i = winner; i <= 1; i++)
	{
		cout << endl << "X turn" << endl;
		movex();
		printboard();
		cout << endl << "O turn" << endl;
		moveo();
		printboard();
		checkgameover();
		checkwinner();
		if(winner == 5)
		{
			cout << "Stale mate. Nobody wins.";
			system("PAUSE>nul");
		}
		else if (winner == 1)
		{
			cout << "Congrats " << playername1 << "! X wins!" << endl;
			system("PAUSE>nul");
		}
		else if (winner == 2)
		{
		cout << "Congrats " << playername2 << "! O wins!" << endl;
		system("PAUSE>nul");
	}
}
}
I went and dug up some old code that I wrote a while back for checking the winner on a tic-tac-toe board:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int TicTacToe::GetStatus(){ //returns 0 representing a tie, 1 representing a win by Player1, 2 representing a win by Player2, and 3 otherwise.
	for(int i=0; i<3; i++){
		if (board[i][0]==board[i][1] && board[i][1]==board[i][2]){ // 3 in a row check
			if (board[i][0]=='X') return 1;
			if (board[i][0]=='O') return 2;
		}
		if (board[0][i]==board[1][i] && board[1][i]==board[2][i]){ // 3 in a column check
			if (board[0][i]=='X') return 1;
			if (board[0][i]=='O') return 2;
		}
	}
	if ((board[0][0]==board[1][1] && board[1][1]==board[2][2]) || (board[0][2]==board[1][1] && board[1][1]==board[2][0])){
		if (board[1][1]=='X') return 1; // 3 in a diagonal check
		if (board[1][1]=='O') return 2;
	}
	bool tieTest = true;
	for (int i=0; i<3; i++) // checks for tie game
		for(int j=0; j<3; j++)
			if (board[i][j]==' ') tieTest = false;
	if (tieTest) return 0;
	return 3;
}

I hope this helps with you finding the winner or loser.

As far as what tips I may have to offer... I just have to say repeated code usually means you can write a new function from the repeated code.
I would simplify the board by using '1' - '9' rather than having to input the column and row. You also shouldn't have global variables. If you need to get a variable to a function, then simply pass it to the function! They are bad practice and completely unnecessary. You could re-code it and get it finished in about 70 lines of code.

Here are two functions I wrote about two months ago. They aren't anything special, but it gets the job done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool checkWin(const char board[3][3], const char p1, const char p2) {
	for(int l = 0; l < 2; ++l) {
		const char cP = (l == 0?p1:p2); // currentPlayer
		for(int i = 0; i < 3; ++i) {
			for(int x = 0; x < 3; ++x) {
				if(board[i][0] == cP && board[i][x+1] == cP && board[i][x+2] == cP) // horizontal win
					return true;
				else if(board[i][x] == cP && board[i+1][x] == cP && board[i+2][x] == cP) // vertical win
					return true;
			}
		}
	}
	return false;
}


1
2
3
4
5
6
7
8
9
void newMove(char board[3][3], const char currentPlayer, const int newSpot) {
	int counter = 1;
	for(int i = 0; i < 3; ++i) {
		for(int x = 0; x < 3; ++x, ++counter) {
			if(counter == newSpot && (board[i][x] != 'X' && board[i][x] != 'O'))
				board[i][x] = currentPlayer;
		}
	}
}

I went and dug up some old code that I wrote a while back for checking the winner on a tic-tac-toe board:

Is there any reason why TicTacToe was a class?
Last edited on
Yeah it was for a class. Pun fully intended.
Last edited on
Ok thanks for the help. Is there anything you guys recommend for going beyond console programs? They're getting a bit boring
Topic archived. No new replies allowed.