src/game.c

changeset 12
84880c7e1ea6
parent 11
08d7a6e3ec31
child 13
faec61c4901f
     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  }

mercurial