Console Tic-Tac-Toe help

Okay, so to start it off, I've just recently started getting into c++. I created this console tic-tac-toe program today to test myself, however, I probably made it more complicated than it should have been. I'd really appreciate it if anyone could give me advice if I did anything sloppy in this.

Now for the question. I have the entire program working except for the check for draw area near the end of my playGame function. I've tried multiple times to get it working, and yet, I haven't found a way to get it right. So could anyone perhaps point me in the right direction on this? Thank you in advance.

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
#include <iostream>
#include <string>
using namespace std;

void getInfo(string& a, string& b);
void gameInfo();
void playGame(string a, string b);

int main()
{
	string player1name;
	string player2name;
	
	getInfo(player1name, player2name);
	playGame(player1name, player2name);

	cout << endl;
	return 0;
}

/*
name: getInfo
given: player1name, player2name
task: Asks the players for their names, and stores them in the main function.
return: player1name, player2name
*/
void getInfo(string& a, string& b)
{
	bool berror = 0;

	cout << "To play this game properly, you need 2 players!\n";
	cout << "Please enter the player info below...\n";
	cout << "Player one, please enter your name: ";
	cin >> a;
	cout << "Player one is now " << a << ".\n";
	cout << "Player two, please enter your name: ";
	cin >> b;
	if (b == a)
	{
		cout << "\nBOTH PLAYERS HAVE THE SAME NAME!" << "\nWould Player 2 like to change their name or continue?\n";
		cout << "ENTER [1] to change name || ENTER [0] to continue\n";
		cin >> berror;
		if (berror)
		{
			cout << "Player 2, please enter your name: ";
			cin >> b;
			if (b == a)
			{
				cout << "\nPlayer 2, you have chosen the same name twice now, automatically changing your name...";
				b = a+"(1)";
			}
		}
		else if (!berror)
		{
			b = a+"(1)";
		}
	}
	cout << endl;
	cout << "Player one is " << a <<  " [X's]" << endl;
	cout << "Player two is " << b <<  " [O's]" << endl;
	gameInfo();
}

/*
name: gameInfo
task: Shows the players the board and instructions on how to play.
*/
void gameInfo()
{
	cout << endl << endl << "The board you are playing Tic-Tac-Toe on is shown below." << endl << endl;
	cout << " 1 | 2 | 3 " << endl << "---+---+---" << endl << " 4 | 5 | 6 " << endl << "---+---+---" << endl << " 7 | 8 | 9 " << endl;
	cout << "\nHow to play: \n" << "Enter the number you would like to mark with an X or O during your turn.\nThe board will update after each turn. Selecting marked numbers is restricted.\nHave fun!";
	cout << "\n\nENTER TO START GAME..." << endl;
	cin.ignore();
	cin.get();
/*clears the board*/
	for (int clear = 0; clear < 25; clear++)
	{
		cout << endl;
	}
}

/*
name: playGame
given: player1name, player2name
task: This is the entire game, it outputs the board, winners, moves, and is the game itself in a single function
*/
void playGame(string a, string b)
{

	int choice;
	int turn = 2;
	int winner = 0;
	string player = a;
	char move = 'X';
	char board[9] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
	bool game = 1;
	bool validity = 0;

/*controls the game*/
		do
		{
			cout << " " << board[0] << " | " << board[1] << " | "  << board[2] << " " << endl << "---+---+---" << endl << " " << board[3] << " | " << board[4] << " | " << board[5] << " " << endl << "---+---+---" << endl << " " << board[6] << " | " << board[7] << " | " << board[8] << " " << endl;
			if (turn % 2 == 0)
			{
				player = a;
			}
			else
			{
				player = b;
			}
/*This loop makes the player enter a new key if their move was invalid*/
			do
			{
				cout << endl << player << ", select a valid number: ";
				move = (player==a) ? 'X' : 'O';
				cin >> choice;
				if ((choice == 1) && (board[0] == '1'))
				{
					board[0] = move;
					validity = 1;
				}
				else if ((choice == 2) && (board[1] == '2'))
				{
					board[1] = move;
					validity = 1;
				}
				else if ((choice == 3) && (board[2] == '3'))
				{
					board[2] = move;
					validity = 1;
				}
				else if ((choice == 4) && (board[3] == '4'))
				{
					board[3] = move;
					validity = 1;
				}
				else if ((choice == 5) && (board[4] == '5'))
				{
					board[4] = move;
					validity = 1;
				}
				else if ((choice == 6) && (board[5] == '6'))
				{
					board[5] = move;
					validity = 1;
				}
				else if ((choice == 7) && (board[6] == '7'))
				{
					board[6] = move;
					validity = 1;
				}
				else if ((choice == 8) && (board[7] == '8'))
				{
					board[7] = move;
					validity = 1;
				}
				else if ((choice == 9) && (board[8] == '9'))
				{
					board[8] = move;
					validity = 1;
				}
				else
				{
					cout << "\nThat is not a valid move! Enter a VALID number!";
					validity = 0;
				}
			}while(validity == 0);
			turn++;
			cout << endl;

/*Check Horizontal Board*/
			if ((board[0] == 'X') && (board[1] == 'X') && (board[2] == 'X') || (board[3] == 'X') && (board[4] == 'X') && (board[5] == 'X') || (board[6] == 'X') && (board[7] == 'X') && (board[8] == 'X'))
			{
				winner = 1;
				game = 0;
			}
			else if ((board[0] == 'O') && (board[1] == 'O') && (board[2] == 'O') || (board[3] == 'O') && (board[4] == 'O') && (board[5] == 'O') || (board[6] == 'O') && (board[7] == 'O') && (board[8] == 'O'))
			{
				winner = 2;
				game = 0;
			}
/*Check Vertical Board*/
			else if ((board[0] == 'X') && (board[3] == 'X') && (board[6] == 'X') || (board[1] == 'X') && (board[4] == 'X') && (board[7] == 'X') || (board[2] == 'X') && (board[5] == 'X') && (board[8] == 'X'))
			{
				winner = 1;
				game = 0;
			}
			else if ((board[0] == 'O') && (board[3] == 'O') && (board[6] == 'O') || (board[1] == 'O') && (board[4] == 'O') && (board[7] == 'O') || (board[2] == 'O') && (board[5] == 'O') && (board[8] == 'O'))
			{
				winner = 2;
				game = 0;
			}
/*Check Diagonal Board*/
			else if ((board[0] == 'X') && (board[4] == 'X') && (board[8] == 'X') || (board[6] == 'X') && (board[4] == 'X') && (board[2] == 'X'))
			{
				winner = 1;
				game = 0;
			}
			else if ((board[0] == 'O') && (board[4] == 'O') && (board[8] == 'O') || (board[6] == 'O') && (board[4] == 'O') && (board[2] == 'O'))
			{
				winner = 2;
				game = 0;
			}
/*Check for Draw*/

//		blah

/*ends the game if changed to 0*/
		}while (game == 1);
/*clears the board*/
		for (int clear = 0; clear < 25; clear++)
		{
			cout << endl;
		}
/*Final game board to show winners move*/
			cout << " " << board[0] << " | " << board[1] << " | "  << board[2] << " " << endl << "---+---+---" << endl << " " << board[3] << " | " << board[4] << " | " << board[5] << " " << endl << "---+---+---" << endl << " " << board[6] << " | " << board[7] << " | " << board[8] << " " << endl;
/*Outputs who the winner is and congratulates them*/
		if (winner == 1)
		{ 
			cout << a << " wins! Congratulations!" << endl;
		}
		else if (winner == 2)
		{
			cout << b << " wins! Congratulations!" << endl;
		}
		else
		{
			cout << "The game was a draw!" << endl;
		}
}
Topic archived. No new replies allowed.