src/chess/rules.h

Wed, 11 Jun 2014 16:54:20 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 11 Jun 2014 16:54:20 +0200
changeset 49
02c509a44e98
parent 48
0cedda2544da
child 50
41017d0a72c5
permissions
-rw-r--r--

logging string representation of moves in short algebraic notation

     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 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     uint8_t mycolor;
   114     MoveList* movelist;
   115     MoveList* lastmove;
   116     _Bool checkmate;
   117     _Bool stalemate;
   118 } GameState;
   120 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
   122 #define POS_UNSPECIFIED UINT8_MAX
   123 #define mdst(b,m) b[(m)->torow][(m)->tofile]
   124 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
   126 #define isidx(idx) ((uint8_t)(idx) < 8)
   128 #define isfile(file) (file >= 'a' && file <= 'h')
   129 #define isrow(row) (row >= '1' && row <= '8')
   131 #define rowidx(row) (row-'1')
   132 #define fileidx(file) (file-'a')
   134 #define rowchr(row) (row+'1')
   135 #define filechr(file) (file+'a')
   137 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
   138         isidx((move)->tofile) && isidx((move)->torow))
   140 /* secure versions - use, if index is not checked with isidx() */
   141 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
   142 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
   144 /**
   145  * Cleans up a game state and frees the memory for the movement list.
   146  * @param gamestate the game state to clean up
   147  */
   148 void gamestate_cleanup(GameState *gamestate);
   150 /**
   151  * Maps a character to a piece.
   152  * 
   153  * Does not work for pawns, since they don't have a character.
   154  * 
   155  * @param c one of R,N,B,Q,K
   156  * @return numeric value for the specified piece
   157  */
   158 uint8_t getpiece(char c);
   160 /**
   161  * Maps a piece to a character.
   162  * 
   163  * Does not work for pawns, scince they don't have a character.
   164  * 
   165  * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
   166  * @return character value for the specified piece
   167  */
   168 char getpiecechr(uint8_t piece);
   170 /**
   171  * Checks, if a specified field is covered by a piece of a certain color.
   172  * 
   173  * The out-parameters may both be NULL, but if any of them is set, the other
   174  * must be set, too.
   175  * 
   176  * @param gamestate the current game state
   177  * @param row row of the field to check
   178  * @param file file of the field to check
   179  * @param color the color of the piece that should threaten the field
   180  * @param threats the array where to store the threats (should be able to hold
   181  * the rare maximum of 16 elements)
   182  * @param threatcount a pointer to an uint8_t where the count of threats is
   183  * stored
   184  * @return TRUE, if any piece of the specified color threatens the specified
   185  * field (i.e. could capture an opponent piece)
   186  */
   187 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
   188         uint8_t color, Move* threats, uint8_t* threatcount);
   190 /**
   191  * Checks, if a specified field is covered by a piece of a certain color AND
   192  * if this piece is not pinned and therefore able to perform the move.
   193  * 
   194  * The out-parameters may both be NULL, but if any of them is set, the other
   195  * must be set, too.
   196  * 
   197  * @param gamestate the current game state
   198  * @param row row of the field to check
   199  * @param file file of the field to check
   200  * @param color the color of the piece that should threaten the field
   201  * @param threats the array where to store the threats (should be able to hold
   202  * the rare maximum of 16 elements)
   203  * @param threatcount a pointer to an uint8_t where the count of threats is
   204  * stored
   205  * @return TRUE, if any piece of the specified color threatens the specified
   206  * field (i.e. could capture an opponent piece)
   207  */
   208 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
   209         uint8_t color, Move* threats, uint8_t* threatcount);
   211 /**
   212  * Checks, if a specified field is covered by a piece of a certain color.
   213  * 
   214  * @param gamestate the current game state
   215  * @param row row of the field to check
   216  * @param file file of the field to check
   217  * @param color the color of the piece that should cover the field
   218  * @return TRUE, if any piece of the specified color threatens the specified
   219  * field
   220  */
   221 #define is_covered(gamestate, row, file, color) \
   222     get_threats(gamestate, row, file, color, NULL, NULL)
   224 /**
   225  * Checks, if a specified field is attacked by a piece of a certain color.
   226  * 
   227  * I.e. the field is covered by a piece AND this piece is not pinned and
   228  * therefore able to perform the move.
   229  * 
   230  * @param gamestate the current game state
   231  * @param row row of the field to check
   232  * @param file file of the field to check
   233  * @param color the color of the piece that should cover the field
   234  * @return TRUE, if any piece of the specified color threatens the specified
   235  * field and could capture an opponent piece
   236  */
   237 #define is_attacked(gamestate, row, file, color) \
   238     get_threats(gamestate, row, file, color, NULL, NULL)
   240 /**
   241  * Checks, if a specified field is protected by a piece of a certain color.
   242  * 
   243  * I.e. the field is covered by a piece that is NOT the king AND this piece is
   244  * not pinned and therefore able to perform the move.
   245  * 
   246  * @param gamestate the current game state
   247  * @param row row of the field to check
   248  * @param file file of the field to check
   249  * @param color the color of the piece that should cover the field
   250  * @return TRUE, if any piece (excluding the king) of the specified color
   251  * threatens the specified field and could capture an opponent piece
   252  */
   253 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
   254         uint8_t color);
   256 /**
   257  * Checks, if the specified move cannot be performed, because the piece is
   258  * either pinned or cannot remove the check.
   259  * 
   260  * Note: in chess a piece is pinned, when it can't be moved because the move
   261  * would result in a check position. But this function <u>also</u> returns true,
   262  * if the king is already in check position and the specified move does not
   263  * protect the king.
   264  * 
   265  * @param gamestate the current game state
   266  * @param move the move to check
   267  * @return TRUE, if the move cannot be performed because the king would be in
   268  * check after the move
   269  */
   270 _Bool is_pinned(GameState *gamestate, Move *move);
   272 /**
   273  * Evaluates a move syntactically and stores the move data in the specified
   274  * object.
   275  * 
   276  * @param gamestate the current game state
   277  * @param mstr the input string to parse
   278  * @param move a pointer to object where the move data shall be stored
   279  * @return status code (see macros in this file for the list of codes)
   280  */
   281 int eval_move(GameState *gamestate, char *mstr, Move *move);
   283 /**
   284  * Validates move by applying chess rules.
   285  * @param gamestate the current game state
   286  * @param move the move to validate
   287  * @return status code (see macros in this file for the list of codes)
   288  */
   289 int validate_move(GameState *gamestate, Move *move);
   291 /**
   292  * Applies a move and deletes captured pieces.
   293  * 
   294  * @param gamestate the current game state
   295  * @param move the move to apply
   296  */
   297 void apply_move(GameState *gamestate, Move *move);
   300 /**
   301  * Returns the remaining time on the clock for the specified player.
   302  *
   303  * @param gameinfo the information about the game
   304  * @param gamestate the current game state
   305  * @param color either BLACK or WHITE
   306  * @return the remaining time - if time control is disabled, this function
   307  * always returns zero
   308  */
   309 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
   310         uint8_t color);
   312 #endif	/* RULES_H */

mercurial