Mon, 02 Jun 2025 19:40:24 +0200
improve code structure
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> | |
34 | ||
35 | #define WHITE 0x10 | |
36 | #define BLACK 0x20 | |
37 | #define opponent_color(color) ((color)==WHITE?BLACK:WHITE) | |
38 | ||
39 | #define PIECE_MASK 0x0F | |
40 | #define COLOR_MASK 0x30 | |
41 | ||
42 | #define PAWN 0x01 | |
43 | #define ROOK 0x02 | |
44 | #define KNIGHT 0x03 | |
45 | #define BISHOP 0x04 | |
46 | #define QUEEN 0x05 | |
47 | #define KING 0x06 | |
48 | ||
49 | #define WPAWN (WHITE|PAWN) | |
50 | #define WROOK (WHITE|ROOK) | |
51 | #define WKNIGHT (WHITE|KNIGHT) | |
52 | #define WBISHOP (WHITE|BISHOP) | |
53 | #define WQUEEN (WHITE|QUEEN) | |
54 | #define WKING (WHITE|KING) | |
55 | #define BPAWN (BLACK|PAWN) | |
56 | #define BROOK (BLACK|ROOK) | |
57 | #define BKNIGHT (BLACK|KNIGHT) | |
58 | #define BBISHOP (BLACK|BISHOP) | |
59 | #define BQUEEN (BLACK|QUEEN) | |
60 | #define BKING (BLACK|KING) | |
61 | ||
62 | typedef uint8_t Board[8][8]; | |
63 | ||
64 | struct movetimeval { | |
65 | uint64_t tv_sec; | |
66 | int32_t tv_usec; | |
67 | }; | |
68 | ||
69 | typedef struct { | |
70 | uint8_t piece; | |
71 | uint8_t fromfile; | |
72 | uint8_t fromrow; | |
73 | uint8_t tofile; | |
74 | uint8_t torow; | |
75 | uint8_t promotion; | |
76 | uint8_t check; | |
77 | uint8_t capture; | |
78 | struct movetimeval timestamp; | |
79 | struct movetimeval movetime; | |
80 | char string[8]; | |
81 | } Move; | |
82 | ||
83 | typedef struct MoveList MoveList; | |
84 | ||
85 | struct MoveList { | |
86 | Move move; | |
87 | MoveList* next; | |
88 | }; | |
89 | ||
90 | typedef struct { | |
91 | uint8_t servercolor; | |
92 | uint8_t timecontrol; | |
93 | uint16_t time; | |
94 | uint16_t addtime; | |
95 | } GameInfo; | |
96 | ||
97 | typedef struct { | |
98 | Board board; | |
99 | MoveList* movelist; | |
100 | MoveList* lastmove; | |
101 | unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */ | |
102 | _Bool checkmate; | |
103 | _Bool stalemate; | |
104 | _Bool remis; | |
105 | _Bool resign; | |
106 | } GameState; | |
107 | ||
108 | ||
109 | #define is_game_running(gamestate) !((gamestate)->checkmate || \ | |
110 | (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) | |
111 | ||
112 | /** | |
113 | * Initializes a game state and prepares the chess board. | |
114 | * @param gamestate the game state to initialize | |
115 | */ | |
116 | void gamestate_init(GameState *gamestate); | |
117 | ||
118 | /** | |
119 | * Cleans up a game state and frees the memory for the movement list. | |
120 | * @param gamestate the game state to clean up | |
121 | */ | |
122 | void gamestate_cleanup(GameState *gamestate); | |
123 | ||
124 | #endif |