moved chess rules to separate lib

Mon, 31 Mar 2014 11:16:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 11:16:32 +0200
changeset 19
6a26114297a1
parent 18
6008840b859e
child 20
fd1eb081de40

moved chess rules to separate lib

Makefile file | annotate | diff | comparison | revisions
conf.mk file | annotate | diff | comparison | revisions
src/Makefile file | annotate | diff | comparison | revisions
src/chess/Makefile file | annotate | diff | comparison | revisions
src/chess/bishop.c file | annotate | diff | comparison | revisions
src/chess/bishop.h file | annotate | diff | comparison | revisions
src/chess/chess.h file | annotate | diff | comparison | revisions
src/chess/conf.mk file | annotate | diff | comparison | revisions
src/chess/king.c file | annotate | diff | comparison | revisions
src/chess/king.h file | annotate | diff | comparison | revisions
src/chess/knight.c file | annotate | diff | comparison | revisions
src/chess/knight.h file | annotate | diff | comparison | revisions
src/chess/pawn.c file | annotate | diff | comparison | revisions
src/chess/pawn.h file | annotate | diff | comparison | revisions
src/chess/queen.c file | annotate | diff | comparison | revisions
src/chess/queen.h file | annotate | diff | comparison | revisions
src/chess/rook.c file | annotate | diff | comparison | revisions
src/chess/rook.h file | annotate | diff | comparison | revisions
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
src/game.h file | annotate | diff | comparison | revisions
src/rules/bishop.c file | annotate | diff | comparison | revisions
src/rules/bishop.h file | annotate | diff | comparison | revisions
src/rules/king.c file | annotate | diff | comparison | revisions
src/rules/king.h file | annotate | diff | comparison | revisions
src/rules/knight.c file | annotate | diff | comparison | revisions
src/rules/knight.h file | annotate | diff | comparison | revisions
src/rules/pawn.c file | annotate | diff | comparison | revisions
src/rules/pawn.h file | annotate | diff | comparison | revisions
src/rules/queen.c file | annotate | diff | comparison | revisions
src/rules/queen.h file | annotate | diff | comparison | revisions
src/rules/rook.c file | annotate | diff | comparison | revisions
src/rules/rook.h file | annotate | diff | comparison | revisions
src/rules/rules.h file | annotate | diff | comparison | revisions
--- a/Makefile	Sat Mar 29 16:53:58 2014 +0100
+++ b/Makefile	Mon Mar 31 11:16:32 2014 +0200
@@ -28,15 +28,14 @@
 
 include conf.mk
 
-all: build build/rules
+all: build chess
 	cd src; $(MAKE)
 
 build:
 	$(MKDIR) build
 
-build/rules:
-	$(MKDIR) build/rules
+chess:
+	cd src/chess; $(MAKE) BUILDDIR=../../build
 	
 clean:
 	$(RM) -f -R build
-	$(RM) -f -R build/rules
--- a/conf.mk	Sat Mar 29 16:53:58 2014 +0100
+++ b/conf.mk	Mon Mar 31 11:16:32 2014 +0200
@@ -26,14 +26,14 @@
 # POSSIBILITY OF SUCH DAMAGE.
 #
 
-# system related
 MKDIR   = mkdir
 RM      = rm
 
-# build related
 BIN     = terminal-chess
 CC      = gcc
 CFLAGS  = -g -O2 -std=gnu99 -Wall -Werror -pedantic
 LD      = gcc
 LDFLAGS = -lncurses
+
+LIB_EXT = .a
 OBJ_EXT = .o
--- a/src/Makefile	Sat Mar 29 16:53:58 2014 +0100
+++ b/src/Makefile	Mon Mar 31 11:16:32 2014 +0200
@@ -35,17 +35,10 @@
 SRC += client.c
 SRC += game.c
 
-SRC += rules/pawn.c
-SRC += rules/rook.c
-SRC += rules/knight.c
-SRC += rules/bishop.c
-SRC += rules/queen.c
-SRC += rules/king.c
-
 OBJ = $(SRC:%.c=../build/%$(OBJ_EXT))
 
 all: $(OBJ)
-	$(LD) -o ../build/$(BIN) $^ $(LDFLAGS)
+	$(LD) -o ../build/$(BIN) $^ ../build/chess$(LIB_EXT) $(LDFLAGS)
 	
 
 ../build/%$(OBJ_EXT): %.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/Makefile	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,54 @@
+#
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+#
+# Copyright 2014 Mike Becker. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+#   1. Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above copyright
+#      notice, this list of conditions and the following disclaimer in the
+#      documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+BUILDDIR = ../build
+
+include conf.mk
+
+SRC += pawn.c
+SRC += rook.c
+SRC += knight.c
+SRC += bishop.c
+SRC += queen.c
+SRC += king.c
+SRC += rules.c
+
+OBJ = $(SRC:%.c=$(BUILDDIR)/%$(OBJ_EXT))
+
+all: $(OBJ)
+	$(AR) $(ARFLAGS) $(BUILDDIR)/chess$(LIB_EXT) $(OBJ)
+	
+
+$(BUILDDIR)/%$(OBJ_EXT): %.c
+	$(CC) -o $@ $(CFLAGS) -c $<
+
+$(BUILDDIR):
+	$(MKDIR) $(MKDIRFLAGS) $(BUILDDIR)
+	
+clear:
+	$(RM) $(RMFLAGS) $(BUILDDIR)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/bishop.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,128 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "bishop.h"
+#include "rules.h"
+#include <stdlib.h>
+
+_Bool bishop_chkrules(Move* move) {
+    return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile);
+}
+
+_Bool bishop_isblocked(Board board, Move *move) {
+    int dy = move->torow > move->fromrow ? 1 : -1;
+    int dx = move->tofile > move->fromfile ? 1 : -1;
+    
+    uint8_t y = move->fromrow;
+    uint8_t x = move->fromfile;
+    
+    do {
+        x += dx;
+        y += dy;
+        if (board[y][x]) {
+            return 1;
+        }
+    } while (x != move->tofile && y != move->torow);
+    
+    return 0;
+}
+
+static int bishop_getloc_fixedfile(Board board, Move *move) {
+    uint8_t d = abs(move->fromfile - move->tofile);
+    if (board[move->torow - d][move->fromfile] == move->piece) {
+        move->fromrow = move->torow - d;
+    }
+    if (board[move->torow + d][move->fromfile] == move->piece) {
+        if (move->fromrow == POS_UNSPECIFIED) {
+            move->fromrow = move->torow + d;
+        } else {
+            return AMBIGUOUS_MOVE; /* rare situation after promotion */
+        }
+    }
+    return move->fromrow == POS_UNSPECIFIED ?
+        INVALID_POSITION : VALID_MOVE_SYNTAX;
+}
+
+static int bishop_getloc_fixedrow(Board board, Move *move) {
+    uint8_t d = abs(move->fromrow - move->torow);
+    if (board[move->fromrow][move->tofile - d] == move->piece) {
+        move->fromfile = move->tofile - d;
+    }
+    if (board[move->fromrow][move->tofile + d] == move->piece) {
+        if (move->fromfile == POS_UNSPECIFIED) {
+            move->fromfile = move->tofile + d;
+        } else {
+            return AMBIGUOUS_MOVE; /* rare situation after promotion */
+        }
+    }
+    return move->fromfile == POS_UNSPECIFIED ?
+        INVALID_POSITION : VALID_MOVE_SYNTAX;
+}
+
+int bishop_getlocation(Board board, Move *move) {
+    
+    if (move->fromfile != POS_UNSPECIFIED) {
+        return bishop_getloc_fixedfile(board, move);
+    }
+    
+    if (move->fromrow != POS_UNSPECIFIED) {
+        return bishop_getloc_fixedrow(board, move);
+    }
+    
+    _Bool amb = 0;
+    for (int d = -7 ; d < 8  ; d++) {
+        uint8_t row = move->torow + d;
+        if (isidx(row)) {
+            uint8_t file = move->tofile + d;
+            if (isidx(file) && board[row][file] == move->piece) {
+                if (amb) {
+                    return AMBIGUOUS_MOVE;
+                }
+                amb = 1;
+                move->fromrow = row;
+                move->fromfile = file;
+            }
+            file = move->tofile - d;
+            if (isidx(file) && board[row][file] == move->piece) {
+                if (amb) {
+                    return AMBIGUOUS_MOVE;
+                }
+                amb = 1;
+                move->fromrow = row;
+                move->fromfile = file;
+            }
+        }
+    }
+    
+    if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) {
+        return INVALID_POSITION;
+    } else {
+        return VALID_MOVE_SYNTAX;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/bishop.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef BISHOP_H
+#define	BISHOP_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool bishop_chkrules(Move *move);
+_Bool bishop_isblocked(Board board, Move *move);
+int bishop_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* BISHOP_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/chess.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,36 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "rules.h"
+#include "pawn.h"
+#include "rook.h"
+#include "knight.h"
+#include "bishop.h"
+#include "queen.h"
+#include "king.h"
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/conf.mk	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,40 @@
+#
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+#
+# Copyright 2014 Mike Becker. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are met:
+#
+#   1. Redistributions of source code must retain the above copyright
+#      notice, this list of conditions and the following disclaimer.
+#
+#   2. Redistributions in binary form must reproduce the above copyright
+#      notice, this list of conditions and the following disclaimer in the
+#      documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+#
+
+MKDIR   = mkdir
+RM      = rm
+AR      = ar
+
+CC         = gcc
+CFLAGS     = -g -O2 -std=gnu99 -Wall -Werror -pedantic
+ARFLAGS    = -r
+MKDIRFLAGS = -p
+RMFLAGS    = -f -R
+
+OBJ_EXT = .o
+LIB_EXT = .a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/king.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "rules.h"
+#include "king.h"
+
+_Bool king_chkrules(Board board, Move* move) {
+    // TODO: implement
+    return 0;
+}
+
+_Bool king_isblocked(Board board, Move *move) {
+    // TODO: implement
+    return 1;
+}
+
+int king_getlocation(Board board, Move *move) {
+    // TODO: implement
+    return INVALID_MOVE_SYNTAX;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/king.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,49 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+
+#ifndef KING_H
+#define	KING_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool king_chkrules(Board board, Move *move);
+_Bool king_isblocked(Board board, Move *move);
+int king_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* KING_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/knight.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,133 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "knight.h"
+#include "rules.h"
+#include <stdlib.h>
+
+_Bool knight_chkrules(Move *move) {
+    int dx = abs(move->fromfile - move->tofile);
+    int dy = abs(move->fromrow - move->torow);
+    
+    return (dx == 2 && dy == 1) || (dx == 1 && dy == 2);
+}
+
+static int knight_getloc_fixedrow(Board board, Move *move) {
+    int d = 3 - abs(move->fromrow - move->torow);
+    
+    if (d == 1 || d == 2) {
+        if (move->tofile < 6 &&
+            board[move->fromrow][move->tofile + d] == move->piece) {
+            if (move->fromfile == POS_UNSPECIFIED) {
+                move->fromfile = move->tofile + d;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }
+        if (move->tofile > 1 &&
+            board[move->fromrow][move->tofile - d] == move->piece) {
+            if (move->fromfile == POS_UNSPECIFIED) {
+                move->fromfile = move->tofile - d;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }
+    }
+    
+    return INVALID_POSITION;
+}
+
+static int knight_getloc_fixedfile(Board board, Move *move) {
+    int d = 3 - abs(move->fromfile - move->tofile);
+    
+    if (d == 1 || d == 2) {
+        if (move->torow < 6 &&
+            board[move->torow + d][move->fromfile] == move->piece) {
+            if (move->fromrow == POS_UNSPECIFIED) {
+                move->fromrow = move->torow + d;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }
+        if (move->torow > 1 &&
+            board[move->torow - d][move->fromfile] == move->piece) {
+            if (move->fromrow == POS_UNSPECIFIED) {
+                move->fromrow = move->torow - d;
+                return VALID_MOVE_SYNTAX;
+            } else {
+                return AMBIGUOUS_MOVE;
+            }
+        }
+    }
+    
+    return INVALID_POSITION;
+}
+
+int knight_getlocation(Board board, Move *move) {
+    
+    if (move->fromfile != POS_UNSPECIFIED) {
+        return knight_getloc_fixedfile(board, move);
+    }
+    
+    if (move->fromrow != POS_UNSPECIFIED) {
+        return knight_getloc_fixedrow(board, move);
+    }
+    
+    for (int x = -2 ; x <= 2 ; x++) {
+        if (x == 0) {
+            continue;
+        }
+        for (int y = -2 ; y <= 2 ; y++) {
+            if (y == 0 || y == x) {
+                continue;
+            }
+            uint8_t cx = move->tofile + x;
+            uint8_t cy = move->torow + y;
+
+            if (isidx(cx) && isidx(cy) && board[cy][cx] == move->piece) {
+                if (move->fromfile == POS_UNSPECIFIED
+                    && move->fromrow == POS_UNSPECIFIED) {
+                    move->fromfile = cx;
+                    move->fromrow = cy;
+                } else {
+                    return AMBIGUOUS_MOVE;
+                }
+            }
+        }
+    }
+    
+    if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) {
+        return INVALID_POSITION;
+    } else {
+        return VALID_MOVE_SYNTAX;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/knight.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef KNIGHT_H
+#define	KNIGHT_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool knight_chkrules(Move *move);
+#define knight_isblocked(b,m) 0
+int knight_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* KNIGHT_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/pawn.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,97 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "pawn.h"
+#include "rules.h"
+
+_Bool pawn_chkrules(Board board, Move *move) {
+    int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1);
+    
+    if (move->torow == (d < 0 ? 7 : 0)) {
+        if (move->promotion) {
+            uint8_t promopiece = move->promotion & PIECE_MASK;
+            if (!promopiece || promopiece == PAWN || promopiece == KING) {
+                return 0;
+            }
+        } else {
+            return 0;
+        }
+    } else {
+        if (move->promotion) {
+            return 0;
+        }
+    }
+    
+    if (move->capture) {
+        if (move->fromrow == move->torow + d && (
+            move->fromfile == move->tofile + 1 ||
+            move->fromfile == move->tofile - 1)) {
+
+            return mdst(board,move)
+                || (board[move->fromrow][move->tofile] & ENPASSANT_THREAT);
+        } else {
+            return 0;
+        }
+    } else {
+        if (move->fromfile == move->tofile) {
+            return (move->fromrow == move->torow + d) ||
+                (move->fromrow == (d < 0 ? 1 : 6) && /* advanced first move */
+                move->fromrow == move->torow + d*2);
+        } else {
+            return 0;
+        }
+    }
+}
+
+_Bool pawn_isblocked(Board board, Move *move) {
+    return mdst(board,move) && !move->capture;
+}
+
+int pawn_getlocation(Board board, Move *move) {
+    int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1);
+    
+    if (move->fromfile == POS_UNSPECIFIED) {
+        move->fromfile = move->tofile;
+    }
+    move->fromrow = move->torow + d;
+    if (move->fromrow > 6) {
+        return INVALID_POSITION;
+    } else {
+        /* advanced first move */
+        if (move->fromrow == (d < 0 ? 2 : 5) &&
+            msrc(board,move) != move->piece) {
+
+            move->fromrow += d;
+            if (move->fromrow > 6) {
+                return INVALID_POSITION;
+            }
+        }
+    }
+    return VALID_MOVE_SYNTAX;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/pawn.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef PAWN_H
+#define	PAWN_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool pawn_chkrules(Board board, Move *move);
+_Bool pawn_isblocked(Board board, Move *move);
+int pawn_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* PAWN_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/queen.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "rules.h"
+#include "queen.h"
+
+_Bool queen_chkrules(Move* move) {
+    // TODO: implement
+    return 0;
+}
+
+_Bool queen_isblocked(Board board, Move *move) {
+    // TODO: implement
+    return 1;
+}
+
+int queen_getlocation(Board board, Move *move) {
+    // TODO: implement
+    return INVALID_MOVE_SYNTAX;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/queen.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef QUEEN_H
+#define	QUEEN_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool queen_chkrules(Move *move);
+_Bool queen_isblocked(Board board, Move *move);
+int queen_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* QUEEN_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/rook.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,46 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "rules.h"
+#include "rook.h"
+
+_Bool rook_chkrules(Move *move) {
+    // TODO: implement
+    return 0;
+}
+
+_Bool rook_isblocked(Board board, Move *move) {
+    // TODO: implement
+    return 1;
+}
+
+int rook_getlocation(Board board, Move *move) {
+    // TODO: implement
+    return INVALID_MOVE_SYNTAX;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/rook.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,48 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef ROOK_H
+#define	ROOK_H
+
+#include "rules.h"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+_Bool rook_chkrules(Move *move);
+_Bool rook_isblocked(Board board, Move *move);
+int rook_getlocation(Board board, Move *move);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* ROOK_H */
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/rules.c	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,303 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include "rules.h"
+#include "chess.h"
+#include <string.h>
+
+char getpiecechr(uint8_t piece) {
+    switch (piece & PIECE_MASK) {
+    case ROOK: return 'R';
+    case KNIGHT: return 'N';
+    case BISHOP: return 'B';
+    case QUEEN: return 'Q';
+    case KING: return 'K';
+    default: return '\0';
+    }
+}
+
+uint8_t getpiece(char c) {
+    switch (c) {
+        case 'R': return ROOK;
+        case 'N': return KNIGHT;
+        case 'B': return BISHOP;
+        case 'Q': return QUEEN;
+        case 'K': return KING;
+        default: return 0;
+    }
+}
+
+/**
+ * Guesses the location of a piece for short algebraic notation.
+ * 
+ * @param board the current state of the board
+ * @param move the move date to operate on
+ * @return status code (see rules/rules.h for the codes)
+ */
+static int getlocation(Board board, Move *move) {   
+    uint8_t piece = move->piece & PIECE_MASK;
+    switch (piece) {
+        case PAWN: return pawn_getlocation(board, move);
+        case ROOK: return rook_getlocation(board, move);
+        case KNIGHT: return knight_getlocation(board, move);
+        case BISHOP: return bishop_getlocation(board, move);
+        case QUEEN: return queen_getlocation(board, move);
+        case KING: return king_getlocation(board, move);
+        default: return INVALID_MOVE_SYNTAX;
+    }
+}
+
+
+void apply_move(Board board, Move *move) {
+    uint8_t piece = move->piece & PIECE_MASK;
+    uint8_t color = move->piece & COLOR_MASK;
+    
+    /* en passant capture */
+    if (move->capture && piece == PAWN &&
+        mdst(board, move) == 0) {
+        board[move->fromrow][move->tofile] = 0;
+    }
+    
+    /* remove old en passant threats */
+    for (uint8_t file = 0 ; file < 8 ; file++) {
+        board[3][file] &= ~ENPASSANT_THREAT;
+        board[4][file] &= ~ENPASSANT_THREAT;
+    }
+    
+    /* add new en passant threat */
+    if (piece == PAWN && (
+        (move->fromrow == 1 && move->torow == 3) ||
+        (move->fromrow == 6 && move->torow == 4))) {
+        move->piece |= ENPASSANT_THREAT;
+    }
+    
+    /* move (and maybe capture or promote) */
+    msrc(board, move) = 0;
+    if (move->promotion) {
+        mdst(board, move) = move->promotion;
+    } else {
+        mdst(board, move) = move->piece;
+    }
+    
+    /* castling */
+    if (piece == KING &&
+        move->fromfile == fileidx('e')) {
+        
+        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;
+        }
+    }
+}
+
+_Bool validate_move(Board board, Move *move) {
+    _Bool result;
+    
+    /* validate indices (don't trust opponent) */
+    if (!chkidx(move)) {
+        return 0;
+    }
+    
+    /* does piece exist */
+    result = msrc(board, move) == move->piece;
+    
+    /* can't capture own pieces */
+    if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
+        return 0;
+    }
+    
+    /* validate individual rules */
+    switch (move->piece & PIECE_MASK) {
+    case PAWN:
+        result = result && pawn_chkrules(board, move);
+        result = result && !pawn_isblocked(board, move);
+        break;
+    case ROOK:
+        result = result && rook_chkrules(move);
+        result = result && !rook_isblocked(board, move);
+        break;
+    case KNIGHT:
+        result = result && knight_chkrules(move);
+        result = result && !knight_isblocked(board, move);
+        break;
+    case BISHOP:
+        result = result && bishop_chkrules(move);
+        result = result && !bishop_isblocked(board, move);
+        break;
+    case QUEEN:
+        result = result && queen_chkrules(move);
+        result = result && !queen_isblocked(board, move);
+        break;
+    case KING:
+        result = result && king_chkrules(board, move);
+        result = result && !king_isblocked(board, move);
+        break;
+    default:
+        result = 0;
+    }
+    
+    /* is piece pinned */
+    // TODO: make it so
+    
+    /* correct check and checkmate flags */
+    // TODO: make it so
+    
+    return result;
+}
+
+int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
+    memset(move, 0, sizeof(Move));
+    move->fromfile = POS_UNSPECIFIED;
+    move->fromrow = POS_UNSPECIFIED;
+
+    size_t len = strlen(mstr);
+    
+    /* evaluate check/checkmate flags */
+    if (mstr[len-1] == '+') {
+        len--; mstr[len] = '\0';
+        move->check = 1;
+    } else if (mstr[len-1] == '#') {
+        len--; mstr[len] = '\0';
+        move->checkmate = 1;
+    }
+    
+    /* evaluate promotion */
+    if (len > 3 && mstr[len-2] == '=') {
+        move->promotion = getpiece(mstr[len-1]);
+        if (!move->promotion) {
+            return INVALID_MOVE_SYNTAX;
+        } else {
+            move->promotion |= mycolor;
+            len -= 2;
+            mstr[len] = 0;
+        }
+    }
+    
+    if (len == 2) {
+        /* pawn move (e.g. "e4") */
+        move->piece = PAWN;
+        move->tofile = fileidx(mstr[0]);
+        move->torow = rowidx(mstr[1]);
+    } else if (len == 3) {
+        if (strcmp(mstr, "O-O") == 0) {
+            /* king side castling */
+            move->piece = KING;
+            move->fromfile = fileidx('e');
+            move->tofile = fileidx('g');
+            move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
+        } else {
+            /* move (e.g. "Nf3") */
+            move->piece = getpiece(mstr[0]);
+            move->tofile = fileidx(mstr[1]);
+            move->torow = rowidx(mstr[2]);
+        }
+        
+    } else if (len == 4) {
+        move->piece = getpiece(mstr[0]);
+        if (!move->piece) {
+            move->piece = PAWN;
+            move->fromfile = fileidx(mstr[0]);
+        }
+        if (mstr[1] == 'x') {
+            /* capture (e.g. "Nxf3", "dxe5") */
+            move->capture = 1;
+        } else {
+            /* move (e.g. "Ndf3", "N2c3", "e2e4") */
+            if (isfile(mstr[1])) {
+                move->fromfile = fileidx(mstr[1]);
+                if (move->piece == PAWN) {
+                    move->piece = 0;
+                }
+            } else {
+                move->fromrow = rowidx(mstr[1]);
+            }
+        }
+        move->tofile = fileidx(mstr[2]);
+        move->torow = rowidx(mstr[3]);
+    } else if (len == 5) {
+        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 {
+            move->piece = getpiece(mstr[0]);
+            if (mstr[2] == 'x') {
+                move->capture = 1;
+                if (move->piece) {
+                    /* capture (e.g. "Ndxf3") */
+                    move->fromfile = fileidx(mstr[1]);
+                } else {
+                    /* long notation capture (e.g. "e5xf6") */
+                    move->piece = PAWN;
+                    move->fromfile = fileidx(mstr[0]);
+                    move->fromrow = rowidx(mstr[1]);
+                }
+            } else {
+                /* long notation move (e.g. "Nc5a4") */
+                move->fromfile = fileidx(mstr[1]);
+                move->fromrow = rowidx(mstr[2]);
+            }
+            move->tofile = fileidx(mstr[3]);
+            move->torow = rowidx(mstr[4]);
+        }
+    } else if (len == 6) {
+        /* long notation capture (e.g. "Nc5xf3") */
+        if (mstr[3] == 'x') {
+            move->capture = 1;
+            move->piece = getpiece(mstr[0]);
+            move->fromfile = fileidx(mstr[1]);
+            move->fromrow = rowidx(mstr[2]);
+            move->tofile = fileidx(mstr[4]);
+            move->torow = rowidx(mstr[5]);
+        }
+    }
+
+    
+    if (move->piece) {
+        if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0)
+            && !move->promotion) {
+            return NEED_PROMOTION;
+        }
+        
+        move->piece |= mycolor;
+        if (move->fromfile == POS_UNSPECIFIED
+            || move->fromrow == POS_UNSPECIFIED) {
+            return getlocation(board, move);
+        } else {
+            return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
+        }
+    } else {
+        return INVALID_MOVE_SYNTAX;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/chess/rules.h	Mon Mar 31 11:16:32 2014 +0200
@@ -0,0 +1,154 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2014 Mike Becker. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef RULES_H
+#define	RULES_H
+
+#include <stdint.h>
+
+#define VALID_MOVE_SYNTAX   0
+#define INVALID_MOVE_SYNTAX 1
+#define INVALID_POSITION    2
+#define AMBIGUOUS_MOVE      3
+#define NEED_PROMOTION      4
+
+
+#define PIECE_MASK       0x0F
+#define COLOR_MASK       0x30
+#define ENPASSANT_THREAT 0x40
+
+#define WHITE 0x10
+#define BLACK 0x20
+
+#define PAWN   0x01
+#define ROOK   0x02
+#define KNIGHT 0x03
+#define BISHOP 0x04
+#define QUEEN  0x05
+#define KING   0x06
+
+#define WPAWN   (WHITE|PAWN)
+#define WROOK   (WHITE|ROOK)
+#define WKNIGHT (WHITE|KNIGHT)
+#define WBISHOP (WHITE|BISHOP)
+#define WQUEEN  (WHITE|QUEEN)
+#define WKING   (WHITE|KING)
+#define BPAWN   (BLACK|PAWN)
+#define BROOK   (BLACK|ROOK)
+#define BKNIGHT (BLACK|KNIGHT)
+#define BBISHOP (BLACK|BISHOP)
+#define BQUEEN  (BLACK|QUEEN)
+#define BKING   (BLACK|KING)
+
+typedef uint8_t Board[8][8];
+
+typedef struct {
+    uint8_t piece;
+    uint8_t fromfile;
+    uint8_t fromrow;
+    uint8_t tofile;
+    uint8_t torow;
+    uint8_t promotion;
+    _Bool check;
+    _Bool checkmate;
+    _Bool capture;
+} Move;
+
+#define POS_UNSPECIFIED UINT8_MAX
+#define mdst(b,m) b[(m)->torow][(m)->tofile]
+#define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
+
+#define isidx(idx) ((uint8_t)idx < 8)
+
+#define isfile(file) (file >= 'a' && file <= 'h')
+#define isrow(row) (row >= '1' && row <= '8')
+
+#define rowidx(row) (row-'1')
+#define fileidx(file) (file-'a')
+
+#define rowchr(row) (row+'1')
+#define filechr(file) (file+'a')
+
+#define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
+        isidx((move)->tofile) && isidx((move)->torow))
+
+/* secure versions - use, if index is not checked with isidx() */
+#define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
+#define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
+
+/**
+ * Maps a character to a piece.
+ * 
+ * Does not work for pawns, since they don't have a character.
+ * 
+ * @param c one of R,N,B,Q,K
+ * @return numeric value for the specified piece
+ */
+uint8_t getpiece(char c);
+
+/**
+ * Maps a piece to a character.
+ * 
+ * Does not work for pawns, scince they don't have a character.
+ * 
+ * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
+ * @return character value for the specified piece
+ */
+char getpiecechr(uint8_t piece);
+
+/**
+ * Evaluates a move syntactically and stores the move data in the specified
+ * object.
+ * 
+ * @param board the current state of the board
+ * @param mycolor the color of the current player
+ * @param mstr the input string to parse
+ * @param move a pointer to object where the move data shall be stored
+ * @return status code (see rules/rules.h for the list of codes)
+ */
+int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move);
+
+/**
+ * Validates move by applying chess rules.
+ * @param board the current board state
+ * @param move the move to validate
+ * @return TRUE, if the move complies to chess rules, FALSE otherwise
+ */
+_Bool validate_move(Board board, Move *move);
+
+/**
+ * Applies a move and deletes captured pieces.
+ * 
+ * @param board the current board state
+ * @param move the move to apply
+ */
+void apply_move(Board board, Move *move);
+
+#endif	/* RULES_H */
+
--- a/src/game.c	Sat Mar 29 16:53:58 2014 +0100
+++ b/src/game.c	Mon Mar 31 11:16:32 2014 +0200
@@ -28,64 +28,13 @@
  */
 
 #include "game.h"
+#include "network.h"
 #include "input.h"
-#include "rules/rules.h"
 #include <ncurses.h>
 #include <string.h>
 
 static const uint8_t boardx = 10, boardy = 10;
 
-static uint8_t getpiecechr(uint8_t piece) {
-    switch (piece & PIECE_MASK) {
-    case ROOK: return 'R';
-    case KNIGHT: return 'N';
-    case BISHOP: return 'B';
-    case QUEEN: return 'Q';
-    case KING: return 'K';
-    default: return '\0';
-    }
-}
-
-/**
- * Maps a character to a piece.
- * 
- * Does not work for pawns, since they don't have a character.
- * 
- * @param c one of R,N,B,Q,K
- * @return numeric value for the specified piece
- */
-static uint8_t getpiece(char c) {
-    switch (c) {
-        case 'R': return ROOK;
-        case 'N': return KNIGHT;
-        case 'B': return BISHOP;
-        case 'Q': return QUEEN;
-        case 'K': return KING;
-        default: return 0;
-    }
-}
-
-/**
- * Guesses the location of a piece for short algebraic notation.
- * 
- * @param board the current state of the board
- * @param move the move date to operate on
- * @return status code (see rules/rules.h for the codes)
- */
-static int getlocation(Board board, Move *move) {   
-    uint8_t piece = move->piece & PIECE_MASK;
-    switch (piece) {
-        case PAWN: return pawn_getlocation(board, move);
-        case ROOK: return rook_getlocation(board, move);
-        case KNIGHT: return knight_getlocation(board, move);
-        case BISHOP: return bishop_getlocation(board, move);
-        case QUEEN: return queen_getlocation(board, move);
-        case KING: return king_getlocation(board, move);
-        default: return INVALID_MOVE_SYNTAX;
-    }
-}
-
-
 static void draw_board(Board board, MoveListRoot *movelist, uint8_t mycolor) {
     
     for (uint8_t y = 0 ; y < 8 ; y++) {
@@ -158,255 +107,6 @@
     }
 }
 
-/**
- * Applies a move and deletes captured pieces.
- * 
- * @param board the current board state
- * @param move the move to apply
- */
-static void apply_move(Board board, Move *move) {
-    uint8_t piece = move->piece & PIECE_MASK;
-    uint8_t color = move->piece & COLOR_MASK;
-    
-    /* en passant capture */
-    if (move->capture && piece == PAWN &&
-        mdst(board, move) == 0) {
-        board[move->fromrow][move->tofile] = 0;
-    }
-    
-    /* remove old en passant threats */
-    for (uint8_t file = 0 ; file < 8 ; file++) {
-        board[3][file] &= ~ENPASSANT_THREAT;
-        board[4][file] &= ~ENPASSANT_THREAT;
-    }
-    
-    /* add new en passant threat */
-    if (piece == PAWN && (
-        (move->fromrow == 1 && move->torow == 3) ||
-        (move->fromrow == 6 && move->torow == 4))) {
-        move->piece |= ENPASSANT_THREAT;
-    }
-    
-    /* move (and maybe capture or promote) */
-    msrc(board, move) = 0;
-    if (move->promotion) {
-        mdst(board, move) = move->promotion;
-    } else {
-        mdst(board, move) = move->piece;
-    }
-    
-    /* castling */
-    if (piece == KING &&
-        move->fromfile == fileidx('e')) {
-        
-        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;
-        }
-    }
-}
-
-/**
- * Validates move by applying chess rules.
- * @param board the current board state
- * @param move the move to validate
- * @return TRUE, if the move complies to chess rules, FALSE otherwise
- */
-static _Bool validate_move(Board board, Move *move) {
-    _Bool result;
-    
-    /* validate indices (don't trust opponent) */
-    if (!chkidx(move)) {
-        return FALSE;
-    }
-    
-    /* does piece exist */
-    result = msrc(board, move) == move->piece;
-    
-    /* can't capture own pieces */
-    if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
-        return FALSE;
-    }
-    
-    /* validate individual rules */
-    switch (move->piece & PIECE_MASK) {
-    case PAWN:
-        result = result && pawn_chkrules(board, move);
-        result = result && !pawn_isblocked(board, move);
-        break;
-    case ROOK:
-        result = result && rook_chkrules(move);
-        result = result && !rook_isblocked(board, move);
-        break;
-    case KNIGHT:
-        result = result && knight_chkrules(move);
-        result = result && !knight_isblocked(board, move);
-        break;
-    case BISHOP:
-        result = result && bishop_chkrules(move);
-        result = result && !bishop_isblocked(board, move);
-        break;
-    case QUEEN:
-        result = result && queen_chkrules(move);
-        result = result && !queen_isblocked(board, move);
-        break;
-    case KING:
-        result = result && king_chkrules(board, move);
-        result = result && !king_isblocked(board, move);
-        break;
-    default:
-        result = FALSE;
-    }
-    
-    /* is piece pinned */
-    // TODO: make it so
-    
-    /* correct check and checkmate flags */
-    // TODO: make it so
-    
-    return result;
-}
-
-/**
- * Evaluates a move syntactically and stores the move data in the specified
- * object.
- * 
- * @param board the current state of the board
- * @param mycolor the color of the current player
- * @param mstr the input string to parse
- * @param move a pointer to object where the move data shall be stored
- * @return status code (see rules/rules.h for the list of codes)
- */
-static int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
-    memset(move, 0, sizeof(Move));
-    move->fromfile = POS_UNSPECIFIED;
-    move->fromrow = POS_UNSPECIFIED;
-
-    size_t len = strlen(mstr);
-    
-    /* 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;
-    }
-    
-    /* evaluate promotion */
-    if (len > 3 && mstr[len-2] == '=') {
-        move->promotion = getpiece(mstr[len-1]);
-        if (!move->promotion) {
-            return INVALID_MOVE_SYNTAX;
-        } else {
-            move->promotion |= mycolor;
-            len -= 2;
-            mstr[len] = 0;
-        }
-    }
-    
-    if (len == 2) {
-        /* pawn move (e.g. "e4") */
-        move->piece = PAWN;
-        move->tofile = fileidx(mstr[0]);
-        move->torow = rowidx(mstr[1]);
-    } else if (len == 3) {
-        if (strcmp(mstr, "O-O") == 0) {
-            /* king side castling */
-            move->piece = KING;
-            move->fromfile = fileidx('e');
-            move->tofile = fileidx('g');
-            move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
-        } else {
-            /* move (e.g. "Nf3") */
-            move->piece = getpiece(mstr[0]);
-            move->tofile = fileidx(mstr[1]);
-            move->torow = rowidx(mstr[2]);
-        }
-        
-    } else if (len == 4) {
-        move->piece = getpiece(mstr[0]);
-        if (!move->piece) {
-            move->piece = PAWN;
-            move->fromfile = fileidx(mstr[0]);
-        }
-        if (mstr[1] == 'x') {
-            /* capture (e.g. "Nxf3", "dxe5") */
-            move->capture = TRUE;
-        } else {
-            /* move (e.g. "Ndf3", "N2c3", "e2e4") */
-            if (isfile(mstr[1])) {
-                move->fromfile = fileidx(mstr[1]);
-                if (move->piece == PAWN) {
-                    move->piece = 0;
-                }
-            } else {
-                move->fromrow = rowidx(mstr[1]);
-            }
-        }
-        move->tofile = fileidx(mstr[2]);
-        move->torow = rowidx(mstr[3]);
-    } else if (len == 5) {
-        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 {
-            move->piece = getpiece(mstr[0]);
-            if (mstr[2] == 'x') {
-                move->capture = TRUE;
-                if (move->piece) {
-                    /* capture (e.g. "Ndxf3") */
-                    move->fromfile = fileidx(mstr[1]);
-                } else {
-                    /* long notation capture (e.g. "e5xf6") */
-                    move->piece = PAWN;
-                    move->fromfile = fileidx(mstr[0]);
-                    move->fromrow = rowidx(mstr[1]);
-                }
-            } else {
-                /* long notation move (e.g. "Nc5a4") */
-                move->fromfile = fileidx(mstr[1]);
-                move->fromrow = rowidx(mstr[2]);
-            }
-            move->tofile = fileidx(mstr[3]);
-            move->torow = rowidx(mstr[4]);
-        }
-    } else if (len == 6) {
-        /* long notation capture (e.g. "Nc5xf3") */
-        if (mstr[3] == 'x') {
-            move->capture = TRUE;
-            move->piece = getpiece(mstr[0]);
-            move->fromfile = fileidx(mstr[1]);
-            move->fromrow = rowidx(mstr[2]);
-            move->tofile = fileidx(mstr[4]);
-            move->torow = rowidx(mstr[5]);
-        }
-    }
-
-    
-    if (move->piece) {
-        if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0)
-            && !move->promotion) {
-            return NEED_PROMOTION;
-        }
-        
-        move->piece |= mycolor;
-        if (move->fromfile == POS_UNSPECIFIED
-            || move->fromrow == POS_UNSPECIFIED) {
-            return getlocation(board, move);
-        } else {
-            return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
-        }
-    } else {
-        return INVALID_MOVE_SYNTAX;
-    }
-}
 
 static int sendmove(Board board, MoveListRoot *movelist,
     uint8_t mycolor, int opponent) {
--- a/src/game.h	Sat Mar 29 16:53:58 2014 +0100
+++ b/src/game.h	Mon Mar 31 11:16:32 2014 +0200
@@ -30,54 +30,13 @@
 #ifndef GAME_H
 #define	GAME_H
 
+#include "chess/chess.h"
 #include "terminal-chess.h"
-#include <stdint.h>
 
 #ifdef	__cplusplus
 extern "C" {
 #endif
 
-#define PIECE_MASK       0x0F
-#define COLOR_MASK       0x30
-#define ENPASSANT_THREAT 0x40
-
-#define WHITE 0x10
-#define BLACK 0x20
-
-#define PAWN   0x01
-#define ROOK   0x02
-#define KNIGHT 0x03
-#define BISHOP 0x04
-#define QUEEN  0x05
-#define KING   0x06
-
-#define WPAWN   (WHITE|PAWN)
-#define WROOK   (WHITE|ROOK)
-#define WKNIGHT (WHITE|KNIGHT)
-#define WBISHOP (WHITE|BISHOP)
-#define WQUEEN  (WHITE|QUEEN)
-#define WKING   (WHITE|KING)
-#define BPAWN   (BLACK|PAWN)
-#define BROOK   (BLACK|ROOK)
-#define BKNIGHT (BLACK|KNIGHT)
-#define BBISHOP (BLACK|BISHOP)
-#define BQUEEN  (BLACK|QUEEN)
-#define BKING   (BLACK|KING)
-
-typedef uint8_t Board[8][8];
-
-typedef struct {
-    uint8_t piece;
-    uint8_t fromfile;
-    uint8_t fromrow;
-    uint8_t tofile;
-    uint8_t torow;
-    uint8_t promotion;
-    _Bool check;
-    _Bool checkmate;
-    _Bool capture;
-} Move;
-
 typedef struct MoveList MoveList;
 typedef struct MoveListRoot MoveListRoot;
 
@@ -91,27 +50,6 @@
     MoveList* next;
 };
 
-#define POS_UNSPECIFIED UINT8_MAX
-#define mdst(b,m) b[(m)->torow][(m)->tofile]
-#define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
-
-#define isidx(idx) ((uint8_t)idx < 8)
-
-#define isfile(file) (file >= 'a' && file <= 'h')
-#define isrow(row) (row >= '1' && row <= '8')
-
-#define rowidx(row) (row-'1')
-#define fileidx(file) (file-'a')
-
-#define rowchr(row) (row+'1')
-#define filechr(file) (file+'a')
-
-#define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
-        isidx((move)->tofile) && isidx((move)->torow))
-
-/* secure versions - use, if index is not checked with isidx() */
-#define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
-#define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
 
 void game_start(Settings *settings, int opponent);
 
--- a/src/rules/bishop.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,128 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "bishop.h"
-#include "rules.h"
-#include <math.h>
-
-_Bool bishop_chkrules(Move* move) {
-    return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile);
-}
-
-_Bool bishop_isblocked(Board board, Move *move) {
-    int dy = move->torow > move->fromrow ? 1 : -1;
-    int dx = move->tofile > move->fromfile ? 1 : -1;
-    
-    uint8_t y = move->fromrow;
-    uint8_t x = move->fromfile;
-    
-    do {
-        x += dx;
-        y += dy;
-        if (board[y][x]) {
-            return TRUE;
-        }
-    } while (x != move->tofile && y != move->torow);
-    
-    return FALSE;
-}
-
-static int bishop_getloc_fixedfile(Board board, Move *move) {
-    uint8_t d = abs(move->fromfile - move->tofile);
-    if (board[move->torow - d][move->fromfile] == move->piece) {
-        move->fromrow = move->torow - d;
-    }
-    if (board[move->torow + d][move->fromfile] == move->piece) {
-        if (move->fromrow == POS_UNSPECIFIED) {
-            move->fromrow = move->torow + d;
-        } else {
-            return AMBIGUOUS_MOVE; /* rare situation after promotion */
-        }
-    }
-    return move->fromrow == POS_UNSPECIFIED ?
-        INVALID_POSITION : VALID_MOVE_SYNTAX;
-}
-
-static int bishop_getloc_fixedrow(Board board, Move *move) {
-    uint8_t d = abs(move->fromrow - move->torow);
-    if (board[move->fromrow][move->tofile - d] == move->piece) {
-        move->fromfile = move->tofile - d;
-    }
-    if (board[move->fromrow][move->tofile + d] == move->piece) {
-        if (move->fromfile == POS_UNSPECIFIED) {
-            move->fromfile = move->tofile + d;
-        } else {
-            return AMBIGUOUS_MOVE; /* rare situation after promotion */
-        }
-    }
-    return move->fromfile == POS_UNSPECIFIED ?
-        INVALID_POSITION : VALID_MOVE_SYNTAX;
-}
-
-int bishop_getlocation(Board board, Move *move) {
-    
-    if (move->fromfile != POS_UNSPECIFIED) {
-        return bishop_getloc_fixedfile(board, move);
-    }
-    
-    if (move->fromrow != POS_UNSPECIFIED) {
-        return bishop_getloc_fixedrow(board, move);
-    }
-    
-    _Bool amb = FALSE;
-    for (int d = -7 ; d < 8  ; d++) {
-        uint8_t row = move->torow + d;
-        if (isidx(row)) {
-            uint8_t file = move->tofile + d;
-            if (isidx(file) && board[row][file] == move->piece) {
-                if (amb) {
-                    return AMBIGUOUS_MOVE;
-                }
-                amb = TRUE;
-                move->fromrow = row;
-                move->fromfile = file;
-            }
-            file = move->tofile - d;
-            if (isidx(file) && board[row][file] == move->piece) {
-                if (amb) {
-                    return AMBIGUOUS_MOVE;
-                }
-                amb = TRUE;
-                move->fromrow = row;
-                move->fromfile = file;
-            }
-        }
-    }
-    
-    if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) {
-        return INVALID_POSITION;
-    } else {
-        return VALID_MOVE_SYNTAX;
-    }
-}
--- a/src/rules/bishop.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef BISHOP_H
-#define	BISHOP_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool bishop_chkrules(Move *move);
-_Bool bishop_isblocked(Board board, Move *move);
-int bishop_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* BISHOP_H */
-
--- a/src/rules/king.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "rules.h"
-#include "king.h"
-
-_Bool king_chkrules(Board board, Move* move) {
-    // TODO: implement
-    return FALSE;
-}
-
-_Bool king_isblocked(Board board, Move *move) {
-    // TODO: implement
-    return TRUE;
-}
-
-int king_getlocation(Board board, Move *move) {
-    // TODO: implement
-    return INVALID_MOVE_SYNTAX;
-}
--- a/src/rules/king.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,49 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-
-#ifndef KING_H
-#define	KING_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool king_chkrules(Board board, Move *move);
-_Bool king_isblocked(Board board, Move *move);
-int king_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* KING_H */
-
--- a/src/rules/knight.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,133 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "knight.h"
-#include "rules.h"
-#include <math.h>
-
-_Bool knight_chkrules(Move *move) {
-    int dx = abs(move->fromfile - move->tofile);
-    int dy = abs(move->fromrow - move->torow);
-    
-    return (dx == 2 && dy == 1) || (dx == 1 && dy == 2);
-}
-
-static int knight_getloc_fixedrow(Board board, Move *move) {
-    int d = 3 - abs(move->fromrow - move->torow);
-    
-    if (d == 1 || d == 2) {
-        if (move->tofile < 6 &&
-            board[move->fromrow][move->tofile + d] == move->piece) {
-            if (move->fromfile == POS_UNSPECIFIED) {
-                move->fromfile = move->tofile + d;
-                return VALID_MOVE_SYNTAX;
-            } else {
-                return AMBIGUOUS_MOVE;
-            }
-        }
-        if (move->tofile > 1 &&
-            board[move->fromrow][move->tofile - d] == move->piece) {
-            if (move->fromfile == POS_UNSPECIFIED) {
-                move->fromfile = move->tofile - d;
-                return VALID_MOVE_SYNTAX;
-            } else {
-                return AMBIGUOUS_MOVE;
-            }
-        }
-    }
-    
-    return INVALID_POSITION;
-}
-
-static int knight_getloc_fixedfile(Board board, Move *move) {
-    int d = 3 - abs(move->fromfile - move->tofile);
-    
-    if (d == 1 || d == 2) {
-        if (move->torow < 6 &&
-            board[move->torow + d][move->fromfile] == move->piece) {
-            if (move->fromrow == POS_UNSPECIFIED) {
-                move->fromrow = move->torow + d;
-                return VALID_MOVE_SYNTAX;
-            } else {
-                return AMBIGUOUS_MOVE;
-            }
-        }
-        if (move->torow > 1 &&
-            board[move->torow - d][move->fromfile] == move->piece) {
-            if (move->fromrow == POS_UNSPECIFIED) {
-                move->fromrow = move->torow - d;
-                return VALID_MOVE_SYNTAX;
-            } else {
-                return AMBIGUOUS_MOVE;
-            }
-        }
-    }
-    
-    return INVALID_POSITION;
-}
-
-int knight_getlocation(Board board, Move *move) {
-    
-    if (move->fromfile != POS_UNSPECIFIED) {
-        return knight_getloc_fixedfile(board, move);
-    }
-    
-    if (move->fromrow != POS_UNSPECIFIED) {
-        return knight_getloc_fixedrow(board, move);
-    }
-    
-    for (int x = -2 ; x <= 2 ; x++) {
-        if (x == 0) {
-            continue;
-        }
-        for (int y = -2 ; y <= 2 ; y++) {
-            if (y == 0 || y == x) {
-                continue;
-            }
-            uint8_t cx = move->tofile + x;
-            uint8_t cy = move->torow + y;
-
-            if (isidx(cx) && isidx(cy) && board[cy][cx] == move->piece) {
-                if (move->fromfile == POS_UNSPECIFIED
-                    && move->fromrow == POS_UNSPECIFIED) {
-                    move->fromfile = cx;
-                    move->fromrow = cy;
-                } else {
-                    return AMBIGUOUS_MOVE;
-                }
-            }
-        }
-    }
-    
-    if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) {
-        return INVALID_POSITION;
-    } else {
-        return VALID_MOVE_SYNTAX;
-    }
-}
--- a/src/rules/knight.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef KNIGHT_H
-#define	KNIGHT_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool knight_chkrules(Move *move);
-#define knight_isblocked(b,m) FALSE
-int knight_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* KNIGHT_H */
-
--- a/src/rules/pawn.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "pawn.h"
-#include "rules.h"
-
-_Bool pawn_chkrules(Board board, Move *move) {
-    int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1);
-    
-    if (move->torow == (d < 0 ? 7 : 0)) {
-        if (move->promotion) {
-            uint8_t promopiece = move->promotion & PIECE_MASK;
-            if (!promopiece || promopiece == PAWN || promopiece == KING) {
-                return FALSE;
-            }
-        } else {
-            return FALSE;
-        }
-    } else {
-        if (move->promotion) {
-            return FALSE;
-        }
-    }
-    
-    if (move->capture) {
-        if (move->fromrow == move->torow + d && (
-            move->fromfile == move->tofile + 1 ||
-            move->fromfile == move->tofile - 1)) {
-
-            return mdst(board,move)
-                || (board[move->fromrow][move->tofile] & ENPASSANT_THREAT);
-        } else {
-            return FALSE;
-        }
-    } else {
-        if (move->fromfile == move->tofile) {
-            return (move->fromrow == move->torow + d) ||
-                (move->fromrow == (d < 0 ? 1 : 6) && /* advanced first move */
-                move->fromrow == move->torow + d*2);
-        } else {
-            return FALSE;
-        }
-    }
-}
-
-_Bool pawn_isblocked(Board board, Move *move) {
-    return mdst(board,move) && !move->capture;
-}
-
-int pawn_getlocation(Board board, Move *move) {
-    int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1);
-    
-    if (move->fromfile == POS_UNSPECIFIED) {
-        move->fromfile = move->tofile;
-    }
-    move->fromrow = move->torow + d;
-    if (move->fromrow > 6) {
-        return INVALID_POSITION;
-    } else {
-        /* advanced first move */
-        if (move->fromrow == (d < 0 ? 2 : 5) &&
-            msrc(board,move) != move->piece) {
-
-            move->fromrow += d;
-            if (move->fromrow > 6) {
-                return INVALID_POSITION;
-            }
-        }
-    }
-    return VALID_MOVE_SYNTAX;
-}
--- a/src/rules/pawn.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef PAWN_H
-#define	PAWN_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool pawn_chkrules(Board board, Move *move);
-_Bool pawn_isblocked(Board board, Move *move);
-int pawn_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* PAWN_H */
-
--- a/src/rules/queen.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "rules.h"
-#include "queen.h"
-
-_Bool queen_chkrules(Move* move) {
-    // TODO: implement
-    return FALSE;
-}
-
-_Bool queen_isblocked(Board board, Move *move) {
-    // TODO: implement
-    return TRUE;
-}
-
-int queen_getlocation(Board board, Move *move) {
-    // TODO: implement
-    return INVALID_MOVE_SYNTAX;
-}
--- a/src/rules/queen.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef QUEEN_H
-#define	QUEEN_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool queen_chkrules(Move *move);
-_Bool queen_isblocked(Board board, Move *move);
-int queen_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* QUEEN_H */
-
--- a/src/rules/rook.c	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,46 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#include "rules.h"
-#include "rook.h"
-
-_Bool rook_chkrules(Move *move) {
-    // TODO: implement
-    return FALSE;
-}
-
-_Bool rook_isblocked(Board board, Move *move) {
-    // TODO: implement
-    return TRUE;
-}
-
-int rook_getlocation(Board board, Move *move) {
-    // TODO: implement
-    return INVALID_MOVE_SYNTAX;
-}
--- a/src/rules/rook.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,48 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef ROOK_H
-#define	ROOK_H
-
-#include "../game.h"
-
-#ifdef	__cplusplus
-extern "C" {
-#endif
-
-_Bool rook_chkrules(Move *move);
-_Bool rook_isblocked(Board board, Move *move);
-int rook_getlocation(Board board, Move *move);
-
-#ifdef	__cplusplus
-}
-#endif
-
-#endif	/* ROOK_H */
-
--- a/src/rules/rules.h	Sat Mar 29 16:53:58 2014 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2014 Mike Becker. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- *
- */
-
-#ifndef RULES_H
-#define	RULES_H
-
-#include "pawn.h"
-#include "rook.h"
-#include "knight.h"
-#include "bishop.h"
-#include "queen.h"
-#include "king.h"
-
-#define VALID_MOVE_SYNTAX   0
-#define INVALID_MOVE_SYNTAX 1
-#define INVALID_POSITION    2
-#define AMBIGUOUS_MOVE      3
-#define NEED_PROMOTION      4
-
-#endif	/* RULES_H */
-

mercurial