Tic Tac Toe Board Correctly

I already posted about this and I think my explanation of my question was too confusing. If you would allow me to explain it one more time in a more clear manner...

So I believe that the problem lies in the function printBoard. I am trying to make it so that the following occurs when the user inputs 1.

1 2 3
4 5 6
7 8 9

O 2 3
4 5 6
7 8 9

However, it is not working and I do know why. If you can offer some suggestions to solve this problem, that would be awesome.

P.S. Do not worry about the two functions nearing the end of the code.

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
void printBoard(int board[], char numbers[], int j){
	for(int x = 1; x < 10; x++)
	{
		int y = x - 1; 
		if(board[j] == x)
		{
			numbers[y] = 'O';

		}
	}

	cout << " " << numbers[0] << " " << "|" << " " << numbers[1] << " " << "|" << " " << numbers[2] << " " << endl;
	cout << "---|---|---" << endl;
	cout << " " << numbers[3] << " " << "|" << " " << numbers[4] << " " << "|" << " " << numbers[5] << " " << endl;
	cout << "---|---|---" << endl;
	cout << " " << numbers[6] << " " << "|" << " " << numbers[7] << " " << "|" << " " << numbers[8] << " " << endl;
}

int main() 
{
	int j = 0; 
	int k = 1; 
	char numbers[9] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; 
	int board[9]; 
	for(int i = 1; i < 10; i++)
	{ 
		if(i == 1 || i == 3 || i == 5 || i == 7 || i == 9)
		{
			cin >> board[j];
			j = j + 2;
			printBoard(board, numbers, j); 
		}

		if(i == 2 || i == 4 || i == 6 || i == 8)
		{
			cin >> board[k]; 
			k = k + 2; 
		}
	}



	playerOneWin(board);
	playerTwoWin(board);  
return 0; 
}
Last edited on
If you can answer the following question, I think that would solve this problem. Why doesn't the value assigned to 'j', one of the parameters of the printBoard function, transfer over to its function. For example, for the first time the loop is run, j is initialized as 0. However, when j does not actually go through the function. So if I tried to write "cout << board[j]" inside the printBoard function, it does not work. I hope I made my self clear. If I did not, please let me know.
Last edited on
I've seena lot of tic-tac-toe posts on this forum and this is one of the more confusing ones I've seen.

1) Why are you even passing j to printBoard? printBoard should print out the board as it exists. There should be no need to pass it a subscript.

2) Not clear why you have two arrays. You can easily convert a binary number to a displayable character: char c = n + '0';

3) Lines 2-10: What is the purpose of this loop? If you're trying to determine which cells have been played, shouldn't you also be placing 'X's for display?

4) Lines 29,36: Why is the user inputting directly to board? You should be prompting for a square, then edit checking that the number is valis and that the square is not occupied.

My suggestion is to rename numbers to board. Get rid of board as an int array. printBoard should take one argument: board



Topic archived. No new replies allowed.