src/game.c

changeset 16
a298c6637c30
parent 15
7ffd66591afe
child 17
2aed5418e142
     1.1 --- a/src/game.c	Fri Mar 28 11:45:01 2014 +0100
     1.2 +++ b/src/game.c	Fri Mar 28 14:32:52 2014 +0100
     1.3 @@ -147,19 +147,19 @@
     1.4          result = result && !pawn_isblocked(board, move);
     1.5          break;
     1.6      case ROOK:
     1.7 -        result = result && rook_chkrules(board, move);
     1.8 +        result = result && rook_chkrules(move);
     1.9          result = result && !rook_isblocked(board, move);
    1.10          break;
    1.11      case KNIGHT:
    1.12 -        result = result && knight_chkrules(board, move);
    1.13 +        result = result && knight_chkrules(move);
    1.14          result = result && !knight_isblocked(board, move);
    1.15          break;
    1.16      case BISHOP:
    1.17 -        result = result && bishop_chkrules(board, move);
    1.18 +        result = result && bishop_chkrules(move);
    1.19          result = result && !bishop_isblocked(board, move);
    1.20          break;
    1.21      case QUEEN:
    1.22 -        result = result && queen_chkrules(board, move);
    1.23 +        result = result && queen_chkrules(move);
    1.24          result = result && !queen_isblocked(board, move);
    1.25          break;
    1.26      case KING:
    1.27 @@ -203,10 +203,9 @@
    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 + * @return status code (see rules/rules.h for the codes)
    1.34   */
    1.35 -static _Bool getlocation(Board board, Move *move) {   
    1.36 +static int getlocation(Board board, Move *move) {   
    1.37      uint8_t piece = move->piece & PIECE_MASK;
    1.38      switch (piece) {
    1.39          case PAWN: return pawn_getlocation(board, move);
    1.40 @@ -215,7 +214,7 @@
    1.41          case BISHOP: return bishop_getlocation(board, move);
    1.42          case QUEEN: return queen_getlocation(board, move);
    1.43          case KING: return king_getlocation(board, move);
    1.44 -        default: return FALSE;
    1.45 +        default: return INVALID_MOVE_SYNTAX;
    1.46      }
    1.47  }
    1.48  
    1.49 @@ -227,9 +226,9 @@
    1.50   * @param mycolor the color of the current player
    1.51   * @param mstr the input string to parse
    1.52   * @param move a pointer to object where the move data shall be stored
    1.53 - * @return TRUE, if the move is syntactically valid, FALSE otherwise
    1.54 + * @return status code (see rules/rules.h for the list of codes)
    1.55   */
    1.56 -static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
    1.57 +static int 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 @@ -331,14 +330,13 @@
    1.62          move->piece |= mycolor;
    1.63          if (move->fromfile == POS_UNSPECIFIED
    1.64              || move->fromrow == POS_UNSPECIFIED) {
    1.65 -            return getlocation(board, move) && chkidx(move);
    1.66 +            return getlocation(board, move);
    1.67          } else {
    1.68 -            return chkidx(move);
    1.69 +            return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
    1.70          }
    1.71      } else {
    1.72 -        return FALSE;
    1.73 +        return INVALID_MOVE_SYNTAX;
    1.74      }
    1.75 -    // TODO: return status code to indicate the error type
    1.76  }
    1.77  
    1.78  static int sendmove(Board board, uint8_t mycolor, int opponent) {
    1.79 @@ -366,6 +364,7 @@
    1.80  
    1.81          if (strncmp(movestr, "surr", buflen) == 0) {
    1.82              printw("You surrendered!");
    1.83 +            clrtoeol();
    1.84              refresh();
    1.85              net_send_code(opponent, NETCODE_SURRENDER);
    1.86              return 1;
    1.87 @@ -385,28 +384,40 @@
    1.88              }
    1.89          } else {
    1.90              Move move;
    1.91 -            if (eval_move(board, mycolor, movestr, &move)) {
    1.92 -                net_send_code(opponent, NETCODE_MOVE);
    1.93 -                net_send_data(opponent, &move, sizeof(Move));
    1.94 -                code = net_recieve_code(opponent);
    1.95 -                move.check = code == NETCODE_CHECK;
    1.96 -                move.checkmate = code == NETCODE_CHECKMATE;
    1.97 -                // TODO: record move
    1.98 -                if (code == NETCODE_DECLINE) {
    1.99 -                    printw("Invalid move.");
   1.100 -                    clrtoeol();
   1.101 -                } else {
   1.102 -                    apply_move(board, &move);
   1.103 -                    if (move.checkmate) {
   1.104 -                        printw("Checkmate!");
   1.105 -                        return 1;
   1.106 +            int eval_result = eval_move(board, mycolor, movestr, &move);
   1.107 +            switch (eval_result) {
   1.108 +                case VALID_MOVE_SYNTAX:
   1.109 +                    net_send_code(opponent, NETCODE_MOVE);
   1.110 +                    net_send_data(opponent, &move, sizeof(Move));
   1.111 +                    code = net_recieve_code(opponent);
   1.112 +                    move.check = code == NETCODE_CHECK;
   1.113 +                    move.checkmate = code == NETCODE_CHECKMATE;
   1.114 +                    // TODO: record move
   1.115 +                    if (code == NETCODE_DECLINE) {
   1.116 +                        printw("Invalid move.");
   1.117                      } else {
   1.118 -                        return 0;
   1.119 +                        apply_move(board, &move);
   1.120 +                        if (move.checkmate) {
   1.121 +                            printw("Checkmate!");
   1.122 +                            clrtoeol();
   1.123 +                            return 1;
   1.124 +                        } else {
   1.125 +                            return 0;
   1.126 +                        }
   1.127                      }
   1.128 -                }
   1.129 -            } else {
   1.130 -                printw("Can't interpret move - please use algebraic notation.");
   1.131 +                    break;
   1.132 +                case AMBIGUOUS_MOVE:
   1.133 +                    printw("Ambiguous move - "
   1.134 +                        "please specify the piece to move.");
   1.135 +                    break;
   1.136 +                case INVALID_POSITION:
   1.137 +                    printw("Cannot find the piece that shall be moved.");
   1.138 +                    break;
   1.139 +                default:
   1.140 +                    printw("Can't interpret move - "
   1.141 +                        "please use algebraic notation.");
   1.142              }
   1.143 +            clrtoeol();
   1.144          }
   1.145      }
   1.146  }

mercurial