Debian Codegraph @

src/echeckers.cc

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2002-2003 Andi Peredri *
3 * [email protected] *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21
22 // aw?1 - due to english rules a man reaching the king-row becomes a king
23 // and the is complete.
24 //
25 // English Checkers
26
27
28 #include "echeckers.h"
29
30
31 ///////////////////////////////////////////////////
32 //
33 // Player Functions
34 //
35 ///////////////////////////////////////////////////
36
37
38 bool ECheckers::go1(int from, int field)
39 {
40 from=internal(from);
41 field=internal(field);
42
43 to=field;
44
45 if(checkCapture1()) {
46 bool capture=false;
47
48 switch(board[from]) {
49 case MAN1:
50 if(manCapture1(from, UL, capture)) return true;
51 if(manCapture1(from, UR, capture)) return true;
52 return false;
53 case KING1:
54 if(kingCapture1(from, UL, capture)) return true;
55 if(kingCapture1(from, UR, capture)) return true;
56 if(kingCapture1(from, DL, capture)) return true;
57 if(kingCapture1(from, DR, capture)) return true;
58 return false;
59 }
60
61 } else {
62 switch(board[from]) {
63 case MAN1:
64 if((to==(from-6))||(to==(from-5))) {
65 board[from]=FREE;
66 if(to<10)
67 board[to]=KING1;
68 else
69 board[to]=MAN1;
70 return true;
71 }
72 return false;
73 case KING1:
74 if((to==(from-6))||(to==(from-5)) ||
75 (to==(from+5))||(to==(from+6)) ) {
76 board[from]=FREE;
77 board[to]=KING1;
78 return true;
79 }
80 return false;
81 }
82 }
83
84 return false;
85 }
86
87
88 bool ECheckers::checkCapture1() const
89 {
90 for(int i=6;i<48;i++) {
91 Captures* captures = getPossibleCapture(i);
92 bool result = ! captures->isEmpty();
93 delete captures;
94 if (result) {
95 return true;
96 }
97 }
98
99 return false;
100 }
101
102
103 Captures* ECheckers::getPossibleCapture(int i) const
104 {
105 switch(board[i]) {
106 case MAN1:
107 // forward-left
108 if(board[i-6]==MAN2 || board[i-6]==KING2)
109 if(board[i-12]==FREE) return new Captures(i, i-6, i-12);
110 // forward-right
111 if(board[i-5]==MAN2 || board[i-5]==KING2)
112 if(board[i-10]==FREE) return new Captures(i, i-5, i-10);
113 break;
114
115 case KING1:
116 // forward-left
117 if(board[i-6]==MAN2 || board[i-6]==KING2)
118 if(board[i-12]==FREE) return new Captures(i, i-6, i-12);
119 // forward-right
120 if(board[i-5]==MAN2 || board[i-5]==KING2)
121 if(board[i-10]==FREE) return new Captures(i, i-5, i-10);
122 // backward-left
123 if(board[i+5]==MAN2 || board[i+5]==KING2)
124 if(board[i+10]==FREE) return new Captures(i, i+5, i+10);
125 // backward-right
126 if(board[i+6]==MAN2 || board[i+6]==KING2)
127 if(board[i+12]==FREE) return new Captures(i, i+6, i+12);
128 }
129
130 return new Captures();
131 }
132
133
134 /* ORIG FUNC aw???
135 bool ECheckers::checkCapture1()
136 {
137 for(int i=6;i<48;i++) {
138 switch(board[i]) {
139 case MAN1:
140 // forward-left
141 if(board[i-6]==MAN2 || board[i-6]==KING2)
142 if(board[i-12]==FREE) return true;
143 // forward-right
144 if(board[i-5]==MAN2 || board[i-5]==KING2)
145 if(board[i-10]==FREE) return true;
146 break;
147
148 case KING1:
149 // forward-left
150 if(board[i-6]==MAN2 || board[i-6]==KING2)
151 if(board[i-12]==FREE) return true;
152 // forward-right
153 if(board[i-5]==MAN2 || board[i-5]==KING2)
154 if(board[i-10]==FREE) return true;
155 // backward-left
156 if(board[i+5]==MAN2 || board[i+5]==KING2)
157 if(board[i+10]==FREE) return true;
158 // backward-right
159 if(board[i+6]==MAN2 || board[i+6]==KING2)
160 if(board[i+12]==FREE) return true;
161 }
162 }
163
164 return false;
165 }
166 */
167
168
169 // Return TRUE if a course of the player true
170 // Return FALSE if a course of the player incorrect
171
172 bool ECheckers::manCapture1(int from, int direction, bool& capture)
173 {
174 int i=from+direction;
175
176 if(board[i]==MAN2 || board[i]==KING2) {
177 int k=i+direction;
178 if(board[k]==FREE) {
179 bool next=false;
180 int save=board[i];
181 board[from]=FREE;
182 board[i]=NONE;
183
184 // become a king!
185 if(k<10) {
186 board[k]=KING1;
187 /*aw?1
188 if(kingCapture1(k, direction+11, next)) {
189 board[i]=FREE;
190 return true;
191 }
192 */
193 } else {
194 board[k]=MAN1;
195 if(manCapture1(k,UL,next)) {board[i]=FREE; return true;}
196 if(manCapture1(k,UR,next)) {board[i]=FREE; return true;}
197 }
198
199 //?? make move here, too???
200 if((!next) && k==to) {board[i]=FREE; return true;}// move success
201
202 // move failed, restore
203 board[k]=FREE;
204 board[i]=save;
205 board[from]=MAN1;
206 capture=true;
207 }
208 }
209
210 return false;
211 }
212
213
214 bool ECheckers::kingCapture1(int from, int direction, bool& capture)
215 {
216 int i=from+direction;
217 if(board[i]==MAN2 || board[i]==KING2)
218 {
219 int k=i+direction;
220 if(board[k]==FREE)
221 {
222 bool next=false;
223 int save=board[i];
224 board[from]=FREE;
225 board[i]=NONE;
226 board[k]=KING1;
227
228 if(direction==UL || direction==DR) {
229 if(kingCapture1(k,UR,next)) {board[i]=FREE;return true;}
230 if(kingCapture1(k,DL,next)) {board[i]=FREE;return true;}
231 } else {
232 if(kingCapture1(k,UL,next)) {board[i]=FREE;return true;}
233 if(kingCapture1(k,DR,next)) {board[i]=FREE;return true;}
234 }
235 if(kingCapture1(k,direction,next)) {board[i]=FREE;return true;}
236
237 if((!next) && k==to) {board[i]=FREE;return true;}// move ok
238
239 // move failed, restore
240 board[k]=FREE;
241 board[i]=save;
242 board[from]=KING1;
243 capture=true;
244 }
245 }
246 return false;
247 }
248
249
250 ////////////////////////////////////////////////////
251 //
252 // Computer Functions
253 //
254 ////////////////////////////////////////////////////
255
256
257 void ECheckers::kingMove2(int from, int& resMax)
258 {
259 board[from]=FREE;
260
261 int i=from-6;
262 if(board[i]==FREE) {
263 board[i]=KING2;
264 turn(resMax);
265 board[i]=FREE;
266 }
267
268 i=from-5;
269 if(board[i]==FREE) {
270 board[i]=KING2;
271 turn(resMax);
272 board[i]=FREE;
273 }
274
275 i=from+5;
276 if(board[i]==FREE) {
277 board[i]=KING2;
278 turn(resMax);
279 board[i]=FREE;
280 }
281
282 i=from+6;
283 if(board[i]==FREE) {
284 board[i]=KING2;
285 turn(resMax);
286 board[i]=FREE;
287 }
288
289 board[from]=KING2;
290 }
291
292
293 bool ECheckers::checkCapture2() const
294 {
295 for(int i=6;i<48;i++)
296 {
297 switch(board[i])
298 {
299 case MAN2:
300 if(board[i+5]==MAN1 || board[i+5]==KING1)
301 if(board[i+10]==FREE) return true;
302 if(board[i+6]==MAN1 || board[i+6]==KING1)
303 if(board[i+12]==FREE) return true;
304 break;
305 case KING2:
306 if(board[i-6]==MAN1 || board[i-6]==KING1)
307 if(board[i-12]==FREE) return true;
308 if(board[i-5]==MAN1 || board[i-5]==KING1)
309 if(board[i-10]==FREE) return true;
310 if(board[i+5]==MAN1 || board[i+5]==KING1)
311 if(board[i+10]==FREE) return true;
312 if(board[i+6]==MAN1 || board[i+6]==KING1)
313 if(board[i+12]==FREE) return true;
314 }
315 }
316 return false;
317 }
318
319
320 // Return TRUE if it is possible to capture
321 // Return FALSE if it is impossible to capture
322 bool ECheckers::manCapture2(int from, int& resMax)
323 {
324 bool capture=false;
325
326 // try left-down
327 int i=from+5;
328 if(board[i]==MAN1 || board[i]==KING1) {
329 int k=from+10;
330 if(board[k]==FREE) {
331 int save=board[i];
332 board[from]=FREE;
333 board[i]=NONE;
334 resMax--;
335
336 // become a king!
337 if(from>32) {
338 board[k]=KING2;
339 // aw?1
340 turn(resMax, true); //aw???
341 //aw??if(!kingCapture2(k, UL, resMax)) turn(resMax, true);
342 } else {
343 board[k]=MAN2;
344 if(!manCapture2(k, resMax)) turn(resMax, true);
345 }
346
347 // restore
348 resMax++;
349 board[k]=FREE;
350 board[i]=save;
351 board[from]=MAN2;
352 capture=true;
353 }
354 }
355
356 // now right-down
357 i=from+6;
358 if(board[i]==MAN1 || board[i]==KING1) {
359 int k=from+12;
360 if(board[k]==FREE) {
361 int save=board[i];
362 board[from]=FREE;
363 board[i]=NONE;
364 resMax--;
365
366 // become a king!
367 if(from>32) {
368 board[k]=KING2;
369 // aw?1
370 turn(resMax, true); // aw???
371 //aw???if(!kingCapture2(k,UR,resMax)) turn(resMax,true);
372 } else {
373 board[k]=MAN2;
374 if(!manCapture2(k,resMax)) turn(resMax,true);
375 }
376
377 // restore
378 resMax++;
379 board[k]=FREE;
380 board[i]=save;
381 board[from]=MAN2;
382 capture=true;
383 }
384 }
385
386 if(capture) return true;
387 return false;
388 }
389
390
391 bool ECheckers::kingCapture2(int from, int direction, int &resMax)
392 {
393 int i=from+direction;
394 if(board[i]==MAN1 || board[i]==KING1)
395 {
396 int k=i+direction;
397 if(board[k]==FREE)
398 {
399 bool capture=false;
400 int save=board[i];
401 board[from]=FREE;
402 board[i]=NONE;
403 resMax--;
404
405 board[k]=KING2;
406 if(direction==UL || direction==DR) {
407 if(kingCapture2(k,UR,resMax)) capture=true;
408 if(kingCapture2(k,DL,resMax)) capture=true;
409 } else {
410 if(kingCapture2(k,UL,resMax)) capture=true;
411 if(kingCapture2(k,DR,resMax)) capture=true;
412 }
413 if(kingCapture2(k,direction,resMax)) capture=true;
414
415 if(!capture) turn(resMax,true);
416 board[k]=FREE;
417
418 //restore
419 resMax++;
420 board[i]=save;
421 board[from]=KING2;
422 return true;
423 }
424 }
425 return false;
426 }
427
428
429 QString ECheckers::getFieldNotation(int i, bool bottom_is_white) const {
430 QString notation = QString(ENOTATION);
431 if (bottom_is_white) {
432 return notation.mid(i*2, 2).trimmed();
433 } else {
434 return notation.mid(62-i*2, 2).trimmed();
435 }
436 }
437