src/rules/bishop.c

changeset 17
2aed5418e142
parent 16
a298c6637c30
child 18
6008840b859e
     1.1 --- a/src/rules/bishop.c	Fri Mar 28 14:32:52 2014 +0100
     1.2 +++ b/src/rules/bishop.c	Sat Mar 29 14:46:33 2014 +0100
     1.3 @@ -29,18 +29,100 @@
     1.4  
     1.5  #include "bishop.h"
     1.6  #include "rules.h"
     1.7 +#include <math.h>
     1.8  
     1.9  _Bool bishop_chkrules(Move* move) {
    1.10 -    // TODO: implement
    1.11 +    return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile);
    1.12 +}
    1.13 +
    1.14 +_Bool bishop_isblocked(Board board, Move *move) {
    1.15 +    int dy = move->torow > move->fromrow ? 1 : -1;
    1.16 +    int dx = move->tofile > move->fromfile ? 1 : -1;
    1.17 +    
    1.18 +    uint8_t y = move->fromrow;
    1.19 +    uint8_t x = move->fromfile;
    1.20 +    
    1.21 +    do {
    1.22 +        x += dx;
    1.23 +        y += dy;
    1.24 +        if (board[y][x]) {
    1.25 +            return TRUE;
    1.26 +        }
    1.27 +    } while (x != move->tofile && y != move->torow);
    1.28 +    
    1.29      return FALSE;
    1.30  }
    1.31  
    1.32 -_Bool bishop_isblocked(Board board, Move *move) {
    1.33 -    // TODO: implement
    1.34 -    return TRUE;
    1.35 +static int bishop_getloc_fixedfile(Board board, Move *move) {
    1.36 +    uint8_t d = abs(move->fromfile - move->tofile);
    1.37 +    if (board[move->torow - d][move->fromfile] == move->piece) {
    1.38 +        move->fromrow = move->torow - d;
    1.39 +    }
    1.40 +    if (board[move->torow + d][move->fromfile] == move->piece) {
    1.41 +        if (move->fromrow == POS_UNSPECIFIED) {
    1.42 +            move->fromrow = move->torow + d;
    1.43 +        } else {
    1.44 +            return AMBIGUOUS_MOVE; /* rare situation after promotion */
    1.45 +        }
    1.46 +    }
    1.47 +    return move->fromrow == POS_UNSPECIFIED ?
    1.48 +        INVALID_POSITION : VALID_MOVE_SYNTAX;
    1.49 +}
    1.50 +
    1.51 +static int bishop_getloc_fixedrow(Board board, Move *move) {
    1.52 +    uint8_t d = abs(move->fromrow - move->torow);
    1.53 +    if (board[move->fromrow][move->tofile - d] == move->piece) {
    1.54 +        move->fromfile = move->tofile - d;
    1.55 +    }
    1.56 +    if (board[move->fromrow][move->tofile + d] == move->piece) {
    1.57 +        if (move->fromfile == POS_UNSPECIFIED) {
    1.58 +            move->fromfile = move->tofile + d;
    1.59 +        } else {
    1.60 +            return AMBIGUOUS_MOVE; /* rare situation after promotion */
    1.61 +        }
    1.62 +    }
    1.63 +    return move->fromfile == POS_UNSPECIFIED ?
    1.64 +        INVALID_POSITION : VALID_MOVE_SYNTAX;
    1.65  }
    1.66  
    1.67  int bishop_getlocation(Board board, Move *move) {
    1.68 -    // TODO: implement
    1.69 -    return INVALID_MOVE_SYNTAX;
    1.70 +    
    1.71 +    if (move->fromfile != POS_UNSPECIFIED) {
    1.72 +        return bishop_getloc_fixedfile(board, move);
    1.73 +    }
    1.74 +    
    1.75 +    if (move->fromrow != POS_UNSPECIFIED) {
    1.76 +        return bishop_getloc_fixedrow(board, move);
    1.77 +    }
    1.78 +    
    1.79 +    _Bool amb = FALSE;
    1.80 +    for (int d = -7 ; d < 8  ; d++) {
    1.81 +        uint8_t row = move->torow + d;
    1.82 +        if (isidx(row)) {
    1.83 +            uint8_t file = move->tofile + d;
    1.84 +            if (isidx(file) && board[row][file] == move->piece) {
    1.85 +                if (amb) {
    1.86 +                    return AMBIGUOUS_MOVE;
    1.87 +                }
    1.88 +                amb = TRUE;
    1.89 +                move->fromrow = row;
    1.90 +                move->fromfile = file;
    1.91 +            }
    1.92 +            file = move->tofile - d;
    1.93 +            if (isfile(file) && board[row][file] == move->piece) {
    1.94 +                if (amb) {
    1.95 +                    return AMBIGUOUS_MOVE;
    1.96 +                }
    1.97 +                amb = TRUE;
    1.98 +                move->fromrow = row;
    1.99 +                move->fromfile = file;
   1.100 +            }
   1.101 +        }
   1.102 +    }
   1.103 +    
   1.104 +    if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) {
   1.105 +        return INVALID_POSITION;
   1.106 +    } else {
   1.107 +        return VALID_MOVE_SYNTAX;
   1.108 +    }
   1.109  }

mercurial