src/chess/rules.c

changeset 156
05fc8a454700
parent 155
835776e0338f
equal deleted inserted replaced
155:835776e0338f 156:05fc8a454700
678 bool incheck = gamestate->movecount > 0 ? last_move(gamestate).check:false; 678 bool incheck = gamestate->movecount > 0 ? last_move(gamestate).check:false;
679 679
680 Move threats[16], *threat = NULL; 680 Move threats[16], *threat = NULL;
681 uint8_t threatcount; 681 uint8_t threatcount;
682 682
683 /* determine all threats and sort out the unreal threats on our own */
683 if (get_threats(gamestate, move->torow, move->tofile, color, 684 if (get_threats(gamestate, move->torow, move->tofile, color,
684 threats, &threatcount)) { 685 threats, &threatcount)) {
685 686
686 int reason = PIECE_NOT_FOUND; 687 bool found = false;
687 688
688 /* find threats for the specified position */ 689 /* find threats for the specified position */
689 for (uint8_t i = 0 ; i < threatcount ; i++) { 690 for (uint8_t i = 0 ; i < threatcount ; i++) {
690 if (threats[i].piece == move->piece && 691 if (threats[i].piece == move->piece &&
691 (move->fromrow == POS_UNSPECIFIED || 692 (move->fromrow == POS_UNSPECIFIED ||
692 move->fromrow == threats[i].fromrow) && 693 move->fromrow == threats[i].fromrow) &&
693 (move->fromfile == POS_UNSPECIFIED || 694 (move->fromfile == POS_UNSPECIFIED ||
694 move->fromfile == threats[i].fromfile)) { 695 move->fromfile == threats[i].fromfile)) {
695 696
696 if (threat) { 697 /* found a threat, here it does not matter if it's valid! */
697 return AMBIGUOUS_MOVE; 698 found = true;
698 } else { 699
699 /* found threat is no real threat */ 700 /* discard pinned pieces */
700 // TODO: why didn't we call get_real_threats() ? 701 if (!is_pinned(gamestate, &(threats[i]))) {
701 if (is_pinned(gamestate, &(threats[i]))) { 702 if (threat) {
702 reason = incheck?KING_IN_CHECK: 703 /* we've already found a valid threat */
703 (piece==KING?KING_MOVES_INTO_CHECK:PIECE_PINNED); 704 return AMBIGUOUS_MOVE;
704 } else { 705 } else {
705 threat = &(threats[i]); 706 threat = &(threats[i]);
706 } 707 }
707 } 708 }
708 } 709 }
709 } 710 }
710 711
711 /* can't threaten specified position */ 712 /* can't threaten specified position */
712 if (!threat) { 713 if (!threat) {
713 return reason; 714 if (found) {
715 if (piece == KING) {
716 return KING_MOVES_INTO_CHECK;
717 } else if (incheck) {
718 return KING_IN_CHECK;
719 } else {
720 return PIECE_PINNED;
721 }
722 } else {
723 return PIECE_NOT_FOUND;
724 }
714 } 725 }
715 726
716 /* found a threat, copy the source location */ 727 /* found a threat, copy the source location */
717 move->fromrow = threat->fromrow; 728 move->fromrow = threat->fromrow;
718 move->fromfile = threat->fromfile; 729 move->fromfile = threat->fromfile;

mercurial