src/chess/rules.h

Wed, 09 Apr 2014 09:34:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 09:34:07 +0200
changeset 32
8a0b85303ee8
parent 30
a285ee393860
child 33
866025982aa9
permissions
-rw-r--r--

improved async input + improved build system + added time values to move struct

     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 <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];
    73 typedef struct {
    74     uint8_t piece;
    75     uint8_t fromfile;
    76     uint8_t fromrow;
    77     uint8_t tofile;
    78     uint8_t torow;
    79     uint8_t promotion;
    80     struct timespec timestamp;
    81     struct timespec movetime;
    82     _Bool check;
    83     _Bool capture;
    84 } Move;
    86 typedef struct MoveList MoveList;
    88 struct MoveList {
    89     Move move;
    90     MoveList* next;
    91 };
    94 typedef struct {
    95     uint8_t servercolor;
    96     _Bool timecontrol;
    97     uint16_t time;
    98     uint16_t addtime;
    99 } GameInfo;
   101 typedef struct {
   102     Board board;
   103     uint8_t mycolor;
   104     MoveList* movelist;
   105     MoveList* lastmove;
   106     _Bool checkmate;
   107     _Bool stalemate;
   108 } GameState;
   110 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
   112 #define POS_UNSPECIFIED UINT8_MAX
   113 #define mdst(b,m) b[(m)->torow][(m)->tofile]
   114 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
   116 #define isidx(idx) ((uint8_t)(idx) < 8)
   118 #define isfile(file) (file >= 'a' && file <= 'h')
   119 #define isrow(row) (row >= '1' && row <= '8')
   121 #define rowidx(row) (row-'1')
   122 #define fileidx(file) (file-'a')
   124 #define rowchr(row) (row+'1')
   125 #define filechr(file) (file+'a')
   127 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
   128         isidx((move)->tofile) && isidx((move)->torow))
   130 /* secure versions - use, if index is not checked with isidx() */
   131 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
   132 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
   134 void gamestate_cleanup(GameState *gamestate);
   136 /**
   137  * Maps a character to a piece.
   138  * 
   139  * Does not work for pawns, since they don't have a character.
   140  * 
   141  * @param c one of R,N,B,Q,K
   142  * @return numeric value for the specified piece
   143  */
   144 uint8_t getpiece(char c);
   146 /**
   147  * Maps a piece to a character.
   148  * 
   149  * Does not work for pawns, scince they don't have a character.
   150  * 
   151  * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
   152  * @return character value for the specified piece
   153  */
   154 char getpiecechr(uint8_t piece);
   156 /**
   157  * Checks, if a specified field is covered by a piece of a certain color.
   158  * 
   159  * The out-parameters may both be NULL, but if any of them is set, the other
   160  * must be set, too.
   161  * 
   162  * @param gamestate the current game state
   163  * @param row row of the field to check
   164  * @param file file of the field to check
   165  * @param color the color of the piece that should threaten the field
   166  * @param threats the array where to store the threats (should be able to the
   167  * rare maximum of 16 elements)
   168  * @param threatcount a pointer to an uint8_t where to store the amount of threats
   169  * @return TRUE, if any piece of the specified color threatens the specified
   170  * field (i.e. could capture an opponent piece)
   171  */
   172 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
   173         uint8_t color, Move* threats, uint8_t* threatcount);
   175 /**
   176  * Checks, if a specified field is covered by a piece of a certain color AND
   177  * if this piece is not pinned and therefore able to perform the move.
   178  * 
   179  * The out-parameters may both be NULL, but if any of them is set, the other
   180  * must be set, too.
   181  * 
   182  * @param gamestate the current game state
   183  * @param row row of the field to check
   184  * @param file file of the field to check
   185  * @param color the color of the piece that should threaten the field
   186  * @param threats the array where to store the threats (should be able to the
   187  * rare maximum of 16 elements)
   188  * @param threatcount a pointer to an uint8_t where to store the amount of threats
   189  * @return TRUE, if any piece of the specified color threatens the specified
   190  * field (i.e. could capture an opponent piece)
   191  */
   192 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
   193         uint8_t color, Move* threats, uint8_t* threatcount);
   195 /**
   196  * Checks, if a specified field is covered by a piece of a certain color.
   197  * 
   198  * @param gamestate the current game state
   199  * @param row row of the field to check
   200  * @param file file of the field to check
   201  * @param color the color of the piece that should cover the field
   202  * @return TRUE, if any piece of the specified color threatens the specified
   203  * field
   204  */
   205 #define is_covered(gamestate, row, file, color) \
   206     get_threats(gamestate, row, file, color, NULL, NULL)
   208 /**
   209  * Checks, if a specified field is attacked by a piece of a certain color.
   210  * 
   211  * I.e. the field is covered by a piece AND this piece is not pinned and
   212  * therefore able to perform the move.
   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 and could capture an opponent piece
   220  */
   221 #define is_attacked(gamestate, row, file, color) \
   222     get_threats(gamestate, row, file, color, NULL, NULL)
   224 /**
   225  * Checks, if a specified field is protected by a piece of a certain color.
   226  * 
   227  * I.e. the field is covered by a piece that is NOT the king AND this piece is
   228  * not pinned and 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 (excluding the king) of the specified color
   235  * threatens the specified field and could capture an opponent piece
   236  */
   237 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
   238         uint8_t color);
   240 /**
   241  * Evaluates a move syntactically and stores the move data in the specified
   242  * object.
   243  * 
   244  * @param gamestate the current game state
   245  * @param mstr the input string to parse
   246  * @param move a pointer to object where the move data shall be stored
   247  * @return status code (see rules/rules.h for the list of codes)
   248  */
   249 int eval_move(GameState *gamestate, char *mstr, Move *move);
   251 /**
   252  * Validates move by applying chess rules.
   253  * @param gamestate the current game state
   254  * @param move the move to validate
   255  * @return TRUE, if the move complies to chess rules, FALSE otherwise
   256  */
   257 _Bool validate_move(GameState *gamestate, Move *move);
   259 /**
   260  * Applies a move and deletes captured pieces.
   261  * 
   262  * @param gamestate the current game state
   263  * @param move the move to apply
   264  */
   265 void apply_move(GameState *gamestate, Move *move);
   267 #endif	/* RULES_H */

mercurial