implemented bishop rules

Sat, 29 Mar 2014 14:46:33 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 29 Mar 2014 14:46:33 +0100
changeset 17
2aed5418e142
parent 16
a298c6637c30
child 18
6008840b859e

implemented bishop rules

src/game.c file | annotate | diff | comparison | revisions
src/rules/bishop.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/game.c	Fri Mar 28 14:32:52 2014 +0100
     1.2 +++ b/src/game.c	Sat Mar 29 14:46:33 2014 +0100
     1.3 @@ -232,7 +232,7 @@
     1.4      memset(move, 0, sizeof(Move));
     1.5      move->fromfile = POS_UNSPECIFIED;
     1.6      move->fromrow = POS_UNSPECIFIED;
     1.7 -    
     1.8 +    // TODO: promotion
     1.9      size_t len = strlen(mstr);
    1.10      
    1.11      /* evaluate check/checkmate flags */
     2.1 --- a/src/rules/bishop.c	Fri Mar 28 14:32:52 2014 +0100
     2.2 +++ b/src/rules/bishop.c	Sat Mar 29 14:46:33 2014 +0100
     2.3 @@ -29,18 +29,100 @@
     2.4  
     2.5  #include "bishop.h"
     2.6  #include "rules.h"
     2.7 +#include <math.h>
     2.8  
     2.9  _Bool bishop_chkrules(Move* move) {
    2.10 -    // TODO: implement
    2.11 +    return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile);
    2.12 +}
    2.13 +
    2.14 +_Bool bishop_isblocked(Board board, Move *move) {
    2.15 +    int dy = move->torow > move->fromrow ? 1 : -1;
    2.16 +    int dx = move->tofile > move->fromfile ? 1 : -1;
    2.17 +    
    2.18 +    uint8_t y = move->fromrow;
    2.19 +    uint8_t x = move->fromfile;
    2.20 +    
    2.21 +    do {
    2.22 +        x += dx;
    2.23 +        y += dy;
    2.24 +        if (board[y][x]) {
    2.25 +            return TRUE;
    2.26 +        }
    2.27 +    } while (x != move->tofile && y != move->torow);
    2.28 +    
    2.29      return FALSE;
    2.30  }
    2.31  
    2.32 -_Bool bishop_isblocked(Board board, Move *move) {
    2.33 -    // TODO: implement
    2.34 -    return TRUE;
    2.35 +static int bishop_getloc_fixedfile(Board board, Move *move) {
    2.36 +    uint8_t d = abs(move->fromfile - move->tofile);
    2.37 +    if (board[move->torow - d][move->fromfile] == move->piece) {
    2.38 +        move->fromrow = move->torow - d;
    2.39 +    }
    2.40 +    if (board[move->torow + d][move->fromfile] == move->piece) {
    2.41 +        if (move->fromrow == POS_UNSPECIFIED) {
    2.42 +            move->fromrow = move->torow + d;
    2.43 +        } else {
    2.44 +            return AMBIGUOUS_MOVE; /* rare situation after promotion */
    2.45 +        }
    2.46 +    }
    2.47 +    return move->fromrow == POS_UNSPECIFIED ?
    2.48 +        INVALID_POSITION : VALID_MOVE_SYNTAX;
    2.49 +}
    2.50 +
    2.51 +static int bishop_getloc_fixedrow(Board board, Move *move) {
    2.52 +    uint8_t d = abs(move->fromrow - move->torow);
    2.53 +    if (board[move->fromrow][move->tofile - d] == move->piece) {
    2.54 +        move->fromfile = move->tofile - d;
    2.55 +    }
    2.56 +    if (board[move->fromrow][move->tofile + d] == move->piece) {
    2.57 +        if (move->fromfile == POS_UNSPECIFIED) {
    2.58 +            move->fromfile = move->tofile + d;
    2.59 +        } else {
    2.60 +            return AMBIGUOUS_MOVE; /* rare situation after promotion */
    2.61 +        }
    2.62 +    }
    2.63 +    return move->fromfile == POS_UNSPECIFIED ?
    2.64 +        INVALID_POSITION : VALID_MOVE_SYNTAX;
    2.65  }
    2.66  
    2.67  int bishop_getlocation(Board board, Move *move) {
    2.68 -    // TODO: implement
    2.69 -    return INVALID_MOVE_SYNTAX;
    2.70 +    
    2.71 +    if (move->fromfile != POS_UNSPECIFIED) {
    2.72 +        return bishop_getloc_fixedfile(board, move);
    2.73 +    }
    2.74 +    
    2.75 +    if (move->fromrow != POS_UNSPECIFIED) {
    2.76 +        return bishop_getloc_fixedrow(board, move);
    2.77 +    }
    2.78 +    
    2.79 +    _Bool amb = FALSE;
    2.80 +    for (int d = -7 ; d < 8  ; d++) {
    2.81 +        uint8_t row = move->torow + d;
    2.82 +        if (isidx(row)) {
    2.83 +            uint8_t file = move->tofile + d;
    2.84 +            if (isidx(file) && board[row][file] == move->piece) {
    2.85 +                if (amb) {
    2.86 +                    return AMBIGUOUS_MOVE;
    2.87 +                }
    2.88 +                amb = TRUE;
    2.89 +                move->fromrow = row;
    2.90 +                move->fromfile = file;
    2.91 +            }
    2.92 +            file = move->tofile - d;
    2.93 +            if (isfile(file) && board[row][file] == move->piece) {
    2.94 +                if (amb) {
    2.95 +                    return AMBIGUOUS_MOVE;
    2.96 +                }
    2.97 +                amb = TRUE;
    2.98 +                move->fromrow = row;
    2.99 +                move->fromfile = file;
   2.100 +            }
   2.101 +        }
   2.102 +    }
   2.103 +    
   2.104 +    if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) {
   2.105 +        return INVALID_POSITION;
   2.106 +    } else {
   2.107 +        return VALID_MOVE_SYNTAX;
   2.108 +    }
   2.109  }

mercurial