I want to Shorten the algorithm of "check()" function in this program.

I want to Shorten the algorithm of "check()" function in this program.
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
void check()
{
	/*for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			if((game[i][j]=='-')&&(go=1))
			{
				if((((game[i][j+1]=='O')&&(game[i][j+2]=='O'))||((game[i][j+1]=='O')&&(game[i][j-1]=='O'))||((game[i][j-1]=='O')&&(game[i][j-2]=='O')))&&    ((game[i][j+2]>0)&&(game[i][j+1]>0)&&(game[i][j-1]>0)&&(game[i][j-2]>0))   )
				{
					game[i][j]=='O';
					cout<<"You Win!!!"<<endl;
					go=0;
				}
				else if((((game[i+1][j]=='O')&&(game[i+2][j]=='O'))||((game[i+1][j]=='O')&&(game[i-1][j]=='O'))||((game[i-1][j]=='O')&&(game[i-2][j]=='O')))&&   ((game[i-2][j]>0)&&(game[i-1][j]>0)&&(game[i+1][j]>0)&&(game[i+2][j]>0))   )
				{
					game[i][j]=='O';
					cout<<"You Win!!!"<<endl;
					go=0;
				}
				else if((((game[i+1][j+1]=='O')&&(game[i+2][j+2]=='O'))||((game[i+1][j+1]=='O')&&(game[i-1][j-1]=='O'))||((game[i-1][j-1]=='O')&&(game[i-2][j-2]=='O')))&&    ((game[i+2][j+2]>0)&&(game[i+1][j+1]>0)&&(game[i-1][j-1]>0)&&(game[i-2][j-2]>0))   )
				{
					game[i][j]=='O';
					cout<<"You Win!!!"<<endl;
					go=0;
				}
				else if((((game[i+1][j-1]=='O')&&(game[i-1][j+1]=='O'))||((game[i+1][j-1]=='O')&&(game[i+2][j-2]=='O'))||((game[i-1][j+1]=='O')&&(game[i-2][j+2]=='O')))&&    ((game[i+1][j-1]>0)&&(game[i+2][j-2]>0)&&(game[i-1][j+1]>0)&&(game[i-2][j+2]>0))      )
				{
					game[i][j]=='O';
					cout<<"You Win!!!"<<endl;
					go=0;
				}
			}
		}
	}
*/
		if(game[0][0]=='O')
			{
				if((game[0][1]=='O')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][0]=='O')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='O')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[0][1]=='-')&&(game[0][2]=='O'))
				{
					game[0][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				
			}
			if(game[2][2]=='O'&&(go==1))
			{
				if((game[1][2]=='O')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[2][1]=='O')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='O')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[2][1]=='-')&&(game[2][0]=='O'))
				{
					game[2][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][2]=='-')&&(game[0][2]=='O'))
				{
					game[1][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='-')&&(game[0][0]=='O'))
				{
					game[1][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}
			if(game[0][2]=='O'&&(go==1))
			{
				if((game[0][1]=='O')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][2]=='O')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='O')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='-')&&(game[2][0]=='O'))
				{
					game[1][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][2]=='-')&&(game[2][2]=='O'))
				{
					game[1][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}
			if(game[2][0]=='O'&&(go==1))
			{
				if((game[1][0]=='O')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[2][1]=='O')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][1]=='O')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[2][1]=='-')&&(game[2][2]=='O'))
				{
					game[2][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][0]=='-')&&(game[0][0]=='O'))
				{
					game[1][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}
			if(game[0][1]=='O'&&(go==1))
			{
				if((game[1][1]=='-')&&(game[2][1]=='O'))
				{
					game[1][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}
			if(game[1][0]=='O'&&(go==1))
			{
				if((game[1][1]=='-')&&(game[1][2]=='O'))
				{
					game[1][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}
			if(game[1][1]=='O'&&(go==1))
			{
				if((game[1][0]=='-')&&(game[1][2]=='O'))
				{
					game[1][0]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[1][0]=='O')&&(game[1][2]=='-'))
				{
					game[1][2]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[0][1]=='O')&&(game[2][1]=='-'))
				{
					game[2][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
				else if((game[0][1]=='-')&&(game[2][1]=='0'))
				{
					game[0][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}

	
}
Last edited on
My Rest Of The Code Is This
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream>
#include <stdlib.h>
#include <time.h>  
using namespace std;
char game[3][3];
int count,go=1,co=1;
void check();
void check2();
void Third();
void Condition1();
void Condition2();
void First();
void Second();
void game_();
void print();
int main()
{
	char yes;
	int g=1,x=1,row,coloum;
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			game[i][j]='-';
		}
	}
	Condition1();
	while(g)
	{
		system("cls");
		print();
		cout<<"Enter row"<<endl;
		cin>>row;
		--row;
		cout<<"Enter coloum"<<endl;
		cin>>coloum;
		--coloum;
		if(game[row][coloum]=='-')
		{
			game[row][coloum]='X';
			++count;
			game_();
		}
		else
		{
			cout<<"Element already entered Sharafat sy Dobara koi dosra karo"<<endl;
		}
		cout<<"q for quit any character to do again: "<<endl;
		cin>>yes;
		if(yes=='q'&&go==1)
		{
			g=0;
			cout<<"khailny ka shukria!!!!"<<endl;
		}
	}
	system("pause");
}
void game_()
{
	int row,coloum;
	if(count==1)
	{
		First();
	}
	else if(count==2)
	{
		check();
		check2();
		if(go==1)
		{
			Second();
		}
		go=1;
	}
	else if(count==3)
	{
		
		check();
		check2();
		if(go==1)
		{
			Third();
		}
		go=1;
	}
	else if(count==4)
	{
		
		check();
		check2();
		if(go==1)
		{
			Third();
		}
		go=1;
	}
	print();
}
void First()
{
	int row,coloum;
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			if((game[i][j]=='X')&&(game[1][1]=='-'))
			{
				row=1;
				coloum=1;
			}
		}
	}
	game[row][coloum]='O';
}
void Condition1() //all empty
{
	int row,coloum;
	srand (time(NULL));
	int ray= rand() % 2;
	int caw= (rand()-5) % 2;
	if(ray==0)
	{
		row=0;
	}
	else if(ray==1)
	{
		row=2;
	}
	if(caw==0)
	{
		coloum=0;
	}
	else if(caw==1)
	{
		coloum=2;
	}
	game[row][coloum]='O';
}
void Second()
{
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			if((i+j)%2==1)
			{
				if((i==1)&&(game[i][j]=='-')&&(((game[i-1][j]=='O')&&(game[i-1][j]!='X'))||((game[i+1][j]=='O')&&(game[i+1][j]!='X')))&&(co==1))
				{
					if(game[i-1][j]=='O')
					{
						game[i+1][j]='O';
					}
					else if(game[i+1][j]=='O')
					{
						game[i-1][j]='O';
					}
					co=0;
				}
				else if((j==1)&&(game[i][j]=='-')&&(((game[i][j-1]=='O')&&(game[i][j-1]!='X'))||((game[i][j+1]=='O')&&(game[i][j+1]!='X')))&&(co==1))
				{
					if((game[i][j-1]=='O'))
					{
						game[i][j+1]='O';
					}
					else if(game[i][j+1]=='O')
					{
						game[i][j-1]='O';
					}
					co=0;
				}
			}
		}
	}
}

void Third()
{
	int row,coluom;
	int c=1;
	for(int i=0;i<3;++i)
	{
		for(int j=0;j<3;++j)
		{
			if(game[i][j]=='-'&&c==1)
			{
				game[i][j]='O';
				c=0;
			}
		}
	}

}

void print()
{
	for(int i=0;i<3;++i)
	{
		cout<<"          ";
		for(int j=0;j<3;++j)
		{
			cout<<"    "<<game[i][j];
		}
		cout<<endl;
	}
}

void check2()
{
		if(game[0][0]=='X')
			{
				if((game[0][1]=='X')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					go=0;
				}
				else if((game[1][0]=='X')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					go=0;
				}
				else if((game[1][1]=='X')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					go=0;
				}
				else if((game[0][1]=='-')&&(game[0][2]=='X'))
				{
					game[0][1]='O';
					go=0;
				}
				else if((game[1][0]=='-')&&(game[2][0]=='X'))
				{
					game[1][0]='O';
					go=0;
				}
			}
			if(game[2][2]=='X'&&(go==1))
			{
				if((game[1][2]=='X')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					go=0;
				}
				else if((game[2][1]=='X')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					go=0;
				}
				else if((game[1][1]=='X')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					go=0;
				}
				else if((game[2][1]=='-')&&(game[2][0]=='X'))
				{
					game[2][1]='O';
					go=0;
				}
				else if((game[1][2]=='-')&&(game[0][2]=='X'))
				{
					game[1][2]='O';
					go=0;
				}
				else if((game[1][1]=='-')&&(game[0][0]=='X'))
				{
					game[1][1]='O';
					go=0;
				}
			}
			if(game[0][2]=='X'&&(go==1))
			{
				if((game[0][1]=='X')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					go=0;
				}
				else if((game[1][2]=='X')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					go=0;
				}
				else if((game[1][1]=='X')&&(game[2][0]=='-'))
				{
					game[2][0]='O';
					go=0;
				}
				else if((game[1][1]=='-')&&(game[2][0]=='X'))
				{
					game[1][1]='O';
					go=0;
				}
				else if((game[0][1]=='-')&&(game[0][0]=='X'))
				{
					game[0][1]='O';
					go=0;
				}
			}
			if(game[2][0]=='X'&&(go==1))
			{
				if((game[1][0]=='X')&&(game[0][0]=='-'))
				{
					game[0][0]='O';
					go=0;
				}
				else if((game[2][1]=='X')&&(game[2][2]=='-'))
				{
					game[2][2]='O';
					go=0;
				}
				else if((game[1][1]=='X')&&(game[0][2]=='-'))
				{
					game[0][2]='O';
					go=0;
				}	
			}
			if(game[0][1]=='X'&&(go==1))
			{
				if((game[1][1]=='-')&&(game[2][1]=='X'))
				{
					game[1][1]='O';
					go=0;
				}
			}
			if(game[1][0]=='X'&&(go==1))
			{
				if((game[1][1]=='-')&&(game[1][2]=='X'))
				{
					game[1][1]='O';
					go=0;
				}
			}

	
}
Your game logic is wrong, but here's a shortened version that behaves like your original.

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
#include <array>

using triplet = std::array<unsigned, 3>;
std::array<triplet, 8> triplets =
{
    {
        {0,1,2},
        {3,4,5},
        {6,7,8},
        {0,3,6},
        {1,4,7},
        {2,5,8},
        {0,4,8},
        {6,4,1}
    }
};

/*
 0 | 1 | 2
 --+---+--
 3 | 4 | 5
 --+---+--
 6 | 7 | 8
*/

bool winner(const triplet& trip, char player)
{
    unsigned player_count = 0;
    unsigned dash_count = 0;
    unsigned dash_pos = 0;

    for (auto pos : trip)
    {
        char ch = game[pos / 3][pos % 3];

        if (ch == player)
            ++player_count;
        else if (ch == '-')
        {
            ++dash_count;
            dash_pos = pos;
        }
    }

    if (player_count == 2 && dash_count == 1)
    {
        game[dash_pos / 3][dash_pos % 3] = player;
        player_count = 3;
    }

    return player_count == 3;
}

void check(char player = 'O')
{
    for (std::size_t i = 0; i < triplets.size(); ++i)
    {
        if (winner(triplets[i], player))
        {
            std::cout << "You wIn!!!\n";
            go = 0;
            break;
        }
    }
}


[edit: Oops. Noticed a cut/paste error. Fixed.]
Last edited on
Mr.cire I Cant understand your code beacuse I am just a Beginner, and one thing which i should tell you that originally cout<<"You wIn!!!"<<endl;
should be written cout<<"Computer wIn!!!"<<endl;
1
2
3
4
5
6
7
8
9
if(game[0][1]=='O'&&(go==1))
			{
				if((game[1][1]=='-')&&(game[2][1]=='O'))
				{
					game[1][1]='O';
					cout<<"You wIn!!!"<<endl;
					go=0;
				}
			}


And how i can fit your code in my program..
Last edited on
Topic archived. No new replies allowed.