how to solved the problem with check winner in tictactoe

Hi guys. I started the TicTacToe nxn project. I thought the program was working properly because entering 5 of the same characters vertically, horizontally or diagonally gave a win, but it turned out that it was enough to enter 5 of the same characters anywhere and the computer recognizes it as a win. how to solve this problem? I'm a suoer beginner, thanks for your help :)

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  #include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdlib>
using namespace std;

void mein_menu(int choose_menu){
    system("cls");
        cout << "############################################" << endl;
        cout << "#         +---------------------+          #" << endl;
        cout << "#         |      TicTacToe      |          #" << endl;
        cout << "#         +---------------------+          #" << endl;
        cout << "#                                          #" << endl;
    if(choose_menu == 1){
        cout << "#            >>[ New Game ]<<              #" << endl;
    }else{
        cout << "#                New Game                  #" << endl;
    }
        cout << "#                                          #" << endl;
    if(choose_menu == 2){
        cout << "#           >>[ Game Rules ]<<             #" << endl;
    }else{
        cout << "#               Game Rules                 #" << endl;
    }
        cout << "#                                          #" << endl;
    if(choose_menu == 3){
        cout << "#            >>[ Authors ]<<               #" << endl;
    }else{
        cout << "#                Authors                   #" << endl;
    }
        cout << "#                                          #" << endl;
        cout << "############################################" << endl;
        cout << "#        w/s - up/down // z - approve      #" << endl;
        cout << "#              q - quit game               #" << endl;
        cout << "############################################" << endl;
}
void show_board(string **board, int row, int col){
    system("cls");
    for(int a = -2 ; a<col*3; a++){
        cout << "#";
    }
        cout << "\n TicTacToe\n";
    for(int a = -1 ; a<col*3; a++){
        cout << "#";
    }
        cout << "#\n#";

    for (int i=0; i<row; i++){
        for(int j=0 ; j<col; j++){
            cout << "[" <<board[i][j] <<"]" ;
    }
            cout<<"#\n#";
    }
    for(int a = -1; a<col*3; a++){
        cout << "#";
    }
}

void winning(int winning_o){
    if(winning_o){
        cout << "\nWin O! GG!";
    }else{
         cout << "\nWin X! GG!";}
}

int check_winner(string **board, int x, int y){
    int number= 0;
    int anyone_has_won = 0;

    for (int a=0; a<x; a++){ // Row (0 - x)
        for (int b=0; b<y; b++){ // col  ( 0 - y)
            for(int p=-5; p<=5; p++){ //  Row (from -5 to 5)
                for(int q=-5; q<=5; q++){ // Col (from -5 to 5)
                    if(a+p>=0 && b+q>=0 && a+p<y && b+q<x){
                        if(board[a][b] == board[a+p][b+q] && board[a][b] != " "){
                            number+= 1;
                            if(number>= 5){
                                show_board(board, x, y);
                                anyone_has_won = 1;
                            }
                        }
                    }

                }

            }
            number= 0;

        }

        number= 0;
    }

    return anyone_has_won;
}

void new_game(){
    // System gry pvp
    system("cls");
    int row, col, column, rows;
    cout << "You start for a moment, choose your board size: " << endl;
    cout << "Enter the numbers of row: ";
    cin >> row;
    cout << "Enter the numbers of column: ";
    cin >> col;

    string** board = new string*[row];

    for(int i=0; i<row; i++)
        board[i] = new string[col];

    for(int i=0; i<row; i++)
        for(int j=0; j<col; j++)
            board[i][j] = " ";

    show_board(board, row, col);

    bool turn_o = true;
    int anyone_has_won = 0;
    while(1==1){
        show_board(board, row, col);
        if(turn_o){
            cout << "\nTurn O !\n";
        }else{
            cout << "\nTurn X!\n";
        }
            cout << "Enter the numbers of column: ";
            cin >> column;

    while(1==1){
        if(column >= 1 && col < column ){
            cout << "Wrong numbers! Try again:\n";
            cin >> column;
        }else{
            break;}}

            cout << "Enter the numbers of row: ";
            cin >> rows;

    while(1==1){
        if(rows >= 1 && row < rows ){
            cout << "Wrong numbers! Try again:\n";
            cin >> rows;
        }else{
            break;}
            }
        if(board[rows-1][column-1] == " "){
            if(turn_o){
                board[rows-1][column-1] = "O";
            }else{
                board[rows-1][column-1] = "X";
            }
            anyone_has_won = check_winner(board, row, col);
            if(anyone_has_won){
                winning(turn_o);
                system("pause");
                break;
            }else{
                turn_o = !turn_o;
            }
    } else{
            cout << "The place is taken!";
            Sleep(2000);
        }
    }


}

void game_rules(){

    system("cls");

    cout << " ##########################################################" << endl;
    cout << " #                 +-------------------+                  #" << endl;
    cout << " #                 |    Game Rules     |                  #" << endl;
    cout << " #                 +-------------------+                  #" << endl;
    cout << " ##########################################################" << endl;
    cout << " #                  any key = back menu                   #" << endl;
    cout << " ##########################################################" << endl;

    system("pause >nul");

}


void authors(){

    system("cls");
    cout << " ############################################" << endl;
    cout << " #          +--------------------+          #" << endl;
    cout << " #          |      Authors       |          #" << endl;
    cout << " #          +--------------------+          #" << endl;
    cout << " ############################################" << endl;
    cout << " #             any key = back menu          #" << endl;
    cout << " ############################################" << endl;

    system("pause >nul");


}


int main()
{

    int choose = 1;

    string letter;
     while(1==1){

    while(1==1){
        mein_menu(choose);
        letter = getch();
        if(letter == "w"){
            if(choose > 1){
                choose -= 1;
            }
        }
        if(letter == "s"){
            if(choose < 3){
                choose += 1;
            }
        }
        if(letter == "z"){
            break;}

        if(letter == "q"){
            return 0;}
        }



    if(choose == 1){
        new_game();
    }
    else if(choose == 2){
        game_rules();
    }else if(choose == 3){
        authors();
    }else{}

     }
    return 0;

    }

As re-formatted for easier read (with a couple of changes to remove windows.h for Sleep and use thread instead):

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include <iostream>
#include <conio.h>
#include <thread>
#include <chrono>
#include <string>
using namespace std;

void mein_menu(int choose_menu) {
	system("cls");

	cout << "############################################" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#         |      TicTacToe      |          #" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#                                          #" << endl;

	if (choose_menu == 1) {
		cout << "#            >>[ New Game ]<<              #" << endl;
	} else {
		cout << "#                New Game                  #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 2) {
		cout << "#           >>[ Game Rules ]<<             #" << endl;
	} else {
		cout << "#               Game Rules                 #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 3) {
		cout << "#            >>[ Authors ]<<               #" << endl;
	} else {
		cout << "#                Authors                   #" << endl;
	}

	cout << "#                                          #" << endl;
	cout << "############################################" << endl;
	cout << "#        w/s - up/down // z - approve      #" << endl;
	cout << "#              q - quit game               #" << endl;
	cout << "############################################" << endl;
}

void show_board(string** board, int row, int col) {
	system("cls");

	for (int a = -2; a < col * 3; a++) {
		cout << "#";
	}

	cout << "\n TicTacToe\n";

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}

	cout << "#\n#";

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << "[" << board[i][j] << "]";
		}

		cout << "#\n#";
	}

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}
}

void winning(int winning_o) {
	if (winning_o) {
		cout << "\nWin O! GG!";
	} else {
		cout << "\nWin X! GG!";
	}
}

int check_winner(string** board, int x, int y) {
	int number = 0;
	int anyone_has_won = 0;

	for (int a = 0; a < x; a++) { // Row (0 - x)
		for (int b = 0; b < y; b++) { // col  ( 0 - y)
			for (int p = -5; p <= 5; p++) { //  Row (from -5 to 5)
				for (int q = -5; q <= 5; q++) { // Col (from -5 to 5)
					if (a + p >= 0 && b + q >= 0 && a + p < y && b + q < x) {
						if (board[a][b] == board[a + p][b + q] && board[a][b] != " ") {
							number += 1;
							if (number >= 5) {
								show_board(board, x, y);
								anyone_has_won = 1;
							}
						}
					}

				}
			}
			number = 0;
		}
		number = 0;
	}

	return anyone_has_won;
}

void new_game() {
	// System gry pvp
	system("cls");

	int row, col, column, rows;

	cout << "You start for a moment, choose your board size: " << endl;
	cout << "Enter the numbers of row: ";
	cin >> row;

	cout << "Enter the numbers of column: ";
	cin >> col;

	string** board = new string * [row];

	for (int i = 0; i < row; i++)
		board[i] = new string[col];

	for (int i = 0; i < row; i++)
		for (int j = 0; j < col; j++)
			board[i][j] = " ";

	show_board(board, row, col);

	bool turn_o = true;
	int anyone_has_won = 0;

	while (1 == 1) {
		show_board(board, row, col);
		if (turn_o) {
			cout << "\nTurn O !\n";
		} else {
			cout << "\nTurn X!\n";
		}

		cout << "Enter the numbers of column: ";
		cin >> column;

		while (1 == 1) {
			if (column >= 1 && col < column) {
				cout << "Wrong numbers! Try again:\n";
				cin >> column;
			} else {
				break;
			}
		}

		cout << "Enter the numbers of row: ";
		cin >> rows;

		while (1 == 1) {
			if (rows >= 1 && row < rows) {
				cout << "Wrong numbers! Try again:\n";
				cin >> rows;
			} else {
				break;
			}
		}

		if (board[rows - 1][column - 1] == " ") {
			if (turn_o) {
				board[rows - 1][column - 1] = "O";
			} else {
				board[rows - 1][column - 1] = "X";
			}

			anyone_has_won = check_winner(board, row, col);

			if (anyone_has_won) {
				winning(turn_o);
				system("pause");
				break;
			} else {
				turn_o = !turn_o;
			}
		} else {
			cout << "The place is taken!";
			//Sleep(2000);
			std::this_thread::sleep_for(std::chrono::seconds(2));
		}
	}
}

void game_rules() {

	system("cls");

	cout << " ##########################################################" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " #                 |    Game Rules     |                  #" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " ##########################################################" << endl;
	cout << " #                  any key = back menu                   #" << endl;
	cout << " ##########################################################" << endl;

	system("pause >nul");
}

void authors() {
	system("cls");

	cout << " ############################################" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " #          |      Authors       |          #" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " ############################################" << endl;
	cout << " #             any key = back menu          #" << endl;
	cout << " ############################################" << endl;

	system("pause >nul");
}

int main()
{
	int choose = 1;
	string letter;

	while (1 == 1) {
		while (1 == 1) {
			mein_menu(choose);
			letter = static_cast<char>(_getch());

			if (letter == "w") {
				if (choose > 1) {
					choose -= 1;
				}
			}

			if (letter == "s") {
				if (choose < 3) {
					choose += 1;
				}
			}

			if (letter == "z") {
				break;
			}

			if (letter == "q") {
				return 0;
			}
		}

		if (choose == 1) {
			new_game();
		} else if (choose == 2) {
			game_rules();
		} else if (choose == 3) {
			authors();
		} else {}
	}

	return 0;
}


why pass std::string via pointer-pointer? Why not just by ref?

Rather than while (1 == 1) for an infinite loop with break, just use while (true)

Instead of endl, use '\n' which doesn't keep doing a buffer flush every time.

how to solve this problem


Now is a good time to get to grips with the debugger. As you're designed and written the code, you should know how it is expected to work. Use the debugger to step through the code to see its flow and the contents of variables. When something is not as expected, then you're found an issue. Repeat until the program works.

Which compiler are you using?
Last edited on
why pass std::string via pointer-pointer? Why not just by ref?

I was looking on the internet how I can do it and I came across, for example, with pointers-thats why. Can you explain how to do this by ref?please


Rather than while (1 == 1) for an infinite loop with break, just use while (true)

ok

Instead of endl, use '\n' which doesn't keep doing a buffer flush every time.

ok

Which compiler are you using?

I'm using codeblocks GNU GCC Compiler
It's easier if you use a std::vector rather than allocating memory - and then freeing it (which isn't being done). Also why string and not char - you're only storing one char. That's what threw me in the first place when I saw string**. To test for a win, shouldn't you test each col for the same piece, each row for the same piece and the diagonals?

Excluding diagonals, possibly:

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <conio.h>
using namespace std;

using Board = vector<vector<char>>;

void mein_menu(int choose_menu) {
	system("cls");

	cout << "############################################" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#         |      TicTacToe      |          #" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#                                          #" << endl;

	if (choose_menu == 1) {
		cout << "#            >>[ New Game ]<<              #" << endl;
	} else {
		cout << "#                New Game                  #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 2) {
		cout << "#           >>[ Game Rules ]<<             #" << endl;
	} else {
		cout << "#               Game Rules                 #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 3) {
		cout << "#            >>[ Authors ]<<               #" << endl;
	} else {
		cout << "#                Authors                   #" << endl;
	}

	cout << "#                                          #" << endl;
	cout << "############################################" << endl;
	cout << "#        w/s - up/down // z - approve      #" << endl;
	cout << "#              q - quit game               #" << endl;
	cout << "############################################" << endl;
}

void show_board(const Board& board, int row, int col) {
	system("cls");

	for (int a = -2; a < col * 3; a++) {
		cout << "#";
	}

	cout << "\n TicTacToe\n";

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}

	cout << "#\n#";

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << "[" << board[i][j] << "]";
		}

		cout << "#\n#";
	}

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}
}

void winning(int winning_o) {
	if (winning_o) {
		cout << "\nWin O! GG!";
	} else {
		cout << "\nWin X! GG!";
	}
}

int check_winner(const Board& board, int x, int y) {
	for (int i = 0; i < x; ++i)
		if (board[i][0] != ' ') {
			int cnt = 0;

			for (int j = 0; j < y; ++j)
				cnt += board[i][j] == board[i][0];

			if (cnt == y)
				return 1;
		}

	for (int i = 0; i < y; ++i)
		if (board[0][i] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[0][i];

			if (cnt == x)
				return 1;
		}

	// Check diagonals to do

	return 0;
}
void new_game() {
	// System gry pvp
	system("cls");

	int row, col, column, rows;

	cout << "You start for a moment, choose your board size: " << endl;
	cout << "Enter the numbers of row: ";
	cin >> row;

	cout << "Enter the numbers of column: ";
	cin >> col;

	Board board(row, vector<char>(col, ' '));

	show_board(board, row, col);

	bool turn_o = true;
	int anyone_has_won = 0;

	while (1 == 1) {
		show_board(board, row, col);
		if (turn_o) {
			cout << "\nTurn O !\n";
		} else {
			cout << "\nTurn X!\n";
		}

		cout << "Enter the numbers of column: ";
		cin >> column;

		while (1 == 1) {
			if (column >= 1 && col < column) {
				cout << "Wrong numbers! Try again:\n";
				cin >> column;
			} else {
				break;
			}
		}

		cout << "Enter the numbers of row: ";
		cin >> rows;

		while (1 == 1) {
			if (rows >= 1 && row < rows) {
				cout << "Wrong numbers! Try again:\n";
				cin >> rows;
			} else {
				break;
			}
		}

		if (board[rows - 1][column - 1] == ' ') {
			if (turn_o) {
				board[rows - 1][column - 1] = 'O';
			} else {
				board[rows - 1][column - 1] = 'X';
			}

			anyone_has_won = check_winner(board, row, col);

			if (anyone_has_won) {
				winning(turn_o);
				system("pause");
				break;
			} else {
				turn_o = !turn_o;
			}
		} else {
			cout << "The place is taken!";
			std::this_thread::sleep_for(std::chrono::seconds(2));
		}
	}
}

void game_rules() {

	system("cls");

	cout << " ##########################################################" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " #                 |    Game Rules     |                  #" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " ##########################################################" << endl;
	cout << " #                  any key = back menu                   #" << endl;
	cout << " ##########################################################" << endl;

	system("pause >nul");
}

void authors() {
	system("cls");

	cout << " ############################################" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " #          |      Authors       |          #" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " ############################################" << endl;
	cout << " #             any key = back menu          #" << endl;
	cout << " ############################################" << endl;

	system("pause >nul");
}

int main()
{
	int choose = 1;
	char letter;

	while (1 == 1) {
		while (1 == 1) {
			mein_menu(choose);
			letter = static_cast<char>(_getch());

			if (letter == 'w') {
				if (choose > 1) {
					choose -= 1;
				}
			}

			if (letter == 's') {
				if (choose < 3) {
					choose += 1;
				}
			}

			if (letter == 'z') {
				break;
			}

			if (letter == 'q') {
				return 0;
			}
		}

		if (choose == 1) {
			new_game();
		} else if (choose == 2) {
			game_rules();
		} else if (choose == 3) {
			authors();
		} else {}
	}

	return 0;
}

Last edited on
i tried somethig like this, but doesnt work? why? i want check only 5 charackters :/

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <conio.h>
using namespace std;

using Board = vector<vector<char>>;

int inrow = 5;

void mein_menu(int choose_menu) {
	system("cls");

	cout << "############################################" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#         |      TicTacToe      |          #" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#                                          #" << endl;

	if (choose_menu == 1) {
		cout << "#            >>[ New Game ]<<              #" << endl;
	} else {
		cout << "#                New Game                  #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 2) {
		cout << "#           >>[ Game Rules ]<<             #" << endl;
	} else {
		cout << "#               Game Rules                 #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 3) {
		cout << "#            >>[ Authors ]<<               #" << endl;
	} else {
		cout << "#                Authors                   #" << endl;
	}

	cout << "#                                          #" << endl;
	cout << "############################################" << endl;
	cout << "#        w/s - up/down // z - approve      #" << endl;
	cout << "#              q - quit game               #" << endl;
	cout << "############################################" << endl;
}

void show_board(const Board& board, int row, int col) {
	system("cls");

	for (int a = -2; a < col * 3; a++) {
		cout << "#";
	}

	cout << "\n TicTacToe\n";

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}

	cout << "#\n#";

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < col; j++) {
			cout << "[" << board[i][j] << "]";
		}

		cout << "#\n#";
	}

	for (int a = -1; a < col * 3; a++) {
		cout << "#";
	}
}

void winning(int winning_o) {
	if (winning_o) {
		cout << "\nWin O! GG!";
	} else {
		cout << "\nWin X! GG!";
	}
}

int check_winner(const Board& board, int x, int y) {

	for (int i = 0; i < x; ++i)
		if (board[i][0] != ' ') {
			int cnt = 0;

			for (int j = 0; j < y; ++j)
				cnt += board[i][j] == board[i][0];

			if (cnt == inrow)
				return 1;


		}

	for (int i = 0; i < y; ++i)
		if (board[0][i] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[0][i];

			if (cnt == inrow)
				return 1;

            cnt = -1;           // diagonal left to right
            for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[j][j];

			if (cnt == inrow)
				return 1;

		}



	return 0;
}
void new_game() {
	// System gry pvp
	system("cls");

	int row, col, column, rows;

	cout << "You start for a moment, choose your board size: " << endl;
	cout << "Enter the numbers of row: ";
	cin >> row;

	cout << "Enter the numbers of column: ";
	cin >> col;

	Board board(row, vector<char>(col, ' '));

	show_board(board, row, col);

	bool turn_o = true;
	int anyone_has_won = 0;

	while (1 == 1) {
		show_board(board, row, col);
		if (turn_o) {
			cout << "\nTurn O !\n";
		} else {
			cout << "\nTurn X!\n";
		}

		cout << "Enter the numbers of column: ";
		cin >> column;

		while (1 == 1) {
			if (column >= 1 && col < column) {
				cout << "Wrong numbers! Try again:\n";
				cin >> column;
			} else {
				break;
			}
		}

		cout << "Enter the numbers of row: ";
		cin >> rows;

		while (1 == 1) {
			if (rows >= 1 && row < rows) {
				cout << "Wrong numbers! Try again:\n";
				cin >> rows;
			} else {
				break;
			}
		}

		if (board[rows - 1][column - 1] == ' ') {
			if (turn_o) {
				board[rows - 1][column - 1] = 'O';
			} else {
				board[rows - 1][column - 1] = 'X';
			}

			anyone_has_won = check_winner(board, row, col);

			if (anyone_has_won) {
				winning(turn_o);
				system("pause");
				break;
			} else {
				turn_o = !turn_o;
			}
		} else {
			cout << "The place is taken!";
//			std::this_thread::sleep_for(std::chrono::seconds(2));
		}
	}
}

void game_rules() {

	system("cls");

	cout << " ##########################################################" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " #                 |    Game Rules     |                  #" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " ##########################################################" << endl;
	cout << " #                  any key = back menu                   #" << endl;
	cout << " ##########################################################" << endl;

	system("pause >nul");
}

void authors() {
	system("cls");

	cout << " ############################################" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " #          |      Authors       |          #" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " ############################################" << endl;
	cout << " #             any key = back menu          #" << endl;
	cout << " ############################################" << endl;

	system("pause >nul");
}

int main()
{
	int choose = 1;
	char letter;

	while (1 == 1) {
		while (1 == 1) {
			mein_menu(choose);
			letter = static_cast<char>(_getch());

			if (letter == 'w') {
				if (choose > 1) {
					choose -= 1;
				}
			}

			if (letter == 's') {
				if (choose < 3) {
					choose += 1;
				}
			}

			if (letter == 'z') {
				break;
			}

			if (letter == 'q') {
				return 0;
			}
		}

		if (choose == 1) {
			new_game();
		} else if (choose == 2) {
			game_rules();
		} else if (choose == 3) {
			authors();
		} else {}
	}

	return 0;
} 
L10 No. You don't need inrow. See my code L92 L103.

The diagonal code needs to first check that [0][0] is not a space as per the previous checks. It's also in the wrong place. It needs to come after the previous loop, not as part of it.

ok so i start

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
int check_winner(const Board& board, int x, int y) {
	for (int i = 0; i < x; ++i)
		if (board[i][0] != ' ') {
			int cnt = 0;

			for (int j = 0; j < y; ++j)
				cnt += board[i][j] == board[i][0];

			if (cnt == y)
				return 1;
		}

	for (int i = 0; i < y; ++i)
		if (board[0][i] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[0][i];

			if (cnt == x)
				return 1;
		}

        if(board[0][0] != ' '){
            int cnt = 0;
    }
	// Check diagonals to do

	return 0;
}


right?
next double loop for x ,y?
For a non-square board, what does a diagonal win look like?
you ask about it?

1
5 2
9 6 3
13 10 7 4
17 14 11 8
18 15 12
19 16
20
For a non-square board, aren't there more than 2 winning diagonals - or what is being called a diagonal win?? For a 3 col 2 row board, there could be 4 winning diagonals depending upon what is classed as a diagonal win.
I got lost in all this, I am not a programmer, a board of a certain size is easier to understand, it is difficult for me. If you know what the code should look like and would like to present it to me with an explanation, I would be very grateful. If that's too much for you, no problem. Thank you for your advices earlier :)
Before anyone writes code for diagonal wins for a non-square board, it needs to be known what constitutes a diagonal win. You need to specify this.

Alternatively, just use a square board with the same number of rows and cols. This makes the code easier.

I am not a programmer


But you're writing a tictactoe game?
But you're writing a tictactoe game?

yup, just for fun.

Before anyone writes code for diagonal wins for a non-square board, it needs to be known what constitutes a diagonal win. You need to specify this.


100% agree, it is difficult for me so I will try with the same number of columns, any advice?

how to convert this code ?
For a square board, possibly:

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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include <iostream>
#include <vector>
#include <thread>
#include <chrono>
#include <conio.h>
using namespace std;

using Board = vector<vector<char>>;

void mein_menu(int choose_menu) {
	system("cls");

	cout << "############################################" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#         |      TicTacToe      |          #" << endl;
	cout << "#         +---------------------+          #" << endl;
	cout << "#                                          #" << endl;

	if (choose_menu == 1) {
		cout << "#            >>[ New Game ]<<              #" << endl;
	} else {
		cout << "#                New Game                  #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 2) {
		cout << "#           >>[ Game Rules ]<<             #" << endl;
	} else {
		cout << "#               Game Rules                 #" << endl;
	}

	cout << "#                                          #" << endl;

	if (choose_menu == 3) {
		cout << "#            >>[ Authors ]<<               #" << endl;
	} else {
		cout << "#                Authors                   #" << endl;
	}

	cout << "#                                          #" << endl;
	cout << "############################################" << endl;
	cout << "#        w/s - up/down // z - approve      #" << endl;
	cout << "#              q - quit game               #" << endl;
	cout << "############################################" << endl;
}

void show_board(const Board& board, int row) {
	system("cls");

	for (int a = -2; a < row * 3; a++) {
		cout << "#";
	}

	cout << "\n TicTacToe\n";

	for (int a = -1; a < row * 3; a++) {
		cout << "#";
	}

	cout << "#\n#";

	for (int i = 0; i < row; i++) {
		for (int j = 0; j < row; j++) {
			cout << "[" << board[i][j] << "]";
		}

		cout << "#\n#";
	}

	for (int a = -1; a < row * 3; a++) {
		cout << "#";
	}
}

void winning(int winning_o) {
	if (winning_o) {
		cout << "\nWin O! GG!";
	} else {
		cout << "\nWin X! GG!";
	}
}

int check_winner(const Board& board, int x) {
	for (int i = 0; i < x; ++i)
		if (board[i][0] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[i][j] == board[i][0];

			if (cnt == x)
				return 1;
		}

	for (int i = 0; i < x; ++i)
		if (board[0][i] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[0][i];

			if (cnt == x)
				return 1;
		}

	// Check square diagonals
	if (board[0][0] != ' ') {
		int cnt = 0;

		for (int i = 0; i < x; ++i)
			cnt += board[i][i] == board[0][0];

		if (cnt == x)
			return 1;
	}

	if (board[0][x - 1] != ' ') {
		int cnt = 0;

		for (int i = 0; i < x; ++i)
			cnt += board[x - i - 1][i] == board[0][x - 1];

		if (cnt == x)
			return 1;
	}

	return 0;
}

void new_game() {
	// System gry pvp
	system("cls");

	int row, column, rows;

	cout << "You start for a moment, choose your board size: " << endl;
	cout << "Enter the number of rows/cols: ";
	cin >> row;

	//cout << "Enter the numbers of column: ";
	//cin >> col;

	Board board(row, vector<char>(row, ' '));

	show_board(board, row);

	bool turn_o = true;
	int anyone_has_won = 0;

	while (1 == 1) {
		show_board(board, row);
		if (turn_o) {
			cout << "\nTurn O !\n";
		} else {
			cout << "\nTurn X!\n";
		}

		cout << "Enter the column number: ";
		cin >> column;

		while (1 == 1) {
			if (column >= 1 && row < column) {
				cout << "Wrong numbers! Try again:\n";
				cin >> column;
			} else {
				break;
			}
		}

		cout << "Enter the row number: ";
		cin >> rows;

		while (1 == 1) {
			if (rows >= 1 && row < rows) {
				cout << "Wrong numbers! Try again:\n";
				cin >> rows;
			} else {
				break;
			}
		}

		if (board[rows - 1][column - 1] == ' ') {
			if (turn_o) {
				board[rows - 1][column - 1] = 'O';
			} else {
				board[rows - 1][column - 1] = 'X';
			}

			anyone_has_won = check_winner(board, row);

			if (anyone_has_won) {
				show_board(board, row);
				winning(turn_o);
				system("pause");
				break;
			} else {
				turn_o = !turn_o;
			}
		} else {
			cout << "The place is taken!";
			std::this_thread::sleep_for(std::chrono::seconds(2));
		}
	}
}

void game_rules() {

	system("cls");

	cout << " ##########################################################" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " #                 |    Game Rules     |                  #" << endl;
	cout << " #                 +-------------------+                  #" << endl;
	cout << " ##########################################################" << endl;
	cout << " #                  any key = back menu                   #" << endl;
	cout << " ##########################################################" << endl;

	system("pause >nul");
}

void authors() {
	system("cls");

	cout << " ############################################" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " #          |      Authors       |          #" << endl;
	cout << " #          +--------------------+          #" << endl;
	cout << " ############################################" << endl;
	cout << " #             any key = back menu          #" << endl;
	cout << " ############################################" << endl;

	system("pause >nul");
}

int main()
{
	int choose = 1;
	char letter;

	while (1 == 1) {
		while (1 == 1) {
			mein_menu(choose);
			letter = static_cast<char>(_getch());

			if (letter == 'w') {
				if (choose > 1) {
					choose -= 1;
				}
			}

			if (letter == 's') {
				if (choose < 3) {
					choose += 1;
				}
			}

			if (letter == 'z') {
				break;
			}

			if (letter == 'q') {
				return 0;
			}
		}

		if (choose == 1) {
			new_game();
		} else if (choose == 2) {
			game_rules();
		} else if (choose == 3) {
			authors();
		} else {}
	}

	return 0;
}

wow! thank you :) what about draw?. I need one more if?

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
int check_winner(const Board& board, int x) {
	for (int i = 0; i < x; ++i)
		if (board[i][0] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[i][j] == board[i][0];

			if (cnt == x)
				return 1;
		}

	for (int i = 0; i < x; ++i)
		if (board[0][i] != ' ') {
			int cnt = 0;

			for (int j = 0; j < x; ++j)
				cnt += board[j][i] == board[0][i];

			if (cnt == x)
				return 1;
		}

	// Check square diagonals
	if (board[0][0] != ' ') {
		int cnt = 0;

		for (int i = 0; i < x; ++i)
			cnt += board[i][i] == board[0][0];

		if (cnt == x)
			return 1;
	}

	if (board[0][x - 1] != ' ') {
		int cnt = 0;

		for (int i = 0; i < x; ++i)
			cnt += board[x - i - 1][i] == board[0][x - 1];

		if (cnt == x)
			return 1;
	}

	return 0;

}

// here draw? 


A draw would happen if all spaces are taken and no one has won. You can use an int to count each turn. If the int reaches the total of number of spaces and no winner is declared, then call it game over and it is a draw.
For a draw (no square is ' ' - or not a draw is if at least one square is ' '), you'd need to have check_winner() to return say -1 for a draw and also have the calling code check for that value as well as 0 or 1.

Replace return 0 in check_winner() with:

1
2
3
4
5
6
for (int i = 0; i < x; ++i)
    for (int j = 0; j < x; ++j)
        if (board[i][j] == ' ')
            return 0;

return -1;


and after L190 in my code test for anyone_has_won == -1 for a draw.

thank you guys!
Topic archived. No new replies allowed.