src/chess/rules.c

changeset 25
3ab0c2e1a4e2
parent 23
824c9522ce66
child 27
efeb98bc69c9
     1.1 --- a/src/chess/rules.c	Tue Apr 01 10:28:08 2014 +0200
     1.2 +++ b/src/chess/rules.c	Tue Apr 01 12:30:25 2014 +0200
     1.3 @@ -90,6 +90,30 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +_Bool is_covered(GameState *gamestate,uint8_t row,uint8_t file,uint8_t color) {
     1.8 +    Move threats[16];
     1.9 +    int threatcount = 0;
    1.10 +    for (uint8_t r = 0 ; r < 8 ; r++) {
    1.11 +        for (uint8_t f = 0 ; f < 8 ; f++) {
    1.12 +            if ((gamestate->board[r][f] & COLOR_MASK) == color) {
    1.13 +                threats[threatcount].piece = gamestate->board[r][f];
    1.14 +                threats[threatcount].fromrow = r;
    1.15 +                threats[threatcount].fromfile = f;
    1.16 +                threats[threatcount].torow = row;
    1.17 +                threats[threatcount].tofile = file;
    1.18 +                threatcount++;
    1.19 +            }
    1.20 +        }
    1.21 +    }
    1.22 +    
    1.23 +    for (int i = 0 ; i < threatcount ; i++) {
    1.24 +        if (validate_move(gamestate, &(threats[i]))) {
    1.25 +            return 1;
    1.26 +        }
    1.27 +    }
    1.28 +    
    1.29 +    return 0;
    1.30 +}
    1.31  
    1.32  void apply_move(GameState *gamestate, Move *move) {
    1.33      uint8_t piece = move->piece & PIECE_MASK;
    1.34 @@ -190,10 +214,15 @@
    1.35          result = 0;
    1.36      }
    1.37      
    1.38 +    /* cancel processing to avoid recursion overflow with is_covered() */
    1.39 +    if (!result) {
    1.40 +        return 0;
    1.41 +    }
    1.42 +    
    1.43      /* is piece pinned */
    1.44      // TODO: make it so
    1.45      
    1.46 -    /* correct check and checkmate flags */
    1.47 +    /* correct check and checkmate flags (move is still valid) */
    1.48      // TODO: make it so
    1.49      
    1.50      return result;

mercurial