implemented castling (without validation)

Sat, 22 Mar 2014 16:25:49 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Mar 2014 16:25:49 +0100
changeset 9
4e4f156bba58
parent 8
52d742aee695
child 10
1347e4dabac0

implemented castling (without validation)

src/game.c file | annotate | diff | comparison | revisions
--- a/src/game.c	Sat Mar 22 16:04:02 2014 +0100
+++ b/src/game.c	Sat Mar 22 16:25:49 2014 +0100
@@ -74,6 +74,20 @@
     board[move->fromrow][move->fromfile] = 0;
     // TODO: care for en passant capture
     board[move->torow][move->tofile] = move->piece;
+    
+    /* castling */
+    if ((move->piece & PIECE_MASK) == KING &&
+        move->fromfile == fileidx('e')) {
+        uint8_t color = move->piece & COLOR_MASK;
+        
+        if (move->tofile == fileidx('g')) {
+            board[move->torow][fileidx('h')] = 0;
+            board[move->torow][fileidx('f')] = color|ROOK;
+        } else if (move->tofile == fileidx('c')) {
+            board[move->torow][fileidx('a')] = 0;
+            board[move->torow][fileidx('d')] = color|ROOK;
+        }
+    }
 }
 
 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
@@ -82,7 +96,7 @@
     /* does piece exist */
     result &= board[move->fromrow][move->fromfile] == move->piece;
     
-    /* is move rule conform */
+    /* does move comply to rules */
     // TODO: make it so
     
     /* is piece blocked */
@@ -94,24 +108,24 @@
     return result;
 }
 
-static _Bool eval_move(Board board, uint8_t mycolor, char *movestr, Move *move) {
+static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
     memset(move, 0, sizeof(Move));
     
-    size_t len = strlen(movestr);
+    size_t len = strlen(mstr);
     
     /* remove check */
-    if (movestr[len-1] == '+') {
-        len--; movestr[len] = '\0';
+    if (mstr[len-1] == '+') {
+        len--; mstr[len] = '\0';
         move->check = TRUE;
     }
     
     if (len == 2) {
         /* pawn move (e.g. "e4") */
-        if (isfile(movestr[0]) && isrow(movestr[1])) {
+        if (isfile(mstr[0]) && isrow(mstr[1])) {
             move->piece = PAWN;
-            move->fromfile = move->tofile = fileidx(movestr[0]);
-            move->torow = rowidx(movestr[1]);
-            move->fromrow = rowidx(movestr[1]) + (mycolor == WHITE ? -1 : 1);
+            move->fromfile = move->tofile = fileidx(mstr[0]);
+            move->torow = rowidx(mstr[1]);
+            move->fromrow = rowidx(mstr[1]) + (mycolor == WHITE ? -1 : 1);
             if (move->fromrow > 6) {
                 move->piece = 0;
             } else {
@@ -127,11 +141,11 @@
             }
         }
     } else if (len == 3) {
-        if (strcmp(movestr, "0-0") == 0) {
+        if (strcmp(mstr, "O-O") == 0) {
             /* king side castling */
             move->piece = KING;
             move->fromfile = fileidx('e');
-            move->fromfile = fileidx('g');
+            move->tofile = fileidx('g');
             move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
         } else {
             /* unambiguous move (e.g. "Nf3") */
@@ -143,13 +157,19 @@
         /* unambiguous capture (e.g. "Nxf3", "dxe5") */
         
     } else if (len == 5) {
-        /* queen side castling "O-O-O" */
-        
-        /* ambiguous capture (e.g. "Ndxf3") */
-        
-        /* long notation move (e.g. "Nc5a4") */
-        
-        /* long notation capture (e.g. "e5xf6") */
+        if (strcmp(mstr, "O-O-O") == 0) {
+            /* queen side castling "O-O-O" */
+            move->piece = KING;
+            move->fromfile = fileidx('e');
+            move->tofile = fileidx('c');
+            move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
+        } else {
+            /* ambiguous capture (e.g. "Ndxf3") */
+
+            /* long notation move (e.g. "Nc5a4") */
+
+            /* long notation capture (e.g. "e5xf6") */
+        }
     } else if (len == 6) {
         /* long notation capture (e.g. "Nc5xf3") */
     }

mercurial