src/chess/rules.h

Tue, 18 Sep 2018 15:02:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Sep 2018 15:02:08 +0200
changeset 69
c8f2c280cff7
parent 63
611332453da0
permissions
-rw-r--r--

adds unicode support

     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 may have color or additional flags
   178  * @return character value for the specified piece
   179  */
   180 char getpiecechr(uint8_t piece);
   182 /**
   183  * Maps a piece to a unicode character sequence.
   184  * 
   185  * The returned unicode is for black pieces.
   186  * You may colorize the output by setting the terminal foreground color.
   187  * 
   188  * @param piece the piece to dispaly
   189  * @return unicode character sequence for the specified piece
   190  */
   191 unsigned char* getpieceunicode(uint8_t piece);
   193 /**
   194  * Checks, if a specified field is covered by a piece of a certain color.
   195  * 
   196  * The out-parameters may both be NULL, but if any of them is set, the other
   197  * must be set, too.
   198  * 
   199  * @param gamestate the current game state
   200  * @param row row of the field to check
   201  * @param file file of the field to check
   202  * @param color the color of the piece that should threaten the field
   203  * @param threats the array where to store the threats (should be able to hold
   204  * the rare maximum of 16 elements)
   205  * @param threatcount a pointer to an uint8_t where the count of threats is
   206  * stored
   207  * @return TRUE, if any piece of the specified color threatens the specified
   208  * field (i.e. could capture an opponent piece)
   209  */
   210 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
   211         uint8_t color, Move* threats, uint8_t* threatcount);
   213 /**
   214  * Checks, if a specified field is covered by a piece of a certain color AND
   215  * if this piece is not pinned and therefore able to perform the move.
   216  * 
   217  * The out-parameters may both be NULL, but if any of them is set, the other
   218  * must be set, too.
   219  * 
   220  * @param gamestate the current game state
   221  * @param row row of the field to check
   222  * @param file file of the field to check
   223  * @param color the color of the piece that should threaten the field
   224  * @param threats the array where to store the threats (should be able to hold
   225  * the rare maximum of 16 elements)
   226  * @param threatcount a pointer to an uint8_t where the count of threats is
   227  * stored
   228  * @return TRUE, if any piece of the specified color threatens the specified
   229  * field (i.e. could capture an opponent piece)
   230  */
   231 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
   232         uint8_t color, Move* threats, uint8_t* threatcount);
   234 /**
   235  * Checks, if a specified field is covered by a piece of a certain color.
   236  * 
   237  * @param gamestate the current game state
   238  * @param row row of the field to check
   239  * @param file file of the field to check
   240  * @param color the color of the piece that should cover the field
   241  * @return TRUE, if any piece of the specified color threatens the specified
   242  * field
   243  */
   244 #define is_covered(gamestate, row, file, color) \
   245     get_threats(gamestate, row, file, color, NULL, NULL)
   247 /**
   248  * Checks, if a specified field is attacked by a piece of a certain color.
   249  * 
   250  * I.e. the field is covered by a piece AND this piece is not pinned and
   251  * therefore able to perform the move.
   252  * 
   253  * @param gamestate the current game state
   254  * @param row row of the field to check
   255  * @param file file of the field to check
   256  * @param color the color of the piece that should cover the field
   257  * @return TRUE, if any piece of the specified color threatens the specified
   258  * field and could capture an opponent piece
   259  */
   260 #define is_attacked(gamestate, row, file, color) \
   261     get_real_threats(gamestate, row, file, color, NULL, NULL)
   263 /**
   264  * Checks, if a specified field is protected by a piece of a certain color.
   265  * 
   266  * I.e. the field is covered by a piece that is NOT the king AND this piece is
   267  * not pinned and therefore able to perform the move.
   268  * 
   269  * @param gamestate the current game state
   270  * @param row row of the field to check
   271  * @param file file of the field to check
   272  * @param color the color of the piece that should cover the field
   273  * @return TRUE, if any piece (excluding the king) of the specified color
   274  * threatens the specified field and could capture an opponent piece
   275  */
   276 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
   277         uint8_t color);
   279 /**
   280  * Checks, if the specified move cannot be performed, because the piece is
   281  * either pinned or cannot remove the check.
   282  * 
   283  * Note: in chess a piece is pinned, when it can't be moved because the move
   284  * would result in a check position. But this function <u>also</u> returns true,
   285  * if the king is already in check position and the specified move does not
   286  * protect the king.
   287  * 
   288  * @param gamestate the current game state
   289  * @param move the move to check
   290  * @return TRUE, if the move cannot be performed because the king would be in
   291  * check after the move
   292  */
   293 _Bool is_pinned(GameState *gamestate, Move *move);
   295 /**
   296  * Evaluates a move syntactically and stores the move data in the specified
   297  * object.
   298  * 
   299  * @param gamestate the current game state
   300  * @param mstr the input string to parse
   301  * @param move a pointer to object where the move data shall be stored
   302  * @param color the color of the player to evaluate the move for
   303  * @return status code (see macros in this file for the list of codes)
   304  */
   305 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color);
   307 /**
   308  * Validates move by applying chess rules.
   309  * @param gamestate the current game state
   310  * @param move the move to validate
   311  * @return status code (see macros in this file for the list of codes)
   312  */
   313 int validate_move(GameState *gamestate, Move *move);
   315 /**
   316  * Applies a move and deletes captured pieces.
   317  * 
   318  * @param gamestate the current game state
   319  * @param move the move to apply
   320  */
   321 void apply_move(GameState *gamestate, Move *move);
   324 /**
   325  * Returns the remaining time on the clock for the specified player.
   326  *
   327  * @param gameinfo the information about the game
   328  * @param gamestate the current game state
   329  * @param color either BLACK or WHITE
   330  * @return the remaining time - if time control is disabled, this function
   331  * always returns zero
   332  */
   333 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
   334         uint8_t color);
   336 #endif	/* RULES_H */

mercurial