Tic tac toe- what if the user types in a number that is already played?

Hey all, So I've written a tic tac toe problem that works! yay! sort of... the issue that im having is when the user types in a number that has already been played. I've tried many things, but they all have the same error... when it compiles, and the user enters an already chosen number, it gives the error i want, then runs how i want, but at the end it messes up, in that it replaces the pieces with the numbers that were entered repeatedly. Here's the code so far, just the implementation 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//Implementaion file that includes all of the classes and their innards
//classImp.h

#include <iostream>
#include "decs.h"

#define LINES "\n\n\n\n\n\n\n\n\n\n\n\n"
#define TAB "   "
#define BIGTAB "\t\t\t" 
using namespace std;

void Draw::info(string &player1, string &player2)
{
	cout << "Player 1 name: ";
	getline(cin, player1);
	cout << "Player 2 name: ";
	getline(cin, player2);
	cout << endl;
	cout << "To choose a square, type in the corresponding number.\n";
}

void Draw::cells()
{
	board[0][0] = '1';
	board[0][1] = '2';
	board[0][2] = '3';
	board[1][0] = '4';
	board[1][1] = '5';
	board[1][2] = '6';
	board[2][0] = '7';
	board[2][1] = '8';
	board[2][2] = '9';
	
}

//Draws the empty playing board
int Draw::drawBoard()
{
	cout << LINES;
	cout << BIGTAB << TAB << board[0][0] << " |" << TAB << board[0][1] << " |" << TAB << board[0][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << " " << "---------------\n";
	cout << BIGTAB << TAB << board[1][0] << " |" << TAB << board[1][1] << " |" << TAB << board[1][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << " " << "---------------\n";
	cout << BIGTAB << TAB << board[2][0] << " |" << TAB << board[2][1] << " |" << TAB << board[2][2] << "\n";
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	cout << BIGTAB << TAB << "  |" << TAB << "  |" << endl;
	if (player == 0 || player == 2)
		player = 1;
	else
		player = 2;
	int win = testWinner();
	//if there is a winner, exit the program
	if (win == 1)
		return EXIT_SUCCESS;
	if (win == -1)
		playerMoves(player1, player2, player);	
}

void Draw::playerMoves(string player1, string player2, int player)
{
	if (player == 1)
		cout << player1 << "'s turn: ";
	else
		cout << player2 << "'s turn: ";
	string input;		//each player's move
	getline(cin, input);
	while (input != "1" && input != "2" && input != "3" && input != "4" && input != "5" &&
		   input != "6" && input != "7" && input != "8" && input != "9")
	{
		cout << "Error: Please enter a cell 1-9\n";
		cout << "Please choose again: ";
		getline(cin, input);
	}
	char move = input[0];		
	count++;
	//draws the new board with the correct choice
	newBoard(move, player);	
}

void Draw::isSame()
{
	cout << "Error: That cell has already been chosen\n"
		<< "Enter another number: ";
	playerMoves(player1, player2, player);
	
}

//inserts and  'X' or 'O' into the correct cell
void Draw::newBoard(int move, int player)
{
	switch (move)
	{
	case '1':
		//tests if the cell has already been played
		if (board[0][0] == 'X' || board[0][0] == 'O')
			isSame();
		//if not, it decides which player it is, then places the piece on the cell
		if (player == 1)
			board[0][0] = 'X';
		else
			board[0][0] = 'O';
		drawBoard();
		break;
	case '2':
		if (board[0][1] == 'X' || board[0][1] == 'O')
			isSame();
		if (player == 1)
			board[0][1] = 'X';
		else
			board[0][1] = 'O';
		drawBoard();
		break;
	case '3':
		if (board[0][2] == 'X' || board[0][2] == 'O')
			isSame();
		if (player == 1)
			board[0][2] = 'X';
		else
			board[0][2] = 'O';
		drawBoard();
		break;
	case '4':
		if (board[1][0] == 'X' || board[1][0] == 'O')
			isSame();
		if (player == 1)
			board[1][0] = 'X';
		else
			board[1][0] = 'O';
		drawBoard();
		break;
	case '5':
		if (board[1][1] == 'X' || board[1][1] == 'O')
			isSame();
		if (player == 1)
			board[1][1] = 'X';
		else
			board[1][1] = 'O';
		drawBoard();
		break;
	case '6':
		if (board[1][2] == 'X' || board[1][2] == 'O')
			isSame();
		if (player == 1)
			board[1][2] = 'X';
		else
			board[1][2] = 'O';
		drawBoard();
		break;
	case '7':
		if (board[2][0] == 'X' || board[2][0] == 'O')
			isSame();
		if (player == 1)
			board[2][0] = 'X';
		else
			board[2][0] = 'O';
		drawBoard();
		break;
	case '8':
		if (board[2][1] == 'X' || board[2][1] == 'O')
			isSame();
		if (player == 1)
			board[2][1] = 'X';
		else
			board[2][1] = 'O';
		drawBoard();
		break;
	case '9':
		if (board[2][2] == 'X' || board[2][2] == 'O')
			isSame();
		if (player == 1)
			board[2][2] = 'X';
		else
			board[2][2] = 'O';
		drawBoard();
		break;
	}
}

//tests to see if there is a winner or not
int Draw::testWinner()
{
	//tests every possible winning combination
	//Player 1 wins
	if ((board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X') ||
		(board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X') ||
		(board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X') ||
		(board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X') ||
		(board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X') ||
		(board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X') ||
		(board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X') ||
		(board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X'))
	{
		cout << player1 << " wins!!" << endl;
		return 1;
	}
	//Player 2 wins
	if ((board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O') ||
		(board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O') ||
		(board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O') ||
		(board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O') ||
		(board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O') ||
		(board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O') ||
		(board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O') ||
		(board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O'))
	{
		cout << player2 << " wins!!" << endl;
		return 1;
	}
	//if no winner
	else
		return -1;
}
Topic archived. No new replies allowed.