src/chess/rules.h

changeset 19
6a26114297a1
parent 18
6008840b859e
child 23
824c9522ce66
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/chess/rules.h	Mon Mar 31 11:16:32 2014 +0200
     1.3 @@ -0,0 +1,154 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2014 Mike Becker. All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + *
    1.31 + */
    1.32 +
    1.33 +#ifndef RULES_H
    1.34 +#define	RULES_H
    1.35 +
    1.36 +#include <stdint.h>
    1.37 +
    1.38 +#define VALID_MOVE_SYNTAX   0
    1.39 +#define INVALID_MOVE_SYNTAX 1
    1.40 +#define INVALID_POSITION    2
    1.41 +#define AMBIGUOUS_MOVE      3
    1.42 +#define NEED_PROMOTION      4
    1.43 +
    1.44 +
    1.45 +#define PIECE_MASK       0x0F
    1.46 +#define COLOR_MASK       0x30
    1.47 +#define ENPASSANT_THREAT 0x40
    1.48 +
    1.49 +#define WHITE 0x10
    1.50 +#define BLACK 0x20
    1.51 +
    1.52 +#define PAWN   0x01
    1.53 +#define ROOK   0x02
    1.54 +#define KNIGHT 0x03
    1.55 +#define BISHOP 0x04
    1.56 +#define QUEEN  0x05
    1.57 +#define KING   0x06
    1.58 +
    1.59 +#define WPAWN   (WHITE|PAWN)
    1.60 +#define WROOK   (WHITE|ROOK)
    1.61 +#define WKNIGHT (WHITE|KNIGHT)
    1.62 +#define WBISHOP (WHITE|BISHOP)
    1.63 +#define WQUEEN  (WHITE|QUEEN)
    1.64 +#define WKING   (WHITE|KING)
    1.65 +#define BPAWN   (BLACK|PAWN)
    1.66 +#define BROOK   (BLACK|ROOK)
    1.67 +#define BKNIGHT (BLACK|KNIGHT)
    1.68 +#define BBISHOP (BLACK|BISHOP)
    1.69 +#define BQUEEN  (BLACK|QUEEN)
    1.70 +#define BKING   (BLACK|KING)
    1.71 +
    1.72 +typedef uint8_t Board[8][8];
    1.73 +
    1.74 +typedef struct {
    1.75 +    uint8_t piece;
    1.76 +    uint8_t fromfile;
    1.77 +    uint8_t fromrow;
    1.78 +    uint8_t tofile;
    1.79 +    uint8_t torow;
    1.80 +    uint8_t promotion;
    1.81 +    _Bool check;
    1.82 +    _Bool checkmate;
    1.83 +    _Bool capture;
    1.84 +} Move;
    1.85 +
    1.86 +#define POS_UNSPECIFIED UINT8_MAX
    1.87 +#define mdst(b,m) b[(m)->torow][(m)->tofile]
    1.88 +#define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
    1.89 +
    1.90 +#define isidx(idx) ((uint8_t)idx < 8)
    1.91 +
    1.92 +#define isfile(file) (file >= 'a' && file <= 'h')
    1.93 +#define isrow(row) (row >= '1' && row <= '8')
    1.94 +
    1.95 +#define rowidx(row) (row-'1')
    1.96 +#define fileidx(file) (file-'a')
    1.97 +
    1.98 +#define rowchr(row) (row+'1')
    1.99 +#define filechr(file) (file+'a')
   1.100 +
   1.101 +#define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
   1.102 +        isidx((move)->tofile) && isidx((move)->torow))
   1.103 +
   1.104 +/* secure versions - use, if index is not checked with isidx() */
   1.105 +#define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
   1.106 +#define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
   1.107 +
   1.108 +/**
   1.109 + * Maps a character to a piece.
   1.110 + * 
   1.111 + * Does not work for pawns, since they don't have a character.
   1.112 + * 
   1.113 + * @param c one of R,N,B,Q,K
   1.114 + * @return numeric value for the specified piece
   1.115 + */
   1.116 +uint8_t getpiece(char c);
   1.117 +
   1.118 +/**
   1.119 + * Maps a piece to a character.
   1.120 + * 
   1.121 + * Does not work for pawns, scince they don't have a character.
   1.122 + * 
   1.123 + * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
   1.124 + * @return character value for the specified piece
   1.125 + */
   1.126 +char getpiecechr(uint8_t piece);
   1.127 +
   1.128 +/**
   1.129 + * Evaluates a move syntactically and stores the move data in the specified
   1.130 + * object.
   1.131 + * 
   1.132 + * @param board the current state of the board
   1.133 + * @param mycolor the color of the current player
   1.134 + * @param mstr the input string to parse
   1.135 + * @param move a pointer to object where the move data shall be stored
   1.136 + * @return status code (see rules/rules.h for the list of codes)
   1.137 + */
   1.138 +int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move);
   1.139 +
   1.140 +/**
   1.141 + * Validates move by applying chess rules.
   1.142 + * @param board the current board state
   1.143 + * @param move the move to validate
   1.144 + * @return TRUE, if the move complies to chess rules, FALSE otherwise
   1.145 + */
   1.146 +_Bool validate_move(Board board, Move *move);
   1.147 +
   1.148 +/**
   1.149 + * Applies a move and deletes captured pieces.
   1.150 + * 
   1.151 + * @param board the current board state
   1.152 + * @param move the move to apply
   1.153 + */
   1.154 +void apply_move(Board board, Move *move);
   1.155 +
   1.156 +#endif	/* RULES_H */
   1.157 +

mercurial