src/main.c

changeset 131
c33567d61ba7
parent 130
3fc6b1d6cbe9
equal deleted inserted replaced
130:3fc6b1d6cbe9 131:c33567d61ba7
729 } else { 729 } else {
730 net_send_code(opponent, NETCODE_DECLINE); 730 net_send_code(opponent, NETCODE_DECLINE);
731 } 731 }
732 } 732 }
733 break; 733 break;
734 case NETCODE_THREEFOLD:
735 /* validate the claim */
736 if (check_threefold_repetition(gamestate)) {
737 /* auto-accept the claim */
738 gamestate->threefold = true;
739 net_send_code(opponent, NETCODE_ACCEPT);
740 return 1;
741 } else {
742 /* silently decline, do not add message to UI */
743 net_send_code(opponent, NETCODE_DECLINE);
744 }
745 break;
734 case NETCODE_MOVE: { 746 case NETCODE_MOVE: {
735 Move move; 747 Move move;
736 net_recieve_data(opponent, &move, sizeof(Move)); 748 net_recieve_data(opponent, &move, sizeof(Move));
737 code = validate_move(gamestate, &move); 749 code = validate_move(gamestate, &move);
738 if (code == VALID_MOVE_SEMANTICS) { 750 if (code == VALID_MOVE_SEMANTICS) {
746 } else if (move.check) { 758 } else if (move.check) {
747 net_send_code(opponent, NETCODE_CHECK); 759 net_send_code(opponent, NETCODE_CHECK);
748 } else { 760 } else {
749 net_send_code(opponent, NETCODE_ACCEPT); 761 net_send_code(opponent, NETCODE_ACCEPT);
750 } 762 }
751 return 0; 763 /* check for threefold repetition */
764 /* Note: in our implementation it is an auto-draw claimed
765 * by the player confronted with the position. By standard
766 * chess rules, also the player who creates the position may
767 * claim the draw. But since we do it automatically, it makes
768 * no practical difference.
769 */
770 if (check_threefold_repetition(gamestate)) {
771 /* send this as a new package (basically as next move) */
772 net_send_code(opponent, NETCODE_THREEFOLD);
773 /* the protocol supports declining the claim */
774 uint8_t resp = net_recieve_code(opponent);
775 if (resp == NETCODE_ACCEPT) {
776 gamestate->threefold = true;
777 return 1;
778 } else {
779 /* does not happen in our implementation */
780 // TODO: somehow add a message to the UI
781 return 0;
782 }
783 } else {
784 return 0;
785 }
752 } else { 786 } else {
753 uint32_t reason = htonl(code); 787 uint32_t reason = htonl(code);
754 net_send_data(opponent, NETCODE_DECLINE, 788 net_send_data(opponent, NETCODE_DECLINE,
755 &reason, sizeof(uint32_t)); 789 &reason, sizeof(uint32_t));
756 } 790 }

mercurial