tic tac toe game acting weird

hello...

i'm trying to code a simple tic tac toe game. i use a class and oop and i coded the first condition for winning however if both players choose same square the program gets into infinite loop - i'm using two boolean conditions to switch turns between players one for turns second for input correctness -

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

class ticTacToe {
public:
	void initBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				_board[y][x] = ' ';
			}
		}
	}

	void drawBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				std::cout << _board[y][x];
			}std::cout << std::endl;
		}
	}

	bool setChar(int y, int x, char mark) {
		if (_board[y][x] == ' ') {
			_board[y][x] = mark;
			return true;
		}
		else {
			std::cout << "PLACE IS OCCUPID TRY AGAIN!\n";
			return false;
		}
	}

	bool iswinner() {
		if (((_board[0][0] != ' ') && (_board[0][0] == _board[0][1]) && (_board[0][1] == _board[0][2])) ||
			((_board[1][0] != ' ') && (_board[1][0] == _board[1][1]) && (_board[1][1] == _board[1][2])) ||
			((_board[2][0] != ' ') && (_board[2][0] == _board[2][1]) && (_board[2][1] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		return false;
	}

private:

	char _board[3][3];

};






int main() {
	bool iscorrect = true;
	bool turn = false;
	bool isDone = false;
	ticTacToe play;
	play.initBoard();
	while (isDone == false) {
		std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
		play.drawBoard();
		if (turn == false && iscorrect == true) {
			std::cout << "first player enter row and column to play: ";
			int y{}, x{};
			std::cin >> y >> x;
			iscorrect = play.setChar(y, x, 'X');
			play.drawBoard();
			isDone = play.iswinner();
			turn = true;
		}
		if (turn = true && iscorrect == true) {
			std::cout << "second player enter row and column to play: ";
			int y{}, x{};
			std::cin >> y >> x;
			iscorrect = play.setChar(y, x, 'O');
			isDone = play.iswinner();
			play.drawBoard();
			turn = false;
		}
	}




	system("PAUSE");
	return 0;
}

i'm on win 10 msvc compiler visual studio ide
Last edited on
if (turn = true

"=" != "=="


You don't need equality tests for bools anyway.

if (turn == false && iscorrect == true)
becomes just
if (!turn && iscorrect )
etc.
hello thank you for your answer - i've changed the code and now it doesn't enter infinitely loops...

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

class ticTacToe {
public:
	void initBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				_board[y][x] = ' ';
			}
		}
	}

	void drawBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				std::cout << _board[y][x];
			}std::cout << std::endl;
		}
	}

	bool setChar(int y, int x, char mark) {
		if (_board[y][x] == ' ') {
			_board[y][x] = mark;
			return true;
		}
		else {
			std::cout << "PLACE IS OCCUPID TRY AGAIN!\n";
			return false;
		}
	}

	bool iswinner() {
		if (((_board[0][0] != ' ') && (_board[0][0] == _board[0][1]) && (_board[0][1] == _board[0][2])) ||
			((_board[1][0] != ' ') && (_board[1][0] == _board[1][1]) && (_board[1][1] == _board[1][2])) ||
			((_board[2][0] != ' ') && (_board[2][0] == _board[2][1]) && (_board[2][1] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		return false;
	}

private:

	char _board[3][3];

};






int main() {
	bool iscorrect = true;
	bool turn = false;
	bool isDone = false;
	ticTacToe play;
	play.initBoard();
	while (isDone == false) {
		std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
		play.drawBoard();
		if (turn == false) {
			do {
				std::cout << "first player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'X');
			} while (iscorrect == false);
			play.drawBoard();
			isDone = play.iswinner();
			turn = true;
		}
		if (turn == true) {
			do {
				std::cout << "second player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'O');
			} while (iscorrect == false);
			isDone = play.iswinner();
			play.drawBoard();
			turn = false;
		}
	}




	system("PAUSE");
	return 0;
}
Instead of
if (turn == false) {
you can just write
if (!turn) {

And instead of
if (turn == true) {
you can just write
if (turn) {

In fact, you could just use an if ... else ... construct.


There is little point in doing an == operation with a boolean variable. It is already true or false.
okay here is my almost full tic tac toe game code it has a bug in the algorithm that checks if there is a winner and i didn't succeeded in adding a counter in order to know when there are no more moves left and it's a tie - i'm tired i've been working on this all day and now it's time to prepare to sleep i'll continue tomorrow yet any help will be appreciated :)

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

class ticTacToe {
public:
	void initBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				_board[y][x] = ' ';
			}
		}
	}

	void drawBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				std::cout << _board[y][x];
			}std::cout << std::endl;
		}
	}

	bool setChar(int y, int x, char mark) {
		if (_board[y][x] == ' ') {
			_board[y][x] = mark;
			return true;
		}
		else {
			std::cout << "PLACE IS OCCUPID TRY AGAIN!\n";
			return false;
		}
	}

	bool iswinner() {
		if (((_board[0][0] != ' ') && (_board[0][0] == _board[0][1]) && (_board[0][1] == _board[0][2])) ||
			((_board[1][0] != ' ') && (_board[1][0] == _board[1][1]) && (_board[1][1] == _board[1][2])) ||
			((_board[2][0] != ' ') && (_board[2][0] == _board[2][1]) && (_board[2][1] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		else if (((_board[0][0] != ' ') && (_board[0][0] == _board[1][0]) && (_board[1][0] == _board[2][0])) ||
		((_board[0][1] != ' ') && ( _board[0][1] == _board[1][1]) && (_board[1][1] == _board[2][1])) ||
		((_board[0][2] != ' ') && (_board[0][2] == _board[1][2]) && (_board[1][2] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		else if ( ((_board[0][0] != ' ') && (_board[0][0] == _board[1][1]) && (_board[1][1] == _board[2][2])) ||
		((_board[0][2] != ' ') && (_board[0][2] == _board[1][1]) && (_board[1][1] == _board[0][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		return false;
	}

private:

	char _board[3][3];

};






int main() {
	int counter = 1;
	bool iscorrect = true;
	bool turn = false;
	bool isDone = false;
	ticTacToe play;
	play.initBoard();
	while (isDone == false) {
		system("cls");
		std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
		play.drawBoard();
		if (!turn) {
			do {
				std::cout << "first player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'X');
			} while (iscorrect == false);
			system("cls");
			std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
			play.drawBoard();
			isDone = play.iswinner();
			//counter++;
			turn = true;
		}
		if (turn) {
			do {
				std::cout << "second player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'O');
			} while (iscorrect == false);
			isDone = play.iswinner();
			system("cls");
			std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
			play.drawBoard();
			//counter++;
			turn = false;
		}
	}




	system("PAUSE");
	return 0;
}
((_board[0][2] != ' ') && (_board[0][2] == _board[1][1]) && (_board[1][1] == _board[0][2])))

I presume the last position of all is _board[2][0], not _board[0][2].
:D hello thank you for helping me today i finished the game (so i hope) i corrected the algorithm for checking is there a winner and added a counter to check when there are no more turns left and it's a tie - :D here is 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
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
#include <iostream>
#include <string>

class ticTacToe {
public:
	void initBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				_board[y][x] = ' ';
			}
		}
	}

	void drawBoard() {
		for (int y{}; y < 3; y++) {
			for (int x{}; x < 3; x++) {
				std::cout << _board[y][x];
			}std::cout << std::endl;
		}
	}

	bool setChar(int y, int x, char mark) {
		if (_board[y][x] == ' ') {
			_board[y][x] = mark;
			return true;
		}
		else {
			std::cout << "PLACE IS OCCUPID TRY AGAIN!\n";
			return false;
		}
	}

	bool iswinner() {
		if (((_board[0][0] != ' ') && (_board[0][0] == _board[0][1]) && (_board[0][1] == _board[0][2])) ||
			((_board[1][0] != ' ') && (_board[1][0] == _board[1][1]) && (_board[1][1] == _board[1][2])) ||
			((_board[2][0] != ' ') && (_board[2][0] == _board[2][1]) && (_board[2][1] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		else if (((_board[0][0] != ' ') && (_board[0][0] == _board[1][0]) && (_board[1][0] == _board[2][0])) ||
		((_board[0][1] != ' ') && ( _board[0][1] == _board[1][1]) && (_board[1][1] == _board[2][1])) ||
		((_board[0][2] != ' ') && (_board[0][2] == _board[1][2]) && (_board[1][2] == _board[2][2]))) {
			std::cout << "there is a winner!";
			return true;
		}
		else if ( ((_board[0][0] != ' ') && (_board[0][0] == _board[1][1]) && (_board[1][1] == _board[2][2])) ||
		((_board[0][2] != ' ') && (_board[0][2] == _board[1][1]) && (_board[1][1] == _board[2][0]))) {
			std::cout << "there is a winner!";
			return true;
		}
		return false;
	}

private:

	char _board[3][3];

};






int main() {
	int counter = 0;
	bool iscorrect = true;
	bool turn = false;
	bool isDone = false;
	ticTacToe play;
	play.initBoard();
	while (isDone == false) {
		system("cls");
		std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
		play.drawBoard();
		if (!turn) {
			do {
				std::cout << "first player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'X');
			} while (iscorrect == false);
			system("cls");
			std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
			play.drawBoard();
			isDone = play.iswinner();
			counter++;
			if (counter >= 9) {
				std::cout << "\nthere is a tie! no winner!\n";
				break;
			}
			turn = true;
		}
		if (turn) {
			do {
				std::cout << "second player enter row and column to play: ";
				int y{}, x{};
				std::cin >> y >> x;
				iscorrect = play.setChar(y, x, 'O');
			} while (iscorrect == false);
			isDone = play.iswinner();
			system("cls");
			std::cout << "*** TIC TAC TOE GAME: ***" << std::endl;
			play.drawBoard();
			counter++;
			turn = false;
		}
	}




	system("PAUSE");
	return 0;
}
Topic archived. No new replies allowed.