src/main.c

changeset 179
5ef724e21702
parent 176
ef8c39362342
equal deleted inserted replaced
178:724e8acff6f8 179:5ef724e21702
477 movestr, curcolor, &move); 477 movestr, curcolor, &move);
478 if (result == VALID_MOVE_SYNTAX) { 478 if (result == VALID_MOVE_SYNTAX) {
479 result = validate_move(gamestate, &move); 479 result = validate_move(gamestate, &move);
480 if (result == VALID_MOVE_SEMANTICS) { 480 if (result == VALID_MOVE_SEMANTICS) {
481 apply_move(gamestate, &move); 481 apply_move(gamestate, &move);
482 if (gamestate->checkmate) { 482 if (is_game_running(gamestate)) {
483 return 0;
484 } else {
483 return 1; 485 return 1;
484 } else if (gamestate->stalemate) {
485 return 1;
486 } else {
487 return 0;
488 } 486 }
489 } else { 487 } else {
490 eval_move_failed_msg(result); 488 eval_move_failed_msg(result);
491 } 489 }
492 } else { 490 } else {
625 reason = ntohl(reason); 623 reason = ntohl(reason);
626 eval_move_failed_msg(reason); 624 eval_move_failed_msg(reason);
627 } else if (code == NETCODE_ACCEPT 625 } else if (code == NETCODE_ACCEPT
628 || code == NETCODE_CHECK 626 || code == NETCODE_CHECK
629 || code == NETCODE_CHECKMATE 627 || code == NETCODE_CHECKMATE
630 || code == NETCODE_STALEMATE) { 628 || code == NETCODE_STALEMATE
629 || code == NETCODE_NOMATERIAL) {
631 apply_move(gamestate, &move); 630 apply_move(gamestate, &move);
632 if (gamestate->checkmate || gamestate->stalemate) { 631 if (is_game_running(gamestate)) {
632 return 0;
633 } else {
633 return 1; 634 return 1;
634 } else {
635 return 0;
636 } 635 }
637 } else if (code == NETCODE_CONNLOST) { 636 } else if (code == NETCODE_CONNLOST) {
638 printw("Your opponent left the game."); 637 printw("Your opponent left the game.");
639 return 1; 638 return 1;
640 } else { 639 } else {
770 net_send_code(opponent, NETCODE_CHECKMATE); 769 net_send_code(opponent, NETCODE_CHECKMATE);
771 return 1; 770 return 1;
772 } else if (gamestate->stalemate) { 771 } else if (gamestate->stalemate) {
773 net_send_code(opponent, NETCODE_STALEMATE); 772 net_send_code(opponent, NETCODE_STALEMATE);
774 return 1; 773 return 1;
774 } else if (gamestate->nomaterial) {
775 net_send_code(opponent, NETCODE_NOMATERIAL);
776 return 1;
775 } else if (move.check) { 777 } else if (move.check) {
776 net_send_code(opponent, NETCODE_CHECK); 778 net_send_code(opponent, NETCODE_CHECK);
777 } else { 779 } else {
778 net_send_code(opponent, NETCODE_ACCEPT); 780 net_send_code(opponent, NETCODE_ACCEPT);
779 } 781 }
782 * by the player confronted with the position. By standard 784 * by the player confronted with the position. By standard
783 * chess rules, also the player who creates the position may 785 * chess rules, also the player who creates the position may
784 * claim the draw. But since we do it automatically, it makes 786 * claim the draw. But since we do it automatically, it makes
785 * no practical difference. 787 * no practical difference.
786 */ 788 */
787 if (check_threefold_repetition(gamestate)) { 789 if (gamestate->threefold) {
788 /* send this as a new package (basically as next move) */ 790 /* send this as a new package (basically as next move) */
789 net_send_code(opponent, NETCODE_THREEFOLD); 791 net_send_code(opponent, NETCODE_THREEFOLD);
790 /* the protocol supports declining the claim */ 792 /* the protocol supports declining the claim */
791 uint8_t resp = net_recieve_code(opponent); 793 uint8_t resp = net_recieve_code(opponent);
792 if (resp == NETCODE_ACCEPT) { 794 if (resp == NETCODE_ACCEPT) {
793 gamestate->threefold = true;
794 return 1; 795 return 1;
795 } else { 796 } else {
796 /* does not happen in our implementation */ 797 /* does not happen in our implementation */
797 // TODO: somehow add a message to the UI 798 // TODO: but what do we do if some other client declines? simply rage quit?
799 // should we instead rework the protocol that it's not possible to decline?
798 return 0; 800 return 0;
799 } 801 }
800 } else { 802 } else {
801 return 0; 803 return 0;
802 } 804 }
846 addstr("The game ended remis.\n"); 848 addstr("The game ended remis.\n");
847 } else if (gamestate->stalemate) { 849 } else if (gamestate->stalemate) {
848 addstr("The game ended in a stalemate.\n"); 850 addstr("The game ended in a stalemate.\n");
849 } else if (gamestate->threefold) { 851 } else if (gamestate->threefold) {
850 addstr("The game was drawn after threefold repetition.\n"); 852 addstr("The game was drawn after threefold repetition.\n");
853 } else if (gamestate->nomaterial) {
854 addstr("The game is drawn due to insufficient material.\n");
851 } else if (gamestate->checkmate) { 855 } else if (gamestate->checkmate) {
852 printw("%s was checkmated.\n", 856 printw("%s was checkmated.\n",
853 gamestate->movecount % 2 == 0 ? "White" : "Black"); 857 gamestate->movecount % 2 == 0 ? "White" : "Black");
854 } else if (gamestate->ragequit) { 858 } else if (gamestate->ragequit) {
855 printw("Your opponent disconnected.\n"); 859 printw("Your opponent disconnected.\n");

mercurial