Mon, 31 Mar 2014 11:16:32 +0200
moved chess rules to separate lib
1.1 --- a/Makefile Sat Mar 29 16:53:58 2014 +0100 1.2 +++ b/Makefile Mon Mar 31 11:16:32 2014 +0200 1.3 @@ -28,15 +28,14 @@ 1.4 1.5 include conf.mk 1.6 1.7 -all: build build/rules 1.8 +all: build chess 1.9 cd src; $(MAKE) 1.10 1.11 build: 1.12 $(MKDIR) build 1.13 1.14 -build/rules: 1.15 - $(MKDIR) build/rules 1.16 +chess: 1.17 + cd src/chess; $(MAKE) BUILDDIR=../../build 1.18 1.19 clean: 1.20 $(RM) -f -R build 1.21 - $(RM) -f -R build/rules
2.1 --- a/conf.mk Sat Mar 29 16:53:58 2014 +0100 2.2 +++ b/conf.mk Mon Mar 31 11:16:32 2014 +0200 2.3 @@ -26,14 +26,14 @@ 2.4 # POSSIBILITY OF SUCH DAMAGE. 2.5 # 2.6 2.7 -# system related 2.8 MKDIR = mkdir 2.9 RM = rm 2.10 2.11 -# build related 2.12 BIN = terminal-chess 2.13 CC = gcc 2.14 CFLAGS = -g -O2 -std=gnu99 -Wall -Werror -pedantic 2.15 LD = gcc 2.16 LDFLAGS = -lncurses 2.17 + 2.18 +LIB_EXT = .a 2.19 OBJ_EXT = .o
3.1 --- a/src/Makefile Sat Mar 29 16:53:58 2014 +0100 3.2 +++ b/src/Makefile Mon Mar 31 11:16:32 2014 +0200 3.3 @@ -35,17 +35,10 @@ 3.4 SRC += client.c 3.5 SRC += game.c 3.6 3.7 -SRC += rules/pawn.c 3.8 -SRC += rules/rook.c 3.9 -SRC += rules/knight.c 3.10 -SRC += rules/bishop.c 3.11 -SRC += rules/queen.c 3.12 -SRC += rules/king.c 3.13 - 3.14 OBJ = $(SRC:%.c=../build/%$(OBJ_EXT)) 3.15 3.16 all: $(OBJ) 3.17 - $(LD) -o ../build/$(BIN) $^ $(LDFLAGS) 3.18 + $(LD) -o ../build/$(BIN) $^ ../build/chess$(LIB_EXT) $(LDFLAGS) 3.19 3.20 3.21 ../build/%$(OBJ_EXT): %.c
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 4.2 +++ b/src/chess/Makefile Mon Mar 31 11:16:32 2014 +0200 4.3 @@ -0,0 +1,54 @@ 4.4 +# 4.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 4.6 +# 4.7 +# Copyright 2014 Mike Becker. All rights reserved. 4.8 +# 4.9 +# Redistribution and use in source and binary forms, with or without 4.10 +# modification, are permitted provided that the following conditions are met: 4.11 +# 4.12 +# 1. Redistributions of source code must retain the above copyright 4.13 +# notice, this list of conditions and the following disclaimer. 4.14 +# 4.15 +# 2. Redistributions in binary form must reproduce the above copyright 4.16 +# notice, this list of conditions and the following disclaimer in the 4.17 +# documentation and/or other materials provided with the distribution. 4.18 +# 4.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 4.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4.22 +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 4.23 +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 4.24 +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 4.25 +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 4.26 +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 4.27 +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4.28 +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 4.29 +# POSSIBILITY OF SUCH DAMAGE. 4.30 +# 4.31 + 4.32 +BUILDDIR = ../build 4.33 + 4.34 +include conf.mk 4.35 + 4.36 +SRC += pawn.c 4.37 +SRC += rook.c 4.38 +SRC += knight.c 4.39 +SRC += bishop.c 4.40 +SRC += queen.c 4.41 +SRC += king.c 4.42 +SRC += rules.c 4.43 + 4.44 +OBJ = $(SRC:%.c=$(BUILDDIR)/%$(OBJ_EXT)) 4.45 + 4.46 +all: $(OBJ) 4.47 + $(AR) $(ARFLAGS) $(BUILDDIR)/chess$(LIB_EXT) $(OBJ) 4.48 + 4.49 + 4.50 +$(BUILDDIR)/%$(OBJ_EXT): %.c 4.51 + $(CC) -o $@ $(CFLAGS) -c $< 4.52 + 4.53 +$(BUILDDIR): 4.54 + $(MKDIR) $(MKDIRFLAGS) $(BUILDDIR) 4.55 + 4.56 +clear: 4.57 + $(RM) $(RMFLAGS) $(BUILDDIR)
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/src/chess/bishop.c Mon Mar 31 11:16:32 2014 +0200 5.3 @@ -0,0 +1,128 @@ 5.4 +/* 5.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 5.6 + * 5.7 + * Copyright 2014 Mike Becker. All rights reserved. 5.8 + * 5.9 + * Redistribution and use in source and binary forms, with or without 5.10 + * modification, are permitted provided that the following conditions are met: 5.11 + * 5.12 + * 1. Redistributions of source code must retain the above copyright 5.13 + * notice, this list of conditions and the following disclaimer. 5.14 + * 5.15 + * 2. Redistributions in binary form must reproduce the above copyright 5.16 + * notice, this list of conditions and the following disclaimer in the 5.17 + * documentation and/or other materials provided with the distribution. 5.18 + * 5.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 5.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 5.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 5.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 5.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 5.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 5.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 5.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 5.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 5.29 + * POSSIBILITY OF SUCH DAMAGE. 5.30 + * 5.31 + */ 5.32 + 5.33 +#include "bishop.h" 5.34 +#include "rules.h" 5.35 +#include <stdlib.h> 5.36 + 5.37 +_Bool bishop_chkrules(Move* move) { 5.38 + return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile); 5.39 +} 5.40 + 5.41 +_Bool bishop_isblocked(Board board, Move *move) { 5.42 + int dy = move->torow > move->fromrow ? 1 : -1; 5.43 + int dx = move->tofile > move->fromfile ? 1 : -1; 5.44 + 5.45 + uint8_t y = move->fromrow; 5.46 + uint8_t x = move->fromfile; 5.47 + 5.48 + do { 5.49 + x += dx; 5.50 + y += dy; 5.51 + if (board[y][x]) { 5.52 + return 1; 5.53 + } 5.54 + } while (x != move->tofile && y != move->torow); 5.55 + 5.56 + return 0; 5.57 +} 5.58 + 5.59 +static int bishop_getloc_fixedfile(Board board, Move *move) { 5.60 + uint8_t d = abs(move->fromfile - move->tofile); 5.61 + if (board[move->torow - d][move->fromfile] == move->piece) { 5.62 + move->fromrow = move->torow - d; 5.63 + } 5.64 + if (board[move->torow + d][move->fromfile] == move->piece) { 5.65 + if (move->fromrow == POS_UNSPECIFIED) { 5.66 + move->fromrow = move->torow + d; 5.67 + } else { 5.68 + return AMBIGUOUS_MOVE; /* rare situation after promotion */ 5.69 + } 5.70 + } 5.71 + return move->fromrow == POS_UNSPECIFIED ? 5.72 + INVALID_POSITION : VALID_MOVE_SYNTAX; 5.73 +} 5.74 + 5.75 +static int bishop_getloc_fixedrow(Board board, Move *move) { 5.76 + uint8_t d = abs(move->fromrow - move->torow); 5.77 + if (board[move->fromrow][move->tofile - d] == move->piece) { 5.78 + move->fromfile = move->tofile - d; 5.79 + } 5.80 + if (board[move->fromrow][move->tofile + d] == move->piece) { 5.81 + if (move->fromfile == POS_UNSPECIFIED) { 5.82 + move->fromfile = move->tofile + d; 5.83 + } else { 5.84 + return AMBIGUOUS_MOVE; /* rare situation after promotion */ 5.85 + } 5.86 + } 5.87 + return move->fromfile == POS_UNSPECIFIED ? 5.88 + INVALID_POSITION : VALID_MOVE_SYNTAX; 5.89 +} 5.90 + 5.91 +int bishop_getlocation(Board board, Move *move) { 5.92 + 5.93 + if (move->fromfile != POS_UNSPECIFIED) { 5.94 + return bishop_getloc_fixedfile(board, move); 5.95 + } 5.96 + 5.97 + if (move->fromrow != POS_UNSPECIFIED) { 5.98 + return bishop_getloc_fixedrow(board, move); 5.99 + } 5.100 + 5.101 + _Bool amb = 0; 5.102 + for (int d = -7 ; d < 8 ; d++) { 5.103 + uint8_t row = move->torow + d; 5.104 + if (isidx(row)) { 5.105 + uint8_t file = move->tofile + d; 5.106 + if (isidx(file) && board[row][file] == move->piece) { 5.107 + if (amb) { 5.108 + return AMBIGUOUS_MOVE; 5.109 + } 5.110 + amb = 1; 5.111 + move->fromrow = row; 5.112 + move->fromfile = file; 5.113 + } 5.114 + file = move->tofile - d; 5.115 + if (isidx(file) && board[row][file] == move->piece) { 5.116 + if (amb) { 5.117 + return AMBIGUOUS_MOVE; 5.118 + } 5.119 + amb = 1; 5.120 + move->fromrow = row; 5.121 + move->fromfile = file; 5.122 + } 5.123 + } 5.124 + } 5.125 + 5.126 + if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) { 5.127 + return INVALID_POSITION; 5.128 + } else { 5.129 + return VALID_MOVE_SYNTAX; 5.130 + } 5.131 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/src/chess/bishop.h Mon Mar 31 11:16:32 2014 +0200 6.3 @@ -0,0 +1,48 @@ 6.4 +/* 6.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 6.6 + * 6.7 + * Copyright 2014 Mike Becker. All rights reserved. 6.8 + * 6.9 + * Redistribution and use in source and binary forms, with or without 6.10 + * modification, are permitted provided that the following conditions are met: 6.11 + * 6.12 + * 1. Redistributions of source code must retain the above copyright 6.13 + * notice, this list of conditions and the following disclaimer. 6.14 + * 6.15 + * 2. Redistributions in binary form must reproduce the above copyright 6.16 + * notice, this list of conditions and the following disclaimer in the 6.17 + * documentation and/or other materials provided with the distribution. 6.18 + * 6.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 6.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 6.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 6.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 6.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 6.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 6.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 6.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 6.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 6.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 6.29 + * POSSIBILITY OF SUCH DAMAGE. 6.30 + * 6.31 + */ 6.32 + 6.33 +#ifndef BISHOP_H 6.34 +#define BISHOP_H 6.35 + 6.36 +#include "rules.h" 6.37 + 6.38 +#ifdef __cplusplus 6.39 +extern "C" { 6.40 +#endif 6.41 + 6.42 +_Bool bishop_chkrules(Move *move); 6.43 +_Bool bishop_isblocked(Board board, Move *move); 6.44 +int bishop_getlocation(Board board, Move *move); 6.45 + 6.46 +#ifdef __cplusplus 6.47 +} 6.48 +#endif 6.49 + 6.50 +#endif /* BISHOP_H */ 6.51 +
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 7.2 +++ b/src/chess/chess.h Mon Mar 31 11:16:32 2014 +0200 7.3 @@ -0,0 +1,36 @@ 7.4 +/* 7.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7.6 + * 7.7 + * Copyright 2014 Mike Becker. All rights reserved. 7.8 + * 7.9 + * Redistribution and use in source and binary forms, with or without 7.10 + * modification, are permitted provided that the following conditions are met: 7.11 + * 7.12 + * 1. Redistributions of source code must retain the above copyright 7.13 + * notice, this list of conditions and the following disclaimer. 7.14 + * 7.15 + * 2. Redistributions in binary form must reproduce the above copyright 7.16 + * notice, this list of conditions and the following disclaimer in the 7.17 + * documentation and/or other materials provided with the distribution. 7.18 + * 7.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 7.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 7.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 7.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 7.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 7.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 7.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 7.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 7.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 7.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 7.29 + * POSSIBILITY OF SUCH DAMAGE. 7.30 + * 7.31 + */ 7.32 + 7.33 +#include "rules.h" 7.34 +#include "pawn.h" 7.35 +#include "rook.h" 7.36 +#include "knight.h" 7.37 +#include "bishop.h" 7.38 +#include "queen.h" 7.39 +#include "king.h" 7.40 \ No newline at end of file
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/src/chess/conf.mk Mon Mar 31 11:16:32 2014 +0200 8.3 @@ -0,0 +1,40 @@ 8.4 +# 8.5 +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 8.6 +# 8.7 +# Copyright 2014 Mike Becker. All rights reserved. 8.8 +# 8.9 +# Redistribution and use in source and binary forms, with or without 8.10 +# modification, are permitted provided that the following conditions are met: 8.11 +# 8.12 +# 1. Redistributions of source code must retain the above copyright 8.13 +# notice, this list of conditions and the following disclaimer. 8.14 +# 8.15 +# 2. Redistributions in binary form must reproduce the above copyright 8.16 +# notice, this list of conditions and the following disclaimer in the 8.17 +# documentation and/or other materials provided with the distribution. 8.18 +# 8.19 +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 8.20 +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 8.21 +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 8.22 +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 8.23 +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 8.24 +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 8.25 +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 8.26 +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 8.27 +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 8.28 +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 8.29 +# POSSIBILITY OF SUCH DAMAGE. 8.30 +# 8.31 + 8.32 +MKDIR = mkdir 8.33 +RM = rm 8.34 +AR = ar 8.35 + 8.36 +CC = gcc 8.37 +CFLAGS = -g -O2 -std=gnu99 -Wall -Werror -pedantic 8.38 +ARFLAGS = -r 8.39 +MKDIRFLAGS = -p 8.40 +RMFLAGS = -f -R 8.41 + 8.42 +OBJ_EXT = .o 8.43 +LIB_EXT = .a
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/src/chess/king.c Mon Mar 31 11:16:32 2014 +0200 9.3 @@ -0,0 +1,46 @@ 9.4 +/* 9.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 9.6 + * 9.7 + * Copyright 2014 Mike Becker. All rights reserved. 9.8 + * 9.9 + * Redistribution and use in source and binary forms, with or without 9.10 + * modification, are permitted provided that the following conditions are met: 9.11 + * 9.12 + * 1. Redistributions of source code must retain the above copyright 9.13 + * notice, this list of conditions and the following disclaimer. 9.14 + * 9.15 + * 2. Redistributions in binary form must reproduce the above copyright 9.16 + * notice, this list of conditions and the following disclaimer in the 9.17 + * documentation and/or other materials provided with the distribution. 9.18 + * 9.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 9.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 9.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 9.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 9.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 9.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 9.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 9.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 9.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 9.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 9.29 + * POSSIBILITY OF SUCH DAMAGE. 9.30 + * 9.31 + */ 9.32 + 9.33 +#include "rules.h" 9.34 +#include "king.h" 9.35 + 9.36 +_Bool king_chkrules(Board board, Move* move) { 9.37 + // TODO: implement 9.38 + return 0; 9.39 +} 9.40 + 9.41 +_Bool king_isblocked(Board board, Move *move) { 9.42 + // TODO: implement 9.43 + return 1; 9.44 +} 9.45 + 9.46 +int king_getlocation(Board board, Move *move) { 9.47 + // TODO: implement 9.48 + return INVALID_MOVE_SYNTAX; 9.49 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/src/chess/king.h Mon Mar 31 11:16:32 2014 +0200 10.3 @@ -0,0 +1,49 @@ 10.4 +/* 10.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 10.6 + * 10.7 + * Copyright 2014 Mike Becker. All rights reserved. 10.8 + * 10.9 + * Redistribution and use in source and binary forms, with or without 10.10 + * modification, are permitted provided that the following conditions are met: 10.11 + * 10.12 + * 1. Redistributions of source code must retain the above copyright 10.13 + * notice, this list of conditions and the following disclaimer. 10.14 + * 10.15 + * 2. Redistributions in binary form must reproduce the above copyright 10.16 + * notice, this list of conditions and the following disclaimer in the 10.17 + * documentation and/or other materials provided with the distribution. 10.18 + * 10.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 10.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 10.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 10.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 10.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 10.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 10.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 10.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 10.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 10.29 + * POSSIBILITY OF SUCH DAMAGE. 10.30 + * 10.31 + */ 10.32 + 10.33 + 10.34 +#ifndef KING_H 10.35 +#define KING_H 10.36 + 10.37 +#include "rules.h" 10.38 + 10.39 +#ifdef __cplusplus 10.40 +extern "C" { 10.41 +#endif 10.42 + 10.43 +_Bool king_chkrules(Board board, Move *move); 10.44 +_Bool king_isblocked(Board board, Move *move); 10.45 +int king_getlocation(Board board, Move *move); 10.46 + 10.47 +#ifdef __cplusplus 10.48 +} 10.49 +#endif 10.50 + 10.51 +#endif /* KING_H */ 10.52 +
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/src/chess/knight.c Mon Mar 31 11:16:32 2014 +0200 11.3 @@ -0,0 +1,133 @@ 11.4 +/* 11.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 11.6 + * 11.7 + * Copyright 2014 Mike Becker. All rights reserved. 11.8 + * 11.9 + * Redistribution and use in source and binary forms, with or without 11.10 + * modification, are permitted provided that the following conditions are met: 11.11 + * 11.12 + * 1. Redistributions of source code must retain the above copyright 11.13 + * notice, this list of conditions and the following disclaimer. 11.14 + * 11.15 + * 2. Redistributions in binary form must reproduce the above copyright 11.16 + * notice, this list of conditions and the following disclaimer in the 11.17 + * documentation and/or other materials provided with the distribution. 11.18 + * 11.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 11.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 11.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 11.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 11.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 11.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 11.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 11.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 11.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 11.29 + * POSSIBILITY OF SUCH DAMAGE. 11.30 + * 11.31 + */ 11.32 + 11.33 +#include "knight.h" 11.34 +#include "rules.h" 11.35 +#include <stdlib.h> 11.36 + 11.37 +_Bool knight_chkrules(Move *move) { 11.38 + int dx = abs(move->fromfile - move->tofile); 11.39 + int dy = abs(move->fromrow - move->torow); 11.40 + 11.41 + return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); 11.42 +} 11.43 + 11.44 +static int knight_getloc_fixedrow(Board board, Move *move) { 11.45 + int d = 3 - abs(move->fromrow - move->torow); 11.46 + 11.47 + if (d == 1 || d == 2) { 11.48 + if (move->tofile < 6 && 11.49 + board[move->fromrow][move->tofile + d] == move->piece) { 11.50 + if (move->fromfile == POS_UNSPECIFIED) { 11.51 + move->fromfile = move->tofile + d; 11.52 + return VALID_MOVE_SYNTAX; 11.53 + } else { 11.54 + return AMBIGUOUS_MOVE; 11.55 + } 11.56 + } 11.57 + if (move->tofile > 1 && 11.58 + board[move->fromrow][move->tofile - d] == move->piece) { 11.59 + if (move->fromfile == POS_UNSPECIFIED) { 11.60 + move->fromfile = move->tofile - d; 11.61 + return VALID_MOVE_SYNTAX; 11.62 + } else { 11.63 + return AMBIGUOUS_MOVE; 11.64 + } 11.65 + } 11.66 + } 11.67 + 11.68 + return INVALID_POSITION; 11.69 +} 11.70 + 11.71 +static int knight_getloc_fixedfile(Board board, Move *move) { 11.72 + int d = 3 - abs(move->fromfile - move->tofile); 11.73 + 11.74 + if (d == 1 || d == 2) { 11.75 + if (move->torow < 6 && 11.76 + board[move->torow + d][move->fromfile] == move->piece) { 11.77 + if (move->fromrow == POS_UNSPECIFIED) { 11.78 + move->fromrow = move->torow + d; 11.79 + return VALID_MOVE_SYNTAX; 11.80 + } else { 11.81 + return AMBIGUOUS_MOVE; 11.82 + } 11.83 + } 11.84 + if (move->torow > 1 && 11.85 + board[move->torow - d][move->fromfile] == move->piece) { 11.86 + if (move->fromrow == POS_UNSPECIFIED) { 11.87 + move->fromrow = move->torow - d; 11.88 + return VALID_MOVE_SYNTAX; 11.89 + } else { 11.90 + return AMBIGUOUS_MOVE; 11.91 + } 11.92 + } 11.93 + } 11.94 + 11.95 + return INVALID_POSITION; 11.96 +} 11.97 + 11.98 +int knight_getlocation(Board board, Move *move) { 11.99 + 11.100 + if (move->fromfile != POS_UNSPECIFIED) { 11.101 + return knight_getloc_fixedfile(board, move); 11.102 + } 11.103 + 11.104 + if (move->fromrow != POS_UNSPECIFIED) { 11.105 + return knight_getloc_fixedrow(board, move); 11.106 + } 11.107 + 11.108 + for (int x = -2 ; x <= 2 ; x++) { 11.109 + if (x == 0) { 11.110 + continue; 11.111 + } 11.112 + for (int y = -2 ; y <= 2 ; y++) { 11.113 + if (y == 0 || y == x) { 11.114 + continue; 11.115 + } 11.116 + uint8_t cx = move->tofile + x; 11.117 + uint8_t cy = move->torow + y; 11.118 + 11.119 + if (isidx(cx) && isidx(cy) && board[cy][cx] == move->piece) { 11.120 + if (move->fromfile == POS_UNSPECIFIED 11.121 + && move->fromrow == POS_UNSPECIFIED) { 11.122 + move->fromfile = cx; 11.123 + move->fromrow = cy; 11.124 + } else { 11.125 + return AMBIGUOUS_MOVE; 11.126 + } 11.127 + } 11.128 + } 11.129 + } 11.130 + 11.131 + if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) { 11.132 + return INVALID_POSITION; 11.133 + } else { 11.134 + return VALID_MOVE_SYNTAX; 11.135 + } 11.136 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/src/chess/knight.h Mon Mar 31 11:16:32 2014 +0200 12.3 @@ -0,0 +1,48 @@ 12.4 +/* 12.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 12.6 + * 12.7 + * Copyright 2014 Mike Becker. All rights reserved. 12.8 + * 12.9 + * Redistribution and use in source and binary forms, with or without 12.10 + * modification, are permitted provided that the following conditions are met: 12.11 + * 12.12 + * 1. Redistributions of source code must retain the above copyright 12.13 + * notice, this list of conditions and the following disclaimer. 12.14 + * 12.15 + * 2. Redistributions in binary form must reproduce the above copyright 12.16 + * notice, this list of conditions and the following disclaimer in the 12.17 + * documentation and/or other materials provided with the distribution. 12.18 + * 12.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 12.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 12.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 12.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 12.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 12.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 12.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 12.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 12.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 12.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 12.29 + * POSSIBILITY OF SUCH DAMAGE. 12.30 + * 12.31 + */ 12.32 + 12.33 +#ifndef KNIGHT_H 12.34 +#define KNIGHT_H 12.35 + 12.36 +#include "rules.h" 12.37 + 12.38 +#ifdef __cplusplus 12.39 +extern "C" { 12.40 +#endif 12.41 + 12.42 +_Bool knight_chkrules(Move *move); 12.43 +#define knight_isblocked(b,m) 0 12.44 +int knight_getlocation(Board board, Move *move); 12.45 + 12.46 +#ifdef __cplusplus 12.47 +} 12.48 +#endif 12.49 + 12.50 +#endif /* KNIGHT_H */ 12.51 +
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/src/chess/pawn.c Mon Mar 31 11:16:32 2014 +0200 13.3 @@ -0,0 +1,97 @@ 13.4 +/* 13.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 13.6 + * 13.7 + * Copyright 2014 Mike Becker. All rights reserved. 13.8 + * 13.9 + * Redistribution and use in source and binary forms, with or without 13.10 + * modification, are permitted provided that the following conditions are met: 13.11 + * 13.12 + * 1. Redistributions of source code must retain the above copyright 13.13 + * notice, this list of conditions and the following disclaimer. 13.14 + * 13.15 + * 2. Redistributions in binary form must reproduce the above copyright 13.16 + * notice, this list of conditions and the following disclaimer in the 13.17 + * documentation and/or other materials provided with the distribution. 13.18 + * 13.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 13.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 13.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 13.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 13.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 13.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 13.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 13.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 13.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 13.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 13.29 + * POSSIBILITY OF SUCH DAMAGE. 13.30 + * 13.31 + */ 13.32 + 13.33 +#include "pawn.h" 13.34 +#include "rules.h" 13.35 + 13.36 +_Bool pawn_chkrules(Board board, Move *move) { 13.37 + int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1); 13.38 + 13.39 + if (move->torow == (d < 0 ? 7 : 0)) { 13.40 + if (move->promotion) { 13.41 + uint8_t promopiece = move->promotion & PIECE_MASK; 13.42 + if (!promopiece || promopiece == PAWN || promopiece == KING) { 13.43 + return 0; 13.44 + } 13.45 + } else { 13.46 + return 0; 13.47 + } 13.48 + } else { 13.49 + if (move->promotion) { 13.50 + return 0; 13.51 + } 13.52 + } 13.53 + 13.54 + if (move->capture) { 13.55 + if (move->fromrow == move->torow + d && ( 13.56 + move->fromfile == move->tofile + 1 || 13.57 + move->fromfile == move->tofile - 1)) { 13.58 + 13.59 + return mdst(board,move) 13.60 + || (board[move->fromrow][move->tofile] & ENPASSANT_THREAT); 13.61 + } else { 13.62 + return 0; 13.63 + } 13.64 + } else { 13.65 + if (move->fromfile == move->tofile) { 13.66 + return (move->fromrow == move->torow + d) || 13.67 + (move->fromrow == (d < 0 ? 1 : 6) && /* advanced first move */ 13.68 + move->fromrow == move->torow + d*2); 13.69 + } else { 13.70 + return 0; 13.71 + } 13.72 + } 13.73 +} 13.74 + 13.75 +_Bool pawn_isblocked(Board board, Move *move) { 13.76 + return mdst(board,move) && !move->capture; 13.77 +} 13.78 + 13.79 +int pawn_getlocation(Board board, Move *move) { 13.80 + int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1); 13.81 + 13.82 + if (move->fromfile == POS_UNSPECIFIED) { 13.83 + move->fromfile = move->tofile; 13.84 + } 13.85 + move->fromrow = move->torow + d; 13.86 + if (move->fromrow > 6) { 13.87 + return INVALID_POSITION; 13.88 + } else { 13.89 + /* advanced first move */ 13.90 + if (move->fromrow == (d < 0 ? 2 : 5) && 13.91 + msrc(board,move) != move->piece) { 13.92 + 13.93 + move->fromrow += d; 13.94 + if (move->fromrow > 6) { 13.95 + return INVALID_POSITION; 13.96 + } 13.97 + } 13.98 + } 13.99 + return VALID_MOVE_SYNTAX; 13.100 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/src/chess/pawn.h Mon Mar 31 11:16:32 2014 +0200 14.3 @@ -0,0 +1,48 @@ 14.4 +/* 14.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 14.6 + * 14.7 + * Copyright 2014 Mike Becker. All rights reserved. 14.8 + * 14.9 + * Redistribution and use in source and binary forms, with or without 14.10 + * modification, are permitted provided that the following conditions are met: 14.11 + * 14.12 + * 1. Redistributions of source code must retain the above copyright 14.13 + * notice, this list of conditions and the following disclaimer. 14.14 + * 14.15 + * 2. Redistributions in binary form must reproduce the above copyright 14.16 + * notice, this list of conditions and the following disclaimer in the 14.17 + * documentation and/or other materials provided with the distribution. 14.18 + * 14.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 14.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 14.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 14.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 14.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 14.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 14.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 14.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 14.29 + * POSSIBILITY OF SUCH DAMAGE. 14.30 + * 14.31 + */ 14.32 + 14.33 +#ifndef PAWN_H 14.34 +#define PAWN_H 14.35 + 14.36 +#include "rules.h" 14.37 + 14.38 +#ifdef __cplusplus 14.39 +extern "C" { 14.40 +#endif 14.41 + 14.42 +_Bool pawn_chkrules(Board board, Move *move); 14.43 +_Bool pawn_isblocked(Board board, Move *move); 14.44 +int pawn_getlocation(Board board, Move *move); 14.45 + 14.46 +#ifdef __cplusplus 14.47 +} 14.48 +#endif 14.49 + 14.50 +#endif /* PAWN_H */ 14.51 +
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/src/chess/queen.c Mon Mar 31 11:16:32 2014 +0200 15.3 @@ -0,0 +1,46 @@ 15.4 +/* 15.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 15.6 + * 15.7 + * Copyright 2014 Mike Becker. All rights reserved. 15.8 + * 15.9 + * Redistribution and use in source and binary forms, with or without 15.10 + * modification, are permitted provided that the following conditions are met: 15.11 + * 15.12 + * 1. Redistributions of source code must retain the above copyright 15.13 + * notice, this list of conditions and the following disclaimer. 15.14 + * 15.15 + * 2. Redistributions in binary form must reproduce the above copyright 15.16 + * notice, this list of conditions and the following disclaimer in the 15.17 + * documentation and/or other materials provided with the distribution. 15.18 + * 15.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 15.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 15.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 15.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 15.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 15.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 15.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 15.29 + * POSSIBILITY OF SUCH DAMAGE. 15.30 + * 15.31 + */ 15.32 + 15.33 +#include "rules.h" 15.34 +#include "queen.h" 15.35 + 15.36 +_Bool queen_chkrules(Move* move) { 15.37 + // TODO: implement 15.38 + return 0; 15.39 +} 15.40 + 15.41 +_Bool queen_isblocked(Board board, Move *move) { 15.42 + // TODO: implement 15.43 + return 1; 15.44 +} 15.45 + 15.46 +int queen_getlocation(Board board, Move *move) { 15.47 + // TODO: implement 15.48 + return INVALID_MOVE_SYNTAX; 15.49 +}
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/src/chess/queen.h Mon Mar 31 11:16:32 2014 +0200 16.3 @@ -0,0 +1,48 @@ 16.4 +/* 16.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 16.6 + * 16.7 + * Copyright 2014 Mike Becker. All rights reserved. 16.8 + * 16.9 + * Redistribution and use in source and binary forms, with or without 16.10 + * modification, are permitted provided that the following conditions are met: 16.11 + * 16.12 + * 1. Redistributions of source code must retain the above copyright 16.13 + * notice, this list of conditions and the following disclaimer. 16.14 + * 16.15 + * 2. Redistributions in binary form must reproduce the above copyright 16.16 + * notice, this list of conditions and the following disclaimer in the 16.17 + * documentation and/or other materials provided with the distribution. 16.18 + * 16.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 16.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 16.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 16.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 16.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 16.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 16.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 16.29 + * POSSIBILITY OF SUCH DAMAGE. 16.30 + * 16.31 + */ 16.32 + 16.33 +#ifndef QUEEN_H 16.34 +#define QUEEN_H 16.35 + 16.36 +#include "rules.h" 16.37 + 16.38 +#ifdef __cplusplus 16.39 +extern "C" { 16.40 +#endif 16.41 + 16.42 +_Bool queen_chkrules(Move *move); 16.43 +_Bool queen_isblocked(Board board, Move *move); 16.44 +int queen_getlocation(Board board, Move *move); 16.45 + 16.46 +#ifdef __cplusplus 16.47 +} 16.48 +#endif 16.49 + 16.50 +#endif /* QUEEN_H */ 16.51 +
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/src/chess/rook.c Mon Mar 31 11:16:32 2014 +0200 17.3 @@ -0,0 +1,46 @@ 17.4 +/* 17.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 17.6 + * 17.7 + * Copyright 2014 Mike Becker. All rights reserved. 17.8 + * 17.9 + * Redistribution and use in source and binary forms, with or without 17.10 + * modification, are permitted provided that the following conditions are met: 17.11 + * 17.12 + * 1. Redistributions of source code must retain the above copyright 17.13 + * notice, this list of conditions and the following disclaimer. 17.14 + * 17.15 + * 2. Redistributions in binary form must reproduce the above copyright 17.16 + * notice, this list of conditions and the following disclaimer in the 17.17 + * documentation and/or other materials provided with the distribution. 17.18 + * 17.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 17.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 17.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 17.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 17.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 17.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 17.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 17.29 + * POSSIBILITY OF SUCH DAMAGE. 17.30 + * 17.31 + */ 17.32 + 17.33 +#include "rules.h" 17.34 +#include "rook.h" 17.35 + 17.36 +_Bool rook_chkrules(Move *move) { 17.37 + // TODO: implement 17.38 + return 0; 17.39 +} 17.40 + 17.41 +_Bool rook_isblocked(Board board, Move *move) { 17.42 + // TODO: implement 17.43 + return 1; 17.44 +} 17.45 + 17.46 +int rook_getlocation(Board board, Move *move) { 17.47 + // TODO: implement 17.48 + return INVALID_MOVE_SYNTAX; 17.49 +}
18.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 18.2 +++ b/src/chess/rook.h Mon Mar 31 11:16:32 2014 +0200 18.3 @@ -0,0 +1,48 @@ 18.4 +/* 18.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 18.6 + * 18.7 + * Copyright 2014 Mike Becker. All rights reserved. 18.8 + * 18.9 + * Redistribution and use in source and binary forms, with or without 18.10 + * modification, are permitted provided that the following conditions are met: 18.11 + * 18.12 + * 1. Redistributions of source code must retain the above copyright 18.13 + * notice, this list of conditions and the following disclaimer. 18.14 + * 18.15 + * 2. Redistributions in binary form must reproduce the above copyright 18.16 + * notice, this list of conditions and the following disclaimer in the 18.17 + * documentation and/or other materials provided with the distribution. 18.18 + * 18.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 18.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 18.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 18.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 18.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 18.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 18.29 + * POSSIBILITY OF SUCH DAMAGE. 18.30 + * 18.31 + */ 18.32 + 18.33 +#ifndef ROOK_H 18.34 +#define ROOK_H 18.35 + 18.36 +#include "rules.h" 18.37 + 18.38 +#ifdef __cplusplus 18.39 +extern "C" { 18.40 +#endif 18.41 + 18.42 +_Bool rook_chkrules(Move *move); 18.43 +_Bool rook_isblocked(Board board, Move *move); 18.44 +int rook_getlocation(Board board, Move *move); 18.45 + 18.46 +#ifdef __cplusplus 18.47 +} 18.48 +#endif 18.49 + 18.50 +#endif /* ROOK_H */ 18.51 +
19.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 19.2 +++ b/src/chess/rules.c Mon Mar 31 11:16:32 2014 +0200 19.3 @@ -0,0 +1,303 @@ 19.4 +/* 19.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 19.6 + * 19.7 + * Copyright 2014 Mike Becker. All rights reserved. 19.8 + * 19.9 + * Redistribution and use in source and binary forms, with or without 19.10 + * modification, are permitted provided that the following conditions are met: 19.11 + * 19.12 + * 1. Redistributions of source code must retain the above copyright 19.13 + * notice, this list of conditions and the following disclaimer. 19.14 + * 19.15 + * 2. Redistributions in binary form must reproduce the above copyright 19.16 + * notice, this list of conditions and the following disclaimer in the 19.17 + * documentation and/or other materials provided with the distribution. 19.18 + * 19.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 19.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 19.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 19.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 19.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 19.29 + * POSSIBILITY OF SUCH DAMAGE. 19.30 + * 19.31 + */ 19.32 + 19.33 +#include "rules.h" 19.34 +#include "chess.h" 19.35 +#include <string.h> 19.36 + 19.37 +char getpiecechr(uint8_t piece) { 19.38 + switch (piece & PIECE_MASK) { 19.39 + case ROOK: return 'R'; 19.40 + case KNIGHT: return 'N'; 19.41 + case BISHOP: return 'B'; 19.42 + case QUEEN: return 'Q'; 19.43 + case KING: return 'K'; 19.44 + default: return '\0'; 19.45 + } 19.46 +} 19.47 + 19.48 +uint8_t getpiece(char c) { 19.49 + switch (c) { 19.50 + case 'R': return ROOK; 19.51 + case 'N': return KNIGHT; 19.52 + case 'B': return BISHOP; 19.53 + case 'Q': return QUEEN; 19.54 + case 'K': return KING; 19.55 + default: return 0; 19.56 + } 19.57 +} 19.58 + 19.59 +/** 19.60 + * Guesses the location of a piece for short algebraic notation. 19.61 + * 19.62 + * @param board the current state of the board 19.63 + * @param move the move date to operate on 19.64 + * @return status code (see rules/rules.h for the codes) 19.65 + */ 19.66 +static int getlocation(Board board, Move *move) { 19.67 + uint8_t piece = move->piece & PIECE_MASK; 19.68 + switch (piece) { 19.69 + case PAWN: return pawn_getlocation(board, move); 19.70 + case ROOK: return rook_getlocation(board, move); 19.71 + case KNIGHT: return knight_getlocation(board, move); 19.72 + case BISHOP: return bishop_getlocation(board, move); 19.73 + case QUEEN: return queen_getlocation(board, move); 19.74 + case KING: return king_getlocation(board, move); 19.75 + default: return INVALID_MOVE_SYNTAX; 19.76 + } 19.77 +} 19.78 + 19.79 + 19.80 +void apply_move(Board board, Move *move) { 19.81 + uint8_t piece = move->piece & PIECE_MASK; 19.82 + uint8_t color = move->piece & COLOR_MASK; 19.83 + 19.84 + /* en passant capture */ 19.85 + if (move->capture && piece == PAWN && 19.86 + mdst(board, move) == 0) { 19.87 + board[move->fromrow][move->tofile] = 0; 19.88 + } 19.89 + 19.90 + /* remove old en passant threats */ 19.91 + for (uint8_t file = 0 ; file < 8 ; file++) { 19.92 + board[3][file] &= ~ENPASSANT_THREAT; 19.93 + board[4][file] &= ~ENPASSANT_THREAT; 19.94 + } 19.95 + 19.96 + /* add new en passant threat */ 19.97 + if (piece == PAWN && ( 19.98 + (move->fromrow == 1 && move->torow == 3) || 19.99 + (move->fromrow == 6 && move->torow == 4))) { 19.100 + move->piece |= ENPASSANT_THREAT; 19.101 + } 19.102 + 19.103 + /* move (and maybe capture or promote) */ 19.104 + msrc(board, move) = 0; 19.105 + if (move->promotion) { 19.106 + mdst(board, move) = move->promotion; 19.107 + } else { 19.108 + mdst(board, move) = move->piece; 19.109 + } 19.110 + 19.111 + /* castling */ 19.112 + if (piece == KING && 19.113 + move->fromfile == fileidx('e')) { 19.114 + 19.115 + if (move->tofile == fileidx('g')) { 19.116 + board[move->torow][fileidx('h')] = 0; 19.117 + board[move->torow][fileidx('f')] = color|ROOK; 19.118 + } else if (move->tofile == fileidx('c')) { 19.119 + board[move->torow][fileidx('a')] = 0; 19.120 + board[move->torow][fileidx('d')] = color|ROOK; 19.121 + } 19.122 + } 19.123 +} 19.124 + 19.125 +_Bool validate_move(Board board, Move *move) { 19.126 + _Bool result; 19.127 + 19.128 + /* validate indices (don't trust opponent) */ 19.129 + if (!chkidx(move)) { 19.130 + return 0; 19.131 + } 19.132 + 19.133 + /* does piece exist */ 19.134 + result = msrc(board, move) == move->piece; 19.135 + 19.136 + /* can't capture own pieces */ 19.137 + if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) { 19.138 + return 0; 19.139 + } 19.140 + 19.141 + /* validate individual rules */ 19.142 + switch (move->piece & PIECE_MASK) { 19.143 + case PAWN: 19.144 + result = result && pawn_chkrules(board, move); 19.145 + result = result && !pawn_isblocked(board, move); 19.146 + break; 19.147 + case ROOK: 19.148 + result = result && rook_chkrules(move); 19.149 + result = result && !rook_isblocked(board, move); 19.150 + break; 19.151 + case KNIGHT: 19.152 + result = result && knight_chkrules(move); 19.153 + result = result && !knight_isblocked(board, move); 19.154 + break; 19.155 + case BISHOP: 19.156 + result = result && bishop_chkrules(move); 19.157 + result = result && !bishop_isblocked(board, move); 19.158 + break; 19.159 + case QUEEN: 19.160 + result = result && queen_chkrules(move); 19.161 + result = result && !queen_isblocked(board, move); 19.162 + break; 19.163 + case KING: 19.164 + result = result && king_chkrules(board, move); 19.165 + result = result && !king_isblocked(board, move); 19.166 + break; 19.167 + default: 19.168 + result = 0; 19.169 + } 19.170 + 19.171 + /* is piece pinned */ 19.172 + // TODO: make it so 19.173 + 19.174 + /* correct check and checkmate flags */ 19.175 + // TODO: make it so 19.176 + 19.177 + return result; 19.178 +} 19.179 + 19.180 +int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { 19.181 + memset(move, 0, sizeof(Move)); 19.182 + move->fromfile = POS_UNSPECIFIED; 19.183 + move->fromrow = POS_UNSPECIFIED; 19.184 + 19.185 + size_t len = strlen(mstr); 19.186 + 19.187 + /* evaluate check/checkmate flags */ 19.188 + if (mstr[len-1] == '+') { 19.189 + len--; mstr[len] = '\0'; 19.190 + move->check = 1; 19.191 + } else if (mstr[len-1] == '#') { 19.192 + len--; mstr[len] = '\0'; 19.193 + move->checkmate = 1; 19.194 + } 19.195 + 19.196 + /* evaluate promotion */ 19.197 + if (len > 3 && mstr[len-2] == '=') { 19.198 + move->promotion = getpiece(mstr[len-1]); 19.199 + if (!move->promotion) { 19.200 + return INVALID_MOVE_SYNTAX; 19.201 + } else { 19.202 + move->promotion |= mycolor; 19.203 + len -= 2; 19.204 + mstr[len] = 0; 19.205 + } 19.206 + } 19.207 + 19.208 + if (len == 2) { 19.209 + /* pawn move (e.g. "e4") */ 19.210 + move->piece = PAWN; 19.211 + move->tofile = fileidx(mstr[0]); 19.212 + move->torow = rowidx(mstr[1]); 19.213 + } else if (len == 3) { 19.214 + if (strcmp(mstr, "O-O") == 0) { 19.215 + /* king side castling */ 19.216 + move->piece = KING; 19.217 + move->fromfile = fileidx('e'); 19.218 + move->tofile = fileidx('g'); 19.219 + move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 19.220 + } else { 19.221 + /* move (e.g. "Nf3") */ 19.222 + move->piece = getpiece(mstr[0]); 19.223 + move->tofile = fileidx(mstr[1]); 19.224 + move->torow = rowidx(mstr[2]); 19.225 + } 19.226 + 19.227 + } else if (len == 4) { 19.228 + move->piece = getpiece(mstr[0]); 19.229 + if (!move->piece) { 19.230 + move->piece = PAWN; 19.231 + move->fromfile = fileidx(mstr[0]); 19.232 + } 19.233 + if (mstr[1] == 'x') { 19.234 + /* capture (e.g. "Nxf3", "dxe5") */ 19.235 + move->capture = 1; 19.236 + } else { 19.237 + /* move (e.g. "Ndf3", "N2c3", "e2e4") */ 19.238 + if (isfile(mstr[1])) { 19.239 + move->fromfile = fileidx(mstr[1]); 19.240 + if (move->piece == PAWN) { 19.241 + move->piece = 0; 19.242 + } 19.243 + } else { 19.244 + move->fromrow = rowidx(mstr[1]); 19.245 + } 19.246 + } 19.247 + move->tofile = fileidx(mstr[2]); 19.248 + move->torow = rowidx(mstr[3]); 19.249 + } else if (len == 5) { 19.250 + if (strcmp(mstr, "O-O-O") == 0) { 19.251 + /* queen side castling "O-O-O" */ 19.252 + move->piece = KING; 19.253 + move->fromfile = fileidx('e'); 19.254 + move->tofile = fileidx('c'); 19.255 + move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 19.256 + } else { 19.257 + move->piece = getpiece(mstr[0]); 19.258 + if (mstr[2] == 'x') { 19.259 + move->capture = 1; 19.260 + if (move->piece) { 19.261 + /* capture (e.g. "Ndxf3") */ 19.262 + move->fromfile = fileidx(mstr[1]); 19.263 + } else { 19.264 + /* long notation capture (e.g. "e5xf6") */ 19.265 + move->piece = PAWN; 19.266 + move->fromfile = fileidx(mstr[0]); 19.267 + move->fromrow = rowidx(mstr[1]); 19.268 + } 19.269 + } else { 19.270 + /* long notation move (e.g. "Nc5a4") */ 19.271 + move->fromfile = fileidx(mstr[1]); 19.272 + move->fromrow = rowidx(mstr[2]); 19.273 + } 19.274 + move->tofile = fileidx(mstr[3]); 19.275 + move->torow = rowidx(mstr[4]); 19.276 + } 19.277 + } else if (len == 6) { 19.278 + /* long notation capture (e.g. "Nc5xf3") */ 19.279 + if (mstr[3] == 'x') { 19.280 + move->capture = 1; 19.281 + move->piece = getpiece(mstr[0]); 19.282 + move->fromfile = fileidx(mstr[1]); 19.283 + move->fromrow = rowidx(mstr[2]); 19.284 + move->tofile = fileidx(mstr[4]); 19.285 + move->torow = rowidx(mstr[5]); 19.286 + } 19.287 + } 19.288 + 19.289 + 19.290 + if (move->piece) { 19.291 + if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0) 19.292 + && !move->promotion) { 19.293 + return NEED_PROMOTION; 19.294 + } 19.295 + 19.296 + move->piece |= mycolor; 19.297 + if (move->fromfile == POS_UNSPECIFIED 19.298 + || move->fromrow == POS_UNSPECIFIED) { 19.299 + return getlocation(board, move); 19.300 + } else { 19.301 + return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; 19.302 + } 19.303 + } else { 19.304 + return INVALID_MOVE_SYNTAX; 19.305 + } 19.306 +}
20.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 20.2 +++ b/src/chess/rules.h Mon Mar 31 11:16:32 2014 +0200 20.3 @@ -0,0 +1,154 @@ 20.4 +/* 20.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 20.6 + * 20.7 + * Copyright 2014 Mike Becker. All rights reserved. 20.8 + * 20.9 + * Redistribution and use in source and binary forms, with or without 20.10 + * modification, are permitted provided that the following conditions are met: 20.11 + * 20.12 + * 1. Redistributions of source code must retain the above copyright 20.13 + * notice, this list of conditions and the following disclaimer. 20.14 + * 20.15 + * 2. Redistributions in binary form must reproduce the above copyright 20.16 + * notice, this list of conditions and the following disclaimer in the 20.17 + * documentation and/or other materials provided with the distribution. 20.18 + * 20.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 20.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 20.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 20.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 20.29 + * POSSIBILITY OF SUCH DAMAGE. 20.30 + * 20.31 + */ 20.32 + 20.33 +#ifndef RULES_H 20.34 +#define RULES_H 20.35 + 20.36 +#include <stdint.h> 20.37 + 20.38 +#define VALID_MOVE_SYNTAX 0 20.39 +#define INVALID_MOVE_SYNTAX 1 20.40 +#define INVALID_POSITION 2 20.41 +#define AMBIGUOUS_MOVE 3 20.42 +#define NEED_PROMOTION 4 20.43 + 20.44 + 20.45 +#define PIECE_MASK 0x0F 20.46 +#define COLOR_MASK 0x30 20.47 +#define ENPASSANT_THREAT 0x40 20.48 + 20.49 +#define WHITE 0x10 20.50 +#define BLACK 0x20 20.51 + 20.52 +#define PAWN 0x01 20.53 +#define ROOK 0x02 20.54 +#define KNIGHT 0x03 20.55 +#define BISHOP 0x04 20.56 +#define QUEEN 0x05 20.57 +#define KING 0x06 20.58 + 20.59 +#define WPAWN (WHITE|PAWN) 20.60 +#define WROOK (WHITE|ROOK) 20.61 +#define WKNIGHT (WHITE|KNIGHT) 20.62 +#define WBISHOP (WHITE|BISHOP) 20.63 +#define WQUEEN (WHITE|QUEEN) 20.64 +#define WKING (WHITE|KING) 20.65 +#define BPAWN (BLACK|PAWN) 20.66 +#define BROOK (BLACK|ROOK) 20.67 +#define BKNIGHT (BLACK|KNIGHT) 20.68 +#define BBISHOP (BLACK|BISHOP) 20.69 +#define BQUEEN (BLACK|QUEEN) 20.70 +#define BKING (BLACK|KING) 20.71 + 20.72 +typedef uint8_t Board[8][8]; 20.73 + 20.74 +typedef struct { 20.75 + uint8_t piece; 20.76 + uint8_t fromfile; 20.77 + uint8_t fromrow; 20.78 + uint8_t tofile; 20.79 + uint8_t torow; 20.80 + uint8_t promotion; 20.81 + _Bool check; 20.82 + _Bool checkmate; 20.83 + _Bool capture; 20.84 +} Move; 20.85 + 20.86 +#define POS_UNSPECIFIED UINT8_MAX 20.87 +#define mdst(b,m) b[(m)->torow][(m)->tofile] 20.88 +#define msrc(b,m) b[(m)->fromrow][(m)->fromfile] 20.89 + 20.90 +#define isidx(idx) ((uint8_t)idx < 8) 20.91 + 20.92 +#define isfile(file) (file >= 'a' && file <= 'h') 20.93 +#define isrow(row) (row >= '1' && row <= '8') 20.94 + 20.95 +#define rowidx(row) (row-'1') 20.96 +#define fileidx(file) (file-'a') 20.97 + 20.98 +#define rowchr(row) (row+'1') 20.99 +#define filechr(file) (file+'a') 20.100 + 20.101 +#define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \ 20.102 + isidx((move)->tofile) && isidx((move)->torow)) 20.103 + 20.104 +/* secure versions - use, if index is not checked with isidx() */ 20.105 +#define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) 20.106 +#define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) 20.107 + 20.108 +/** 20.109 + * Maps a character to a piece. 20.110 + * 20.111 + * Does not work for pawns, since they don't have a character. 20.112 + * 20.113 + * @param c one of R,N,B,Q,K 20.114 + * @return numeric value for the specified piece 20.115 + */ 20.116 +uint8_t getpiece(char c); 20.117 + 20.118 +/** 20.119 + * Maps a piece to a character. 20.120 + * 20.121 + * Does not work for pawns, scince they don't have a character. 20.122 + * 20.123 + * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING 20.124 + * @return character value for the specified piece 20.125 + */ 20.126 +char getpiecechr(uint8_t piece); 20.127 + 20.128 +/** 20.129 + * Evaluates a move syntactically and stores the move data in the specified 20.130 + * object. 20.131 + * 20.132 + * @param board the current state of the board 20.133 + * @param mycolor the color of the current player 20.134 + * @param mstr the input string to parse 20.135 + * @param move a pointer to object where the move data shall be stored 20.136 + * @return status code (see rules/rules.h for the list of codes) 20.137 + */ 20.138 +int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move); 20.139 + 20.140 +/** 20.141 + * Validates move by applying chess rules. 20.142 + * @param board the current board state 20.143 + * @param move the move to validate 20.144 + * @return TRUE, if the move complies to chess rules, FALSE otherwise 20.145 + */ 20.146 +_Bool validate_move(Board board, Move *move); 20.147 + 20.148 +/** 20.149 + * Applies a move and deletes captured pieces. 20.150 + * 20.151 + * @param board the current board state 20.152 + * @param move the move to apply 20.153 + */ 20.154 +void apply_move(Board board, Move *move); 20.155 + 20.156 +#endif /* RULES_H */ 20.157 +
21.1 --- a/src/game.c Sat Mar 29 16:53:58 2014 +0100 21.2 +++ b/src/game.c Mon Mar 31 11:16:32 2014 +0200 21.3 @@ -28,64 +28,13 @@ 21.4 */ 21.5 21.6 #include "game.h" 21.7 +#include "network.h" 21.8 #include "input.h" 21.9 -#include "rules/rules.h" 21.10 #include <ncurses.h> 21.11 #include <string.h> 21.12 21.13 static const uint8_t boardx = 10, boardy = 10; 21.14 21.15 -static uint8_t getpiecechr(uint8_t piece) { 21.16 - switch (piece & PIECE_MASK) { 21.17 - case ROOK: return 'R'; 21.18 - case KNIGHT: return 'N'; 21.19 - case BISHOP: return 'B'; 21.20 - case QUEEN: return 'Q'; 21.21 - case KING: return 'K'; 21.22 - default: return '\0'; 21.23 - } 21.24 -} 21.25 - 21.26 -/** 21.27 - * Maps a character to a piece. 21.28 - * 21.29 - * Does not work for pawns, since they don't have a character. 21.30 - * 21.31 - * @param c one of R,N,B,Q,K 21.32 - * @return numeric value for the specified piece 21.33 - */ 21.34 -static uint8_t getpiece(char c) { 21.35 - switch (c) { 21.36 - case 'R': return ROOK; 21.37 - case 'N': return KNIGHT; 21.38 - case 'B': return BISHOP; 21.39 - case 'Q': return QUEEN; 21.40 - case 'K': return KING; 21.41 - default: return 0; 21.42 - } 21.43 -} 21.44 - 21.45 -/** 21.46 - * Guesses the location of a piece for short algebraic notation. 21.47 - * 21.48 - * @param board the current state of the board 21.49 - * @param move the move date to operate on 21.50 - * @return status code (see rules/rules.h for the codes) 21.51 - */ 21.52 -static int getlocation(Board board, Move *move) { 21.53 - uint8_t piece = move->piece & PIECE_MASK; 21.54 - switch (piece) { 21.55 - case PAWN: return pawn_getlocation(board, move); 21.56 - case ROOK: return rook_getlocation(board, move); 21.57 - case KNIGHT: return knight_getlocation(board, move); 21.58 - case BISHOP: return bishop_getlocation(board, move); 21.59 - case QUEEN: return queen_getlocation(board, move); 21.60 - case KING: return king_getlocation(board, move); 21.61 - default: return INVALID_MOVE_SYNTAX; 21.62 - } 21.63 -} 21.64 - 21.65 - 21.66 static void draw_board(Board board, MoveListRoot *movelist, uint8_t mycolor) { 21.67 21.68 for (uint8_t y = 0 ; y < 8 ; y++) { 21.69 @@ -158,255 +107,6 @@ 21.70 } 21.71 } 21.72 21.73 -/** 21.74 - * Applies a move and deletes captured pieces. 21.75 - * 21.76 - * @param board the current board state 21.77 - * @param move the move to apply 21.78 - */ 21.79 -static void apply_move(Board board, Move *move) { 21.80 - uint8_t piece = move->piece & PIECE_MASK; 21.81 - uint8_t color = move->piece & COLOR_MASK; 21.82 - 21.83 - /* en passant capture */ 21.84 - if (move->capture && piece == PAWN && 21.85 - mdst(board, move) == 0) { 21.86 - board[move->fromrow][move->tofile] = 0; 21.87 - } 21.88 - 21.89 - /* remove old en passant threats */ 21.90 - for (uint8_t file = 0 ; file < 8 ; file++) { 21.91 - board[3][file] &= ~ENPASSANT_THREAT; 21.92 - board[4][file] &= ~ENPASSANT_THREAT; 21.93 - } 21.94 - 21.95 - /* add new en passant threat */ 21.96 - if (piece == PAWN && ( 21.97 - (move->fromrow == 1 && move->torow == 3) || 21.98 - (move->fromrow == 6 && move->torow == 4))) { 21.99 - move->piece |= ENPASSANT_THREAT; 21.100 - } 21.101 - 21.102 - /* move (and maybe capture or promote) */ 21.103 - msrc(board, move) = 0; 21.104 - if (move->promotion) { 21.105 - mdst(board, move) = move->promotion; 21.106 - } else { 21.107 - mdst(board, move) = move->piece; 21.108 - } 21.109 - 21.110 - /* castling */ 21.111 - if (piece == KING && 21.112 - move->fromfile == fileidx('e')) { 21.113 - 21.114 - if (move->tofile == fileidx('g')) { 21.115 - board[move->torow][fileidx('h')] = 0; 21.116 - board[move->torow][fileidx('f')] = color|ROOK; 21.117 - } else if (move->tofile == fileidx('c')) { 21.118 - board[move->torow][fileidx('a')] = 0; 21.119 - board[move->torow][fileidx('d')] = color|ROOK; 21.120 - } 21.121 - } 21.122 -} 21.123 - 21.124 -/** 21.125 - * Validates move by applying chess rules. 21.126 - * @param board the current board state 21.127 - * @param move the move to validate 21.128 - * @return TRUE, if the move complies to chess rules, FALSE otherwise 21.129 - */ 21.130 -static _Bool validate_move(Board board, Move *move) { 21.131 - _Bool result; 21.132 - 21.133 - /* validate indices (don't trust opponent) */ 21.134 - if (!chkidx(move)) { 21.135 - return FALSE; 21.136 - } 21.137 - 21.138 - /* does piece exist */ 21.139 - result = msrc(board, move) == move->piece; 21.140 - 21.141 - /* can't capture own pieces */ 21.142 - if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) { 21.143 - return FALSE; 21.144 - } 21.145 - 21.146 - /* validate individual rules */ 21.147 - switch (move->piece & PIECE_MASK) { 21.148 - case PAWN: 21.149 - result = result && pawn_chkrules(board, move); 21.150 - result = result && !pawn_isblocked(board, move); 21.151 - break; 21.152 - case ROOK: 21.153 - result = result && rook_chkrules(move); 21.154 - result = result && !rook_isblocked(board, move); 21.155 - break; 21.156 - case KNIGHT: 21.157 - result = result && knight_chkrules(move); 21.158 - result = result && !knight_isblocked(board, move); 21.159 - break; 21.160 - case BISHOP: 21.161 - result = result && bishop_chkrules(move); 21.162 - result = result && !bishop_isblocked(board, move); 21.163 - break; 21.164 - case QUEEN: 21.165 - result = result && queen_chkrules(move); 21.166 - result = result && !queen_isblocked(board, move); 21.167 - break; 21.168 - case KING: 21.169 - result = result && king_chkrules(board, move); 21.170 - result = result && !king_isblocked(board, move); 21.171 - break; 21.172 - default: 21.173 - result = FALSE; 21.174 - } 21.175 - 21.176 - /* is piece pinned */ 21.177 - // TODO: make it so 21.178 - 21.179 - /* correct check and checkmate flags */ 21.180 - // TODO: make it so 21.181 - 21.182 - return result; 21.183 -} 21.184 - 21.185 -/** 21.186 - * Evaluates a move syntactically and stores the move data in the specified 21.187 - * object. 21.188 - * 21.189 - * @param board the current state of the board 21.190 - * @param mycolor the color of the current player 21.191 - * @param mstr the input string to parse 21.192 - * @param move a pointer to object where the move data shall be stored 21.193 - * @return status code (see rules/rules.h for the list of codes) 21.194 - */ 21.195 -static int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { 21.196 - memset(move, 0, sizeof(Move)); 21.197 - move->fromfile = POS_UNSPECIFIED; 21.198 - move->fromrow = POS_UNSPECIFIED; 21.199 - 21.200 - size_t len = strlen(mstr); 21.201 - 21.202 - /* evaluate check/checkmate flags */ 21.203 - if (mstr[len-1] == '+') { 21.204 - len--; mstr[len] = '\0'; 21.205 - move->check = TRUE; 21.206 - } else if (mstr[len-1] == '#') { 21.207 - len--; mstr[len] = '\0'; 21.208 - move->checkmate = TRUE; 21.209 - } 21.210 - 21.211 - /* evaluate promotion */ 21.212 - if (len > 3 && mstr[len-2] == '=') { 21.213 - move->promotion = getpiece(mstr[len-1]); 21.214 - if (!move->promotion) { 21.215 - return INVALID_MOVE_SYNTAX; 21.216 - } else { 21.217 - move->promotion |= mycolor; 21.218 - len -= 2; 21.219 - mstr[len] = 0; 21.220 - } 21.221 - } 21.222 - 21.223 - if (len == 2) { 21.224 - /* pawn move (e.g. "e4") */ 21.225 - move->piece = PAWN; 21.226 - move->tofile = fileidx(mstr[0]); 21.227 - move->torow = rowidx(mstr[1]); 21.228 - } else if (len == 3) { 21.229 - if (strcmp(mstr, "O-O") == 0) { 21.230 - /* king side castling */ 21.231 - move->piece = KING; 21.232 - move->fromfile = fileidx('e'); 21.233 - move->tofile = fileidx('g'); 21.234 - move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 21.235 - } else { 21.236 - /* move (e.g. "Nf3") */ 21.237 - move->piece = getpiece(mstr[0]); 21.238 - move->tofile = fileidx(mstr[1]); 21.239 - move->torow = rowidx(mstr[2]); 21.240 - } 21.241 - 21.242 - } else if (len == 4) { 21.243 - move->piece = getpiece(mstr[0]); 21.244 - if (!move->piece) { 21.245 - move->piece = PAWN; 21.246 - move->fromfile = fileidx(mstr[0]); 21.247 - } 21.248 - if (mstr[1] == 'x') { 21.249 - /* capture (e.g. "Nxf3", "dxe5") */ 21.250 - move->capture = TRUE; 21.251 - } else { 21.252 - /* move (e.g. "Ndf3", "N2c3", "e2e4") */ 21.253 - if (isfile(mstr[1])) { 21.254 - move->fromfile = fileidx(mstr[1]); 21.255 - if (move->piece == PAWN) { 21.256 - move->piece = 0; 21.257 - } 21.258 - } else { 21.259 - move->fromrow = rowidx(mstr[1]); 21.260 - } 21.261 - } 21.262 - move->tofile = fileidx(mstr[2]); 21.263 - move->torow = rowidx(mstr[3]); 21.264 - } else if (len == 5) { 21.265 - if (strcmp(mstr, "O-O-O") == 0) { 21.266 - /* queen side castling "O-O-O" */ 21.267 - move->piece = KING; 21.268 - move->fromfile = fileidx('e'); 21.269 - move->tofile = fileidx('c'); 21.270 - move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 21.271 - } else { 21.272 - move->piece = getpiece(mstr[0]); 21.273 - if (mstr[2] == 'x') { 21.274 - move->capture = TRUE; 21.275 - if (move->piece) { 21.276 - /* capture (e.g. "Ndxf3") */ 21.277 - move->fromfile = fileidx(mstr[1]); 21.278 - } else { 21.279 - /* long notation capture (e.g. "e5xf6") */ 21.280 - move->piece = PAWN; 21.281 - move->fromfile = fileidx(mstr[0]); 21.282 - move->fromrow = rowidx(mstr[1]); 21.283 - } 21.284 - } else { 21.285 - /* long notation move (e.g. "Nc5a4") */ 21.286 - move->fromfile = fileidx(mstr[1]); 21.287 - move->fromrow = rowidx(mstr[2]); 21.288 - } 21.289 - move->tofile = fileidx(mstr[3]); 21.290 - move->torow = rowidx(mstr[4]); 21.291 - } 21.292 - } else if (len == 6) { 21.293 - /* long notation capture (e.g. "Nc5xf3") */ 21.294 - if (mstr[3] == 'x') { 21.295 - move->capture = TRUE; 21.296 - move->piece = getpiece(mstr[0]); 21.297 - move->fromfile = fileidx(mstr[1]); 21.298 - move->fromrow = rowidx(mstr[2]); 21.299 - move->tofile = fileidx(mstr[4]); 21.300 - move->torow = rowidx(mstr[5]); 21.301 - } 21.302 - } 21.303 - 21.304 - 21.305 - if (move->piece) { 21.306 - if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0) 21.307 - && !move->promotion) { 21.308 - return NEED_PROMOTION; 21.309 - } 21.310 - 21.311 - move->piece |= mycolor; 21.312 - if (move->fromfile == POS_UNSPECIFIED 21.313 - || move->fromrow == POS_UNSPECIFIED) { 21.314 - return getlocation(board, move); 21.315 - } else { 21.316 - return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; 21.317 - } 21.318 - } else { 21.319 - return INVALID_MOVE_SYNTAX; 21.320 - } 21.321 -} 21.322 21.323 static int sendmove(Board board, MoveListRoot *movelist, 21.324 uint8_t mycolor, int opponent) {
22.1 --- a/src/game.h Sat Mar 29 16:53:58 2014 +0100 22.2 +++ b/src/game.h Mon Mar 31 11:16:32 2014 +0200 22.3 @@ -30,54 +30,13 @@ 22.4 #ifndef GAME_H 22.5 #define GAME_H 22.6 22.7 +#include "chess/chess.h" 22.8 #include "terminal-chess.h" 22.9 -#include <stdint.h> 22.10 22.11 #ifdef __cplusplus 22.12 extern "C" { 22.13 #endif 22.14 22.15 -#define PIECE_MASK 0x0F 22.16 -#define COLOR_MASK 0x30 22.17 -#define ENPASSANT_THREAT 0x40 22.18 - 22.19 -#define WHITE 0x10 22.20 -#define BLACK 0x20 22.21 - 22.22 -#define PAWN 0x01 22.23 -#define ROOK 0x02 22.24 -#define KNIGHT 0x03 22.25 -#define BISHOP 0x04 22.26 -#define QUEEN 0x05 22.27 -#define KING 0x06 22.28 - 22.29 -#define WPAWN (WHITE|PAWN) 22.30 -#define WROOK (WHITE|ROOK) 22.31 -#define WKNIGHT (WHITE|KNIGHT) 22.32 -#define WBISHOP (WHITE|BISHOP) 22.33 -#define WQUEEN (WHITE|QUEEN) 22.34 -#define WKING (WHITE|KING) 22.35 -#define BPAWN (BLACK|PAWN) 22.36 -#define BROOK (BLACK|ROOK) 22.37 -#define BKNIGHT (BLACK|KNIGHT) 22.38 -#define BBISHOP (BLACK|BISHOP) 22.39 -#define BQUEEN (BLACK|QUEEN) 22.40 -#define BKING (BLACK|KING) 22.41 - 22.42 -typedef uint8_t Board[8][8]; 22.43 - 22.44 -typedef struct { 22.45 - uint8_t piece; 22.46 - uint8_t fromfile; 22.47 - uint8_t fromrow; 22.48 - uint8_t tofile; 22.49 - uint8_t torow; 22.50 - uint8_t promotion; 22.51 - _Bool check; 22.52 - _Bool checkmate; 22.53 - _Bool capture; 22.54 -} Move; 22.55 - 22.56 typedef struct MoveList MoveList; 22.57 typedef struct MoveListRoot MoveListRoot; 22.58 22.59 @@ -91,27 +50,6 @@ 22.60 MoveList* next; 22.61 }; 22.62 22.63 -#define POS_UNSPECIFIED UINT8_MAX 22.64 -#define mdst(b,m) b[(m)->torow][(m)->tofile] 22.65 -#define msrc(b,m) b[(m)->fromrow][(m)->fromfile] 22.66 - 22.67 -#define isidx(idx) ((uint8_t)idx < 8) 22.68 - 22.69 -#define isfile(file) (file >= 'a' && file <= 'h') 22.70 -#define isrow(row) (row >= '1' && row <= '8') 22.71 - 22.72 -#define rowidx(row) (row-'1') 22.73 -#define fileidx(file) (file-'a') 22.74 - 22.75 -#define rowchr(row) (row+'1') 22.76 -#define filechr(file) (file+'a') 22.77 - 22.78 -#define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \ 22.79 - isidx((move)->tofile) && isidx((move)->torow)) 22.80 - 22.81 -/* secure versions - use, if index is not checked with isidx() */ 22.82 -#define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) 22.83 -#define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) 22.84 22.85 void game_start(Settings *settings, int opponent); 22.86
23.1 --- a/src/rules/bishop.c Sat Mar 29 16:53:58 2014 +0100 23.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 23.3 @@ -1,128 +0,0 @@ 23.4 -/* 23.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 23.6 - * 23.7 - * Copyright 2014 Mike Becker. All rights reserved. 23.8 - * 23.9 - * Redistribution and use in source and binary forms, with or without 23.10 - * modification, are permitted provided that the following conditions are met: 23.11 - * 23.12 - * 1. Redistributions of source code must retain the above copyright 23.13 - * notice, this list of conditions and the following disclaimer. 23.14 - * 23.15 - * 2. Redistributions in binary form must reproduce the above copyright 23.16 - * notice, this list of conditions and the following disclaimer in the 23.17 - * documentation and/or other materials provided with the distribution. 23.18 - * 23.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 23.29 - * POSSIBILITY OF SUCH DAMAGE. 23.30 - * 23.31 - */ 23.32 - 23.33 -#include "bishop.h" 23.34 -#include "rules.h" 23.35 -#include <math.h> 23.36 - 23.37 -_Bool bishop_chkrules(Move* move) { 23.38 - return abs(move->torow-move->fromrow) == abs(move->tofile-move->fromfile); 23.39 -} 23.40 - 23.41 -_Bool bishop_isblocked(Board board, Move *move) { 23.42 - int dy = move->torow > move->fromrow ? 1 : -1; 23.43 - int dx = move->tofile > move->fromfile ? 1 : -1; 23.44 - 23.45 - uint8_t y = move->fromrow; 23.46 - uint8_t x = move->fromfile; 23.47 - 23.48 - do { 23.49 - x += dx; 23.50 - y += dy; 23.51 - if (board[y][x]) { 23.52 - return TRUE; 23.53 - } 23.54 - } while (x != move->tofile && y != move->torow); 23.55 - 23.56 - return FALSE; 23.57 -} 23.58 - 23.59 -static int bishop_getloc_fixedfile(Board board, Move *move) { 23.60 - uint8_t d = abs(move->fromfile - move->tofile); 23.61 - if (board[move->torow - d][move->fromfile] == move->piece) { 23.62 - move->fromrow = move->torow - d; 23.63 - } 23.64 - if (board[move->torow + d][move->fromfile] == move->piece) { 23.65 - if (move->fromrow == POS_UNSPECIFIED) { 23.66 - move->fromrow = move->torow + d; 23.67 - } else { 23.68 - return AMBIGUOUS_MOVE; /* rare situation after promotion */ 23.69 - } 23.70 - } 23.71 - return move->fromrow == POS_UNSPECIFIED ? 23.72 - INVALID_POSITION : VALID_MOVE_SYNTAX; 23.73 -} 23.74 - 23.75 -static int bishop_getloc_fixedrow(Board board, Move *move) { 23.76 - uint8_t d = abs(move->fromrow - move->torow); 23.77 - if (board[move->fromrow][move->tofile - d] == move->piece) { 23.78 - move->fromfile = move->tofile - d; 23.79 - } 23.80 - if (board[move->fromrow][move->tofile + d] == move->piece) { 23.81 - if (move->fromfile == POS_UNSPECIFIED) { 23.82 - move->fromfile = move->tofile + d; 23.83 - } else { 23.84 - return AMBIGUOUS_MOVE; /* rare situation after promotion */ 23.85 - } 23.86 - } 23.87 - return move->fromfile == POS_UNSPECIFIED ? 23.88 - INVALID_POSITION : VALID_MOVE_SYNTAX; 23.89 -} 23.90 - 23.91 -int bishop_getlocation(Board board, Move *move) { 23.92 - 23.93 - if (move->fromfile != POS_UNSPECIFIED) { 23.94 - return bishop_getloc_fixedfile(board, move); 23.95 - } 23.96 - 23.97 - if (move->fromrow != POS_UNSPECIFIED) { 23.98 - return bishop_getloc_fixedrow(board, move); 23.99 - } 23.100 - 23.101 - _Bool amb = FALSE; 23.102 - for (int d = -7 ; d < 8 ; d++) { 23.103 - uint8_t row = move->torow + d; 23.104 - if (isidx(row)) { 23.105 - uint8_t file = move->tofile + d; 23.106 - if (isidx(file) && board[row][file] == move->piece) { 23.107 - if (amb) { 23.108 - return AMBIGUOUS_MOVE; 23.109 - } 23.110 - amb = TRUE; 23.111 - move->fromrow = row; 23.112 - move->fromfile = file; 23.113 - } 23.114 - file = move->tofile - d; 23.115 - if (isidx(file) && board[row][file] == move->piece) { 23.116 - if (amb) { 23.117 - return AMBIGUOUS_MOVE; 23.118 - } 23.119 - amb = TRUE; 23.120 - move->fromrow = row; 23.121 - move->fromfile = file; 23.122 - } 23.123 - } 23.124 - } 23.125 - 23.126 - if (move->fromrow == POS_UNSPECIFIED || move->fromfile == POS_UNSPECIFIED) { 23.127 - return INVALID_POSITION; 23.128 - } else { 23.129 - return VALID_MOVE_SYNTAX; 23.130 - } 23.131 -}
24.1 --- a/src/rules/bishop.h Sat Mar 29 16:53:58 2014 +0100 24.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 24.3 @@ -1,48 +0,0 @@ 24.4 -/* 24.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 24.6 - * 24.7 - * Copyright 2014 Mike Becker. All rights reserved. 24.8 - * 24.9 - * Redistribution and use in source and binary forms, with or without 24.10 - * modification, are permitted provided that the following conditions are met: 24.11 - * 24.12 - * 1. Redistributions of source code must retain the above copyright 24.13 - * notice, this list of conditions and the following disclaimer. 24.14 - * 24.15 - * 2. Redistributions in binary form must reproduce the above copyright 24.16 - * notice, this list of conditions and the following disclaimer in the 24.17 - * documentation and/or other materials provided with the distribution. 24.18 - * 24.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 24.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 24.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24.29 - * POSSIBILITY OF SUCH DAMAGE. 24.30 - * 24.31 - */ 24.32 - 24.33 -#ifndef BISHOP_H 24.34 -#define BISHOP_H 24.35 - 24.36 -#include "../game.h" 24.37 - 24.38 -#ifdef __cplusplus 24.39 -extern "C" { 24.40 -#endif 24.41 - 24.42 -_Bool bishop_chkrules(Move *move); 24.43 -_Bool bishop_isblocked(Board board, Move *move); 24.44 -int bishop_getlocation(Board board, Move *move); 24.45 - 24.46 -#ifdef __cplusplus 24.47 -} 24.48 -#endif 24.49 - 24.50 -#endif /* BISHOP_H */ 24.51 -
25.1 --- a/src/rules/king.c Sat Mar 29 16:53:58 2014 +0100 25.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 25.3 @@ -1,46 +0,0 @@ 25.4 -/* 25.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 25.6 - * 25.7 - * Copyright 2014 Mike Becker. All rights reserved. 25.8 - * 25.9 - * Redistribution and use in source and binary forms, with or without 25.10 - * modification, are permitted provided that the following conditions are met: 25.11 - * 25.12 - * 1. Redistributions of source code must retain the above copyright 25.13 - * notice, this list of conditions and the following disclaimer. 25.14 - * 25.15 - * 2. Redistributions in binary form must reproduce the above copyright 25.16 - * notice, this list of conditions and the following disclaimer in the 25.17 - * documentation and/or other materials provided with the distribution. 25.18 - * 25.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 25.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 25.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25.29 - * POSSIBILITY OF SUCH DAMAGE. 25.30 - * 25.31 - */ 25.32 - 25.33 -#include "rules.h" 25.34 -#include "king.h" 25.35 - 25.36 -_Bool king_chkrules(Board board, Move* move) { 25.37 - // TODO: implement 25.38 - return FALSE; 25.39 -} 25.40 - 25.41 -_Bool king_isblocked(Board board, Move *move) { 25.42 - // TODO: implement 25.43 - return TRUE; 25.44 -} 25.45 - 25.46 -int king_getlocation(Board board, Move *move) { 25.47 - // TODO: implement 25.48 - return INVALID_MOVE_SYNTAX; 25.49 -}
26.1 --- a/src/rules/king.h Sat Mar 29 16:53:58 2014 +0100 26.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 26.3 @@ -1,49 +0,0 @@ 26.4 -/* 26.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 26.6 - * 26.7 - * Copyright 2014 Mike Becker. All rights reserved. 26.8 - * 26.9 - * Redistribution and use in source and binary forms, with or without 26.10 - * modification, are permitted provided that the following conditions are met: 26.11 - * 26.12 - * 1. Redistributions of source code must retain the above copyright 26.13 - * notice, this list of conditions and the following disclaimer. 26.14 - * 26.15 - * 2. Redistributions in binary form must reproduce the above copyright 26.16 - * notice, this list of conditions and the following disclaimer in the 26.17 - * documentation and/or other materials provided with the distribution. 26.18 - * 26.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 26.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 26.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26.29 - * POSSIBILITY OF SUCH DAMAGE. 26.30 - * 26.31 - */ 26.32 - 26.33 - 26.34 -#ifndef KING_H 26.35 -#define KING_H 26.36 - 26.37 -#include "../game.h" 26.38 - 26.39 -#ifdef __cplusplus 26.40 -extern "C" { 26.41 -#endif 26.42 - 26.43 -_Bool king_chkrules(Board board, Move *move); 26.44 -_Bool king_isblocked(Board board, Move *move); 26.45 -int king_getlocation(Board board, Move *move); 26.46 - 26.47 -#ifdef __cplusplus 26.48 -} 26.49 -#endif 26.50 - 26.51 -#endif /* KING_H */ 26.52 -
27.1 --- a/src/rules/knight.c Sat Mar 29 16:53:58 2014 +0100 27.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 27.3 @@ -1,133 +0,0 @@ 27.4 -/* 27.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 27.6 - * 27.7 - * Copyright 2014 Mike Becker. All rights reserved. 27.8 - * 27.9 - * Redistribution and use in source and binary forms, with or without 27.10 - * modification, are permitted provided that the following conditions are met: 27.11 - * 27.12 - * 1. Redistributions of source code must retain the above copyright 27.13 - * notice, this list of conditions and the following disclaimer. 27.14 - * 27.15 - * 2. Redistributions in binary form must reproduce the above copyright 27.16 - * notice, this list of conditions and the following disclaimer in the 27.17 - * documentation and/or other materials provided with the distribution. 27.18 - * 27.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 27.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27.29 - * POSSIBILITY OF SUCH DAMAGE. 27.30 - * 27.31 - */ 27.32 - 27.33 -#include "knight.h" 27.34 -#include "rules.h" 27.35 -#include <math.h> 27.36 - 27.37 -_Bool knight_chkrules(Move *move) { 27.38 - int dx = abs(move->fromfile - move->tofile); 27.39 - int dy = abs(move->fromrow - move->torow); 27.40 - 27.41 - return (dx == 2 && dy == 1) || (dx == 1 && dy == 2); 27.42 -} 27.43 - 27.44 -static int knight_getloc_fixedrow(Board board, Move *move) { 27.45 - int d = 3 - abs(move->fromrow - move->torow); 27.46 - 27.47 - if (d == 1 || d == 2) { 27.48 - if (move->tofile < 6 && 27.49 - board[move->fromrow][move->tofile + d] == move->piece) { 27.50 - if (move->fromfile == POS_UNSPECIFIED) { 27.51 - move->fromfile = move->tofile + d; 27.52 - return VALID_MOVE_SYNTAX; 27.53 - } else { 27.54 - return AMBIGUOUS_MOVE; 27.55 - } 27.56 - } 27.57 - if (move->tofile > 1 && 27.58 - board[move->fromrow][move->tofile - d] == move->piece) { 27.59 - if (move->fromfile == POS_UNSPECIFIED) { 27.60 - move->fromfile = move->tofile - d; 27.61 - return VALID_MOVE_SYNTAX; 27.62 - } else { 27.63 - return AMBIGUOUS_MOVE; 27.64 - } 27.65 - } 27.66 - } 27.67 - 27.68 - return INVALID_POSITION; 27.69 -} 27.70 - 27.71 -static int knight_getloc_fixedfile(Board board, Move *move) { 27.72 - int d = 3 - abs(move->fromfile - move->tofile); 27.73 - 27.74 - if (d == 1 || d == 2) { 27.75 - if (move->torow < 6 && 27.76 - board[move->torow + d][move->fromfile] == move->piece) { 27.77 - if (move->fromrow == POS_UNSPECIFIED) { 27.78 - move->fromrow = move->torow + d; 27.79 - return VALID_MOVE_SYNTAX; 27.80 - } else { 27.81 - return AMBIGUOUS_MOVE; 27.82 - } 27.83 - } 27.84 - if (move->torow > 1 && 27.85 - board[move->torow - d][move->fromfile] == move->piece) { 27.86 - if (move->fromrow == POS_UNSPECIFIED) { 27.87 - move->fromrow = move->torow - d; 27.88 - return VALID_MOVE_SYNTAX; 27.89 - } else { 27.90 - return AMBIGUOUS_MOVE; 27.91 - } 27.92 - } 27.93 - } 27.94 - 27.95 - return INVALID_POSITION; 27.96 -} 27.97 - 27.98 -int knight_getlocation(Board board, Move *move) { 27.99 - 27.100 - if (move->fromfile != POS_UNSPECIFIED) { 27.101 - return knight_getloc_fixedfile(board, move); 27.102 - } 27.103 - 27.104 - if (move->fromrow != POS_UNSPECIFIED) { 27.105 - return knight_getloc_fixedrow(board, move); 27.106 - } 27.107 - 27.108 - for (int x = -2 ; x <= 2 ; x++) { 27.109 - if (x == 0) { 27.110 - continue; 27.111 - } 27.112 - for (int y = -2 ; y <= 2 ; y++) { 27.113 - if (y == 0 || y == x) { 27.114 - continue; 27.115 - } 27.116 - uint8_t cx = move->tofile + x; 27.117 - uint8_t cy = move->torow + y; 27.118 - 27.119 - if (isidx(cx) && isidx(cy) && board[cy][cx] == move->piece) { 27.120 - if (move->fromfile == POS_UNSPECIFIED 27.121 - && move->fromrow == POS_UNSPECIFIED) { 27.122 - move->fromfile = cx; 27.123 - move->fromrow = cy; 27.124 - } else { 27.125 - return AMBIGUOUS_MOVE; 27.126 - } 27.127 - } 27.128 - } 27.129 - } 27.130 - 27.131 - if (move->fromfile == POS_UNSPECIFIED || move->fromrow == POS_UNSPECIFIED) { 27.132 - return INVALID_POSITION; 27.133 - } else { 27.134 - return VALID_MOVE_SYNTAX; 27.135 - } 27.136 -}
28.1 --- a/src/rules/knight.h Sat Mar 29 16:53:58 2014 +0100 28.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 28.3 @@ -1,48 +0,0 @@ 28.4 -/* 28.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 28.6 - * 28.7 - * Copyright 2014 Mike Becker. All rights reserved. 28.8 - * 28.9 - * Redistribution and use in source and binary forms, with or without 28.10 - * modification, are permitted provided that the following conditions are met: 28.11 - * 28.12 - * 1. Redistributions of source code must retain the above copyright 28.13 - * notice, this list of conditions and the following disclaimer. 28.14 - * 28.15 - * 2. Redistributions in binary form must reproduce the above copyright 28.16 - * notice, this list of conditions and the following disclaimer in the 28.17 - * documentation and/or other materials provided with the distribution. 28.18 - * 28.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 28.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 28.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.29 - * POSSIBILITY OF SUCH DAMAGE. 28.30 - * 28.31 - */ 28.32 - 28.33 -#ifndef KNIGHT_H 28.34 -#define KNIGHT_H 28.35 - 28.36 -#include "../game.h" 28.37 - 28.38 -#ifdef __cplusplus 28.39 -extern "C" { 28.40 -#endif 28.41 - 28.42 -_Bool knight_chkrules(Move *move); 28.43 -#define knight_isblocked(b,m) FALSE 28.44 -int knight_getlocation(Board board, Move *move); 28.45 - 28.46 -#ifdef __cplusplus 28.47 -} 28.48 -#endif 28.49 - 28.50 -#endif /* KNIGHT_H */ 28.51 -
29.1 --- a/src/rules/pawn.c Sat Mar 29 16:53:58 2014 +0100 29.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 29.3 @@ -1,97 +0,0 @@ 29.4 -/* 29.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 29.6 - * 29.7 - * Copyright 2014 Mike Becker. All rights reserved. 29.8 - * 29.9 - * Redistribution and use in source and binary forms, with or without 29.10 - * modification, are permitted provided that the following conditions are met: 29.11 - * 29.12 - * 1. Redistributions of source code must retain the above copyright 29.13 - * notice, this list of conditions and the following disclaimer. 29.14 - * 29.15 - * 2. Redistributions in binary form must reproduce the above copyright 29.16 - * notice, this list of conditions and the following disclaimer in the 29.17 - * documentation and/or other materials provided with the distribution. 29.18 - * 29.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 29.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 29.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 29.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29.29 - * POSSIBILITY OF SUCH DAMAGE. 29.30 - * 29.31 - */ 29.32 - 29.33 -#include "pawn.h" 29.34 -#include "rules.h" 29.35 - 29.36 -_Bool pawn_chkrules(Board board, Move *move) { 29.37 - int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1); 29.38 - 29.39 - if (move->torow == (d < 0 ? 7 : 0)) { 29.40 - if (move->promotion) { 29.41 - uint8_t promopiece = move->promotion & PIECE_MASK; 29.42 - if (!promopiece || promopiece == PAWN || promopiece == KING) { 29.43 - return FALSE; 29.44 - } 29.45 - } else { 29.46 - return FALSE; 29.47 - } 29.48 - } else { 29.49 - if (move->promotion) { 29.50 - return FALSE; 29.51 - } 29.52 - } 29.53 - 29.54 - if (move->capture) { 29.55 - if (move->fromrow == move->torow + d && ( 29.56 - move->fromfile == move->tofile + 1 || 29.57 - move->fromfile == move->tofile - 1)) { 29.58 - 29.59 - return mdst(board,move) 29.60 - || (board[move->fromrow][move->tofile] & ENPASSANT_THREAT); 29.61 - } else { 29.62 - return FALSE; 29.63 - } 29.64 - } else { 29.65 - if (move->fromfile == move->tofile) { 29.66 - return (move->fromrow == move->torow + d) || 29.67 - (move->fromrow == (d < 0 ? 1 : 6) && /* advanced first move */ 29.68 - move->fromrow == move->torow + d*2); 29.69 - } else { 29.70 - return FALSE; 29.71 - } 29.72 - } 29.73 -} 29.74 - 29.75 -_Bool pawn_isblocked(Board board, Move *move) { 29.76 - return mdst(board,move) && !move->capture; 29.77 -} 29.78 - 29.79 -int pawn_getlocation(Board board, Move *move) { 29.80 - int8_t d = ((move->piece & COLOR_MASK) == WHITE ? -1 : 1); 29.81 - 29.82 - if (move->fromfile == POS_UNSPECIFIED) { 29.83 - move->fromfile = move->tofile; 29.84 - } 29.85 - move->fromrow = move->torow + d; 29.86 - if (move->fromrow > 6) { 29.87 - return INVALID_POSITION; 29.88 - } else { 29.89 - /* advanced first move */ 29.90 - if (move->fromrow == (d < 0 ? 2 : 5) && 29.91 - msrc(board,move) != move->piece) { 29.92 - 29.93 - move->fromrow += d; 29.94 - if (move->fromrow > 6) { 29.95 - return INVALID_POSITION; 29.96 - } 29.97 - } 29.98 - } 29.99 - return VALID_MOVE_SYNTAX; 29.100 -}
30.1 --- a/src/rules/pawn.h Sat Mar 29 16:53:58 2014 +0100 30.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 30.3 @@ -1,48 +0,0 @@ 30.4 -/* 30.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 30.6 - * 30.7 - * Copyright 2014 Mike Becker. All rights reserved. 30.8 - * 30.9 - * Redistribution and use in source and binary forms, with or without 30.10 - * modification, are permitted provided that the following conditions are met: 30.11 - * 30.12 - * 1. Redistributions of source code must retain the above copyright 30.13 - * notice, this list of conditions and the following disclaimer. 30.14 - * 30.15 - * 2. Redistributions in binary form must reproduce the above copyright 30.16 - * notice, this list of conditions and the following disclaimer in the 30.17 - * documentation and/or other materials provided with the distribution. 30.18 - * 30.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 30.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 30.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 30.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 30.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 30.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30.29 - * POSSIBILITY OF SUCH DAMAGE. 30.30 - * 30.31 - */ 30.32 - 30.33 -#ifndef PAWN_H 30.34 -#define PAWN_H 30.35 - 30.36 -#include "../game.h" 30.37 - 30.38 -#ifdef __cplusplus 30.39 -extern "C" { 30.40 -#endif 30.41 - 30.42 -_Bool pawn_chkrules(Board board, Move *move); 30.43 -_Bool pawn_isblocked(Board board, Move *move); 30.44 -int pawn_getlocation(Board board, Move *move); 30.45 - 30.46 -#ifdef __cplusplus 30.47 -} 30.48 -#endif 30.49 - 30.50 -#endif /* PAWN_H */ 30.51 -
31.1 --- a/src/rules/queen.c Sat Mar 29 16:53:58 2014 +0100 31.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 31.3 @@ -1,46 +0,0 @@ 31.4 -/* 31.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 31.6 - * 31.7 - * Copyright 2014 Mike Becker. All rights reserved. 31.8 - * 31.9 - * Redistribution and use in source and binary forms, with or without 31.10 - * modification, are permitted provided that the following conditions are met: 31.11 - * 31.12 - * 1. Redistributions of source code must retain the above copyright 31.13 - * notice, this list of conditions and the following disclaimer. 31.14 - * 31.15 - * 2. Redistributions in binary form must reproduce the above copyright 31.16 - * notice, this list of conditions and the following disclaimer in the 31.17 - * documentation and/or other materials provided with the distribution. 31.18 - * 31.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 31.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 31.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 31.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 31.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 31.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31.29 - * POSSIBILITY OF SUCH DAMAGE. 31.30 - * 31.31 - */ 31.32 - 31.33 -#include "rules.h" 31.34 -#include "queen.h" 31.35 - 31.36 -_Bool queen_chkrules(Move* move) { 31.37 - // TODO: implement 31.38 - return FALSE; 31.39 -} 31.40 - 31.41 -_Bool queen_isblocked(Board board, Move *move) { 31.42 - // TODO: implement 31.43 - return TRUE; 31.44 -} 31.45 - 31.46 -int queen_getlocation(Board board, Move *move) { 31.47 - // TODO: implement 31.48 - return INVALID_MOVE_SYNTAX; 31.49 -}
32.1 --- a/src/rules/queen.h Sat Mar 29 16:53:58 2014 +0100 32.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 32.3 @@ -1,48 +0,0 @@ 32.4 -/* 32.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 32.6 - * 32.7 - * Copyright 2014 Mike Becker. All rights reserved. 32.8 - * 32.9 - * Redistribution and use in source and binary forms, with or without 32.10 - * modification, are permitted provided that the following conditions are met: 32.11 - * 32.12 - * 1. Redistributions of source code must retain the above copyright 32.13 - * notice, this list of conditions and the following disclaimer. 32.14 - * 32.15 - * 2. Redistributions in binary form must reproduce the above copyright 32.16 - * notice, this list of conditions and the following disclaimer in the 32.17 - * documentation and/or other materials provided with the distribution. 32.18 - * 32.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 32.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 32.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 32.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 32.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32.29 - * POSSIBILITY OF SUCH DAMAGE. 32.30 - * 32.31 - */ 32.32 - 32.33 -#ifndef QUEEN_H 32.34 -#define QUEEN_H 32.35 - 32.36 -#include "../game.h" 32.37 - 32.38 -#ifdef __cplusplus 32.39 -extern "C" { 32.40 -#endif 32.41 - 32.42 -_Bool queen_chkrules(Move *move); 32.43 -_Bool queen_isblocked(Board board, Move *move); 32.44 -int queen_getlocation(Board board, Move *move); 32.45 - 32.46 -#ifdef __cplusplus 32.47 -} 32.48 -#endif 32.49 - 32.50 -#endif /* QUEEN_H */ 32.51 -
33.1 --- a/src/rules/rook.c Sat Mar 29 16:53:58 2014 +0100 33.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 33.3 @@ -1,46 +0,0 @@ 33.4 -/* 33.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 33.6 - * 33.7 - * Copyright 2014 Mike Becker. All rights reserved. 33.8 - * 33.9 - * Redistribution and use in source and binary forms, with or without 33.10 - * modification, are permitted provided that the following conditions are met: 33.11 - * 33.12 - * 1. Redistributions of source code must retain the above copyright 33.13 - * notice, this list of conditions and the following disclaimer. 33.14 - * 33.15 - * 2. Redistributions in binary form must reproduce the above copyright 33.16 - * notice, this list of conditions and the following disclaimer in the 33.17 - * documentation and/or other materials provided with the distribution. 33.18 - * 33.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 33.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 33.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 33.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 33.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33.29 - * POSSIBILITY OF SUCH DAMAGE. 33.30 - * 33.31 - */ 33.32 - 33.33 -#include "rules.h" 33.34 -#include "rook.h" 33.35 - 33.36 -_Bool rook_chkrules(Move *move) { 33.37 - // TODO: implement 33.38 - return FALSE; 33.39 -} 33.40 - 33.41 -_Bool rook_isblocked(Board board, Move *move) { 33.42 - // TODO: implement 33.43 - return TRUE; 33.44 -} 33.45 - 33.46 -int rook_getlocation(Board board, Move *move) { 33.47 - // TODO: implement 33.48 - return INVALID_MOVE_SYNTAX; 33.49 -}
34.1 --- a/src/rules/rook.h Sat Mar 29 16:53:58 2014 +0100 34.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 34.3 @@ -1,48 +0,0 @@ 34.4 -/* 34.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 34.6 - * 34.7 - * Copyright 2014 Mike Becker. All rights reserved. 34.8 - * 34.9 - * Redistribution and use in source and binary forms, with or without 34.10 - * modification, are permitted provided that the following conditions are met: 34.11 - * 34.12 - * 1. Redistributions of source code must retain the above copyright 34.13 - * notice, this list of conditions and the following disclaimer. 34.14 - * 34.15 - * 2. Redistributions in binary form must reproduce the above copyright 34.16 - * notice, this list of conditions and the following disclaimer in the 34.17 - * documentation and/or other materials provided with the distribution. 34.18 - * 34.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 34.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 34.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 34.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 34.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 34.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 34.29 - * POSSIBILITY OF SUCH DAMAGE. 34.30 - * 34.31 - */ 34.32 - 34.33 -#ifndef ROOK_H 34.34 -#define ROOK_H 34.35 - 34.36 -#include "../game.h" 34.37 - 34.38 -#ifdef __cplusplus 34.39 -extern "C" { 34.40 -#endif 34.41 - 34.42 -_Bool rook_chkrules(Move *move); 34.43 -_Bool rook_isblocked(Board board, Move *move); 34.44 -int rook_getlocation(Board board, Move *move); 34.45 - 34.46 -#ifdef __cplusplus 34.47 -} 34.48 -#endif 34.49 - 34.50 -#endif /* ROOK_H */ 34.51 -
35.1 --- a/src/rules/rules.h Sat Mar 29 16:53:58 2014 +0100 35.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 35.3 @@ -1,47 +0,0 @@ 35.4 -/* 35.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 35.6 - * 35.7 - * Copyright 2014 Mike Becker. All rights reserved. 35.8 - * 35.9 - * Redistribution and use in source and binary forms, with or without 35.10 - * modification, are permitted provided that the following conditions are met: 35.11 - * 35.12 - * 1. Redistributions of source code must retain the above copyright 35.13 - * notice, this list of conditions and the following disclaimer. 35.14 - * 35.15 - * 2. Redistributions in binary form must reproduce the above copyright 35.16 - * notice, this list of conditions and the following disclaimer in the 35.17 - * documentation and/or other materials provided with the distribution. 35.18 - * 35.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 35.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 35.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 35.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 35.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 35.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.29 - * POSSIBILITY OF SUCH DAMAGE. 35.30 - * 35.31 - */ 35.32 - 35.33 -#ifndef RULES_H 35.34 -#define RULES_H 35.35 - 35.36 -#include "pawn.h" 35.37 -#include "rook.h" 35.38 -#include "knight.h" 35.39 -#include "bishop.h" 35.40 -#include "queen.h" 35.41 -#include "king.h" 35.42 - 35.43 -#define VALID_MOVE_SYNTAX 0 35.44 -#define INVALID_MOVE_SYNTAX 1 35.45 -#define INVALID_POSITION 2 35.46 -#define AMBIGUOUS_MOVE 3 35.47 -#define NEED_PROMOTION 4 35.48 - 35.49 -#endif /* RULES_H */ 35.50 -