src/chess/rules.h

Wed, 16 Apr 2014 21:46:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 Apr 2014 21:46:34 +0200
changeset 41
a8346dcf7bbf
parent 40
47162a7621da
child 47
d726e4b46c33
permissions
-rw-r--r--

faced struct alignment problems

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

mercurial