From 199b2ff1b66bf2e12c9bf0b55a895a5555cc52f7 Mon Sep 17 00:00:00 2001 From: Robby Zambito Date: Fri, 21 Jan 2022 17:29:04 -0500 Subject: Consolidate redundant .c files * Move print_board function to board.c * Move the move validation functions to move.c --- src/board.c | 13 +++++++++++++ src/move.c | 32 +++++++++++++++++++++++++++++++ src/print_board.c | 36 ----------------------------------- src/valid_moves.c | 57 ------------------------------------------------------- 4 files changed, 45 insertions(+), 93 deletions(-) delete mode 100644 src/print_board.c delete mode 100644 src/valid_moves.c (limited to 'src') 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 . - */ - -#define _GNU_SOURCE - -#include - -#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 . - */ - -#define _GNU_SOURCE - -#include - -#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; -} -- cgit v1.2.3