Nearly finished Tic Tac Toe board

This tic tac toe board I have made functions perfectly except for some reason I cannot get the 'X' and 'O' to switch, it always marks the spot 'O'. Even though I have been combing over the code for an hour and it seems like it should run smoothly :( I cant tell what the issue is. I am specifically refering to the bool markSpace function

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
  #include <iostream>
#include <vector>
#include <string>

using namespace std;


void createGameBoard(char bd[][3]);
void printBoard(char bd[][3]);
bool markSpace(char bd[][3], int row, int col);
bool gameOver(char game[][3], char &whoWon);



int main()
{
	char board[3][3];
	bool finished = false;
	int x, y;
	int count = 0;
	char winner = 'a';

	createGameBoard(board);
	printBoard(board);

	while (finished == false)
	{


		cout << "Enter X and Y coordinates in a 3x3 tic tac toe grid to play!" << endl;
		cout << "Enter an X coordinate 1-3 : ";
		cin >> x;
		cout << "Enter a Y coordinate 1-3: ";
		cin >> y;

		int realx = x - 1;
		int realy = y - 1;
		markSpace(board, realx, realy);
		if (markSpace(board, realx, realy) == true)
		{
			count++;
		}

		finished = gameOver(board, winner);
		printBoard(board);
	}
	if (finished == true && count < 9)
	{
		cout << "The winner is " << winner << "!" << endl;
	}
	else if (finished == false && count == 9)
	{
		cout << "You have a tie game" << endl;
	}


	return 0;
}
void createGameBoard(char bd[][3])
{
	for (int row = 0; row < 3; row++)
	{
		for (int col = 0; col < 3; col++)
		{
			bd[row][col] = '-';
		}
	}
}
void printBoard(char bd[][3])
{
	for (int row = 0; row <3; row++)
	{
		for (int col = 0; col < 3; col++)
		{
			cout << bd[row][col] << "\t";
		}
		cout << endl;
	}
}
bool markSpace(char bd[][3], int row, int col)
{

	static bool xTurn = true;

	xTurn = !xTurn;

	if (xTurn == true && bd[row][col] == '-')
	{
		bd[row][col] = 'X';
		system("CLS");

		return true;
	}
	else
	{
		bd[row][col] = 'O';
		system("CLS");

		return true;

	}

	system("CLS");
	return false;

}
bool gameOver(char game[][3], char &whoWon)
{
	char winner = 'a';

	if (game[0][0] != '-' && game[0][0] == game[1][1] && game[0][0] == game[2][2])
	{
		winner = game[0][0];
	}
	else if (game[2][0] != '-' && game[2][0] == game[1][1] && game[2][0] == game[0][2])
	{
		winner = game[2][0];
	}
	else
	{

		for (int row = 0; row <3; row++)
		{
			if (game[row][0] != '-' && game[row][0] == game[row][1] && game[row][0] == game[row][2])
			{
				winner = game[row][0];
				break;
			}
			else if (game[0][row] != '-' && game[0][row] == game[1][row] && game[0][row] == game[2][row])
			{
				winner = game[0][row];
				break;
			}
		}
	}
	if (winner == 'a')
	{
		return false;
	}
	else
	{
		whoWon = winner;
		return true;
	}
}
Last edited on
1.
if (xTurn == true && bd[row][col] == '-')

Should be :
if (xTurn == true)

2.
static bool xTurn = true;

Should be :
static bool xTurn(false);
Last edited on
Thank you for the help! I tried these changes and unfortunately it did nothing :/ still wont work. Tried separately and together
Maybe get rid of line 38.
Why line 38? thats my function to mark the space?
You call markSpace(board, realx, realy) two times. That is why 'X' becomes 'O'.
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
bool markSpace(char bd[][3], int row, int col)
{
	static bool xTurn = true;

	xTurn = !xTurn;

	if (xTurn == true && bd[row][col] == '-')
	{
		bd[row][col] = 'X';
		system("CLS");

		return true;
	}
	else
	{
		bd[row][col] = 'O';
		system("CLS");

		return true;

	}

	system("CLS");
	return false;

}


Should be :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static bool xTurn = false;
bool markSpace(char bd[][3], int row, int col)
{
	xTurn = !xTurn;

	if (xTurn == true)
	{
		bd[row][col] = 'X';
		system("CLS");

		return true;
	}
	else
	{
		bd[row][col] = 'O';
		system("CLS");

		return true;
	}
	system("CLS");
	return false;
}
Thank you! I feel like a fool of a took. Didnt even notice I was double calling the function -__-
Should be :

No, it shouldn't. There is no reason for xTurn to have a broader scope than markSpace if markSpace is the only piece of code that uses that particular state, and I would probably be adding a condition to the else before removing from the condition on the if. The modified function can never return false.
Topic archived. No new replies allowed.