implemented rook + some fixes

Mon, 31 Mar 2014 14:00:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 14:00:58 +0200
changeset 21
2e5846019b4f
parent 20
fd1eb081de40
child 22
41bbfd4d17a3

implemented rook + some fixes

src/chess/bishop.c file | annotate | diff | comparison | revisions
src/chess/rook.c file | annotate | diff | comparison | revisions
src/chess/rules.c file | annotate | diff | comparison | revisions
src/game.c file | annotate | diff | comparison | revisions
--- a/src/chess/bishop.c	Mon Mar 31 11:41:08 2014 +0200
+++ b/src/chess/bishop.c	Mon Mar 31 14:00:58 2014 +0200
@@ -42,13 +42,13 @@
     uint8_t y = move->fromrow;
     uint8_t x = move->fromfile;
     
-    do {
+    while (x != move->tofile-dx && y != move->torow-dy) {
         x += dx;
         y += dy;
         if (board[y][x]) {
             return 1;
         }
-    } while (x != move->tofile && y != move->torow);
+    }
     
     return 0;
 }
--- a/src/chess/rook.c	Mon Mar 31 11:41:08 2014 +0200
+++ b/src/chess/rook.c	Mon Mar 31 14:00:58 2014 +0200
@@ -31,16 +31,122 @@
 #include "rook.h"
 
 _Bool rook_chkrules(Move *move) {
-    // TODO: implement
-    return 0;
+    return move->torow == move->fromrow || move->tofile == move->fromfile;
 }
 
 _Bool rook_isblocked(Board board, Move *move) {
-    // TODO: implement
-    return 1;
+    
+    if (move->torow == move->fromrow) {
+        int d = move->tofile > move->fromfile ? 1 : -1;
+        uint8_t f = move->fromfile;
+        while (f != move->tofile-d) {
+            f += d;
+            if (board[move->fromrow][f]) {
+                return 1;
+            }
+        }
+    } else {
+        int d = move->torow > move->fromrow ? 1 : -1;
+        uint8_t r = move->fromrow;
+        while (r != move->torow - d) {
+            r += d;
+            if (board[r][move->fromfile]) {
+                return 1;
+            }
+        }
+    }
+    
+    return 0;
+}
+
+static int rook_getloc_fixedrow(Board board, Move *move) {
+    uint8_t file = POS_UNSPECIFIED;
+    for (uint8_t f = 0 ; f < 8 ; f++) {
+        if (board[move->fromrow][f] == move->piece) {
+            if (file == POS_UNSPECIFIED) {
+                file = f;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }
+    }
+    if (file == POS_UNSPECIFIED) {
+        return INVALID_POSITION;
+    } else {
+        move->fromfile = file;
+        return VALID_MOVE_SYNTAX;
+    }
+}
+
+static int rook_getloc_fixedfile(Board board, Move *move) {
+    uint8_t row = POS_UNSPECIFIED;
+    for (uint8_t r = 0 ; r < 8 ; r++) {
+        if (board[r][move->fromfile] == move->piece) {
+            if (row == POS_UNSPECIFIED) {
+                row = r;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }   
+    }
+    if (row == POS_UNSPECIFIED) {
+        return INVALID_POSITION;
+    } else {
+        move->fromrow = row;
+        return VALID_MOVE_SYNTAX;
+    }
 }
 
 int rook_getlocation(Board board, Move *move) {
-    // TODO: implement
-    return INVALID_MOVE_SYNTAX;
+    
+    if (move->fromfile != POS_UNSPECIFIED) {
+        if (move->fromfile == move->tofile) {
+            return rook_getloc_fixedfile(board, move);
+        } else {
+            if (board[move->torow][move->fromfile] == move->piece) {
+                move->fromrow = move->torow;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return INVALID_POSITION;
+            }
+        }
+    }
+    
+    if (move->fromrow != POS_UNSPECIFIED) {
+        if (move->fromrow == move->torow) {
+            return rook_getloc_fixedrow(board, move);
+        } else {
+            if (board[move->fromrow][move->tofile] == move->piece) {
+                move->fromfile = move->tofile;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return INVALID_POSITION;
+            }
+        }
+    }
+    
+    Move chkrowmove = *move, chkfilemove = *move;
+    
+    chkrowmove.fromrow = move->torow;
+    int chkrow = rook_getloc_fixedrow(board, &chkrowmove);
+    
+    chkfilemove.fromfile = move->tofile;
+    int chkfile = rook_getloc_fixedfile(board, &chkfilemove);
+    
+    if ((chkrow == VALID_MOVE_SYNTAX && chkfile == VALID_MOVE_SYNTAX) ||
+        chkrow == AMBIGUOUS_MOVE || chkfile == AMBIGUOUS_MOVE) {
+        return AMBIGUOUS_MOVE;
+    }
+    
+    if (chkrow == VALID_MOVE_SYNTAX) {
+        *move = chkrowmove;
+        return VALID_MOVE_SYNTAX;
+    }
+    
+    if (chkfile == VALID_MOVE_SYNTAX) {
+        *move = chkfilemove;
+        return VALID_MOVE_SYNTAX;
+    }
+    
+    return INVALID_POSITION;
 }
--- a/src/chess/rules.c	Mon Mar 31 11:41:08 2014 +0200
+++ b/src/chess/rules.c	Mon Mar 31 14:00:58 2014 +0200
@@ -127,6 +127,11 @@
         return 0;
     }
     
+    /* must move */
+    if (move->fromfile == move->tofile && move->fromrow == move->torow) {
+        return 0;
+    }
+    
     /* does piece exist */
     result = msrc(board, move) == move->piece;
     
--- a/src/game.c	Mon Mar 31 11:41:08 2014 +0200
+++ b/src/game.c	Mon Mar 31 14:00:58 2014 +0200
@@ -164,11 +164,11 @@
                 code = net_recieve_code(opponent);
                 move.check = code == NETCODE_CHECK;
                 move.checkmate = code == NETCODE_CHECKMATE;
-                addmove(movelist, &move);
                 if (code == NETCODE_DECLINE) {
                     printw("Invalid move.");
                 } else {
                     apply_move(board, &move);
+                    addmove(movelist, &move);
                     if (move.checkmate) {
                         printw("Checkmate!");
                         clrtoeol();

mercurial