~ml-launchpad/ubuntu/natty/gcompris/fix-for-777349

« back to all changes in this revision

Viewing changes to src/boards/chess_notation.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Gariepy, Marc Gariepy, Stephane Graber
  • Date: 2010-01-04 17:42:49 UTC
  • mfrom: (1.1.14 upstream)
  • Revision ID: james.westby@ubuntu.com-20100104174249-7bupatd9dtxyhvs4
Tags: 9.0-0ubuntu1
[Marc Gariepy]
* New upstream release (9.0).
* Remove cache.c from POTFILES to avoid FTBFS
* Remove unneeded rm in debian/rules (file no longer exists upstream)

[Stephane Graber]
* Bump Debian standards to 3.8.3
* Add patch system (dpatch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 1999,2001 Robert Wilhelm
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 3 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 
 */
17
 
 
18
 
#include <stdlib.h>
19
 
#include <string.h>
20
 
#include <ctype.h>
21
 
#include "chess_position.h"
22
 
#include "chess_notation.h"
23
 
 
24
 
static char piece_to_ascii_t[]= {' ','N','B','R','Q','K'};
25
 
 
26
 
static int norm_piece (Piece piece);
27
 
 
28
 
static void
29
 
file_to_ascii (char **move, Square square)
30
 
{
31
 
        *(*move)++ = square - square / 10 * 10 + 96;    /* a - h */
32
 
}
33
 
 
34
 
static void
35
 
rank_to_ascii (char **move, Square square)
36
 
{
37
 
        *(*move)++ = square / 10 + 47 ;                 /* 1 - 8 */
38
 
}
39
 
 
40
 
void
41
 
square_to_ascii(char **move, Square square)
42
 
{
43
 
        file_to_ascii (move, square);
44
 
        rank_to_ascii (move, square);
45
 
}
46
 
 
47
 
 
48
 
static int
49
 
same_rank (Square square, Square square2)
50
 
{
51
 
        char *s1;
52
 
        char *s2;
53
 
        char same1;
54
 
        char same2;
55
 
 
56
 
        s1 = &same1;
57
 
        s2 = &same2;
58
 
        rank_to_ascii (&s1, square);
59
 
        rank_to_ascii (&s2, square2);
60
 
 
61
 
        if (same1 == same2)
62
 
                return 1;
63
 
 
64
 
        return 0;
65
 
}
66
 
 
67
 
static int
68
 
same_file (Square square, Square square2)
69
 
{
70
 
        char *s1;
71
 
        char *s2;
72
 
        char same1;
73
 
        char same2;
74
 
 
75
 
        s1 = &same1;
76
 
        s2 = &same2;
77
 
        file_to_ascii (&s1, square);
78
 
        file_to_ascii (&s2, square2);
79
 
        if (same1 == same2)
80
 
                return 1;
81
 
 
82
 
        return 0;
83
 
}
84
 
 
85
 
 
86
 
static void
87
 
delete_x (char *str)
88
 
{
89
 
        char *p = strchr (str, 'x');
90
 
 
91
 
        if (p)
92
 
                while ((*p = *(p+1)))
93
 
                        p++;
94
 
}
95
 
 
96
 
static void
97
 
delete_plus (char *str)
98
 
{
99
 
        char *p = strchr (str, '+');
100
 
 
101
 
        if (p)
102
 
                while ((*p = *(p+1)))
103
 
                        p++;
104
 
}
105
 
 
106
 
static void
107
 
delete_ep (char *str)
108
 
{
109
 
        char *p = strstr (str, "ep");
110
 
 
111
 
        if (p)
112
 
                while ((*p = *(p+2)))
113
 
                        p++;
114
 
}
115
 
 
116
 
static void
117
 
delete_equal (char *str)
118
 
{
119
 
        char *p = strstr (str, "=");
120
 
 
121
 
        if (p)
122
 
                while ((*p = *(p+1)))
123
 
                        p++;
124
 
}
125
 
 
126
 
static void
127
 
delete_hash (char *str)
128
 
{
129
 
        char *p = strstr (str, "#");
130
 
 
131
 
        if (p)
132
 
                while ((*p = *(p+1)))
133
 
                        p++;
134
 
}
135
 
 
136
 
char *
137
 
move_to_ascii (char *p, Square from, Square to)
138
 
{
139
 
        Square a;
140
 
 
141
 
        file_to_ascii (&p, from);
142
 
        rank_to_ascii (&p, from);
143
 
 
144
 
        if (to & 128) {
145
 
                /* promotion */
146
 
                a = to;
147
 
 
148
 
                if (from > E4)
149
 
                        a = (a & 7) + A8; /* white */
150
 
                else
151
 
                        a = (a & 7) + A1; /* black */
152
 
 
153
 
                *p++    = a - a / 10 * 10 + 96;     /*  a - h       */
154
 
                *p++    = a / 10 + 47 ;             /*  1 - 8       */
155
 
                *p++    = '=';
156
 
                *p++ = piece_to_ascii_t[((to >> 3) & 7)-1];
157
 
        } else {
158
 
                file_to_ascii (&p, to);
159
 
                rank_to_ascii (&p, to);
160
 
        }
161
 
 
162
 
        *p = '\0';
163
 
 
164
 
        return p;
165
 
}
166
 
 
167
 
int
168
 
ascii_to_piece (char p)
169
 
{
170
 
        if (p == 'q')
171
 
                return WQ-WP;
172
 
        if (p == 'r')
173
 
                return WR-WP;
174
 
        if (p == 'b')
175
 
                return WB-WP;
176
 
        if (p == 'n')
177
 
                return WN-WP;
178
 
        if (p == 'Q')
179
 
                return WQ-WP;
180
 
        if (p == 'R')
181
 
                return WR-WP;
182
 
        if (p == 'B')
183
 
                return WB-WP;
184
 
        if (p == 'N')
185
 
                return WN-WP;
186
 
 
187
 
        g_assert_not_reached ();
188
 
 
189
 
        return -1;
190
 
}
191
 
 
192
 
char
193
 
piece_to_ascii (int piece)
194
 
{
195
 
  static char piece_to_ascii_full[]= {'P','N','B','R','Q','K'};
196
 
  int i;
197
 
 
198
 
  if(piece == EMPTY)
199
 
    return(' ');
200
 
 
201
 
  i = norm_piece (piece);
202
 
 
203
 
  if(WPIECE(piece))
204
 
    return piece_to_ascii_full[i];
205
 
  else
206
 
    return tolower(piece_to_ascii_full[i]);
207
 
 
208
 
  g_assert_not_reached ();
209
 
 
210
 
  return -1;
211
 
}
212
 
 
213
 
void
214
 
ascii_to_move (Position *pos, char *p, Square *from, Square *to)
215
 
{
216
 
        delete_x (p);
217
 
 
218
 
        if (*p == 'o') {
219
 
                /* Castling */
220
 
                if (!strcmp (p, "o-o-o")) {
221
 
                        if (position_get_color_to_move (pos) == WHITE) {
222
 
                                *from = E1;
223
 
                                *to   = C1;
224
 
                        } else {
225
 
                                *from = E8;
226
 
                                *to   = C8;
227
 
                        }
228
 
                } else {
229
 
                        if (position_get_color_to_move (pos) == WHITE) {
230
 
                                *from = E1;
231
 
                                *to   = G1;
232
 
                        } else {
233
 
                                *from = E8;
234
 
                                *to   = G8;
235
 
                        }
236
 
                }
237
 
                return;
238
 
        }
239
 
 
240
 
        *from = (*p - 'a' + 1) + (*(p + 1) - '1' + 2 ) * 10;
241
 
        p += 2;
242
 
        *to = (*p - 'a' + 1) + (*(p + 1) - '1' + 2 ) * 10;
243
 
        p += 2;
244
 
 
245
 
        if (*p == 'q' || *p == 'r' || *p == 'b' || *p =='n' ||
246
 
            *p =='Q' || *p =='R' || *p == 'B' || *p == 'N' ) {
247
 
                /* Promotion */
248
 
                if (*to < A2)
249
 
                        *to = 128 +  *to - A1 + (ascii_to_piece (*p) + 1) * 8;
250
 
                else if (*to > A7)
251
 
                        *to = 128 +  *to - A8 + (ascii_to_piece (*p) + 1) * 8;
252
 
                else
253
 
                        g_assert_not_reached ();
254
 
        }
255
 
}
256
 
 
257
 
int
258
 
san_to_move (Position *pos, char *str, Square *from, Square *to)
259
 
{
260
 
        Square zugliste[AB_ZUGL];
261
 
        Square *ap, *aq;
262
 
        gshort anz, anz_n, anz_s;
263
 
        gshort i;
264
 
        gchar *p;
265
 
        gchar liste[100][10];
266
 
 
267
 
        delete_x(str);
268
 
        delete_plus(str);
269
 
        delete_ep(str);
270
 
        delete_equal(str);
271
 
        delete_hash(str);
272
 
 
273
 
        ap =  zugliste + AB_ZUG_S;
274
 
        anz = position_legal_move (pos, &ap, &anz_s, &anz_n);
275
 
 
276
 
        for (aq = ap, i = 0; i < anz; i++, aq += 2) {
277
 
                p = liste[i];
278
 
                piece_move_to_ascii (p, pos->square[*aq], *aq, *(aq + 1));
279
 
 
280
 
                if (*p == ' ') {
281
 
                        /* pawn move */
282
 
 
283
 
                        /* e.g. e2e4 */
284
 
                        p++;
285
 
                        if (!strcmp (p, str)) {
286
 
                                *from = *aq;
287
 
                                *to   = *(aq+1);
288
 
                                return 0;
289
 
                        }
290
 
 
291
 
                        /* e.g. ed5 */
292
 
                        p[1]=p[2];
293
 
                        p[2]=p[3];
294
 
                        p[3]=p[4];
295
 
                        p[4]=p[5];
296
 
 
297
 
                        /* not e.g. bb3 */
298
 
                        if (p[0] != p[1])
299
 
                                if (!strcmp(p,str)) {
300
 
                                        *from = *aq;
301
 
                                        *to   = *(aq+1);
302
 
                                        return 0;
303
 
                                }
304
 
 
305
 
                        /* e.g. d5 */
306
 
                        p++;
307
 
                        if (!strcmp(p,str)) {
308
 
                                *from = *aq;
309
 
                                *to   = *(aq+1);
310
 
                                return 0;
311
 
                        }
312
 
 
313
 
                } else {
314
 
                        /* no pawn move */
315
 
                        char  tmp;
316
 
 
317
 
                        /* e.g. Ng1f3 */
318
 
                        if (!strcmp (p, str)) {
319
 
                                *from = *aq;
320
 
                                *to   = *(aq+1);
321
 
                                return 0;
322
 
                        }
323
 
 
324
 
                        /* Ngf3 */
325
 
                        tmp =p[2];
326
 
                        p[2]=p[3];
327
 
                        p[3]=p[4];
328
 
                        p[4]=p[5];
329
 
 
330
 
                        if (!strcmp(p,str)) {
331
 
                                *from = *aq;
332
 
                                *to   = *(aq+1);
333
 
                                return 0;
334
 
                        }
335
 
 
336
 
                        /* N1f3 */
337
 
                        p[1]=tmp;
338
 
 
339
 
                        if (!strcmp(p,str)) {
340
 
                                *from = *aq;
341
 
                                *to   = *(aq+1);
342
 
                                return 0;
343
 
                        }
344
 
 
345
 
                        /* Nf3 */
346
 
                        p[1]=p[2];
347
 
                        p[2]=p[3];
348
 
                        p[3]=p[4];
349
 
 
350
 
                        if (!strcmp(p,str)) {
351
 
                                *from = *aq;
352
 
                                *to   = *(aq+1);
353
 
                                return 0;
354
 
                        }
355
 
                }
356
 
        }
357
 
 
358
 
        return 1;
359
 
}
360
 
 
361
 
static int
362
 
norm_piece (Piece piece)
363
 
{
364
 
        if (WPIECE (piece))
365
 
                return piece - WP;
366
 
        if (BPIECE (piece))
367
 
                return piece - BP;
368
 
 
369
 
        return piece;
370
 
}
371
 
 
372
 
void
373
 
piece_move_to_ascii (char *p, Piece piece, Square from, Square to)
374
 
{
375
 
        int i;
376
 
 
377
 
        if ((piece == WK || piece == BK) && abs (from - to) == 2) {
378
 
                if (to % 10 == 3) {
379
 
                        strcpy (p,"O-O-O");
380
 
                        return;
381
 
                }
382
 
                if (to % 10 == 7) {
383
 
                        strcpy (p,"O-O");
384
 
                        return;
385
 
                }
386
 
                g_assert_not_reached ();
387
 
        }
388
 
 
389
 
        i = norm_piece (piece);
390
 
        *p++ = piece_to_ascii_t[i];
391
 
        move_to_ascii (p, from, to);
392
 
}
393
 
 
394
 
char *
395
 
move_to_san (Position *pos, Square from, Square to)
396
 
{
397
 
        Square checksquare, checkto;
398
 
        Piece piece, promote;
399
 
        int norm, desrank, desfile, inc;
400
 
        int i, tempdesfile, tempdesrank;
401
 
        char *san;
402
 
        char *temp;
403
 
        const int jump[]={ 8, 12,19, 21,-8,-12,-19,-21};
404
 
 
405
 
        san = g_new0 (char, 12);
406
 
        temp = san;
407
 
 
408
 
        desrank = desfile = promote = 0;
409
 
 
410
 
        /* Handle Promotion */
411
 
        if (to & 128) {
412
 
                promote = ((to >> 3) & 7)-1;
413
 
                if ( from > E4) {
414
 
                        to = (to & 7) + A8;
415
 
                        piece = WP;
416
 
                } else {
417
 
                        to = (to & 7) + A1;
418
 
                        piece = BP;
419
 
                }
420
 
        } else {
421
 
                piece = pos->square[to];
422
 
        }
423
 
 
424
 
        /* Check if we have to designate the rank or file */
425
 
        switch (piece) {
426
 
        case WQ:
427
 
        case BQ:
428
 
                /* Check like rooks and bishops */
429
 
        case WR:
430
 
        case BR:
431
 
                /* Check for other rooks/queens */
432
 
                i = 0;
433
 
                while (1) {
434
 
                        tempdesfile = tempdesrank = 0;
435
 
                        if (i == 0) {
436
 
                                checksquare = 20 + (to % 10);
437
 
                                checkto =  90 + (to % 10);
438
 
                                if (from / 10 > to / 10 )
439
 
                                        checkto = to - 10;
440
 
                                else if (from / 10 < to /10)
441
 
                                        checksquare = to + 10;
442
 
                                inc = 10;
443
 
                        } else if (i == 1) {
444
 
                                checksquare = 10 * (to / 10) + 1;
445
 
                                checkto =  10 * (to / 10) + 8;
446
 
                                if (from % 10 > to % 10)
447
 
                                        checkto = to - 1;
448
 
                                else if (from % 10 < to % 10)
449
 
                                        checksquare = to + 1;
450
 
                                inc = 1;
451
 
                        } else {
452
 
                                break;
453
 
                        }
454
 
 
455
 
                        while (checksquare <= checkto) {
456
 
                                if (pos->square[checksquare] == piece && checksquare != to) {
457
 
                                        if (same_rank(from, checksquare))
458
 
                                                tempdesfile = 1;
459
 
                                        else if (same_file(from, checksquare))
460
 
                                                desrank = 1;
461
 
                                        else
462
 
                                                tempdesfile = 1;
463
 
                                } else if (pos->square[checksquare] != EMPTY && checksquare < to) {
464
 
                                        tempdesfile = tempdesrank = 0;  /* A piece is in the way */
465
 
                                } else if (pos->square[checksquare] != EMPTY && checksquare > to) {
466
 
                                        break;                  /* A piece is in the way */
467
 
                                }
468
 
                                checksquare += inc;
469
 
                        }
470
 
                        i++;
471
 
                        if (tempdesfile == 1) desfile = 1;
472
 
                        if (tempdesrank == 1) desrank = 1;
473
 
                }
474
 
                if (piece == WR || piece == BR) break;
475
 
        case WB:
476
 
        case BB:
477
 
                /* Check for other bishops/queens */
478
 
                i = 0;
479
 
                while (1) {
480
 
                        tempdesfile = tempdesrank = 0;
481
 
                        if (i == 0) {
482
 
                                checksquare = to - (((to % 10) - 1) * 11);
483
 
                                checkto =  to + ((9 - (to / 10)) * 11);
484
 
                                if (from % 10 > to % 10  && from / 10 > to / 10)
485
 
                                        checkto = to - 11;
486
 
                                else if (from % 10 < to % 10 && from / 10 < to / 10)
487
 
                                        checksquare = to + 11;
488
 
                                inc = 11;
489
 
                        } else if (i == 1) {
490
 
                                checksquare = to - ((8 - (to % 10)) * 9);
491
 
                                checkto =  to + ((9 - (to / 10)) * 9);
492
 
                                if (from % 10 > to % 10 && from / 10 < to / 10)
493
 
                                        checksquare = to + 9;
494
 
                                else if (from % 10 < to % 10  && from / 10 > to / 10)
495
 
                                        checkto = to - 9;
496
 
                                inc = 9;
497
 
                        } else {
498
 
                                break;
499
 
                        }
500
 
 
501
 
                        while (checksquare <= checkto) {
502
 
                                if (pos->square[checksquare] == piece && checksquare != to) {
503
 
                                        if (same_rank(from, checksquare))
504
 
                                                tempdesfile = 1;
505
 
                                        else if (same_file(from, checksquare))
506
 
                                                desrank = 1;
507
 
                                        else
508
 
                                                tempdesfile = 1;
509
 
                                } else if (pos->square[checksquare] != EMPTY && checksquare < to) {
510
 
                                        tempdesfile = tempdesrank = 0;  /* A piece is in the way */
511
 
                                } else if (pos->square[checksquare] != EMPTY && checksquare > to) {
512
 
                                        break;                  /* A piece is in the way */
513
 
                                }
514
 
                                checksquare += inc;
515
 
                        }
516
 
                        i++;
517
 
                        if (tempdesfile == 1) desfile = 1;
518
 
                        if (tempdesrank == 1) desrank = 1;
519
 
                }
520
 
                break;
521
 
        case WN:
522
 
        case BN:
523
 
                /* Check for other knights */
524
 
                for (i=0;i<8;i++) {
525
 
                        if (pos->square[to+jump[i]] == piece && to+jump[i] >= 0) {
526
 
                                if (same_rank(from, to+jump[i]))
527
 
                                        desfile = 1;
528
 
                                else if (same_file(from, to+jump[i]))
529
 
                                        desrank = 1;
530
 
                                else
531
 
                                        desfile = 1;
532
 
                        }
533
 
                }
534
 
                break;
535
 
 
536
 
        }
537
 
 
538
 
        /* Handle Castling */
539
 
        if ((piece == WK || piece == BK) && abs(from-to) == 2) {
540
 
                if (to % 10 == 3)
541
 
                        strcpy(temp,"O-O-O");
542
 
                if (to % 10 == 7)
543
 
                        strcpy(temp,"O-O");
544
 
        } else {
545
 
                /* The piece letter */
546
 
                norm = norm_piece(piece);
547
 
                if (norm > 0)
548
 
                        *temp++ = piece_to_ascii_t[norm];
549
 
 
550
 
                /* The rank/file designators */
551
 
                if (desfile)
552
 
                        file_to_ascii(&temp, from);
553
 
                if (desrank)
554
 
                        rank_to_ascii(&temp, from);
555
 
 
556
 
                /* If there was a capture */
557
 
                if (position_last_piece_captured (pos) != EMPTY) {
558
 
                        if (piece == WP || piece == BP)
559
 
                                file_to_ascii (&temp, from);
560
 
                        *temp++ = 'x';
561
 
                }
562
 
 
563
 
                /* Destination square */
564
 
                square_to_ascii (&temp, to);
565
 
 
566
 
                /* If there was promotion */
567
 
                if (promote) {
568
 
                        *temp++ = '=';
569
 
                        norm = norm_piece(promote);
570
 
                        *temp++ = piece_to_ascii_t[norm];
571
 
                }
572
 
 
573
 
                *temp = '\0';
574
 
        }
575
 
 
576
 
        temp = san;
577
 
        san = g_strdup (temp);
578
 
        g_free (temp);
579
 
        return san;
580
 
}