]> git.cworth.org Git - loudgame/blob - loa-board.h
4532e01f156c2d22c67ce7549e3c3f3d437cd517
[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 /* The implementation of board_group_size depends on the square of
44  * BOARD_SIZE being less than or equal to 64. */
45 #define LOA_BOARD_SIZE 8
46 #define LOA_DIAG_ARRAY_SIZE (2 * LOA_BOARD_SIZE - 1)
47
48 typedef struct {
49     loa_cell_t cells[LOA_BOARD_SIZE][LOA_BOARD_SIZE];
50
51     /* Number of black and white pieces */
52     int num_pieces[2];
53
54     /* Number of pieces (of either color) in each row, column, and
55      * diagonal. */
56     int row_pieces[LOA_BOARD_SIZE];
57     int col_pieces[LOA_BOARD_SIZE];
58     int diag_grave_pieces[LOA_DIAG_ARRAY_SIZE];
59     int diag_acute_pieces[LOA_DIAG_ARRAY_SIZE];
60
61     loa_player_t player;
62 } loa_board_t;
63
64 /* Initialize a board for a new game of Lines of Action. The 12 pieces
65  * for black and white will be put in their initial places and black
66  * will be set as the current player. */
67 void
68 loa_board_init (loa_board_t *board);
69
70 /* Does the square at (x,y) belong to a winning group? That is, is
71  * there a piece at (x,y) that is 8-way connected to all pieces on the
72  * board of the same color. */
73 int
74 loa_board_is_won (loa_board_t *board, int x, int y);
75
76 /* Move a piece from (x1,y1) to (x2,y2) where (0,0) is at the
77  * upper-left corner of the board. Returns TRUE if the move is legal
78  * and is performed. If the move is not legal this function returns
79  * FALSE, no change will be performed on the board, and *error will be
80  * set to a string describing why the move is illegal.*/
81 int
82 loa_board_move (loa_board_t *board,
83                 int x1, int y1,
84                 int x2, int y2,
85                 char **error);
86
87 /* Execute a 'pass'---changing the player-to-move from the current
88  * player to the opponent. Returns TRUE if the pass is executed.  Will
89  * eventually return FALSE if the current player has a legal move that
90  * could be made, but this is not yet implemented.
91  */
92 int
93 loa_board_pass (loa_board_t *board);
94
95 /* Allocate a new string showing the state of the current board.
96  * When finsihed, the called should free() the resulting value.
97  */
98 char *
99 loa_board_to_string (loa_board_t *board);
100
101 #endif