2 * Copyright (C) 2008 Carl Worth
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.
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.
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/ .
17 * Author: Carl Worth <cworth@cworth.org>
29 #include "loa-board.h"
32 loa_move_is_valid (const loa_move_t *move)
34 return (move->x1 >= 0 && move->x1 < LOA_BOARD_SIZE &&
35 move->y1 >= 0 && move->y1 < LOA_BOARD_SIZE &&
36 move->x2 >= 0 && move->x2 < LOA_BOARD_SIZE &&
37 move->y2 >= 0 && move->y2 < LOA_BOARD_SIZE);
41 loa_move_to_string (const loa_move_t *move)
43 #define LOA_MOVE_STRING_SIZE 6
44 static char move_string[LOA_MOVE_STRING_SIZE];
46 if (! loa_move_is_valid (move)) {
47 strcpy (move_string, "***");
51 snprintf (move_string, LOA_MOVE_STRING_SIZE,
53 'a' + move->x1, LOA_BOARD_SIZE - move->y1,
54 move->is_capture ? 'x' : '-',
55 'a' + move->x2, LOA_BOARD_SIZE - move->y2);
61 loa_move_init_from_string (loa_move_t *move, const char *string)
67 /* Avoid returning uninitialized data on error. */
74 matched = sscanf (string, " %c%d%c%c%d", &xc1, &y1, &sep, &xc2, &y2);
78 x1 = tolower (xc1) - 'a';
79 x2 = tolower (xc2) - 'a';
80 y1 = LOA_BOARD_SIZE - y1;
81 y2 = LOA_BOARD_SIZE - y2;
83 if (x1 < 0 || x1 >= LOA_BOARD_SIZE ||
84 y1 < 0 || y1 >= LOA_BOARD_SIZE ||
85 x2 < 0 || x2 >= LOA_BOARD_SIZE ||
86 y2 < 0 || y2 >= LOA_BOARD_SIZE)
91 if (sep != '-' && sep != 'x' && sep != 'X')
99 if (sep == 'x' || sep == 'X')
100 move->is_capture = TRUE;
102 move->is_capture = FALSE;
107 /* Given an (x,y) position on the board, return the index of the array
108 * used to count pieces in diagonal rows running from upper-left to
109 * lower-right, (like a grave accent: à or like a backslash: \).
111 * This is the array to look into when a move occurs with dx and dy of
114 _grave_index (int x, int y)
116 return x - y + LOA_BOARD_SIZE - 1;
119 /* Given an (x,y) position on the board, return the index of the array
120 * used to count pieces in diagonal rows running from lower-left to
121 * upper-right, (like an acute accent: á or like a forward slash: /).
123 * This is the array to look into when a move occurs with dx and dy of
124 * opposite sign, (one positive, one negative). */
126 _acute_index (int x, int y)
132 loa_board_add_piece (loa_board_t *board, int x, int y, loa_cell_t cell)
134 assert (cell == LOA_CELL_BLACK || cell == LOA_CELL_WHITE);
135 assert (board->cells[x][y] == LOA_CELL_EMPTY);
137 board->col_pieces[x]++;
138 board->row_pieces[y]++;
139 board->diag_grave_pieces[_grave_index (x, y)]++;
140 board->diag_acute_pieces[_acute_index (x, y)]++;
142 board->num_pieces[cell]++;
144 board->cells[x][y] = cell;
148 loa_board_remove_piece (loa_board_t *board, int x, int y)
152 cell = board->cells[x][y];
154 if (cell == LOA_CELL_EMPTY)
155 return LOA_CELL_EMPTY;
157 board->col_pieces[x]--;
158 board->row_pieces[y]--;
159 board->diag_grave_pieces[_grave_index (x, y)]--;
160 board->diag_acute_pieces[_acute_index (x, y)]--;
162 board->num_pieces[cell]--;
164 board->cells[x][y] = LOA_CELL_EMPTY;
170 loa_board_init (loa_board_t *board)
173 board->moves_size = 0;
175 loa_board_reset (board);
179 loa_board_fini (loa_board_t *board)
186 loa_board_reset (loa_board_t *board)
190 for (x = 0; x < LOA_BOARD_SIZE; x++)
191 for (y = 0; y < LOA_BOARD_SIZE; y++)
192 board->cells[x][y] = LOA_CELL_EMPTY;
194 board->num_pieces[LOA_CELL_BLACK] = 0;
195 board->num_pieces[LOA_CELL_WHITE] = 0;
197 for (i = 0; i < LOA_BOARD_SIZE; i++) {
198 board->row_pieces[i] = 0;
199 board->col_pieces[i] = 0;
202 for (i = 0; i < LOA_DIAG_ARRAY_SIZE; i++) {
203 board->diag_grave_pieces[i] = 0;
204 board->diag_acute_pieces[i] = 0;
207 for (i = 1; i < LOA_BOARD_SIZE - 1; i++) {
208 loa_board_add_piece (board, i, 0, LOA_CELL_BLACK);
209 loa_board_add_piece (board, i, LOA_BOARD_SIZE - 1, LOA_CELL_BLACK);
210 loa_board_add_piece (board, 0, i, LOA_CELL_WHITE);
211 loa_board_add_piece (board, LOA_BOARD_SIZE - 1, i, LOA_CELL_WHITE);
214 /* Leave board->moves and board->moves_size as allocated */
215 board->num_moves = 0;
217 board->player = LOA_PLAYER_BLACK;
221 loa_board_group_size_recursive (loa_board_t *board, int x, int y,
230 if (x >= LOA_BOARD_SIZE || y >= LOA_BOARD_SIZE)
233 bit = 1ll << (x * LOA_BOARD_SIZE + y);
239 if (board->cells[x][y] != cell)
243 loa_board_group_size_recursive (board, x-1, y-1, cell, visited) +
244 loa_board_group_size_recursive (board, x-1, y , cell, visited) +
245 loa_board_group_size_recursive (board, x-1, y+1, cell, visited) +
246 loa_board_group_size_recursive (board, x , y-1, cell, visited) +
247 loa_board_group_size_recursive (board, x , y , cell, visited) +
248 loa_board_group_size_recursive (board, x , y+1, cell, visited) +
249 loa_board_group_size_recursive (board, x+1, y-1, cell, visited) +
250 loa_board_group_size_recursive (board, x+1, y , cell, visited) +
251 loa_board_group_size_recursive (board, x+1, y+1, cell, visited);
255 loa_board_group_size (loa_board_t *board, int x, int y)
257 uint64_t visited = 0ll;
258 loa_cell_t cell = board->cells[x][y];
260 return loa_board_group_size_recursive (board, x, y, cell, &visited);
264 loa_board_is_won (loa_board_t *board, int x, int y)
266 loa_cell_t cell = board->cells[x][y];
268 if (cell == LOA_CELL_EMPTY)
271 if (loa_board_group_size (board, x, y) == board->num_pieces[cell])
278 loa_board_move_is_legal (loa_board_t *board,
287 if (! loa_move_is_valid (move))
289 *error = "Invalid coordinates (not on board)";
298 if (board->cells[x1][y1] == LOA_CELL_EMPTY) {
299 *error = "There is no piece there to move";
303 if (board->cells[x1][y1] != board->player) {
304 *error = "You cannot move your opponent's piece";
308 if (board->cells[x2][y2] == board->cells[x1][y1]) {
309 *error = "You cannot capture your own piece";
313 if (board->cells[x2][y2] == LOA_CELL_EMPTY)
314 move->is_capture = FALSE;
316 move->is_capture = TRUE;
321 /* Here's the meat of Lines of Action legaility: Does the distance
322 * moved exactly match the number of pieces (of either color) in
323 * the row, column, or diagonal of the movement. */
326 if (abs (dy) != board->col_pieces[x1]) {
327 *error = "The move distance does not match the number of pieces in that column";
330 } else if (dy == 0) {
332 if (abs (dx) != board->row_pieces[y1]) {
333 *error = "The move distance does not match the number of pieces in that row";
337 if (abs (dx) != abs (dy)) {
338 *error = "That move is not in a row, column, or diagonal";
342 if ((dx > 0) == (dy > 0)) {
343 if (abs (dx) != board->diag_grave_pieces[_grave_index (x1, y1)]) {
344 *error = "The move distance does not match the number of pieces in that diagonal";
348 if (abs (dx) != board->diag_acute_pieces[_acute_index (x1, y1)]) {
349 *error = "The move distance does not match the number of pieces in that diagonal";
355 /* Finally, we have to ensure that no opponent pieces are being
357 step_x = dx ? ((dx < 0) ? -1 : +1) : 0;
358 step_y = dy ? ((dy < 0) ? -1 : +1) : 0;
359 for (x = x1 + step_x, y = y1 + step_y;
361 x += step_x, y += step_y)
363 if (board->cells[x][y] != LOA_CELL_EMPTY &&
364 board->cells[x][y] != board->cells[x1][y1])
366 *error = "You cannot jump an opponent's piece";
375 loa_board_next_player (loa_board_t *board)
377 if (board->player == LOA_PLAYER_BLACK)
378 board->player = LOA_PLAYER_WHITE;
380 board->player = LOA_PLAYER_BLACK;
383 /* XXX: Should check for a legal move for the current player and
384 * return FALSE in that case, (that is, it should be illegal to pass
385 * if there's a legal move available). */
387 loa_board_pass (loa_board_t *board)
389 loa_board_next_player (board);
394 /* Once the move is validated and executed, append it to the moves
395 * array that stores the move history. */
397 loa_board_add_move_to_history (loa_board_t *board,
398 const loa_move_t *move)
402 if (board->num_moves > board->moves_size) {
403 if (board->moves_size)
404 board->moves_size *= 2;
406 board->moves_size = 20;
408 board->moves = realloc (board->moves,
409 board->moves_size * sizeof (loa_move_t));
410 if (board->moves == NULL) {
411 fprintf (stderr, "Out of memory.\n");
416 board->moves[board->num_moves - 1] = *move;
420 loa_board_move (loa_board_t *board,
426 if (! loa_board_move_is_legal (board, move, error))
429 cell = loa_board_remove_piece (board, move->x1, move->y1);
430 assert (cell == board->player);
432 cell = loa_board_remove_piece (board, move->x2, move->y2);
433 assert (cell == LOA_CELL_EMPTY ||
434 (move->is_capture && cell != board->player));
436 loa_board_add_piece (board,
440 loa_board_add_move_to_history (board, move);
442 loa_board_next_player (board);
447 /* A few different ideas for displaying boards:
449 * 8│ │●│●│●│●│●│●│ | 8| |*|*|*|*|*|*| |
450 * 7│○│ │ │ │ │ │ │○| 7|o| | | | | | |o|
451 * 6│○│ │ │ │ │ │ │○| 6|o| | | | | | |o|
452 * 5│○│ │ │ │ │ │ │○| 5|o| | | | | | |o|
453 * 4│○│ │ │ │ │ │ │○| 4|o| | | | | | |o|
454 * 3│○│ │ │ │ │ │ │○| 3|o| | | | | | |o|
455 * 2│○│ │ │ │ │ │ │○| 2|o| | | | | | |o|
456 * 1│ │●│●│●│●│●│●│ | 1| |*|*|*|*|*|*| |
457 * A B C D E F G H A B C D E F G H
459 * ┌───┬───┬───┬───┬───┬───┬───┬───┐ -------------------------------
460 * 8│ │ ● │ ● │ ● │ ● │ ● │ ● │ │ 8| | * | * | * | * | * | * | |
461 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
462 * 7│ ○ │ │ │ │ │ │ │ ○ │ 7| o | | | | | | | o |
463 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
464 * 6│ ○ │ │ │ │ │ │ │ ○ │ 6| o | | | | | | | o |
465 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
466 * 5│ ○ │ │ │ │ │ │ │ ○ │ 5| o | | | | | | | o |
467 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
468 * 4│ ○ │ │ │ │ │ │ │ ○ │ 4| o | | | | | | | o |
469 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
470 * 3│ ○ │ │ │ │ │ │ │ ○ │ 3| o | | | | | | | o |
471 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
472 * 2│ ○ │ │ │ │ │ │ │ ○ │ 2| o | | | | | | | o |
473 * ├───┼───┼───┼───┼───┼───┼───┼───┤ |---+---+---+---+---+---+---+---|
474 * 1│ │ ● │ ● │ ● │ ● │ ● │ ● │ │ 1| | * | * | * | * | * | * | |
475 * └───┴───┴───┴───┴───┴───┴───┴───┘ -------------------------------
476 * A B C D E F G H A B C D E F G H
479 loa_board_to_string (loa_board_t *board)
482 /* In order of BLACK, WHITE, EMPTY */
483 const char* cell_strings[] = {"●","○"," "};
484 const char board_header[] = "┌───┬───┬───┬───┬───┬───┬───┬───┐";
485 const char row_header[] = "│ ";
486 const char cell_separator[] = " │ ";
487 const char row_footer[] = " │";
488 const char row_separator[] = "├───┼───┼───┼───┼───┼───┼───┼───┤";
489 const char board_footer[] = "└───┴───┴───┴───┴───┴───┴───┴───┘";
490 char *board_string = g_strdup ("");
492 #define APPEND(str) do { \
493 char *_new = g_strconcat (board_string, (str), NULL); \
494 free (board_string); \
495 board_string = _new; \
498 #define APPENDF(format, ...) do { \
499 char *str = g_strdup_printf (format, ## __VA_ARGS__); \
504 APPENDF (" %s\n", board_header);
505 for (y = 0; y < LOA_BOARD_SIZE; y++) {
506 APPENDF ("%d%s", LOA_BOARD_SIZE - y, row_header);
507 for (x = 0; x < LOA_BOARD_SIZE; x++) {
508 APPENDF ("%s", cell_strings[board->cells[x][y]]);
509 if (x != LOA_BOARD_SIZE - 1)
510 APPENDF ("%s", cell_separator);
512 APPENDF ("%s\n", row_footer);
513 if (y != LOA_BOARD_SIZE -1)
514 APPENDF (" %s\n", row_separator);
516 APPENDF (" %s\n", board_footer);
519 for (x = 0; x < LOA_BOARD_SIZE; x++) {
520 APPENDF ("%c", 'A' + x);
521 if (x != LOA_BOARD_SIZE - 1)