git.haldean.org grandmaster / 52dbf03
restructure header files Haldean 3 years ago
24 changed file(s) with 345 addition(s) and 345 deletion(s). Raw diff Collapse all Expand all
+0
-73
include/gametree.h less more
0 /*
1 * gametree.h: game-tree-related functions and data
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "grandmaster.h"
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23
24 typedef uint64_t game_id_t;
25 typedef uint64_t player_id_t;
26
27 #define NO_GAME ((game_id_t) -1)
28
29 struct state_node {
30 struct move *move;
31 size_t n_children;
32 struct state_node **children;
33 struct state_node *parent;
34 };
35
36 struct game {
37 game_id_t id;
38 player_id_t player_white;
39 player_id_t player_black;
40 struct state_node *current;
41 };
42
43 struct game_tree {
44 size_t n_states;
45 struct state_node **states;
46 size_t n_games;
47 struct game **games;
48 };
49
50 void
51 init_gametree(struct game_tree *gt);
52
53 game_id_t
54 new_game(struct game_tree *gt, player_id_t white, player_id_t black);
55
56 struct game *
57 get_game(struct game_tree *gt, game_id_t game);
58
59 bool
60 make_move(
61 struct game_tree *gt,
62 game_id_t game,
63 const char *notation);
64
65 void
66 free_game_tree(struct game_tree *gt);
67
68 json_t *
69 game_tree_to_json(struct game_tree *gt);
70
71 void
72 game_tree_from_json(json_t *doc, struct game_tree *gt);
0 /*
1 * core.h: API for grandmaster core
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef __GRANDMASTER_H__
20 #define __GRANDMASTER_H__
21
22 #include <jansson.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25
26 #define NO_PASSANT (-1)
27
28 typedef enum {
29 PAWN = 'p',
30 ROOK = 'R',
31 KNIGHT = 'N',
32 BISHOP = 'B',
33 QUEEN = 'Q',
34 KING = 'K'
35 } piece_type_t;
36
37 #define ALL_PIECES ((piece_type_t[]) { PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING })
38
39 typedef enum {
40 WHITE = 'w',
41 BLACK = 'b'
42 } color_t;
43
44 typedef enum {
45 WHITE_KINGSIDE = 0x01,
46 WHITE_QUEENSIDE = 0x02,
47 BLACK_KINGSIDE = 0x04,
48 BLACK_QUEENSIDE = 0x08
49 } castles_t;
50
51 struct piece {
52 piece_type_t piece_type;
53 color_t color;
54 };
55
56 struct position {
57 int8_t rank;
58 int8_t file;
59 };
60
61 struct move {
62 struct position start;
63 struct position end;
64 color_t player;
65 char *algebraic;
66 struct move *parent;
67
68 struct board *post_board;
69 };
70
71 struct access_map {
72 struct {
73 int n_accessors;
74 struct position *accessors;
75 } board[8][8];
76 };
77
78 struct board {
79 struct piece board[8][8];
80 struct access_map *access_map;
81 uint8_t available_castles;
82 int8_t passant_file;
83 };
84
85 /* Returns the opposite color of the given color. */
86 color_t
87 opposite(color_t color);
88
89 /* Parse algebraic notation and return the result. **out is set to null if the
90 * input was not a valid move. */
91 void
92 parse_algebraic(
93 const char *notation,
94 struct move *last_move,
95 struct move **out);
96
97 /* Returns true if the movement in the move struct represents a valid movement
98 * for the piece that moved. Moves passed into this function must have their
99 * post_board correctly filled out. */
100 bool
101 is_movement_valid(struct move *move);
102
103 /* Returns true if the given player is in checkmate. */
104 bool
105 in_checkmate(struct move *move, color_t player);
106
107 /* Returns true if the given player is in check or checkmate. */
108 bool
109 in_check(struct move *move, color_t player);
110
111 /* Returns true if the given player is in forced stalemate. */
112 bool
113 in_stalemate(struct move *move, color_t player);
114
115 /* Finds all pieces of the given color and type that has access to move->end,
116 * respecting any preexisting values in move->start. Returns a list of positions
117 * where accessible pieces are located. */
118 void
119 find_all_with_access(
120 struct piece piece,
121 struct move *move,
122 int *n_results,
123 struct position **results);
124
125 /* Finds a piece of the given color and type that has access to move->end,
126 * respecting any preexisting values in move->start. Populates the value at
127 * move->start with results if any are found. If none are found, move is
128 * entirely unchanged. */
129 void
130 find_piece_with_access(struct piece piece, struct move *move);
131
132 /* Returns true if the given player can attack the given square. */
133 bool
134 can_attack(struct move *move, struct position position, color_t to_move);
135
136 /* Returns true if the given color can block the piece at the "mover" position
137 * from reaching the "target" position. Assumes that, without blockage, the
138 * mover could move to the target using a valid movement. */
139 bool
140 can_block(
141 struct move *move,
142 struct position mover,
143 struct position target,
144 color_t to_move);
145
146 /* Returns true if the two boards are equivalent. */
147 bool
148 boards_equal(struct board *, struct board *);
149
150 /* Convert a move to JSON. */
151 json_t *
152 move_to_json(const struct move *);
153
154 /* Convert a move to FEN. */
155 char *
156 move_to_fen(const struct move *);
157
158 #endif
0 /*
1 * grandmaster_internal.h: private grandmaster APIs
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef __GRANDMASTER_INTERNAL_H__
20 #define __GRANDMASTER_INTERNAL_H__
21
22 #include "grandmaster/core.h"
23
24 void
25 read_location(const char *str, struct position *result);
26
27 /* Reads PGN data and returns a hierarchy of moves starting at the given start
28 * move. If the provided start move is NULL, a new root node is created. */
29 struct move *
30 parse_pgn(const char *pgn, int n, struct move *start);
31
32 /* Parse FEN data into a move. Private API because it makes a "truncated" move
33 * with no hierarchy associated with it. */
34 struct move *
35 parse_fen(const char *fen, int n);
36
37 /* Print a move to stdout. */
38 void
39 print_move(const struct move *);
40
41 /* Convert a board to JSON. */
42 json_t *
43 board_to_json(const struct board *);
44
45 /* Blindly apply movement represented by start and end points, disregarding the
46 * validity of the move itself. */
47 void
48 apply_movement(struct move *m);
49
50 /* Asserts that the given color is either white or black. */
51 void
52 assert_valid_color(color_t color);
53
54 /* Builds an access map from a move. */
55 void
56 build_access_map(struct move *move, struct access_map *out);
57
58 /* Frees an access map. */
59 void
60 free_access_map(struct access_map *map);
61
62 /* Loads the opening position into a board object. */
63 void
64 load_default_board(struct board *b);
65
66 /* Returns a move that contains the root of the full game tree. */
67 void
68 get_root(struct move *out);
69
70 /* Free a move struct, leaving its parent move untouched. */
71 void
72 free_move(struct move *move);
73
74 /* Free a move struct and all of its parents. */
75 void
76 free_move_tree(struct move *move);
77
78 #endif
0 /*
1 * tree.h: game-tree-related functions and data
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include "grandmaster/core.h"
20 #include <stdbool.h>
21 #include <stdint.h>
22 #include <stdlib.h>
23
24 typedef uint64_t game_id_t;
25 typedef uint64_t player_id_t;
26
27 #define NO_GAME ((game_id_t) -1)
28
29 struct state_node {
30 struct move *move;
31 size_t n_children;
32 struct state_node **children;
33 struct state_node *parent;
34 };
35
36 struct game {
37 game_id_t id;
38 player_id_t player_white;
39 player_id_t player_black;
40 struct state_node *current;
41 };
42
43 struct game_tree {
44 size_t n_states;
45 struct state_node **states;
46 size_t n_games;
47 struct game **games;
48 };
49
50 void
51 init_gametree(struct game_tree *gt);
52
53 game_id_t
54 new_game(struct game_tree *gt, player_id_t white, player_id_t black);
55
56 struct game *
57 get_game(struct game_tree *gt, game_id_t game);
58
59 bool
60 make_move(
61 struct game_tree *gt,
62 game_id_t game,
63 const char *notation);
64
65 void
66 free_game_tree(struct game_tree *gt);
67
68 json_t *
69 game_tree_to_json(struct game_tree *gt);
70
71 void
72 game_tree_from_json(json_t *doc, struct game_tree *gt);
+0
-159
include/grandmaster.h less more
0 /*
1 * grandmaster.h: API for grandmaster
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef __GRANDMASTER_H__
20 #define __GRANDMASTER_H__
21
22 #include <jansson.h>
23 #include <stdbool.h>
24 #include <stdint.h>
25
26 #define NO_PASSANT (-1)
27
28 typedef enum {
29 PAWN = 'p',
30 ROOK = 'R',
31 KNIGHT = 'N',
32 BISHOP = 'B',
33 QUEEN = 'Q',
34 KING = 'K'
35 } piece_type_t;
36
37 #define ALL_PIECES ((piece_type_t[]) { PAWN, ROOK, KNIGHT, BISHOP, QUEEN, KING })
38
39 typedef enum {
40 WHITE = 'w',
41 BLACK = 'b'
42 } color_t;
43
44 typedef enum {
45 WHITE_KINGSIDE = 0x01,
46 WHITE_QUEENSIDE = 0x02,
47 BLACK_KINGSIDE = 0x04,
48 BLACK_QUEENSIDE = 0x08
49 } castles_t;
50
51 struct piece {
52 piece_type_t piece_type;
53 color_t color;
54 };
55
56 struct position {
57 int8_t rank;
58 int8_t file;
59 };
60
61 struct move {
62 struct position start;
63 struct position end;
64 color_t player;
65 char *algebraic;
66 struct move *parent;
67
68 struct board *post_board;
69 };
70
71 struct access_map {
72 struct {
73 int n_accessors;
74 struct position *accessors;
75 } board[8][8];
76 };
77
78 struct board {
79 struct piece board[8][8];
80 struct access_map *access_map;
81 uint8_t available_castles;
82 int8_t passant_file;
83 };
84
85 /* Returns the opposite color of the given color. */
86 color_t
87 opposite(color_t color);
88
89 /* Parse algebraic notation and return the result. **out is set to null if the
90 * input was not a valid move. */
91 void
92 parse_algebraic(
93 const char *notation,
94 struct move *last_move,
95 struct move **out);
96
97 /* Returns true if the movement in the move struct represents a valid movement
98 * for the piece that moved. Moves passed into this function must have their
99 * post_board correctly filled out. */
100 bool
101 is_movement_valid(struct move *move);
102
103 /* Returns true if the given player is in checkmate. */
104 bool
105 in_checkmate(struct move *move, color_t player);
106
107 /* Returns true if the given player is in check or checkmate. */
108 bool
109 in_check(struct move *move, color_t player);
110
111 /* Returns true if the given player is in forced stalemate. */
112 bool
113 in_stalemate(struct move *move, color_t player);
114
115 /* Finds all pieces of the given color and type that has access to move->end,
116 * respecting any preexisting values in move->start. Returns a list of positions
117 * where accessible pieces are located. */
118 void
119 find_all_with_access(
120 struct piece piece,
121 struct move *move,
122 int *n_results,
123 struct position **results);
124
125 /* Finds a piece of the given color and type that has access to move->end,
126 * respecting any preexisting values in move->start. Populates the value at
127 * move->start with results if any are found. If none are found, move is
128 * entirely unchanged. */
129 void
130 find_piece_with_access(struct piece piece, struct move *move);
131
132 /* Returns true if the given player can attack the given square. */
133 bool
134 can_attack(struct move *move, struct position position, color_t to_move);
135
136 /* Returns true if the given color can block the piece at the "mover" position
137 * from reaching the "target" position. Assumes that, without blockage, the
138 * mover could move to the target using a valid movement. */
139 bool
140 can_block(
141 struct move *move,
142 struct position mover,
143 struct position target,
144 color_t to_move);
145
146 /* Returns true if the two boards are equivalent. */
147 bool
148 boards_equal(struct board *, struct board *);
149
150 /* Convert a move to JSON. */
151 json_t *
152 move_to_json(const struct move *);
153
154 /* Convert a move to FEN. */
155 char *
156 move_to_fen(const struct move *);
157
158 #endif
+0
-79
include/grandmaster_internal.h less more
0 /*
1 * grandmaster_internal.h: private grandmaster APIs
2 * Copyright (C) 2015, Haldean Brown
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #ifndef __GRANDMASTER_INTERNAL_H__
20 #define __GRANDMASTER_INTERNAL_H__
21
22 #include "grandmaster.h"
23
24 void
25 read_location(const char *str, struct position *result);
26
27 /* Reads PGN data and returns a hierarchy of moves starting at the given start
28 * move. If the provided start move is NULL, a new root node is created. */
29 struct move *
30 parse_pgn(const char *pgn, int n, struct move *start);
31
32 /* Parse FEN data into a move. Private API because it makes a "truncated" move
33 * with no hierarchy associated with it. */
34 struct move *
35 parse_fen(const char *fen, int n);
36
37 /* Print a move to stdout. */
38 void
39 print_move(const struct move *);
40
41 /* Convert a board to JSON. */
42 json_t *
43 board_to_json(const struct board *);
44
45 /* Blindly apply movement represented by start and end points, disregarding the
46 * validity of the move itself. */
47 void
48 apply_movement(struct move *m);
49
50 /* Asserts that the given color is either white or black. */
51 void
52 assert_valid_color(color_t color);
53
54 /* Builds an access map from a move. */
55 void
56 build_access_map(struct move *move, struct access_map *out);
57
58 /* Frees an access map. */
59 void
60 free_access_map(struct access_map *map);
61
62 /* Loads the opening position into a board object. */
63 void
64 load_default_board(struct board *b);
65
66 /* Returns a move that contains the root of the full game tree. */
67 void
68 get_root(struct move *out);
69
70 /* Free a move struct, leaving its parent move untouched. */
71 void
72 free_move(struct move *move);
73
74 /* Free a move struct and all of its parents. */
75 void
76 free_move_tree(struct move *move);
77
78 #endif
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <string.h>
2323 #include <stdlib.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <stdbool.h>
2323 #include <stdio.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
19 #include "grandmaster/core.h"
2020
2121 #include <assert.h>
2222 #include <stdlib.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <ctype.h>
2323 #include <stdlib.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
21 #include "gametree.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
21 #include "grandmaster/tree.h"
2222
2323 #include <assert.h>
2424 #include <jansson.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <assert.h>
2323 #include <stdio.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
19 #include "grandmaster/core.h"
2020
2121 #include <stdlib.h>
2222
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
19 #include "grandmaster/core.h"
2020
2121 #include <assert.h>
2222 #include <stdlib.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <stdlib.h>
2323 #include <string.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
21 #include "gametree.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
21 #include "grandmaster/tree.h"
2222
2323 #include <stdlib.h>
2424 #include <string.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
19 #include "grandmaster/core.h"
2020
2121 #include <assert.h>
2222
1717 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818 */
1919
20 #include <grandmaster.h>
21 #include <gameio.h>
20 #include <grandmaster/core.h>
21 #include <grandmaster/internal.h>
2222 #include <stdio.h>
2323 #include <string.h>
2424
1717 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818 */
1919
20 #include "grandmaster.h"
21 #include "grandmaster_internal.h"
20 #include "grandmaster/core.h"
21 #include "grandmaster/internal.h"
2222
2323 #include <stdio.h>
2424 #include <string.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "grandmaster_internal.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/internal.h"
2121
2222 #include <jansson.h>
2323 #include <stdio.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include <grandmaster.h>
20 #include <grandmaster_internal.h>
19 #include <grandmaster/core.h>
20 #include <grandmaster/internal.h>
2121 #include <gameio.h>
2222 #include <jansson.h>
2323 #include <stdio.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include <grandmaster.h>
20 #include <gameio.h>
19 #include <grandmaster/core.h>
20 #include <grandmaster/internal.h>
2121 #include <jansson.h>
2222 #include <stdio.h>
2323 #include <string.h>
1717 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818 */
1919
20 #include "grandmaster.h"
21 #include "grandmaster_internal.h"
20 #include "grandmaster/core.h"
21 #include "grandmaster/internal.h"
2222
2323 #include <stdio.h>
2424 #include <string.h>
1616 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1717 */
1818
19 #include "grandmaster.h"
20 #include "gametree.h"
19 #include "grandmaster/core.h"
20 #include "grandmaster/tree.h"
2121
2222 #include <jansson.h>
2323 #include <stdio.h>