Tue, 04 Nov 2025 22:32:14 +0100
update uwproj
| 78 | 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> | |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
34 | #include <stdbool.h> |
| 78 | 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 MoveList MoveList; | |
| 85 | ||
| 86 | struct MoveList { | |
| 87 | Move move; | |
| 88 | MoveList* next; | |
| 89 | }; | |
| 90 | ||
| 91 | typedef struct { | |
| 92 | uint8_t servercolor; | |
| 93 | uint8_t timecontrol; | |
| 94 | uint16_t time; | |
| 95 | uint16_t addtime; | |
| 96 | } GameInfo; | |
| 97 | ||
| 98 | typedef struct { | |
| 99 | Board board; | |
| 100 | MoveList* movelist; | |
| 101 | MoveList* lastmove; | |
| 102 | unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */ | |
|
80
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
103 | bool checkmate; |
|
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
104 | bool stalemate; |
|
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
105 | bool remis; |
|
b980a7192b5a
replace _Bool with bool from C23 and/or stdbool.h
Mike Becker <universe@uap-core.de>
parents:
78
diff
changeset
|
106 | bool resign; |
| 78 | 107 | } GameState; |
| 108 | ||
| 109 | ||
| 110 | #define is_game_running(gamestate) !((gamestate)->checkmate || \ | |
| 111 | (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) | |
| 112 | ||
| 113 | /** | |
| 114 | * Initializes a game state and prepares the chess board. | |
| 115 | * @param gamestate the game state to initialize | |
| 116 | */ | |
| 117 | void gamestate_init(GameState *gamestate); | |
| 118 | ||
| 119 | /** | |
| 120 | * Cleans up a game state and frees the memory for the movement list. | |
| 121 | * @param gamestate the game state to clean up | |
| 122 | */ | |
| 123 | void gamestate_cleanup(GameState *gamestate); | |
| 124 | ||
| 125 | #endif |