src/chess/rules.h

changeset 23
824c9522ce66
parent 19
6a26114297a1
child 25
3ab0c2e1a4e2
equal deleted inserted replaced
22:41bbfd4d17a3 23:824c9522ce66
66 #define BQUEEN (BLACK|QUEEN) 66 #define BQUEEN (BLACK|QUEEN)
67 #define BKING (BLACK|KING) 67 #define BKING (BLACK|KING)
68 68
69 typedef uint8_t Board[8][8]; 69 typedef uint8_t Board[8][8];
70 70
71
71 typedef struct { 72 typedef struct {
72 uint8_t piece; 73 uint8_t piece;
73 uint8_t fromfile; 74 uint8_t fromfile;
74 uint8_t fromrow; 75 uint8_t fromrow;
75 uint8_t tofile; 76 uint8_t tofile;
77 uint8_t promotion; 78 uint8_t promotion;
78 _Bool check; 79 _Bool check;
79 _Bool checkmate; 80 _Bool checkmate;
80 _Bool capture; 81 _Bool capture;
81 } Move; 82 } Move;
83
84 typedef struct MoveList MoveList;
85
86 struct MoveList {
87 Move move;
88 MoveList* next;
89 };
90
91 typedef struct {
92 Board board;
93 uint8_t mycolor;
94 MoveList* movelist;
95 MoveList* lastmove;
96 } GameState;
82 97
83 #define POS_UNSPECIFIED UINT8_MAX 98 #define POS_UNSPECIFIED UINT8_MAX
84 #define mdst(b,m) b[(m)->torow][(m)->tofile] 99 #define mdst(b,m) b[(m)->torow][(m)->tofile]
85 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] 100 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
86 101
99 isidx((move)->tofile) && isidx((move)->torow)) 114 isidx((move)->tofile) && isidx((move)->torow))
100 115
101 /* secure versions - use, if index is not checked with isidx() */ 116 /* secure versions - use, if index is not checked with isidx() */
102 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) 117 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
103 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) 118 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
119
120 void gamestate_cleanup(GameState *gamestate);
104 121
105 /** 122 /**
106 * Maps a character to a piece. 123 * Maps a character to a piece.
107 * 124 *
108 * Does not work for pawns, since they don't have a character. 125 * Does not work for pawns, since they don't have a character.
124 141
125 /** 142 /**
126 * Evaluates a move syntactically and stores the move data in the specified 143 * Evaluates a move syntactically and stores the move data in the specified
127 * object. 144 * object.
128 * 145 *
129 * @param board the current state of the board 146 * @param gamestate the current game state
130 * @param mycolor the color of the current player
131 * @param mstr the input string to parse 147 * @param mstr the input string to parse
132 * @param move a pointer to object where the move data shall be stored 148 * @param move a pointer to object where the move data shall be stored
133 * @return status code (see rules/rules.h for the list of codes) 149 * @return status code (see rules/rules.h for the list of codes)
134 */ 150 */
135 int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move); 151 int eval_move(GameState *gamestate, char *mstr, Move *move);
136 152
137 /** 153 /**
138 * Validates move by applying chess rules. 154 * Validates move by applying chess rules.
139 * @param board the current board state 155 * @param gamestate the current game state
140 * @param move the move to validate 156 * @param move the move to validate
141 * @return TRUE, if the move complies to chess rules, FALSE otherwise 157 * @return TRUE, if the move complies to chess rules, FALSE otherwise
142 */ 158 */
143 _Bool validate_move(Board board, Move *move); 159 _Bool validate_move(GameState *gamestate, Move *move);
144 160
145 /** 161 /**
146 * Applies a move and deletes captured pieces. 162 * Applies a move and deletes captured pieces.
147 * 163 *
148 * @param board the current board state 164 * @param gamestate the current game state
149 * @param move the move to apply 165 * @param move the move to apply
150 */ 166 */
151 void apply_move(Board board, Move *move); 167 void apply_move(GameState *gamestate, Move *move);
152 168
153 #endif /* RULES_H */ 169 #endif /* RULES_H */
154 170

mercurial