summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2022-01-21 17:29:04 -0500
committerRobby Zambito <contact@robbyzambito.me>2022-01-21 17:29:04 -0500
commit199b2ff1b66bf2e12c9bf0b55a895a5555cc52f7 (patch)
tree5ea710b49352d845e6346f7f6a5df5ded6769809 /src
parent1156ea42110ef7bb3d2d73aec1c7ef2344d0900a (diff)
Consolidate redundant .c files
* Move print_board function to board.c * Move the move validation functions to move.c
Diffstat (limited to 'src')
-rw-r--r--src/board.c13
-rw-r--r--src/move.c32
-rw-r--r--src/print_board.c36
-rw-r--r--src/valid_moves.c57
4 files changed, 45 insertions, 93 deletions
diff --git a/src/board.c b/src/board.c
index a03fddd..88c7dff 100644
--- a/src/board.c
+++ b/src/board.c
@@ -160,3 +160,16 @@ enum player_color **scm_board_to_c_board(SCM scm_board) {
return board;
}
+
+void print_board(enum player_color **board) {
+ puts(" 0 1 2 3 4 5 6 7");
+
+ for (int row = 0; row < 8; row++) {
+ printf("%d ", row);
+
+ for (int col = 0; col < 8; col++) {
+ printf("%c ", board[row][col]);
+ }
+ printf("\n");
+ }
+}
diff --git a/src/move.c b/src/move.c
index 4f68df9..58e9911 100644
--- a/src/move.c
+++ b/src/move.c
@@ -280,3 +280,35 @@ struct move get_scm_move(char *strategy_path) {
return scm_move_to_c_move(scm_move);
}
+
+bool is_valid_move(enum player_color **board,
+ const enum player_color current_player,
+ const struct move move) {
+ if (current_player == EMPTY) {
+ return false;
+ }
+
+ // Make a copy of the input board. This is done because we lean on the logic
+ // that exists in apply_move to check if a move is valid. We don't want to
+ // necessarily mutate the board yet though.
+ enum player_color **b = copy_board(board);
+ // Apply the move to the copy of the board.
+ bool res = apply_move(b, current_player, move);
+
+ free_board(b);
+
+ return res;
+}
+
+bool has_valid_moves(enum player_color **board,
+ const enum player_color current_player) {
+ bool result = false;
+ struct move move;
+ for (move.row = 0; move.row < 8 && !result; move.row++) {
+ for (move.col = 0; move.col < 8 && !result; move.col++) {
+ result = is_valid_move(board, current_player, move);
+ }
+ }
+
+ return result;
+}
diff --git a/src/print_board.c b/src/print_board.c
deleted file mode 100644
index a7e332b..0000000
--- a/src/print_board.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/* This file is a part of othello-ai-guile-c
- *
- * Copyright (C) 2021 Robby Zambito
- *
- * othello-ai-guile-c is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * othello-ai-guile-c is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
-#define _GNU_SOURCE
-
-#include <stdio.h>
-
-#include "othello.h"
-
-void print_board(enum player_color **board) {
- puts(" 0 1 2 3 4 5 6 7");
-
- for (int row = 0; row < 8; row++) {
- printf("%d ", row);
-
- for (int col = 0; col < 8; col++) {
- printf("%c ", board[row][col]);
- }
- printf("\n");
- }
-}
diff --git a/src/valid_moves.c b/src/valid_moves.c
deleted file mode 100644
index 11f5926..0000000
--- a/src/valid_moves.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/* This file is a part of othello-ai-guile-c
- *
- * Copyright (C) 2021 Robby Zambito
- *
- * othello-ai-guile-c is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * othello-ai-guile-c is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-
-#define _GNU_SOURCE
-
-#include <stdbool.h>
-
-#include "othello.h"
-#include "othello_board.h"
-#include "othello_move.h"
-
-bool is_valid_move(enum player_color **board,
- const enum player_color current_player,
- const struct move move) {
- if (current_player == EMPTY) {
- return false;
- }
-
- // Make a copy of the input board. This is done because we lean on the logic
- // that exists in apply_move to check if a move is valid. We don't want to
- // necessarily mutate the board yet though.
- enum player_color **b = copy_board(board);
- // Apply the move to the copy of the board.
- bool res = apply_move(b, current_player, move);
-
- free_board(b);
-
- return res;
-}
-
-bool has_valid_moves(enum player_color **board,
- const enum player_color current_player) {
- bool result = false;
- struct move move;
- for (move.row = 0; move.row < 8 && !result; move.row++) {
- for (move.col = 0; move.col < 8 && !result; move.col++) {
- result = is_valid_move(board, current_player, move);
- }
- }
-
- return result;
-}