From 5964cd72392b1fd80b93e60e317caa268a77099a Mon Sep 17 00:00:00 2001 From: MrGeorgen Date: Sun, 10 Dec 2023 22:42:56 +0100 Subject: [PATCH] genDirectionOffset --- .gitmodules | 3 ++ CMakeLists.txt | 3 +- LICENSE | 16 ++++++++- lib/acl | 1 + pieces/black_bishop.svg | 12 +++++++ pieces/black_king.svg | 12 +++++++ pieces/black_knight.svg | 22 ++++++++++++ pieces/black_pawn.svg | 5 +++ pieces/black_queen.svg | 27 ++++++++++++++ pieces/black_rook.svg | 39 ++++++++++++++++++++ pieces/white_bishop.svg | 12 +++++++ pieces/white_king.svg | 9 +++++ pieces/white_knight.svg | 19 ++++++++++ pieces/white_pawn.svg | 5 +++ pieces/white_queen.svg | 15 ++++++++ pieces/white_rook.svg | 25 +++++++++++++ src/main.c | 80 ++++++++++++++++++++++++++--------------- src/move.c | 61 +++++++++++++++++++++++++++++++ src/types.h | 21 +++++++++++ 19 files changed, 356 insertions(+), 31 deletions(-) create mode 100644 .gitmodules create mode 160000 lib/acl create mode 100644 pieces/black_bishop.svg create mode 100644 pieces/black_king.svg create mode 100644 pieces/black_knight.svg create mode 100644 pieces/black_pawn.svg create mode 100644 pieces/black_queen.svg create mode 100644 pieces/black_rook.svg create mode 100644 pieces/white_bishop.svg create mode 100644 pieces/white_king.svg create mode 100644 pieces/white_knight.svg create mode 100644 pieces/white_pawn.svg create mode 100644 pieces/white_queen.svg create mode 100644 pieces/white_rook.svg create mode 100644 src/move.c create mode 100644 src/types.h diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..7dae8f5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "lib/acl"] + path = lib/acl + url = https://git.zinkel.org/MrGeorgen/advanced_C_standard_library.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 2bc6c58..82e1789 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.18) project(chess C) -file(GLOB SOURCES "src/*.c") +#include_directories("lib/acl/include") +#file(GLOB SOURCES "src/*.c" "lib/*/src/*.c") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) find_package(PkgConfig REQUIRED) diff --git a/LICENSE b/LICENSE index 2071b23..105348c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,9 +1,23 @@ +License for the program: + MIT License -Copyright (c) +Copyright (c) 2023 Marcel Zinkel Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +License for the chess pieces (https://commons.wikimedia.org/wiki/Category:SVG_chess_pieces): + +Copyright © Cburnett + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + Neither the name of The author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +This software is provided by The author and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall The author and contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. diff --git a/lib/acl b/lib/acl new file mode 160000 index 0000000..a83de1b --- /dev/null +++ b/lib/acl @@ -0,0 +1 @@ +Subproject commit a83de1b97b4c58b09696c62a53d446763b21bb50 diff --git a/pieces/black_bishop.svg b/pieces/black_bishop.svg new file mode 100644 index 0000000..b6a8006 --- /dev/null +++ b/pieces/black_bishop.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/pieces/black_king.svg b/pieces/black_king.svg new file mode 100644 index 0000000..ba2ac9f --- /dev/null +++ b/pieces/black_king.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/pieces/black_knight.svg b/pieces/black_knight.svg new file mode 100644 index 0000000..203c6fe --- /dev/null +++ b/pieces/black_knight.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + diff --git a/pieces/black_pawn.svg b/pieces/black_pawn.svg new file mode 100644 index 0000000..b534de8 --- /dev/null +++ b/pieces/black_pawn.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/pieces/black_queen.svg b/pieces/black_queen.svg new file mode 100644 index 0000000..e557734 --- /dev/null +++ b/pieces/black_queen.svg @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/pieces/black_rook.svg b/pieces/black_rook.svg new file mode 100644 index 0000000..4eec43c --- /dev/null +++ b/pieces/black_rook.svg @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + diff --git a/pieces/white_bishop.svg b/pieces/white_bishop.svg new file mode 100644 index 0000000..18f034d --- /dev/null +++ b/pieces/white_bishop.svg @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/pieces/white_king.svg b/pieces/white_king.svg new file mode 100644 index 0000000..632ca1a --- /dev/null +++ b/pieces/white_king.svg @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/pieces/white_knight.svg b/pieces/white_knight.svg new file mode 100644 index 0000000..92feeee --- /dev/null +++ b/pieces/white_knight.svg @@ -0,0 +1,19 @@ + + + + + + + + + + diff --git a/pieces/white_pawn.svg b/pieces/white_pawn.svg new file mode 100644 index 0000000..b265fe1 --- /dev/null +++ b/pieces/white_pawn.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/pieces/white_queen.svg b/pieces/white_queen.svg new file mode 100644 index 0000000..8df7c8f --- /dev/null +++ b/pieces/white_queen.svg @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + diff --git a/pieces/white_rook.svg b/pieces/white_rook.svg new file mode 100644 index 0000000..ac03578 --- /dev/null +++ b/pieces/white_rook.svg @@ -0,0 +1,25 @@ + + + + + + + + + + + + + diff --git a/src/main.c b/src/main.c index 3809b04..87eb47e 100644 --- a/src/main.c +++ b/src/main.c @@ -1,42 +1,27 @@ +#include #include #include #include -#include +#include #include #include - -#define BOARD_SIZE 8 -#define LENGTH(array) (sizeof array / sizeof *array) - -enum pieces { - KING, - QUEEN, - ROOK, - BISHOP, - KNIGHT, - PAWN -}; - -enum playerColor { - BLACK, - WHITE -}; - -#define WHITE_PIECE 8 - -typedef uint_least8_t piece_t; +#include "types.h" struct drawData_t { RsvgHandle **piecesSvg; - piece_t *chessboard; + struct piece_t *chessboard; }; +static uint_least8_t pieceToSvgI(struct piece_t piece) { + return piece.white ? piece.type | 8 : piece.type; +} + static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, gpointer data) { const double fieldSize = (double)(width < height ? width : height) / BOARD_SIZE; const double xOffset = (width - fieldSize * BOARD_SIZE) / 2; const double yOffset = (height - fieldSize * BOARD_SIZE) / 2; const struct drawData_t drawData = *(struct drawData_t*)data; - const piece_t *chessboard = drawData.chessboard; + const struct piece_t *chessboard = drawData.chessboard; RsvgHandle **piecesSvg = drawData.piecesSvg; for(int x = 0; x < 8; ++x) { for(int y = 0; y < 8; ++y) { @@ -44,8 +29,11 @@ static void draw_event(GtkDrawingArea *area, cairo_t *cr, int width, int height, if((x + y) & 1) cairo_set_source_rgb(cr, 0.46274, 0.5882, 0.3373); else cairo_set_source_rgb(cr, 0.9333, 0.9333, 0.8235); cairo_fill(cr); - const RsvgRectangle rect = {xOffset + x * fieldSize, yOffset + y * fieldSize, fieldSize, fieldSize}; - rsvg_handle_render_document(piecesSvg[0], cr, &rect, NULL); + const struct piece_t piece = chessboard[x + y * BOARD_SIZE]; + if(piece.type) { + const RsvgRectangle rect = {xOffset + x * fieldSize, yOffset + y * fieldSize, fieldSize, fieldSize}; + rsvg_handle_render_document(piecesSvg[pieceToSvgI(piece)], cr, &rect, NULL); + } } } } @@ -57,7 +45,40 @@ static void on_click(GtkGestureClick *gesture, int n_press, double x, double y, static void app_activate(GApplication *app, gpointer data) { GtkWindow *window = GTK_WINDOW(gtk_window_new()); static RsvgHandle *piecesSvg[16]; - static piece_t chessboard[BOARD_SIZE * BOARD_SIZE]; + static struct piece_t chessboard[BOARD_SIZE * BOARD_SIZE] = {}; + { + struct piece_t *piece = chessboard; + const char startFEN[] = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"; + for(const char *c = startFEN; *c != ' '; ++c) { + switch(tolower(*c)) { + case 'k': + piece->type = KING; + break; + case 'q': + piece->type = QUEEN; + break; + case 'r': + piece->type = ROOK; + break; + case 'n': + piece->type = KNIGHT; + break; + case 'b': + piece->type = BISHOP; + break; + case 'p': + piece->type = PAWN; + break; + case '/': + continue; + default: + if(isdigit(*c)) piece += *c - '0'; + continue; + } + if(isalpha(*c)) piece->white = isupper(*c); + ++piece; + } + } static struct drawData_t drawData = {piecesSvg, chessboard}; { GtkWidget *drawArea = gtk_drawing_area_new(); @@ -76,15 +97,16 @@ static void app_activate(GApplication *app, gpointer data) { "rook", "bishop", "knight", - "pawn" + "pawn", }; for(uint i = 0; i < LENGTH(piecesFilenames); ++i) { const char *const color[] = {"black", "white"}; for(uint j = 0; j < LENGTH(color); ++j) { char filename[25]; GError *error = NULL; + const struct piece_t piece = {i + 1, j}; sprintf(filename, "pieces/%s_%s.svg", color[j], piecesFilenames[i]); - piecesSvg[i | j ? WHITE_PIECE : 0] = rsvg_handle_new_from_file(filename, &error); + piecesSvg[pieceToSvgI(piece)] = rsvg_handle_new_from_file(filename, &error); if(error != NULL) { perror(error->message); g_clear_error(&error); diff --git a/src/move.c b/src/move.c new file mode 100644 index 0000000..4d354af --- /dev/null +++ b/src/move.c @@ -0,0 +1,61 @@ +#include +#include +#include "types.h" +// 218 + +struct move_t { + uint_least8_t src, dst; +}; + +struct addMoveCtx_t { + struct piece_t *board; + struct move_t *moves; + uint_least8_t *movesLength; +}; + +enum directions { + NORTH, WEST, SOUTH, EAST, DIRECTION_LENGTH +}; + +static int_least8_t directionOffset[BOARD_SIZE * DIRECTION_LENGTH]; + +void genDirectionOffset() { + for(uint_least8_t i = 0; i < TOTAL_BOARD_SIZE; ++i) { + uint_least8_t file = i % BOARD_SIZE; + uint_least8_t rank = i / BOARD_SIZE; + directionOffset[i * DIRECTION_LENGTH + NORTH] = rank; + directionOffset[i * DIRECTION_LENGTH + WEST] = file; + directionOffset[i * DIRECTION_LENGTH + EAST] = BOARD_SIZE - file - 1; + directionOffset[i * DIRECTION_LENGTH + SOUTH] = BOARD_SIZE - rank - 1; + } +} + +static void addMove(struct addMoveCtx_t ctx, uint_least8_t src, uint_least8_t dst) { + struct piece_t *board = ctx.board; + struct move_t *moves = ctx.moves; + uint_least8_t *movesLength = ctx.movesLength; + if(board[src].white == board[dst].white) return; + moves[*movesLength].src = src; + moves[*movesLength].dst = dst; + ++movesLength; +} + +void validMoves(struct piece_t *board, bool whiteToMove, struct move_t *moves) { + uint_least8_t movesLength = 0; + struct addMoveCtx_t addmoveCtx = {board, moves, &movesLength}; + for(uint_least8_t i = 0; i < TOTAL_BOARD_SIZE; ++i) { + const struct piece_t piece = board[i]; + if(piece.type && piece.white == whiteToMove) { + uint_least8_t file = i % BOARD_SIZE; + switch(piece.type) { + case KING: + if(i >= BOARD_SIZE) addMove(addmoveCtx, i, i - BOARD_SIZE); + if(i < 7 * BOARD_SIZE) addMove(addmoveCtx, i, i + BOARD_SIZE); + if(file > 0) addMove(addmoveCtx, i, i - 1); + if(file < BOARD_SIZE - 1) addMove(addmoveCtx, i, i + 1); + case ROOK: + + } + } + } +} diff --git a/src/types.h b/src/types.h new file mode 100644 index 0000000..f03e26b --- /dev/null +++ b/src/types.h @@ -0,0 +1,21 @@ +#include +#include + +#define BOARD_SIZE 8 +#define TOTAL_BOARD_SIZE BOARD_SIZE * BOARD_SIZE +#define LENGTH(array) (sizeof array / sizeof *array) + +enum pieces { + NOTHING, + KING, + QUEEN, + ROOK, + BISHOP, + KNIGHT, + PAWN +}; + +struct piece_t { + uint_least8_t type; + bool white; +};