diff -r 7ffd66591afe -r a298c6637c30 src/game.c --- a/src/game.c Fri Mar 28 11:45:01 2014 +0100 +++ b/src/game.c Fri Mar 28 14:32:52 2014 +0100 @@ -147,19 +147,19 @@ result = result && !pawn_isblocked(board, move); break; case ROOK: - result = result && rook_chkrules(board, move); + result = result && rook_chkrules(move); result = result && !rook_isblocked(board, move); break; case KNIGHT: - result = result && knight_chkrules(board, move); + result = result && knight_chkrules(move); result = result && !knight_isblocked(board, move); break; case BISHOP: - result = result && bishop_chkrules(board, move); + result = result && bishop_chkrules(move); result = result && !bishop_isblocked(board, move); break; case QUEEN: - result = result && queen_chkrules(board, move); + result = result && queen_chkrules(move); result = result && !queen_isblocked(board, move); break; case KING: @@ -203,10 +203,9 @@ * * @param board the current state of the board * @param move the move date to operate on - * @return TRUE if the location could be retrieved, FALSE if the location is - * ambiguous + * @return status code (see rules/rules.h for the codes) */ -static _Bool getlocation(Board board, Move *move) { +static int getlocation(Board board, Move *move) { uint8_t piece = move->piece & PIECE_MASK; switch (piece) { case PAWN: return pawn_getlocation(board, move); @@ -215,7 +214,7 @@ case BISHOP: return bishop_getlocation(board, move); case QUEEN: return queen_getlocation(board, move); case KING: return king_getlocation(board, move); - default: return FALSE; + default: return INVALID_MOVE_SYNTAX; } } @@ -227,9 +226,9 @@ * @param mycolor the color of the current player * @param mstr the input string to parse * @param move a pointer to object where the move data shall be stored - * @return TRUE, if the move is syntactically valid, FALSE otherwise + * @return status code (see rules/rules.h for the list of codes) */ -static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { +static int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { memset(move, 0, sizeof(Move)); move->fromfile = POS_UNSPECIFIED; move->fromrow = POS_UNSPECIFIED; @@ -331,14 +330,13 @@ move->piece |= mycolor; if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) { - return getlocation(board, move) && chkidx(move); + return getlocation(board, move); } else { - return chkidx(move); + return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; } } else { - return FALSE; + return INVALID_MOVE_SYNTAX; } - // TODO: return status code to indicate the error type } static int sendmove(Board board, uint8_t mycolor, int opponent) { @@ -366,6 +364,7 @@ if (strncmp(movestr, "surr", buflen) == 0) { printw("You surrendered!"); + clrtoeol(); refresh(); net_send_code(opponent, NETCODE_SURRENDER); return 1; @@ -385,28 +384,40 @@ } } else { Move move; - if (eval_move(board, mycolor, movestr, &move)) { - net_send_code(opponent, NETCODE_MOVE); - net_send_data(opponent, &move, sizeof(Move)); - code = net_recieve_code(opponent); - move.check = code == NETCODE_CHECK; - move.checkmate = code == NETCODE_CHECKMATE; - // TODO: record move - if (code == NETCODE_DECLINE) { - printw("Invalid move."); - clrtoeol(); - } else { - apply_move(board, &move); - if (move.checkmate) { - printw("Checkmate!"); - return 1; + int eval_result = eval_move(board, mycolor, movestr, &move); + switch (eval_result) { + case VALID_MOVE_SYNTAX: + net_send_code(opponent, NETCODE_MOVE); + net_send_data(opponent, &move, sizeof(Move)); + code = net_recieve_code(opponent); + move.check = code == NETCODE_CHECK; + move.checkmate = code == NETCODE_CHECKMATE; + // TODO: record move + if (code == NETCODE_DECLINE) { + printw("Invalid move."); } else { - return 0; + apply_move(board, &move); + if (move.checkmate) { + printw("Checkmate!"); + clrtoeol(); + return 1; + } else { + return 0; + } } - } - } else { - printw("Can't interpret move - please use algebraic notation."); + break; + case AMBIGUOUS_MOVE: + printw("Ambiguous move - " + "please specify the piece to move."); + break; + case INVALID_POSITION: + printw("Cannot find the piece that shall be moved."); + break; + default: + printw("Can't interpret move - " + "please use algebraic notation."); } + clrtoeol(); } } }