]> git.cworth.org Git - loudgame/blob - loa-board.h
Update documentation of move command for new syntax
[loudgame] / loa-board.h
1 /*
2  * Copyright (C) 2008 Carl Worth
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 3 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
15  * along with this program.  If not, see http://www.gnu.org/licenses/ .
16  *
17  * Author: Carl Worth <cworth@cworth.org>
18  */
19
20 #ifndef LOA_BOARD_H
21 #define LOA_BOARD_H
22
23 typedef int loa_bool_t;
24
25 #ifndef FALSE
26 #define FALSE 0
27 #endif
28 #ifndef TRUE
29 #define TRUE  1
30 #endif
31
32 typedef enum {
33     LOA_PLAYER_BLACK,
34     LOA_PLAYER_WHITE
35 } loa_player_t;
36
37 typedef enum {
38     LOA_CELL_BLACK = LOA_PLAYER_BLACK,
39     LOA_CELL_WHITE = LOA_PLAYER_WHITE,
40     LOA_CELL_EMPTY
41 } loa_cell_t;
42
43 typedef struct {
44     int x1; int y1;
45     int x2; int y2;
46     loa_bool_t is_capture;
47 } loa_move_t;
48
49 /* Return a string representation of a move. The return value is a
50  * pointer to a static buffer that will be reused from one call to the
51  * next. So the contents should be copied if needed. This function
52  * call is not thread-safe. */
53 const char *
54 loa_move_to_string (const loa_move_t *move);
55
56 /* Initialize an loa_move_t structure based on a string value,
57  * (presumably the result of loa_move_to_string). Returns TRUE if
58  * successful. */
59 loa_bool_t
60 loa_move_init_from_string (loa_move_t *move, const char *string);
61
62 /* The implementation of board_group_size depends on the square of
63  * BOARD_SIZE being less than or equal to 64. */
64 #define LOA_BOARD_SIZE 8
65 #define LOA_DIAG_ARRAY_SIZE (2 * LOA_BOARD_SIZE - 1)
66
67 typedef struct {
68     loa_cell_t cells[LOA_BOARD_SIZE][LOA_BOARD_SIZE];
69
70     /* Number of black and white pieces */
71     int num_pieces[2];
72
73     /* Number of pieces (of either color) in each row, column, and
74      * diagonal. */
75     int row_pieces[LOA_BOARD_SIZE];
76     int col_pieces[LOA_BOARD_SIZE];
77     int diag_grave_pieces[LOA_DIAG_ARRAY_SIZE];
78     int diag_acute_pieces[LOA_DIAG_ARRAY_SIZE];
79
80     int num_moves;
81     loa_move_t *moves;
82     int moves_size;
83
84     loa_player_t player;
85 } loa_board_t;
86
87 /* Initialize an loa_board_t structure. This function must be called
88  * before passing the board to any other loa_board function. It will
89  * implicitly call loa_board_reset for you. When you are finished
90  * using the board, you should call loa_board_fini. */
91 void
92 loa_board_init (loa_board_t *board);
93
94 /* Free all resources associated with a board. */
95 void
96 loa_board_fini (loa_board_t *board);
97
98 /* Reset board for a new game of Lines of Action. The 12 pieces
99  * for black and white will be put in their initial places and black
100  * will be set as the current player. */
101 void
102 loa_board_reset (loa_board_t *board);
103
104 /* Does the square at (x,y) belong to a winning group? That is, is
105  * there a piece at (x,y) that is 8-way connected to all pieces on the
106  * board of the same color. */
107 int
108 loa_board_is_won (loa_board_t *board, int x, int y);
109
110 /* Move a piece from (move->x1,move->y1) to (move->x2,move->y2) where
111  * (0,0) is at the upper-left corner of the board. Returns TRUE if the
112  * move is legal and is performed. If the move is not legal this
113  * function returns FALSE, no change will be performed on the board,
114  * and *error will be set to a string describing why the move is
115  * illegal.
116  *
117  * After this function returns, the move->is_capture field will be set
118  * as appropriate whether the move is a capture or not.
119  */
120 loa_bool_t
121 loa_board_move (loa_board_t *board,
122                 loa_move_t *move,
123                 char **error);
124
125 /* Check whether a move is legal, but without performing the move.
126  *
127  * After this function returns, the move->is_capture field will be set
128  * as appropriate whether the move is a capture or not.
129  */
130 loa_bool_t
131 loa_board_move_is_legal (loa_board_t     *board,
132                          loa_move_t      *move,
133                          char           **error);
134
135 /* Execute a 'pass'---changing the player-to-move from the current
136  * player to the opponent. Returns TRUE if the pass is executed.  Will
137  * eventually return FALSE if the current player has a legal move that
138  * could be made, but this is not yet implemented.
139  */
140 int
141 loa_board_pass (loa_board_t *board);
142
143 /* Allocate a new string showing the state of the current board.
144  * When finsihed, the called should free() the resulting value.
145  */
146 char *
147 loa_board_to_string (loa_board_t *board);
148
149 #endif