src/chess/rules.h

Thu, 17 Apr 2014 12:16:14 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 17 Apr 2014 12:16:14 +0200
changeset 46
4dcfb4c58b6d
parent 41
a8346dcf7bbf
child 47
d726e4b46c33
permissions
-rw-r--r--

netcode is now aware of connection losses

10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #ifndef RULES_H
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
31 #define RULES_H
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
32
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
33 #include <stdint.h>
36
ebe0c961e9a6 reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
34 #include <sys/time.h>
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
36 #define VALID_MOVE_SYNTAX 0
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
37 #define INVALID_MOVE_SYNTAX 1
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
38 #define INVALID_POSITION 2
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
39 #define AMBIGUOUS_MOVE 3
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 16
diff changeset
40 #define NEED_PROMOTION 4
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
41
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
42
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
43 #define PIECE_MASK 0x0F
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
44 #define COLOR_MASK 0x30
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
45 #define ENPASSANT_THREAT 0x40
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
46
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
47 #define WHITE 0x10
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
48 #define BLACK 0x20
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
49
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
50 #define PAWN 0x01
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
51 #define ROOK 0x02
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
52 #define KNIGHT 0x03
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
53 #define BISHOP 0x04
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
54 #define QUEEN 0x05
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
55 #define KING 0x06
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
56
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
57 #define WPAWN (WHITE|PAWN)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
58 #define WROOK (WHITE|ROOK)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
59 #define WKNIGHT (WHITE|KNIGHT)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
60 #define WBISHOP (WHITE|BISHOP)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
61 #define WQUEEN (WHITE|QUEEN)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
62 #define WKING (WHITE|KING)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
63 #define BPAWN (BLACK|PAWN)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
64 #define BROOK (BLACK|ROOK)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
65 #define BKNIGHT (BLACK|KNIGHT)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
66 #define BBISHOP (BLACK|BISHOP)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
67 #define BQUEEN (BLACK|QUEEN)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
68 #define BKING (BLACK|KING)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
69
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
70 typedef uint8_t Board[8][8];
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
71
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
72 struct movetimeval {
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
73 uint64_t tv_sec;
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
74 int32_t tv_usec;
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
75 };
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
76
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
77 typedef struct {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
78 uint8_t piece;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
79 uint8_t fromfile;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
80 uint8_t fromrow;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
81 uint8_t tofile;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
82 uint8_t torow;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
83 uint8_t promotion;
41
a8346dcf7bbf faced struct alignment problems
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
84 uint8_t check;
a8346dcf7bbf faced struct alignment problems
Mike Becker <universe@uap-core.de>
parents: 40
diff changeset
85 uint8_t capture;
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
86 struct movetimeval timestamp;
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
87 struct movetimeval movetime;
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
88 } Move;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
89
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
90 typedef struct MoveList MoveList;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
91
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
92 struct MoveList {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
93 Move move;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
94 MoveList* next;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
95 };
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
96
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
97
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
98 typedef struct {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
99 uint8_t servercolor;
40
47162a7621da fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents: 36
diff changeset
100 uint8_t timecontrol;
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
101 uint16_t time;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
102 uint16_t addtime;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
103 } GameInfo;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 29
diff changeset
104
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
105 typedef struct {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
106 Board board;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
107 uint8_t mycolor;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
108 MoveList* movelist;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
109 MoveList* lastmove;
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
110 _Bool checkmate;
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
111 _Bool stalemate;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
112 } GameState;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
113
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
114 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
115
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
116 #define POS_UNSPECIFIED UINT8_MAX
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
117 #define mdst(b,m) b[(m)->torow][(m)->tofile]
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
118 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
119
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
120 #define isidx(idx) ((uint8_t)(idx) < 8)
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
121
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
122 #define isfile(file) (file >= 'a' && file <= 'h')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
123 #define isrow(row) (row >= '1' && row <= '8')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
124
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
125 #define rowidx(row) (row-'1')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
126 #define fileidx(file) (file-'a')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
127
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
128 #define rowchr(row) (row+'1')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
129 #define filechr(file) (file+'a')
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
130
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
131 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
132 isidx((move)->tofile) && isidx((move)->torow))
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
133
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
134 /* secure versions - use, if index is not checked with isidx() */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
135 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
136 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
137
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
138 void gamestate_cleanup(GameState *gamestate);
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
139
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
140 /**
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
141 * Maps a character to a piece.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
142 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
143 * Does not work for pawns, since they don't have a character.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
144 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
145 * @param c one of R,N,B,Q,K
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
146 * @return numeric value for the specified piece
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
147 */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
148 uint8_t getpiece(char c);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
149
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
150 /**
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
151 * Maps a piece to a character.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
152 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
153 * Does not work for pawns, scince they don't have a character.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
154 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
155 * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
156 * @return character value for the specified piece
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
157 */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
158 char getpiecechr(uint8_t piece);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
159
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
160 /**
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
161 * Checks, if a specified field is covered by a piece of a certain color.
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
162 *
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
163 * The out-parameters may both be NULL, but if any of them is set, the other
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
164 * must be set, too.
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
165 *
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
166 * @param gamestate the current game state
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
167 * @param row row of the field to check
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
168 * @param file file of the field to check
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
169 * @param color the color of the piece that should threaten the field
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
170 * @param threats the array where to store the threats (should be able to the
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
171 * rare maximum of 16 elements)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
172 * @param threatcount a pointer to an uint8_t where to store the amount of threats
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
173 * @return TRUE, if any piece of the specified color threatens the specified
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
174 * field (i.e. could capture an opponent piece)
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
175 */
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
176 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
177 uint8_t color, Move* threats, uint8_t* threatcount);
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
178
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
179 /**
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
180 * Checks, if a specified field is covered by a piece of a certain color AND
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
181 * if this piece is not pinned and therefore able to perform the move.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
182 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
183 * The out-parameters may both be NULL, but if any of them is set, the other
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
184 * must be set, too.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
185 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
186 * @param gamestate the current game state
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
187 * @param row row of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
188 * @param file file of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
189 * @param color the color of the piece that should threaten the field
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
190 * @param threats the array where to store the threats (should be able to the
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
191 * rare maximum of 16 elements)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
192 * @param threatcount a pointer to an uint8_t where to store the amount of threats
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
193 * @return TRUE, if any piece of the specified color threatens the specified
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
194 * field (i.e. could capture an opponent piece)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
195 */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
196 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
197 uint8_t color, Move* threats, uint8_t* threatcount);
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
198
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
199 /**
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
200 * Checks, if a specified field is covered by a piece of a certain color.
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
201 *
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
202 * @param gamestate the current game state
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
203 * @param row row of the field to check
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
204 * @param file file of the field to check
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
205 * @param color the color of the piece that should cover the field
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
206 * @return TRUE, if any piece of the specified color threatens the specified
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
207 * field
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
208 */
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
209 #define is_covered(gamestate, row, file, color) \
29
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
210 get_threats(gamestate, row, file, color, NULL, NULL)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
211
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
212 /**
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
213 * Checks, if a specified field is attacked by a piece of a certain color.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
214 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
215 * I.e. the field is covered by a piece AND this piece is not pinned and
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
216 * therefore able to perform the move.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
217 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
218 * @param gamestate the current game state
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
219 * @param row row of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
220 * @param file file of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
221 * @param color the color of the piece that should cover the field
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
222 * @return TRUE, if any piece of the specified color threatens the specified
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
223 * field and could capture an opponent piece
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
224 */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
225 #define is_attacked(gamestate, row, file, color) \
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
226 get_threats(gamestate, row, file, color, NULL, NULL)
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
227
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
228 /**
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
229 * Checks, if a specified field is protected by a piece of a certain color.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
230 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
231 * I.e. the field is covered by a piece that is NOT the king AND this piece is
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
232 * not pinned and therefore able to perform the move.
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
233 *
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
234 * @param gamestate the current game state
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
235 * @param row row of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
236 * @param file file of the field to check
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
237 * @param color the color of the piece that should cover the field
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
238 * @return TRUE, if any piece (excluding the king) of the specified color
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
239 * threatens the specified field and could capture an opponent piece
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
240 */
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
241 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
c6a1ad6cf749 fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
242 uint8_t color);
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
243
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
244 /**
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
245 * Evaluates a move syntactically and stores the move data in the specified
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
246 * object.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
247 *
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
248 * @param gamestate the current game state
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
249 * @param mstr the input string to parse
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
250 * @param move a pointer to object where the move data shall be stored
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
251 * @return status code (see rules/rules.h for the list of codes)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
252 */
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
253 int eval_move(GameState *gamestate, char *mstr, Move *move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
254
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
255 /**
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
256 * Validates move by applying chess rules.
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
257 * @param gamestate the current game state
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
258 * @param move the move to validate
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
259 * @return TRUE, if the move complies to chess rules, FALSE otherwise
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
260 */
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
261 _Bool validate_move(GameState *gamestate, Move *move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
262
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
263 /**
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
264 * Applies a move and deletes captured pieces.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
265 *
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
266 * @param gamestate the current game state
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
267 * @param move the move to apply
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
268 */
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 19
diff changeset
269 void apply_move(GameState *gamestate, Move *move);
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
270
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
271
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
272 /**
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
273 * Returns the remaining time on the clock for the specified player.
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
274 *
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
275 * @param gameinfo the information about the game
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
276 * @param gamestate the current game state
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
277 * @param color either BLACK or WHITE
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
278 * @return the remaining time - if time control is disabled, this function
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
279 * always returns zero
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
280 */
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
281 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
282 uint8_t color);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
283
10
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 #endif /* RULES_H */
1347e4dabac0 prepared code base for implementing rules
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285

mercurial