src/main.c

changeset 179
5ef724e21702
parent 176
ef8c39362342
--- a/src/main.c	Fri Aug 21 16:08:50 2026 +0200
+++ b/src/main.c	Fri Aug 21 17:04:43 2026 +0200
@@ -479,12 +479,10 @@
                     result = validate_move(gamestate, &move);
                     if (result == VALID_MOVE_SEMANTICS) {
                         apply_move(gamestate, &move);
-                        if (gamestate->checkmate) {
+                        if (is_game_running(gamestate)) {
+                            return 0;
+                        } else {
                             return 1;
-                        } else if (gamestate->stalemate) {
-                            return 1;
-                        } else {
-                            return 0;
                         }
                     } else {
                         eval_move_failed_msg(result);
@@ -627,12 +625,13 @@
                     } else if (code == NETCODE_ACCEPT
                             || code == NETCODE_CHECK
                             || code == NETCODE_CHECKMATE
-                            || code == NETCODE_STALEMATE) {
+                            || code == NETCODE_STALEMATE
+                            || code == NETCODE_NOMATERIAL) {
                         apply_move(gamestate, &move);
-                        if (gamestate->checkmate || gamestate->stalemate) {
+                        if (is_game_running(gamestate)) {
+                            return 0;
+                        } else {
                             return 1;
-                        } else {
-                            return 0;
                         }
                     } else if (code == NETCODE_CONNLOST) {
                         printw("Your opponent left the game.");
@@ -772,6 +771,9 @@
                 } else if (gamestate->stalemate) {
                     net_send_code(opponent, NETCODE_STALEMATE);
                     return 1;
+                } else if (gamestate->nomaterial) {
+                    net_send_code(opponent, NETCODE_NOMATERIAL);
+                    return 1;
                 } else if (move.check) {
                     net_send_code(opponent, NETCODE_CHECK);
                 } else {
@@ -784,17 +786,17 @@
                  * claim the draw. But since we do it automatically, it makes
                  * no practical difference.
                  */
-                if (check_threefold_repetition(gamestate)) {
+                if (gamestate->threefold) {
                     /* send this as a new package (basically as next move) */
                     net_send_code(opponent, NETCODE_THREEFOLD);
                     /* the protocol supports declining the claim  */
                     uint8_t resp = net_recieve_code(opponent);
                     if (resp == NETCODE_ACCEPT) {
-                        gamestate->threefold = true;
                         return 1;
                     } else {
                         /* does not happen in our implementation */
-                        // TODO: somehow add a message to the UI
+                        // TODO: but what do we do if some other client declines? simply rage quit?
+                        //       should we instead rework the protocol that it's not possible to decline?
                         return 0;
                     }
                 } else {
@@ -848,6 +850,8 @@
                 addstr("The game ended in a stalemate.\n");
             } else if (gamestate->threefold) {
                 addstr("The game was drawn after threefold repetition.\n");
+            } else if (gamestate->nomaterial) {
+                addstr("The game is drawn due to insufficient material.\n");
             } else if (gamestate->checkmate) {
                 printw("%s was checkmated.\n",
                     gamestate->movecount % 2 == 0 ? "White" : "Black");

mercurial