| 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 */ |
|
| 29 |
|
| 30 #ifndef GAME_INFO_H |
|
| 31 #define GAME_INFO_H |
|
| 32 |
|
| 33 #include <stdint.h> |
|
| 34 #include <stdbool.h> |
|
| 35 |
|
| 36 #define WHITE 0x10 |
|
| 37 #define BLACK 0x20 |
|
| 38 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE) |
|
| 39 |
|
| 40 #define PIECE_MASK 0x0F |
|
| 41 #define COLOR_MASK 0x30 |
|
| 42 |
|
| 43 #define PAWN 0x01 |
|
| 44 #define ROOK 0x02 |
|
| 45 #define KNIGHT 0x03 |
|
| 46 #define BISHOP 0x04 |
|
| 47 #define QUEEN 0x05 |
|
| 48 #define KING 0x06 |
|
| 49 |
|
| 50 #define WPAWN (WHITE|PAWN) |
|
| 51 #define WROOK (WHITE|ROOK) |
|
| 52 #define WKNIGHT (WHITE|KNIGHT) |
|
| 53 #define WBISHOP (WHITE|BISHOP) |
|
| 54 #define WQUEEN (WHITE|QUEEN) |
|
| 55 #define WKING (WHITE|KING) |
|
| 56 #define BPAWN (BLACK|PAWN) |
|
| 57 #define BROOK (BLACK|ROOK) |
|
| 58 #define BKNIGHT (BLACK|KNIGHT) |
|
| 59 #define BBISHOP (BLACK|BISHOP) |
|
| 60 #define BQUEEN (BLACK|QUEEN) |
|
| 61 #define BKING (BLACK|KING) |
|
| 62 |
|
| 63 typedef uint8_t Board[8][8]; |
|
| 64 |
|
| 65 struct movetimeval { |
|
| 66 uint64_t tv_sec; |
|
| 67 int32_t tv_usec; |
|
| 68 }; |
|
| 69 |
|
| 70 typedef struct { |
|
| 71 uint8_t piece; |
|
| 72 uint8_t fromfile; |
|
| 73 uint8_t fromrow; |
|
| 74 uint8_t tofile; |
|
| 75 uint8_t torow; |
|
| 76 uint8_t promotion; |
|
| 77 uint8_t check; |
|
| 78 uint8_t capture; |
|
| 79 struct movetimeval timestamp; |
|
| 80 struct movetimeval movetime; |
|
| 81 char string[8]; |
|
| 82 } Move; |
|
| 83 |
|
| 84 typedef struct { |
|
| 85 uint8_t servercolor; |
|
| 86 uint8_t timecontrol; |
|
| 87 uint16_t time; |
|
| 88 uint16_t addtime; |
|
| 89 } GameInfo; |
|
| 90 |
|
| 91 /** The buffer length for player names in GameInfo structures. */ |
|
| 92 #define PLAYER_NAME_BUFLEN 32 |
|
| 93 |
|
| 94 typedef struct { |
|
| 95 Board board; |
|
| 96 Move* moves; |
|
| 97 /** optional name of the white player */ |
|
| 98 char wname[PLAYER_NAME_BUFLEN]; |
|
| 99 /** optional name of the black player */ |
|
| 100 char bname[PLAYER_NAME_BUFLEN]; |
|
| 101 /** capacity of the move array */ |
|
| 102 unsigned movecapacity; |
|
| 103 /** number of (half-)moves (counting BOTH colors) */ |
|
| 104 unsigned int movecount; |
|
| 105 /** a premove that shall be evaluated next time it's our turn */ |
|
| 106 char premove[8]; |
|
| 107 bool checkmate; |
|
| 108 bool stalemate; |
|
| 109 bool remis; |
|
| 110 bool wresign; |
|
| 111 bool bresign; |
|
| 112 /** this flag is only supposed to be set when the opponent disconnects */ |
|
| 113 bool ragequit; |
|
| 114 bool review; |
|
| 115 } GameState; |
|
| 116 |
|
| 117 |
|
| 118 #define is_game_running(gamestate) !((gamestate)->checkmate || \ |
|
| 119 (gamestate)->wresign || (gamestate)->bresign || \ |
|
| 120 (gamestate)->stalemate || (gamestate)->remis || (gamestate)->review) |
|
| 121 |
|
| 122 #define last_move(gamestate) \ |
|
| 123 ((gamestate)->moves[(gamestate)->movecount-1]) |
|
| 124 |
|
| 125 /** |
|
| 126 * Initializes a game state and prepares the chess board. |
|
| 127 * @param gamestate the game state to initialize |
|
| 128 */ |
|
| 129 void gamestate_init(GameState *gamestate); |
|
| 130 |
|
| 131 /** |
|
| 132 * Cleans up a game state and frees the memory for the movement list. |
|
| 133 * @param gamestate the game state to clean up |
|
| 134 */ |
|
| 135 void gamestate_cleanup(GameState *gamestate); |
|
| 136 |
|
| 137 #endif |
|