Trouble With Resetting the Board on Tic- Tac -Toe

I cannot seem to figure out how to reset the board in order to play again without spots already taken. Please help me out and if you see any style problems i will be happy to hear what i can do to fix them.

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

char location[10] = {'0','1','2','3','4','5','6','7','8','9'};

int displayMenu(int &player, int &win);
int checkwin();
void displayBoard();
int get_MultiPlayer_Input(int &player);
int get_SinglePlayer_Input(int &player);
int displayResults(int&, int&);

int main()
{
	int player = 1;
	int	win;
	char again;

	do 
	{
		displayMenu(player, win);
		//get_MultiPlayer_Input(player);
		//displayBoard();
		//displayResults(win, player);
		cout << "\nWould You Like to Play Again" << endl
			 << "Please Enter Y or N" << endl;
		cin >> again;
	} while (again == 'y' || again == 'Y');
	
	return 0;
}

/*********************************************
	FUNCTION TO RETURN GAME STATUS
	1 FOR GAME IS OVER WITH RESULT
	2 FOR GAME IS IN PROGRESS
	O GAME IS OVER AND NO RESULT
**********************************************/

int checkwin()
{
	
	if (location[1] == location[2] && location[2] == location[3])
			return 1;
		else if (location[4] == location[5] && location[5] == location[6])
			return 1;
		else if (location[7] == location[8] && location[8] == location[9])
			return 1;
		else if (location[1] == location[4] && location[4] == location[7])
			return 1;
		else if (location[2] == location[5] && location[5] == location[8])
			return 1;
		else if (location[3] == location[6] && location[6] == location[9])
			return 1;
		else if (location[1] == location[5] && location[5] == location[9])
			return 1;
		else if (location[3] == location[5] && location[5] == location[7])
			return 1;
		else if (location[1] != '1' && location[2] != '2' && location[3] != '3' 
					 && location[4] != '4' && location[5] != '5' && location[6] != '6' 
					 && location[7] != '7' && location[8] != '8' && location[9] != '9')
			return 0;
	else
			return 2;
}

void displayBoard()
{
	system("cls");
	cout << "\n\n\tTic Tac Toe\n\n";

	cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;
	cout << endl;

	cout << "     |     |     " << endl;
	cout << "  " << location[1] << "  |  " << location[2] << "  |  " << location[3] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << location[4] << "  |  " << location[5] << "  |  " << location[6] << endl;

	cout << "_____|_____|_____" << endl;
	cout << "     |     |     " << endl;

	cout << "  " << location[7] << "  |  " << location[8] << "  |  " << location[9] << endl;

	cout << "     |     |     " << endl << endl;
}

int get_MultiPlayer_Input(int &player)
{
	int win,
		choice;
	char mark;

	do
	{
		displayBoard();

		if (player % 2)
		 player = 1;

		else
		  player = 2;

		cout << "Player " << player << ", enter a number:  ";
		cin >> choice;

		if (mark = (player == 1))
			{mark = 'X';}
		else 
			mark = 'O';

		if (choice == 1 && location[1] == '1')

			location[1] = mark;
			else if (choice == 2 && location[2] == '2')
				location[2] = mark;
			else if (choice == 3 && location[3] == '3')
				location[3] = mark;
			else if (choice == 4 && location[4] == '4')
				location[4] = mark;
			else if (choice == 5 && location[5] == '5')
				location[5] = mark;
			else if (choice == 6 && location[6] == '6')
				location[6] = mark;
			else if (choice == 7 && location[7] == '7')
				location[7] = mark;
			else if (choice == 8 && location[8] == '8')
				location[8] = mark;
			else if (choice == 9 && location[9] == '9')
				location[9] = mark;
		else
		{
			cout<<"Invalid move\n";

			player--;
			system("PAUSE");
		}
		
		win = checkwin();
		player++; 
	}while (win == 2);

	return win;
}

int get_SinglePlayer_Input(int &player)
{
	int win,
		choice;
	const int MIN = 1,
			  MAX = 9;
	char mark;

	do
	{
		displayBoard();

		if (player % 2)
		 {
			 player = 1;
			 cout << "Player " << player << ", enter a number:  ";
			 cin >> choice;
		 }

		else
		  {
			  player = 2;
			  cout << "Computer Is Choosing A Location" << endl;
			  srand(time(0));
			  choice =  rand() % (MAX - MIN) + MIN; 
		}

		if (mark = (player == 1))
			{mark = 'X';}
		else 
			mark = 'O';

		if (choice == 1 && location[1] == '1')
			location[1] = mark;
			else if (choice == 2 && location[2] == '2')
				location[2] = mark;
			else if (choice == 3 && location[3] == '3')
				location[3] = mark;
			else if (choice == 4 && location[4] == '4')
				location[4] = mark;
			else if (choice == 5 && location[5] == '5')
				location[5] = mark;
			else if (choice == 6 && location[6] == '6')
				location[6] = mark;
			else if (choice == 7 && location[7] == '7')
				location[7] = mark;
			else if (choice == 8 && location[8] == '8')
				location[8] = mark;
			else if (choice == 9 && location[9] == '9')
				location[9] = mark;
		else
		{
			cout << "Invalid move\n";

			player--;
			system("PAUSE");
		}

		win = checkwin();
		player++; 
	}while (win == 2);

	return win;
}

int displayResults(int &win, int &player)
{
	if (win == 1)

	if (--player == 1)
		cout << "Player "<< player <<" wins ";
		else if (player == 2)
			cout << "Computer Wins" << endl;
		else if (player == 0)
			cout << "Game draw" << endl;
	else 
		cout << "Thank You for Playing. . ." << endl;
	if ( player == 1)
		return player = 1;
		else if (player == 2)
			return player = 2;
	else
		return player = 0;
}
int displayMenu(int &player, int &win)
{
	char menu,
		 game;

	cout << "A : Play the Game" << endl;
	cout << "B : Show Game Stats" << endl;
	cout << "C : Quit The Game" << endl;
	cin >> menu;

	switch(menu)
	{
		case 'A' : 
			cout << "S : SINGLE PLAYER" << endl;
			cout << "M : MULTIPLAYER" << endl;
			cin >> game;
			switch(game)
			{
				case 'S' :
					cout << "Single Player" << endl;
					win = get_SinglePlayer_Input(player);
					return win;
					break;
				case 'M' : 
					cout << "MultiPlayer" << endl;
					win = get_MultiPlayer_Input(player);
					return win;
					break;
			}
			break;
		case 'B' : 
			cout << "Show Game Stats . . ." << endl;
			break;
		case 'C' :
			cout << "Quit the Game" << endl;
			return 0;
			break;
	}
}
/*
void getStats(int &player)
	cout << "Player 1 :\tWins\tLoses\tTied" << endl;
	cout << "          \t" << wins << "\t" << loses << "\t" << tie << endl;
	*/
I cannot seem to figure out how to reset the board in order to play again without spots already taken.

Write a clearBoard() function that resets the elements of your "location" array to their initial values. Then call clearBoard just before line 23.

Please help me out and if you see any style problems

You've done a pretty nice job of dividing the problem into functions but there are easier ways to do some parts:

Lines 117-135 could be
1
2
3
if (choice >=1 && choice <= 9 && location[choice] == choice + '0') {
    location[choice] = mark;
} else

The same thing is true at lines 183-200.

Note that these two sections of code are identical. You might want to pull that code out into a separate function.

At lines 103-107 you switch player numbers. then you do something similar at lines 112-115. Do them together:
1
2
3
4
5
6
7
if (player % 2) {
    player = 1;
    mark = 'X'
} else {
    player = 2;
    mark = '0';
}


Topic archived. No new replies allowed.