moved checkmate and stalemate flags to gamestate

Thu, 03 Apr 2014 16:07:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 03 Apr 2014 16:07:04 +0200
changeset 27
efeb98bc69c9
parent 26
e0a76ee1bb2b
child 28
0c1371488d87

moved checkmate and stalemate flags to gamestate

src/chess/rules.c file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
src/game.c file | annotate | diff | comparison | revisions
--- a/src/chess/rules.c	Tue Apr 01 14:04:00 2014 +0200
+++ b/src/chess/rules.c	Thu Apr 03 16:07:04 2014 +0200
@@ -241,7 +241,7 @@
         move->check = 1;
     } else if (mstr[len-1] == '#') {
         len--; mstr[len] = '\0';
-        move->checkmate = 1;
+        /* ignore - validation should set game state */
     }
     
     /* evaluate promotion */
--- a/src/chess/rules.h	Tue Apr 01 14:04:00 2014 +0200
+++ b/src/chess/rules.h	Thu Apr 03 16:07:04 2014 +0200
@@ -77,8 +77,6 @@
     uint8_t torow;
     uint8_t promotion;
     _Bool check;
-    _Bool checkmate;
-    _Bool stalemate; // TODO: find a better place for checkmate and stalemate
     _Bool capture;
 } Move;
 
@@ -94,6 +92,8 @@
     uint8_t mycolor;
     MoveList* movelist;
     MoveList* lastmove;
+    _Bool checkmate;
+    _Bool stalemate;
 } GameState;
 
 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
--- a/src/game.c	Tue Apr 01 14:04:00 2014 +0200
+++ b/src/game.c	Thu Apr 03 16:07:04 2014 +0200
@@ -95,8 +95,7 @@
                     filechr(move.fromfile), rowchr(move.fromrow),
                     move.capture ? 'x':'\0',
                     filechr(move.tofile), rowchr(move.torow),
-                    move.check ? '+' : (move.checkmate ? '#' : 
-                        (move.promotion ? '=' : '\0')),
+                    move.check ? '+' : (move.promotion ? '=' : '\0'),
                     move.promotion ? getpiecechr(move.promotion) : '\0'
                 };
                 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
@@ -105,6 +104,13 @@
                     }
                 }
             }
+            if (!logelem->next) {
+                if (gamestate->checkmate) {
+                    addch('#');
+                } else if (gamestate->stalemate) {
+                    addstr(" stalemate");
+                }
+            }
             addch(' ');
             
             logelem = logelem->next;
@@ -162,11 +168,11 @@
             case VALID_MOVE_SYNTAX:
                 if (validate_move(gamestate, &move)) {
                     apply_move(gamestate, &move);
-                    if (move.checkmate) {
+                    if (gamestate->checkmate) {
                         printw("Checkmate!");
                         clrtoeol();
                         return 1;
-                    } else if (move.stalemate) {
+                    } else if (gamestate->stalemate) {
                         printw("Stalemate!");
                         clrtoeol();
                         return 1;
@@ -238,17 +244,17 @@
                 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
                 code = net_recieve_code(opponent);
                 move.check = code == NETCODE_CHECK;
-                move.checkmate = code == NETCODE_CHECKMATE;
-                move.stalemate = code == NETCODE_STALEMATE;
+                gamestate->checkmate = code == NETCODE_CHECKMATE;
+                gamestate->stalemate = code == NETCODE_STALEMATE;
                 if (code == NETCODE_DECLINE) {
                     printw("Invalid move.");
                 } else {
                     apply_move(gamestate, &move);
-                    if (move.checkmate) {
+                    if (gamestate->checkmate) {
                         printw("Checkmate!");
                         clrtoeol();
                         return 1;
-                    } else if (move.stalemate) {
+                    } else if (gamestate->stalemate) {
                         printw("Stalemate!");
                         clrtoeol();
                         return 1;
@@ -300,12 +306,12 @@
                     apply_move(gamestate, &move);
                     if (move.check) {
                         net_send_code(opponent, NETCODE_CHECK);
-                    } else if (move.checkmate) {
+                    } else if (gamestate->checkmate) {
                         net_send_code(opponent, NETCODE_CHECKMATE);
                         printw("\rCheckmate!");
                         clrtoeol();
                         return 1;
-                    } else if (move.stalemate) {
+                    } else if (gamestate->stalemate) {
                         net_send_code(opponent, NETCODE_STALEMATE);
                         printw("\rStalemate!");
                         clrtoeol();

mercurial