checker program error

my checker program can work fine on some place without the checker blocked or on the corner but it has bug when in the corner or blocked on the side.

The main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include "/home/programmer/Desktop/C++/Chapter09/CheckerHeaderFile/SetBoard.cxx"
#include "/home/programmer/Desktop/C++/Chapter09/CheckerHeaderFile/Go.cxx"
using namespace std;

int main()
{
	const int COUNT=8;
	char board[COUNT][COUNT];
	setBoard(board, COUNT);
	int turn=0;
	
	while(++turn)
	{
		showBoard(board, COUNT);
		go(board, COUNT, turn);
		//evaluate(board, COUNT);
	}
}
The main and showBoard which looks fine to me too

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
#ifndef SETBOARD_CXX
#define SETBOARD_CXX
#include <iostream>
using namespace std;

void setBoard(char board[][8], const int COUNT)
{
	for(int i=COUNT-1; i>=0; --i)
	{
		for(int j=0; j<COUNT; ++j)
		{
			if((i%2==0 && (j==0
			|| j==2)) || (i%2==1
			&& j==1))
			board[i][j]='w';
			else
			if((i%2==1 && (j==5
			|| j==7)) || (i%2==0
			&& j==6))
			board[i][j]='b';
			else
			board[i][j]='-';
		}
	}
}

void showBoard(char board[][8], const int COUNT)
{
	for(int i=COUNT-1; i>=0; --i)
	{
		cout << i << " ";
		for(int j=0; j<COUNT; ++j)
		{
			cout << board[j][i] << " ";
		}
		cout << endl;
	}
	cout << "  0 1 2 3 4 5 6 7";
}
#endif
I think the problem is in here:

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
335
#include <iostream>
using namespace std;

#ifndef GO_CXX
#define GO_CXX
void whiteGo(char board[][8], const int COUNT);
void blackGo(char board[][8], const int COUNT);
bool blocked(char board[][8], const int COUNT, int x, int y);
// block is checked for if the piece is could not move
void whitePoonGo(char board[][8], const int COUNT, int x, int y, bool &done);
void whiteKingGo(char board[][8], const int COUNT, int x, int y, bool &done);
void blackPoonGo(char board[][8], const int COUNT, int x, int y, bool &done);
void blackKingGo(char board[][8], const int COUNT, int x, int y, bool &done);

void go(char board[][8], const int COUNT, int turn)//see which one go
{
	if(turn%2==1)
	{
		whiteGo(board, COUNT);
	}
	else
	{
		blackGo(board, COUNT);
	}
}

void whiteGo(char board[][8], const int COUNT)
{
	bool done=false;//done is had you gone, false is not gone, true means went already
	int x, y;
	cout << endl << "White's Go!" << endl;
	cout << "Please enter x for white: ";
	cin >> x;
	cout << "Please enter y for white: ";
	cin >> y;
	while((board[x][y]!='w' && board[x][y]!='W')
	|| blocked(board, COUNT, x, y))
	{
		cout << "Please enter correct location for white!" << endl;
		cout << "Please enter x for white again: ";
		cin >> x;
		cout << "Please enter y for white again: ";
		cin >> y;
	}
	if(done==false)
	{
		if(board[x][y]=='w')
		whitePoonGo(board, COUNT, x, y, done);
		else
		whiteKingGo(board, COUNT, x, y, done);
	}
}

void blackGo(char board[][8], const int COUNT)
{
	bool done=false;
	int x, y;
	cout << endl << "Black's Go!" << endl;
	cout << "Please enter x for black: ";
	cin >> x;
	cout << "Please enter y for black: ";
	cin >> y;
	while((board[x][y]!='b' && board[x][y]!='B')
	|| blocked(board, COUNT, x, y))
	{
		cout << "Please enter correct location for black!" << endl;
		cout << "Please enter x for black again: ";
		cin >> x;
		cout << "Please enter y for black again: ";
		cin >> y;
	}
	if(done==false)
	{
		if(board[x][y]=='b')
		blackPoonGo(board, COUNT, x, y, done);
		else
		blackKingGo(board, COUNT, x, y, done);
	}
}

bool blocked(char board[][8], const int COUNT, int x, int y)
{
	bool output=false;
	switch(board[x][y])
	{
		case 'w'://see if four side of 'w' are blocked
		if(((board[x-1][y+1]=='w' || board[x-1][y+1]=='W')
		|| ((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
		&& board[x-2][y+2]!='-'))
		&&
		((board[x+1][y+1]=='w' || board[x+1][y+1]=='W')
		|| ((board[x+1][y+1]=='b' || board[x+1][y+1]=='B')
		&& board[x+2][y+2]!='-')))
		output=true;
		break;
		
		case 'W'://see if four side of 'W' are blocked
		if(((board[x-1][y+1]=='w' || board[x-1][y+1]=='W')
		|| ((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
		&& board[x-2][y+2]!='-'))
		&&
		((board[x+1][y+1]=='w' || board[x+1][y+1]=='W')
		|| ((board[x+1][y+1]=='b' || board[x+1][y+1]=='B')
		&& board[x+2][y+2]!='-'))
		&&
		((board[x+1][y-1]=='w' || board[x+1][y-1]=='W')
		|| ((board[x+1][y-1]=='b' || board[x+1][y-1]=='B')
		&& board[x+2][y-2]!='-'))
		&&
		((board[x-1][y-1]=='w' || board[x-1][y-1]=='W')
		|| ((board[x-1][y-1]=='b' || board[x-1][y-1]=='B')
		&& board[x-2][y-2]!='-')))
		output=true;
		break;
		
		case 'b'://see if four side of 'b' are blocked
		if(((board[x-1][y-1]=='b' || board[x-1][y-1]=='B')
		|| ((board[x-1][y-1]=='w' || board[x-1][y-1]=='W')
		&& board[x-2][y-2]!='-'))
		&&
		((board[x+1][y-1]=='b' || board[x+1][y-1]=='B')
		|| ((board[x+1][y-1]=='w' || board[x+1][y-1]=='W')
		&& board[x+2][y-2]!='-')))
		output=true;
		break;
		
		case 'B'://see if four side of 'B' are blocked
		if(((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
		|| ((board[x-1][y+1]=='w' || board[x-1][y+1]=='w')
		&& board[x-2][y+2]!='-'))
		&&
		((board[x+1][y+1]=='b' || board[x+1][y+1]=='B')
		|| ((board[x+1][y+1]=='w' || board[x+1][y+1]=='W')
		&& board[x+2][y+2]!='-'))
		&&
		((board[x+1][y-1]=='b' || board[x+1][y-1]=='B')
		|| ((board[x+1][y-1]=='w' || board[x+1][y-1]=='W')
		&& board[x+2][y-2]!='-'))
		&&
		((board[x-1][y-1]=='b' || board[x-1][y-1]=='B')
		|| ((board[x-1][y-1]=='w' || board[x-1][y-1]=='W')
		&& board[x-2][y-2]!='-')))
		output=true;
		break;
	}
	return output;
}
	
void whitePoonGo(char board[][8], const int COUNT, int x, int y, bool &done)
{
	int way;
	bool eat=true;
	bool go=false;
	while(eat==true)
	{
		cout << "Please enter you want it to go left front(1) "
		<< "or right front(2) or choose again(5) or stop(6): ";
		cin >> way;
		
		switch(way)
		{
			case 1:
			if((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
			&& (board[x-2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x-1][y+1]='-';
				board[x-2][y+2]=temp;
				x-=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x-1][y+1]=='-')
			{
				board[x-1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 2:
			if((board[x+1][y+1]=='b' || board[x+1][y+1]=='B')
			&& (board[x+2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x+1][y+1]='-';
				board[x+2][y+2]=temp;
				x+=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x+1][y+1]=='-')
			{
				board[x+1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 5:
			if(go==false)
				whiteGo(board, COUNT);
			else
				cout << "You went already!" << endl;
			break;
			
			case 6:
			if(go==true)
				eat=false;
		}
		done=true;
	}
}
			
void whiteKingGo(char board[][8], const int COUNT, int x, int y, bool &done)
{
	int way;
	bool eat=true;
	bool go=false;
	while(eat==true)
	{
		cout << "Please enter you want it to go left front(1) or right front(2) "
		<< "or left back(3) or right back(4) or choose again(5) or stop(6): ";
		cin >> way;
		
		switch(way)
		{
			case 1:
			if((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
			&& (board[x-2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x-1][y+1]='-';
				board[x-2][y+2]=temp;
				x-=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x-1][y+1]=='-')
			{
				board[x-1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 2:
			if((board[x+1][y+1]=='b' || board[x+1][y+1]=='B')
			&& (board[x+2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x+1][y+1]='-';
				board[x+2][y+2]=temp;
				x+=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x+1][y+1]=='-')
			{
				board[x+1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 3:
			if((board[x-1][y-1]=='b' || board[x-1][y-1]=='B')
			&& (board[x-2][y-2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x-1][y-1]='-';
				board[x-2][y-2]=temp;
				x-=2;
				y-=2;
				go=true;
			}
			else 
			if(board[x-1][y-1]=='-')
			{
				board[x-1][y-1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 4:
			if((board[x-1][y+1]=='b' || board[x-1][y+1]=='B')
			&& (board[x-2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x-1][y+1]='-';
				board[x-2][y+2]=temp;
				x-=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x-1][y+1]=='-')
			{
				board[x-1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;
			
			case 5:
			if(go==false)
				whiteGo(board, COUNT);
			else
				cout << "You went already!" << endl;
			break;
			
			case 6:
			if(go==true)
				eat=false;
		}
		done=true;
	}
}


Because of the size I deleted the black poon and black king function, but basicly they are same just like white poon and white king.
If I choose some ok function like x=0, y=2, It is fine


7 - b - b - b - b 
6 b - b - b - b - 
5 - b - b - b - b 
4 - - - - - - - - 
3 - - - - - - - - 
2 w - w - w - w - 
1 - w - w - w - w 
0 w - w - w - w - 
  0 1 2 3 4 5 6 7
White's Go!
Please enter x for white: 0
Please enter y for white: 2
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 2
7 - b - b - b - b 
6 b - b - b - b - 
5 - b - b - b - b 
4 - - - - - - - - 
3 - w - - - - - - 
2 - - w - w - w - 
1 - w - w - w - w 
0 w - w - w - w - 
  0 1 2 3 4 5 6 7
Black's Go!
Please enter x for black: 

but x=0, y=0 will mess up the game


7 - b - b - b - b 
6 b - b - b - b - 
5 - b - b - b - b 
4 - - - - - - - - 
3 - - - - - - - - 
2 w - w - w - w - 
1 - w - w - w - w 
0 w - w - w - w - 
  0 1 2 3 4 5 6 7
White's Go!
Please enter x for white: 0
Please enter y for white: 0
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 1
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 2
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 5

White's Go!
Please enter x for white: 0
Please enter y for white: 2
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 1
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 2
Please enter you want it to go left front(1) or right front(2) or choose again(5) or stop(6): 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch(way)
		{
			case 1:
			if((board[x-1][y+1]=='b' || board[x-1][y+1]=='B') // board[x-1] out of bounds for the array
			&& (board[x-2][y+2]=='-'))
			{
				char temp=board[x][y];
				board[x][y]='-';
				board[x-1][y+1]='-';
				board[x-2][y+2]=temp;
				x-=2;
				y+=2;
				go=true;
			}
			else 
			if(board[x-1][y+1]=='-')
			{
				board[x-1][y+1]=board[x][y];
				board[x][y]='-';
				eat=false;
				go=true;
			}
			break;


As you can see by my comment, you are going out of bounds on the array, so the first part is always false making the whole condition false.

So you need to first check the position to see if it is valid for the requested operation.

Another tip - don't do this:

1
2
else 
if(board[x-1][y+1]=='-')


Do this - which allows for multiple else if's:

1
2
3
4
5
6
7
8
9
10
11
12
if (/* your condition */) {
//your code
}
else if(/* your condition */) {
//your code
}
else if(/* your condition */) {
//your code
}
else {  //use this for anything else - probably an error situation
//your code
}


Hope all goes well :D
Topic archived. No new replies allowed.