interface for retrieving locations when using short algebraic notation

Wed, 26 Mar 2014 13:16:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Mar 2014 13:16:49 +0100
changeset 12
84880c7e1ea6
parent 11
08d7a6e3ec31
child 13
faec61c4901f

interface for retrieving locations when using short algebraic notation
inverted return values of not yet implemented functions to prevent bugs

src/game.c file | annotate | diff | comparison | revisions
src/game.h file | annotate | diff | comparison | revisions
src/rules/bishop.c file | annotate | diff | comparison | revisions
src/rules/king.c file | annotate | diff | comparison | revisions
src/rules/knight.c file | annotate | diff | comparison | revisions
src/rules/pawn.c file | annotate | diff | comparison | revisions
src/rules/queen.c file | annotate | diff | comparison | revisions
src/rules/rook.c file | annotate | diff | comparison | revisions
     1.1 --- a/src/game.c	Sat Mar 22 18:56:52 2014 +0100
     1.2 +++ b/src/game.c	Wed Mar 26 13:16:49 2014 +0100
     1.3 @@ -135,8 +135,60 @@
     1.4      return result;
     1.5  }
     1.6  
     1.7 +/**
     1.8 + * Maps a character to a piece.
     1.9 + * 
    1.10 + * Does not work for pawns, since they don't have a character.
    1.11 + * 
    1.12 + * @param c one of R,N,B,Q,K
    1.13 + * @return numeric value for the specified piece
    1.14 + */
    1.15 +static uint8_t getpiece(char c) {
    1.16 +    switch (c) {
    1.17 +        case 'R': return ROOK;
    1.18 +        case 'N': return KNIGHT;
    1.19 +        case 'B': return BISHOP;
    1.20 +        case 'Q': return QUEEN;
    1.21 +        case 'K': return KING;
    1.22 +        default: return 0;
    1.23 +    }
    1.24 +}
    1.25 +
    1.26 +/**
    1.27 + * Guesses the location of a piece for short algebraic notation.
    1.28 + * 
    1.29 + * @param board the current state of the board
    1.30 + * @param move the move date to operate on
    1.31 + * @return TRUE if the location could be retrieved, FALSE if the location is
    1.32 + * ambiguous
    1.33 + */
    1.34 +static _Bool getlocation(Board board, Move *move) {
    1.35 +    uint8_t piece = move->piece & PIECE_MASK;
    1.36 +    switch (piece) {
    1.37 +        case PAWN: return pawn_getlocation(board, move);
    1.38 +        case ROOK: return rook_getlocation(board, move);
    1.39 +        case KNIGHT: return knight_getlocation(board, move);
    1.40 +        case BISHOP: return bishop_getlocation(board, move);
    1.41 +        case QUEEN: return queen_getlocation(board, move);
    1.42 +        case KING: return king_getlocation(board, move);
    1.43 +        default: return FALSE;
    1.44 +    }
    1.45 +}
    1.46 +
    1.47 +/**
    1.48 + * Evaluates a move syntactically and stores the move data in the specified
    1.49 + * object.
    1.50 + * 
    1.51 + * @param board the current state of the board
    1.52 + * @param mycolor the color of the current player
    1.53 + * @param mstr the input string to parse
    1.54 + * @param move a pointer to object where the move data shall be stored
    1.55 + * @return TRUE, if the move is syntactically valid, FALSE otherwise
    1.56 + */
    1.57  static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
    1.58      memset(move, 0, sizeof(Move));
    1.59 +    move->fromfile = POS_UNSPECIFIED;
    1.60 +    move->fromrow = POS_UNSPECIFIED;
    1.61      
    1.62      size_t len = strlen(mstr);
    1.63      
    1.64 @@ -152,22 +204,22 @@
    1.65      if (len == 2) {
    1.66          /* pawn move (e.g. "e4") */
    1.67          if (isfile(mstr[0]) && isrow(mstr[1])) {
    1.68 -            move->piece = PAWN|mycolor;
    1.69 +            move->piece = PAWN;
    1.70              move->tofile = fileidx(mstr[0]);
    1.71              move->torow = rowidx(mstr[1]);
    1.72 -            if (!pawn_getlocation(board, move)) {
    1.73 -                move->piece = 0;
    1.74 -            }
    1.75          }
    1.76      } else if (len == 3) {
    1.77          if (strcmp(mstr, "O-O") == 0) {
    1.78              /* king side castling */
    1.79 -            move->piece = KING|mycolor;
    1.80 +            move->piece = KING;
    1.81              move->fromfile = fileidx('e');
    1.82              move->tofile = fileidx('g');
    1.83              move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
    1.84          } else {
    1.85              /* unambiguous move (e.g. "Nf3") */
    1.86 +            move->piece = getpiece(mstr[0]);
    1.87 +            move->tofile = isfile(mstr[1]) ? fileidx(mstr[1]) : POS_UNSPECIFIED;
    1.88 +            move->torow = isrow(mstr[2]) ? fileidx(mstr[2]) : POS_UNSPECIFIED;
    1.89          }
    1.90          
    1.91      } else if (len == 4) {
    1.92 @@ -178,7 +230,7 @@
    1.93      } else if (len == 5) {
    1.94          if (strcmp(mstr, "O-O-O") == 0) {
    1.95              /* queen side castling "O-O-O" */
    1.96 -            move->piece = KING|mycolor;
    1.97 +            move->piece = KING;
    1.98              move->fromfile = fileidx('e');
    1.99              move->tofile = fileidx('c');
   1.100              move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
   1.101 @@ -192,6 +244,15 @@
   1.102      } else if (len == 6) {
   1.103          /* long notation capture (e.g. "Nc5xf3") */
   1.104      }
   1.105 +
   1.106 +    if (move->piece) {
   1.107 +        move->piece |= mycolor;
   1.108 +    }
   1.109 +    
   1.110 +    if (!getlocation(board, move)) {
   1.111 +        // TODO: return status code to indicate the error type
   1.112 +        move->piece = 0;
   1.113 +    }
   1.114      
   1.115      return move->piece != 0;
   1.116  }
     2.1 --- a/src/game.h	Sat Mar 22 18:56:52 2014 +0100
     2.2 +++ b/src/game.h	Wed Mar 26 13:16:49 2014 +0100
     2.3 @@ -76,6 +76,8 @@
     2.4      _Bool capture;
     2.5  } Move;
     2.6  
     2.7 +#define POS_UNSPECIFIED UINT8_MAX
     2.8 +
     2.9  #define isfile(file) (file >= 'a' && file <= 'h')
    2.10  #define isrow(row) (row >= '1' && row <= '8')
    2.11  #define rowidx(row) (row-'1')
     3.1 --- a/src/rules/bishop.c	Sat Mar 22 18:56:52 2014 +0100
     3.2 +++ b/src/rules/bishop.c	Wed Mar 26 13:16:49 2014 +0100
     3.3 @@ -31,15 +31,15 @@
     3.4  
     3.5  _Bool bishop_chkrules(Board board, Move* move) {
     3.6      // TODO: implement
     3.7 -    return TRUE;
     3.8 +    return FALSE;
     3.9  }
    3.10  
    3.11  _Bool bishop_isblocked(Board board, Move *move) {
    3.12      // TODO: implement
    3.13 -    return FALSE;
    3.14 +    return TRUE;
    3.15  }
    3.16  
    3.17  _Bool bishop_getlocation(Board board, Move *move) {
    3.18      // TODO: implement
    3.19 -    return TRUE;
    3.20 +    return FALSE;
    3.21  }
     4.1 --- a/src/rules/king.c	Sat Mar 22 18:56:52 2014 +0100
     4.2 +++ b/src/rules/king.c	Wed Mar 26 13:16:49 2014 +0100
     4.3 @@ -31,15 +31,15 @@
     4.4  
     4.5  _Bool king_chkrules(Board board, Move* move) {
     4.6      // TODO: implement
     4.7 -    return TRUE;
     4.8 +    return FALSE;
     4.9  }
    4.10  
    4.11  _Bool king_isblocked(Board board, Move *move) {
    4.12      // TODO: implement
    4.13 -    return FALSE;
    4.14 +    return TRUE;
    4.15  }
    4.16  
    4.17  _Bool king_getlocation(Board board, Move *move) {
    4.18      // TODO: implement
    4.19 -    return TRUE;
    4.20 +    return FALSE;
    4.21  }
     5.1 --- a/src/rules/knight.c	Sat Mar 22 18:56:52 2014 +0100
     5.2 +++ b/src/rules/knight.c	Wed Mar 26 13:16:49 2014 +0100
     5.3 @@ -31,15 +31,15 @@
     5.4  
     5.5  _Bool knight_chkrules(Board board, Move *move) {
     5.6      // TODO: implement
     5.7 -    return TRUE;
     5.8 +    return FALSE;
     5.9  }
    5.10  
    5.11  _Bool knight_isblocked(Board board, Move *move) {
    5.12      // TODO: implement
    5.13 -    return FALSE;
    5.14 +    return TRUE;
    5.15  }
    5.16  
    5.17  _Bool knight_getlocation(Board board, Move *move) {
    5.18      // TODO: implement
    5.19 -    return TRUE;
    5.20 +    return FALSE;
    5.21  }
     6.1 --- a/src/rules/pawn.c	Sat Mar 22 18:56:52 2014 +0100
     6.2 +++ b/src/rules/pawn.c	Wed Mar 26 13:16:49 2014 +0100
     6.3 @@ -42,6 +42,8 @@
     6.4  _Bool pawn_getlocation(Board board, Move *move) {
     6.5      int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1);
     6.6      
     6.7 +    // TODO: battle moves
     6.8 +    
     6.9      move->fromfile = move->tofile;
    6.10      move->fromrow = move->torow + d;
    6.11      if (move->fromrow > 6) {
     7.1 --- a/src/rules/queen.c	Sat Mar 22 18:56:52 2014 +0100
     7.2 +++ b/src/rules/queen.c	Wed Mar 26 13:16:49 2014 +0100
     7.3 @@ -31,15 +31,15 @@
     7.4  
     7.5  _Bool queen_chkrules(Board board, Move* move) {
     7.6      // TODO: implement
     7.7 -    return TRUE;
     7.8 +    return FALSE;
     7.9  }
    7.10  
    7.11  _Bool queen_isblocked(Board board, Move *move) {
    7.12      // TODO: implement
    7.13 -    return FALSE;
    7.14 +    return TRUE;
    7.15  }
    7.16  
    7.17  _Bool queen_getlocation(Board board, Move *move) {
    7.18      // TODO: implement
    7.19 -    return TRUE;
    7.20 +    return FALSE;
    7.21  }
     8.1 --- a/src/rules/rook.c	Sat Mar 22 18:56:52 2014 +0100
     8.2 +++ b/src/rules/rook.c	Wed Mar 26 13:16:49 2014 +0100
     8.3 @@ -31,15 +31,15 @@
     8.4  
     8.5  _Bool rook_chkrules(Board board, Move *move) {
     8.6      // TODO: implement
     8.7 -    return TRUE;
     8.8 +    return FALSE;
     8.9  }
    8.10  
    8.11  _Bool rook_isblocked(Board board, Move *move) {
    8.12      // TODO: implement
    8.13 -    return FALSE;
    8.14 +    return TRUE;
    8.15  }
    8.16  
    8.17  _Bool rook_getlocation(Board board, Move *move) {
    8.18      // TODO: implement
    8.19 -    return TRUE;
    8.20 +    return FALSE;
    8.21  }

mercurial