added network messages for check and checkmate

Sat, 22 Mar 2014 18:56:52 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Mar 2014 18:56:52 +0100
changeset 11
08d7a6e3ec31
parent 10
1347e4dabac0
child 12
84880c7e1ea6

added network messages for check and checkmate

src/game.c file | annotate | diff | comparison | revisions
src/game.h file | annotate | diff | comparison | revisions
src/network.h file | annotate | diff | comparison | revisions
--- a/src/game.c	Sat Mar 22 17:23:07 2014 +0100
+++ b/src/game.c	Sat Mar 22 18:56:52 2014 +0100
@@ -91,7 +91,7 @@
     }
 }
 
-static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
+static _Bool validate_move(Board board, Move *move) {
     _Bool result;
     
     /* does piece exist */
@@ -129,6 +129,9 @@
     /* is piece pinned */
     // TODO: make it so
     
+    /* correct check and checkmate flags */
+    // TODO: make it so
+    
     return result;
 }
 
@@ -137,10 +140,13 @@
     
     size_t len = strlen(mstr);
     
-    /* remove check */
+    /* evaluate check/checkmate flags */
     if (mstr[len-1] == '+') {
         len--; mstr[len] = '\0';
         move->check = TRUE;
+    } else if (mstr[len-1] == '#') {
+        len--; mstr[len] = '\0';
+        move->checkmate = TRUE;
     }
     
     if (len == 2) {
@@ -194,6 +200,7 @@
     const size_t buflen = 8;
     char movestr[buflen];
     _Bool remisrejected = FALSE;
+    uint8_t code;
     
     while (1) {
         move(boardy+3, 0);
@@ -236,12 +243,19 @@
             if (eval_move(board, mycolor, movestr, &move)) {
                 net_send_code(opponent, NETCODE_MOVE);
                 net_send_data(opponent, &move, sizeof(Move));
-                if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
-                    apply_move(board, &move);
-                    return 0;
-                } else {
+                code = net_recieve_code(opponent);
+                move.check = code == NETCODE_CHECK;
+                move.checkmate = code == NETCODE_CHECKMATE;
+                // TODO: record move
+                if (code == NETCODE_DECLINE) {
                     printw("Invalid move.");
                     clrtoeol();
+                } else {
+                    apply_move(board, &move);
+                    if (move.checkmate) {
+                        printw("Checkmate!");
+                        return 1;
+                    }
                 }
             } else {
                 printw("Can't interpret move - please use algebraic notation.");
@@ -250,7 +264,7 @@
     }
 }
 
-static int recvmove(Board board, uint8_t mycolor, int opponent) {
+static int recvmove(Board board, int opponent) {
     
     while (1) {
         move(boardy+3, 0);
@@ -280,9 +294,16 @@
                 break;
             case NETCODE_MOVE:
                 net_recieve_data(opponent, &move, sizeof(Move));
-                if (validate_move(board, mycolor, &move)) {
+                if (validate_move(board, &move)) {
                     apply_move(board, &move);
-                    net_send_code(opponent, NETCODE_ACCEPT);
+                    // TODO: record move
+                    if (move.check) {
+                        net_send_code(opponent, NETCODE_CHECK);
+                    } else if (move.checkmate) {
+                        net_send_code(opponent, NETCODE_CHECKMATE);
+                    } else {
+                        net_send_code(opponent, NETCODE_ACCEPT);
+                    }
                     return 0;
                 } else {
                     net_send_code(opponent, NETCODE_DECLINE);
@@ -315,10 +336,10 @@
         if (myturn) {
             running = !sendmove(board, mycolor, opponent);
         } else {
-            running = !recvmove(board, mycolor, opponent);
+            running = !recvmove(board, opponent);
             flushinp(); // flush any input the user hacked in while waiting
         }
-        myturn ^= 1;
+        myturn ^= TRUE;
     }  while (running);
     
     mvaddstr(getmaxy(tchess_window)-1, 0,
--- a/src/game.h	Sat Mar 22 17:23:07 2014 +0100
+++ b/src/game.h	Sat Mar 22 18:56:52 2014 +0100
@@ -72,6 +72,7 @@
     uint8_t tofile;
     uint8_t torow;
     _Bool check;
+    _Bool checkmate;
     _Bool capture;
 } Move;
 
--- a/src/network.h	Sat Mar 22 17:23:07 2014 +0100
+++ b/src/network.h	Sat Mar 22 18:56:52 2014 +0100
@@ -43,8 +43,10 @@
 #define NETCODE_MOVE 0x20
 #define NETCODE_SURRENDER 0x21
 #define NETCODE_REMIS 0x22
+#define NETCODE_CHECK 0x23
+#define NETCODE_CHECKMATE 0x24
     
-#define NETCODE_VERSION 3
+#define NETCODE_VERSION 4
 
 typedef struct {
     int fd; /* -1, if we are the client */

mercurial