src/game.c

changeset 48
0cedda2544da
parent 47
d726e4b46c33
child 49
02c509a44e98
--- a/src/game.c	Wed May 28 15:47:57 2014 +0200
+++ b/src/game.c	Wed Jun 11 15:38:01 2014 +0200
@@ -162,7 +162,7 @@
         printw("Ambiguous move - please specify the piece to move.");
         break;
     case INVALID_POSITION:
-        printw("Cannot find the piece that shall be moved.");
+        printw("No piece can be moved this way.");
         break;
     case NEED_PROMOTION:
         printw("You need to promote the pawn (append \"=Q\" e.g.)!");
@@ -176,6 +176,12 @@
     case INVALID_MOVE_SYNTAX:
         printw("Can't interpret move - please use algebraic notation.");
         break;
+    case RULES_VIOLATED:
+        printw("Move does not comply chess rules.");
+        break;
+    case KING_MOVES_INTO_CHECK:
+        printw("Can't move the king into a check position.");
+        break;
     default:
         printw("Unknown move parser error.");
     }
@@ -218,7 +224,8 @@
                 int eval_result = eval_move(gamestate, movestr, &move);
                 switch (eval_result) {
                 case VALID_MOVE_SYNTAX:
-                    if (validate_move(gamestate, &move)) {
+                    eval_result = validate_move(gamestate, &move);
+                    if (eval_result == VALID_MOVE_SEMANTICS) {
                         apply_move(gamestate, &move);
                         if (gamestate->checkmate) {
                             printw("Checkmate!");
@@ -232,7 +239,7 @@
                             return 0;
                         }
                     } else {
-                        printw("Invalid move.");
+                        eval_move_failed_msg(eval_result);
                     }
                     break;
                 default:
@@ -306,11 +313,15 @@
                 case VALID_MOVE_SYNTAX:
                     net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
                     code = net_recieve_code(opponent);
-                    move.check = code == NETCODE_CHECK;
+                    move.check = code == NETCODE_CHECK ||
+                        code == NETCODE_CHECKMATE;
                     gamestate->checkmate = code == NETCODE_CHECKMATE;
                     gamestate->stalemate = code == NETCODE_STALEMATE;
                     if (code == NETCODE_DECLINE) {
-                        printw("Invalid move.");
+                        uint32_t reason;
+                        net_recieve_data(opponent, &reason, sizeof(uint32_t));
+                        reason = ntohl(reason);
+                        eval_move_failed_msg(reason);
                     } else if (code == NETCODE_ACCEPT
                             || code == NETCODE_CHECK
                             || code == NETCODE_CHECKMATE
@@ -397,7 +408,8 @@
                 break;
             case NETCODE_MOVE:
                 net_recieve_data(opponent, &move, sizeof(Move));
-                if (validate_move(gamestate, &move)) {
+                code = validate_move(gamestate, &move);
+                if (code == VALID_MOVE_SEMANTICS) {
                     apply_move(gamestate, &move);
                     if (move.check) {
                         net_send_code(opponent, NETCODE_CHECK);
@@ -416,7 +428,9 @@
                     }
                     return 0;
                 } else {
-                    net_send_code(opponent, NETCODE_DECLINE);
+                    uint32_t reason = htonl(code);
+                    net_send_data(opponent, NETCODE_DECLINE,
+                        &reason, sizeof(uint32_t));
                 }
                 break;
             default:

mercurial