Help resetting char array for tic-tac-toe problem.

Im only posting my main because the entire code is too long to post. My bool winner returns false if a player wins and breaks the loop. However when the game starts again, the values are still filled. How do I empty the array? Ive tried some things Ive found online but they dont seem to work.

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
 using namespace std;
const int fill0 = 0;
const int fill1 = 1;
const int fill2 = 2;
void Xturn(char tic[3][3], int row, int column, bool& player);
void didXwin(char tic[3][3], bool& win);
void Yturn(char tic[3][3], int row, int column, bool& player);
void didYwin(char tic[3][3], bool& win);

int main() {
	char keeplaying = 'n';
	char again = 'y';
	cout << "New Game: X goes first." << endl;
	while (again == 'y') {
		char tic[3][3];
		while (keeplaying == 'n') {

			cout << "--------------------------" << endl;
			cout << "|R/C|   0  |  1  |  2   | " << endl;
			cout << "--------------------------" << endl;
			cout << "| 0 |   " << tic[fill0][fill0] << "  |  " << tic[fill0][fill1] << "  |  " << tic[fill0][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "| 1 |   " << tic[fill1][fill0] << "  |  " << tic[fill1][fill1] << "  |  " << tic[fill1][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "| 2 |   " << tic[fill2][fill0] << "  |  " << tic[fill2][fill1] << "  |  " << tic[fill2][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "X's turn" << endl;
			cout << "Where do you want your X placed?" << endl;
			cout << "Please select a number and column number seperate by a space." << endl;
			int row, column, test = 1;
			bool player = true;
			bool winner = true;
			cin >> row >> column;
			Xturn(tic, row, column, player);
			cout << "You have entered row#" << row << endl;
			cout << "And column #" << column << endl;
			cout << "Thankyou for your selection." << endl;
			didXwin(tic, winner);
			if (winner == false)
				break;


			cout << "--------------------------" << endl;
			cout << "|R/C|   0  |  1  |  2   | " << endl;
			cout << "--------------------------" << endl;
			cout << "| 0 |   " << tic[fill0][fill0] << "  |  " << tic[fill0][fill1] << "  |  " << tic[fill0][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "| 1 |   " << tic[fill1][fill0] << "  |  " << tic[fill1][fill1] << "  |  " << tic[fill1][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "| 2 |   " << tic[fill2][fill0] << "  |  " << tic[fill2][fill1] << "  |  " << tic[fill2][fill2] << "   |" << endl;
			cout << "--------------------------" << endl;
			cout << "O's turn." << endl;
			cout << "Where do you want your O placed" << endl;
			cout << "Please enter row number and column number seperated by a space." << endl;
			cin >> row >> column;
			Yturn(tic, row, column, player);
			didYwin(tic, winner);
			if (winner == false)
				break;

		}

		cout << "Would you like to play again?(y/n)" << endl;
		cin >> again;
	}
	}
Last edited on
by placing your array tic within the loop, create space between line 18 and 19 then cut and paste line 9 to the new space
Last edited on
Line 18, 20: You're using the assignment operator (=), not the equality operator (==).

Line 9, 23: You've declared tic twice? Which one do you think you're using? Hint: Line 23 hides line 9.

Line 23: This is an uninitialized array. Lines 25,27,29 are going to be outputting garbage.
"by placing your array tic within the loop, create space between line 18 and 19 then cut and paste line 9 to the new space"
"Line 9, 23: You've declared tic twice? Which one do you think you're using? Hint: Line 23 hides line 9".
Ok so i erased the array outside the main and put the declaration between 18 and 19. I also fixed the = to == and it still doesnt reset the array when a player wins. and even though the array isnt initialized, the output is just a dashed line so i figured it would be fine to leave it.
Last edited on
just updated it.
I think i just figured it out. I made char tic[3][3]={ } so now it is initialized and resets to blanks when the loop ends.
Last edited on
Line 15: tic is still an uninitialized array (contains garbage).

Line 21,23,25: cout of tic is going to output garbage since the array is uninitialized.

What you need is function such as this to initialize the board. Assuming the initialized state of the board is spaces.
1
2
3
4
5
void clear_board (char tic[3][3])
{   for (int i=0;i<3;i++)
        for (int j=0; j<3; j++)
            tic[i][j] = ' ';
}

Or if you're on C++11 or later you can change line 15 to:
 
  char tic[3][3] = { ' ' };
Last edited on
Topic archived. No new replies allowed.