src/chess/rules.c

changeset 115
206201d544be
parent 110
e2bb03494d46
equal deleted inserted replaced
114:12df1b7c792f 115:206201d544be
631 } else { 631 } else {
632 return INVALID_POSITION; 632 return INVALID_POSITION;
633 } 633 }
634 } 634 }
635 635
636 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) { 636 static int eval_move1(char *mstr, Move *move, uint8_t color) {
637 memset(move, 0, sizeof(Move)); 637 memset(move, 0, sizeof(Move));
638 move->fromfile = POS_UNSPECIFIED; 638 move->fromfile = POS_UNSPECIFIED;
639 move->fromrow = POS_UNSPECIFIED; 639 move->fromrow = POS_UNSPECIFIED;
640 640
641 size_t len = strlen(mstr); 641 size_t len = strlen(mstr);
743 move->torow = rowidx(mstr[5]); 743 move->torow = rowidx(mstr[5]);
744 } 744 }
745 } 745 }
746 746
747 747
748 if (move->piece) { 748 if (!move->piece) {
749 if (move->piece == PAWN 749 return INVALID_MOVE_SYNTAX;
750 && move->torow == (color==WHITE?7:0) 750 }
751 && !move->promotion) { 751
752 return NEED_PROMOTION; 752 if (move->piece == PAWN
753 } 753 && move->torow == (color==WHITE?7:0)
754 754 && !move->promotion) {
755 move->piece |= color; 755 return NEED_PROMOTION;
756 }
757
758 move->piece |= color;
759
760 if (!chkidx_to(move)) {
761 return INVALID_POSITION;
762 }
763
764 return VALID_MOVE_SYNTAX;
765 }
766
767 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) {
768 int result = eval_move1(mstr, move, color);
769 if (result == VALID_MOVE_SYNTAX) {
756 if (move->fromfile == POS_UNSPECIFIED 770 if (move->fromfile == POS_UNSPECIFIED
757 || move->fromrow == POS_UNSPECIFIED) { 771 || move->fromrow == POS_UNSPECIFIED) {
758 return getlocation(gamestate, move); 772 return getlocation(gamestate, move);
759 } else { 773 } else if (!chkidx_from(move)) {
760 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; 774 return INVALID_POSITION;
761 } 775 }
762 } else { 776 }
763 return INVALID_MOVE_SYNTAX; 777 return result;
764 } 778 }
779
780 int check_move(char *mstr, uint8_t color) {
781 Move move;
782 return eval_move1(mstr, &move, color);
765 } 783 }
766 784
767 bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, 785 bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
768 uint8_t color) { 786 uint8_t color) {
769 787

mercurial