src/chess/rules.h

Wed, 29 Aug 2018 13:45:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:45:13 +0200
changeset 63
611332453da0
parent 61
e3a1a794351e
child 69
c8f2c280cff7
permissions
-rw-r--r--

move log has now three columns and starts scrolling after 45 moves

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2016 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 VALID_MOVE_SEMANTICS   0 /* use same code for a success */
    38 #define INVALID_MOVE_SYNTAX    1
    39 #define INVALID_POSITION       2
    40 #define AMBIGUOUS_MOVE         3
    41 #define NEED_PROMOTION         4
    42 #define PIECE_PINNED           5
    43 #define KING_IN_CHECK          6
    44 #define KING_MOVES_INTO_CHECK  7
    45 #define RULES_VIOLATED        10
    48 #define PIECE_MASK       0x0F
    49 #define COLOR_MASK       0x30
    50 #define ENPASSANT_THREAT 0x40
    52 #define WHITE 0x10
    53 #define BLACK 0x20
    55 #define PAWN   0x01
    56 #define ROOK   0x02
    57 #define KNIGHT 0x03
    58 #define BISHOP 0x04
    59 #define QUEEN  0x05
    60 #define KING   0x06
    62 #define WPAWN   (WHITE|PAWN)
    63 #define WROOK   (WHITE|ROOK)
    64 #define WKNIGHT (WHITE|KNIGHT)
    65 #define WBISHOP (WHITE|BISHOP)
    66 #define WQUEEN  (WHITE|QUEEN)
    67 #define WKING   (WHITE|KING)
    68 #define BPAWN   (BLACK|PAWN)
    69 #define BROOK   (BLACK|ROOK)
    70 #define BKNIGHT (BLACK|KNIGHT)
    71 #define BBISHOP (BLACK|BISHOP)
    72 #define BQUEEN  (BLACK|QUEEN)
    73 #define BKING   (BLACK|KING)
    75 typedef uint8_t Board[8][8];
    77 struct movetimeval {
    78     uint64_t tv_sec;
    79     int32_t tv_usec;
    80 };
    82 typedef struct {
    83     uint8_t piece;
    84     uint8_t fromfile;
    85     uint8_t fromrow;
    86     uint8_t tofile;
    87     uint8_t torow;
    88     uint8_t promotion;
    89     uint8_t check;
    90     uint8_t capture;
    91     struct movetimeval timestamp;
    92     struct movetimeval movetime;
    93     char string[8];
    94 } Move;
    96 typedef struct MoveList MoveList;
    98 struct MoveList {
    99     Move move;
   100     MoveList* next;
   101 };
   104 typedef struct {
   105     uint8_t servercolor;
   106     uint8_t timecontrol;
   107     uint16_t time;
   108     uint16_t addtime;
   109 } GameInfo;
   111 typedef struct {
   112     Board board;
   113     MoveList* movelist;
   114     MoveList* lastmove;
   115     unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */
   116     _Bool checkmate;
   117     _Bool stalemate;
   118     _Bool remis;
   119     _Bool resign;
   120 } GameState;
   122 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
   124 #define POS_UNSPECIFIED UINT8_MAX
   125 #define mdst(b,m) b[(m)->torow][(m)->tofile]
   126 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
   128 #define isidx(idx) ((uint8_t)(idx) < 8)
   130 #define isfile(file) (file >= 'a' && file <= 'h')
   131 #define isrow(row) (row >= '1' && row <= '8')
   133 #define rowidx(row) (row-'1')
   134 #define fileidx(file) (file-'a')
   136 #define rowchr(row) (row+'1')
   137 #define filechr(file) (file+'a')
   139 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
   140         isidx((move)->tofile) && isidx((move)->torow))
   142 /* secure versions - use, if index is not checked with isidx() */
   143 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
   144 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
   146 #define is_game_running(gamestate) !((gamestate)->checkmate || \
   147     (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis)
   150 /**
   151  * Initializes a game state and prepares the chess board.
   152  * @param gamestate the game state to initialize
   153  */
   154 void gamestate_init(GameState *gamestate);
   156 /**
   157  * Cleans up a game state and frees the memory for the movement list.
   158  * @param gamestate the game state to clean up
   159  */
   160 void gamestate_cleanup(GameState *gamestate);
   162 /**
   163  * Maps a character to a piece.
   164  * 
   165  * Does not work for pawns, since they don't have a character.
   166  * 
   167  * @param c one of R,N,B,Q,K
   168  * @return numeric value for the specified piece
   169  */
   170 uint8_t getpiece(char c);
   172 /**
   173  * Maps a piece to a character.
   174  * 
   175  * Does not work for pawns, scince they don't have a character.
   176  * 
   177  * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
   178  * @return character value for the specified piece
   179  */
   180 char getpiecechr(uint8_t piece);
   182 /**
   183  * Checks, if a specified field is covered by a piece of a certain color.
   184  * 
   185  * The out-parameters may both be NULL, but if any of them is set, the other
   186  * must be set, too.
   187  * 
   188  * @param gamestate the current game state
   189  * @param row row of the field to check
   190  * @param file file of the field to check
   191  * @param color the color of the piece that should threaten the field
   192  * @param threats the array where to store the threats (should be able to hold
   193  * the rare maximum of 16 elements)
   194  * @param threatcount a pointer to an uint8_t where the count of threats is
   195  * stored
   196  * @return TRUE, if any piece of the specified color threatens the specified
   197  * field (i.e. could capture an opponent piece)
   198  */
   199 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
   200         uint8_t color, Move* threats, uint8_t* threatcount);
   202 /**
   203  * Checks, if a specified field is covered by a piece of a certain color AND
   204  * if this piece is not pinned and therefore able to perform the move.
   205  * 
   206  * The out-parameters may both be NULL, but if any of them is set, the other
   207  * must be set, too.
   208  * 
   209  * @param gamestate the current game state
   210  * @param row row of the field to check
   211  * @param file file of the field to check
   212  * @param color the color of the piece that should threaten the field
   213  * @param threats the array where to store the threats (should be able to hold
   214  * the rare maximum of 16 elements)
   215  * @param threatcount a pointer to an uint8_t where the count of threats is
   216  * stored
   217  * @return TRUE, if any piece of the specified color threatens the specified
   218  * field (i.e. could capture an opponent piece)
   219  */
   220 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
   221         uint8_t color, Move* threats, uint8_t* threatcount);
   223 /**
   224  * Checks, if a specified field is covered by a piece of a certain color.
   225  * 
   226  * @param gamestate the current game state
   227  * @param row row of the field to check
   228  * @param file file of the field to check
   229  * @param color the color of the piece that should cover the field
   230  * @return TRUE, if any piece of the specified color threatens the specified
   231  * field
   232  */
   233 #define is_covered(gamestate, row, file, color) \
   234     get_threats(gamestate, row, file, color, NULL, NULL)
   236 /**
   237  * Checks, if a specified field is attacked by a piece of a certain color.
   238  * 
   239  * I.e. the field is covered by a piece AND this piece is not pinned and
   240  * therefore able to perform the move.
   241  * 
   242  * @param gamestate the current game state
   243  * @param row row of the field to check
   244  * @param file file of the field to check
   245  * @param color the color of the piece that should cover the field
   246  * @return TRUE, if any piece of the specified color threatens the specified
   247  * field and could capture an opponent piece
   248  */
   249 #define is_attacked(gamestate, row, file, color) \
   250     get_real_threats(gamestate, row, file, color, NULL, NULL)
   252 /**
   253  * Checks, if a specified field is protected by a piece of a certain color.
   254  * 
   255  * I.e. the field is covered by a piece that is NOT the king AND this piece is
   256  * not pinned and therefore able to perform the move.
   257  * 
   258  * @param gamestate the current game state
   259  * @param row row of the field to check
   260  * @param file file of the field to check
   261  * @param color the color of the piece that should cover the field
   262  * @return TRUE, if any piece (excluding the king) of the specified color
   263  * threatens the specified field and could capture an opponent piece
   264  */
   265 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
   266         uint8_t color);
   268 /**
   269  * Checks, if the specified move cannot be performed, because the piece is
   270  * either pinned or cannot remove the check.
   271  * 
   272  * Note: in chess a piece is pinned, when it can't be moved because the move
   273  * would result in a check position. But this function <u>also</u> returns true,
   274  * if the king is already in check position and the specified move does not
   275  * protect the king.
   276  * 
   277  * @param gamestate the current game state
   278  * @param move the move to check
   279  * @return TRUE, if the move cannot be performed because the king would be in
   280  * check after the move
   281  */
   282 _Bool is_pinned(GameState *gamestate, Move *move);
   284 /**
   285  * Evaluates a move syntactically and stores the move data in the specified
   286  * object.
   287  * 
   288  * @param gamestate the current game state
   289  * @param mstr the input string to parse
   290  * @param move a pointer to object where the move data shall be stored
   291  * @param color the color of the player to evaluate the move for
   292  * @return status code (see macros in this file for the list of codes)
   293  */
   294 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color);
   296 /**
   297  * Validates move by applying chess rules.
   298  * @param gamestate the current game state
   299  * @param move the move to validate
   300  * @return status code (see macros in this file for the list of codes)
   301  */
   302 int validate_move(GameState *gamestate, Move *move);
   304 /**
   305  * Applies a move and deletes captured pieces.
   306  * 
   307  * @param gamestate the current game state
   308  * @param move the move to apply
   309  */
   310 void apply_move(GameState *gamestate, Move *move);
   313 /**
   314  * Returns the remaining time on the clock for the specified player.
   315  *
   316  * @param gameinfo the information about the game
   317  * @param gamestate the current game state
   318  * @param color either BLACK or WHITE
   319  * @return the remaining time - if time control is disabled, this function
   320  * always returns zero
   321  */
   322 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
   323         uint8_t color);
   325 #endif	/* RULES_H */

mercurial