Mon, 02 Jun 2025 19:40:24 +0200
improve code structure
src/Makefile | file | annotate | diff | comparison | revisions | |
src/chess/Makefile | file | annotate | diff | comparison | revisions | |
src/chess/chess.h | file | annotate | diff | comparison | revisions | |
src/chess/game-info.c | file | annotate | diff | comparison | revisions | |
src/chess/game-info.h | file | annotate | diff | comparison | revisions | |
src/chess/pgn.c | file | annotate | diff | comparison | revisions | |
src/chess/rules.c | file | annotate | diff | comparison | revisions | |
src/chess/rules.h | file | annotate | diff | comparison | revisions | |
src/client.c | file | annotate | diff | comparison | revisions | |
src/game.c | file | annotate | diff | comparison | revisions | |
src/game.h | file | annotate | diff | comparison | revisions | |
src/input.c | file | annotate | diff | comparison | revisions | |
src/main.c | file | annotate | diff | comparison | revisions | |
src/server.c | file | annotate | diff | comparison | revisions | |
src/terminal-chess.h | file | annotate | diff | comparison | revisions |
--- a/src/Makefile Mon Jun 02 19:32:37 2025 +0200 +++ b/src/Makefile Mon Jun 02 19:40:24 2025 +0200 @@ -41,35 +41,35 @@ FORCE: - -$(BUILDDIR)/client.o: client.c terminal-chess.h network.h chess/rules.h \ - input.h game.h chess/chess.h chess/rules.h chess/pawn.h chess/rook.h \ - chess/knight.h chess/bishop.h chess/queen.h chess/king.h chess/pgn.h +$(BUILDDIR)/client.o: client.c input.h game.h chess/game-info.h network.h \ + chess/pgn.h chess/rules.h chess/game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< $(BUILDDIR)/colors.o: colors.c colors.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/game.o: game.c game.h chess/chess.h chess/rules.h \ - chess/pawn.h chess/rook.h chess/knight.h chess/bishop.h chess/queen.h \ - chess/king.h chess/pgn.h terminal-chess.h network.h chess/rules.h \ - input.h colors.h +$(BUILDDIR)/game.o: game.c game.h chess/game-info.h network.h input.h \ + colors.h chess/rules.h chess/game-info.h chess/pgn.h chess/rules.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< $(BUILDDIR)/input.o: input.c input.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/main.o: main.c terminal-chess.h network.h chess/rules.h \ - game.h chess/chess.h chess/rules.h chess/pawn.h chess/rook.h \ - chess/knight.h chess/bishop.h chess/queen.h chess/king.h chess/pgn.h \ - input.h colors.h +$(BUILDDIR)/main.o: main.c game.h chess/game-info.h input.h network.h \ + colors.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< $(BUILDDIR)/network.o: network.c network.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/server.o: server.c terminal-chess.h network.h chess/rules.h \ - game.h chess/chess.h chess/rules.h chess/pawn.h chess/rook.h \ - chess/knight.h chess/bishop.h chess/queen.h chess/king.h chess/pgn.h +$(BUILDDIR)/server.o: server.c network.h game.h chess/game-info.h \ + chess/pgn.h chess/rules.h chess/game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $<
--- a/src/chess/Makefile Mon Jun 02 19:32:37 2025 +0200 +++ b/src/chess/Makefile Mon Jun 02 19:40:24 2025 +0200 @@ -28,7 +28,7 @@ include ../../config.mk -SRC = pawn.c rook.c knight.c bishop.c queen.c king.c rules.c pgn.c +SRC = game-info.c pawn.c rook.c knight.c bishop.c queen.c king.c rules.c pgn.c OBJ = $(SRC:%.c=$(BUILDDIR)/%.o) all: $(BUILDDIR)/libchess$(LIB_EXT) FORCE @@ -38,27 +38,40 @@ FORCE: -$(BUILDDIR)/pawn.o: pawn.c pawn.h rules.h +$(BUILDDIR)/bishop.o: bishop.c bishop.h rules.h game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/pgn.o: pgn.c pgn.h rules.h +$(BUILDDIR)/game-info.o: game-info.c game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/rules.o: rules.c rules.h chess.h pawn.h rook.h \ - knight.h bishop.h queen.h king.h pgn.h +$(BUILDDIR)/king.o: king.c rules.h game-info.h king.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + +$(BUILDDIR)/knight.o: knight.c knight.h rules.h game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/bishop.o: bishop.c bishop.h rules.h +$(BUILDDIR)/pawn.o: pawn.c pawn.h rules.h game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/knight.o: knight.c knight.h rules.h +$(BUILDDIR)/pgn.o: pgn.c pgn.h rules.h game-info.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/queen.o: queen.c rules.h rook.h bishop.h queen.h +$(BUILDDIR)/queen.o: queen.c rules.h game-info.h rook.h bishop.h queen.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/king.o: king.c rules.h king.h +$(BUILDDIR)/rook.o: rook.c rules.h game-info.h rook.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< -$(BUILDDIR)/rook.o: rook.c rules.h rook.h +$(BUILDDIR)/rules.o: rules.c rules.h game-info.h pawn.h rook.h knight.h \ + bishop.h queen.h king.h + @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +
--- a/src/chess/chess.h Mon Jun 02 19:32:37 2025 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,37 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER OR 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. - * - */ - -#include "rules.h" -#include "pawn.h" -#include "rook.h" -#include "knight.h" -#include "bishop.h" -#include "queen.h" -#include "king.h" -#include "pgn.h"
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/chess/game-info.c Mon Jun 02 19:40:24 2025 +0200 @@ -0,0 +1,58 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2016 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER OR 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. + * + */ + +#include "game-info.h" +#include <string.h> +#include <stdlib.h> + +void gamestate_init(GameState *gamestate) { + memset(gamestate, 0, sizeof(GameState)); + + Board initboard = { + {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, + {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0, 0, 0}, + {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, + {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} + }; + memcpy(gamestate->board, initboard, sizeof(Board)); +} + +void gamestate_cleanup(GameState *gamestate) { + MoveList *elem; + elem = gamestate->movelist; + while (elem) { + MoveList *cur = elem; + elem = elem->next; + free(cur); + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/chess/game-info.h Mon Jun 02 19:40:24 2025 +0200 @@ -0,0 +1,124 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2016 Mike Becker. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. 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. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER OR 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. + * + */ + +#ifndef GAME_INFO_H +#define GAME_INFO_H + +#include <stdint.h> + +#define WHITE 0x10 +#define BLACK 0x20 +#define opponent_color(color) ((color)==WHITE?BLACK:WHITE) + +#define PIECE_MASK 0x0F +#define COLOR_MASK 0x30 + +#define PAWN 0x01 +#define ROOK 0x02 +#define KNIGHT 0x03 +#define BISHOP 0x04 +#define QUEEN 0x05 +#define KING 0x06 + +#define WPAWN (WHITE|PAWN) +#define WROOK (WHITE|ROOK) +#define WKNIGHT (WHITE|KNIGHT) +#define WBISHOP (WHITE|BISHOP) +#define WQUEEN (WHITE|QUEEN) +#define WKING (WHITE|KING) +#define BPAWN (BLACK|PAWN) +#define BROOK (BLACK|ROOK) +#define BKNIGHT (BLACK|KNIGHT) +#define BBISHOP (BLACK|BISHOP) +#define BQUEEN (BLACK|QUEEN) +#define BKING (BLACK|KING) + +typedef uint8_t Board[8][8]; + +struct movetimeval { + uint64_t tv_sec; + int32_t tv_usec; +}; + +typedef struct { + uint8_t piece; + uint8_t fromfile; + uint8_t fromrow; + uint8_t tofile; + uint8_t torow; + uint8_t promotion; + uint8_t check; + uint8_t capture; + struct movetimeval timestamp; + struct movetimeval movetime; + char string[8]; +} Move; + +typedef struct MoveList MoveList; + +struct MoveList { + Move move; + MoveList* next; +}; + +typedef struct { + uint8_t servercolor; + uint8_t timecontrol; + uint16_t time; + uint16_t addtime; +} GameInfo; + +typedef struct { + Board board; + MoveList* movelist; + MoveList* lastmove; + unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */ + _Bool checkmate; + _Bool stalemate; + _Bool remis; + _Bool resign; +} GameState; + + +#define is_game_running(gamestate) !((gamestate)->checkmate || \ + (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) + +/** + * Initializes a game state and prepares the chess board. + * @param gamestate the game state to initialize + */ +void gamestate_init(GameState *gamestate); + +/** + * Cleans up a game state and frees the memory for the movement list. + * @param gamestate the game state to clean up + */ +void gamestate_cleanup(GameState *gamestate); + +#endif
--- a/src/chess/pgn.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/chess/pgn.c Mon Jun 02 19:40:24 2025 +0200 @@ -29,7 +29,6 @@ #include "pgn.h" #include <ctype.h> -#include <stdlib.h> #include <string.h> enum {
--- a/src/chess/rules.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/chess/rules.c Mon Jun 02 19:40:24 2025 +0200 @@ -28,7 +28,13 @@ */ #include "rules.h" -#include "chess.h" +#include "pawn.h" +#include "rook.h" +#include "knight.h" +#include "bishop.h" +#include "queen.h" +#include "king.h" + #include <string.h> #include <stdlib.h> #include <sys/time.h> @@ -45,32 +51,6 @@ return simulation; } -void gamestate_init(GameState *gamestate) { - memset(gamestate, 0, sizeof(GameState)); - - Board initboard = { - {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, - {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {0, 0, 0, 0, 0, 0, 0, 0}, - {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, - {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} - }; - memcpy(gamestate->board, initboard, sizeof(Board)); -} - -void gamestate_cleanup(GameState *gamestate) { - MoveList *elem; - elem = gamestate->movelist; - while (elem) { - MoveList *cur = elem; - elem = elem->next; - free(cur); - }; -} - /* MUST be called BETWEEN validating AND applying a move to work correctly */ static void format_move(GameState *gamestate, Move *move) { char *string = &(move->string[0]);
--- a/src/chess/rules.h Mon Jun 02 19:32:37 2025 +0200 +++ b/src/chess/rules.h Mon Jun 02 19:40:24 2025 +0200 @@ -30,8 +30,9 @@ #ifndef RULES_H #define RULES_H +#include "game-info.h" + #include <stdint.h> -#include <sys/time.h> #define VALID_MOVE_SYNTAX 0 #define VALID_MOVE_SEMANTICS 0 /* use same code for a success */ @@ -44,83 +45,8 @@ #define KING_MOVES_INTO_CHECK 7 #define RULES_VIOLATED 10 - -#define PIECE_MASK 0x0F -#define COLOR_MASK 0x30 #define ENPASSANT_THREAT 0x40 -#define WHITE 0x10 -#define BLACK 0x20 - -#define PAWN 0x01 -#define ROOK 0x02 -#define KNIGHT 0x03 -#define BISHOP 0x04 -#define QUEEN 0x05 -#define KING 0x06 - -#define WPAWN (WHITE|PAWN) -#define WROOK (WHITE|ROOK) -#define WKNIGHT (WHITE|KNIGHT) -#define WBISHOP (WHITE|BISHOP) -#define WQUEEN (WHITE|QUEEN) -#define WKING (WHITE|KING) -#define BPAWN (BLACK|PAWN) -#define BROOK (BLACK|ROOK) -#define BKNIGHT (BLACK|KNIGHT) -#define BBISHOP (BLACK|BISHOP) -#define BQUEEN (BLACK|QUEEN) -#define BKING (BLACK|KING) - -typedef uint8_t Board[8][8]; - -struct movetimeval { - uint64_t tv_sec; - int32_t tv_usec; -}; - -typedef struct { - uint8_t piece; - uint8_t fromfile; - uint8_t fromrow; - uint8_t tofile; - uint8_t torow; - uint8_t promotion; - uint8_t check; - uint8_t capture; - struct movetimeval timestamp; - struct movetimeval movetime; - char string[8]; -} Move; - -typedef struct MoveList MoveList; - -struct MoveList { - Move move; - MoveList* next; -}; - - -typedef struct { - uint8_t servercolor; - uint8_t timecontrol; - uint16_t time; - uint16_t addtime; -} GameInfo; - -typedef struct { - Board board; - MoveList* movelist; - MoveList* lastmove; - unsigned int movecount; /* number of (half-)moves (counting BOTH colors) */ - _Bool checkmate; - _Bool stalemate; - _Bool remis; - _Bool resign; -} GameState; - -#define opponent_color(color) ((color)==WHITE?BLACK:WHITE) - #define POS_UNSPECIFIED UINT8_MAX #define mdst(b,m) b[(m)->torow][(m)->tofile] #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] @@ -143,22 +69,6 @@ #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) -#define is_game_running(gamestate) !((gamestate)->checkmate || \ - (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) - - -/** - * Initializes a game state and prepares the chess board. - * @param gamestate the game state to initialize - */ -void gamestate_init(GameState *gamestate); - -/** - * Cleans up a game state and frees the memory for the movement list. - * @param gamestate the game state to clean up - */ -void gamestate_cleanup(GameState *gamestate); - /** * Maps a character to a piece. *
--- a/src/client.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/client.c Mon Jun 02 19:40:24 2025 +0200 @@ -27,10 +27,12 @@ * */ -#include "terminal-chess.h" #include "input.h" #include "game.h" +#include "network.h" +#include "chess/pgn.h" #include <ncurses.h> +#include <stdlib.h> static int client_connect(Server *server, char *host, char *port) { if (net_find(server, host, port)) {
--- a/src/game.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/game.c Mon Jun 02 19:40:24 2025 +0200 @@ -31,6 +31,8 @@ #include "network.h" #include "input.h" #include "colors.h" +#include "chess/rules.h" +#include "chess/pgn.h" #include <ncurses.h> #include <string.h> #include <inttypes.h> @@ -569,3 +571,43 @@ game_continue(settings, opponent, &gamestate); } + +void dump_gameinfo(GameInfo *gameinfo) { + int serverwhite = gameinfo->servercolor == WHITE; + attron(A_UNDERLINE); + printw("Game details\n"); + attroff(A_UNDERLINE); + printw(" Server: %s\n Client: %s\n", + serverwhite?"White":"Black", serverwhite?"Black":"White" + ); + if (gameinfo->timecontrol) { + if (gameinfo->time % 60) { + printw(" Time limit: %ds + %ds\n", + gameinfo->time, gameinfo->addtime); + } else { + printw(" Time limit: %dm + %ds\n", + gameinfo->time/60, gameinfo->addtime); + } + } else { + printw(" No time limit\n"); + } + refresh(); +} + +void dump_moveinfo(GameState *gamestate) { + int i = 1; + for (MoveList *movelist = gamestate->movelist ; + movelist ; movelist = movelist->next) { + if (++i % 2 == 0) { + printw("%d. %s", i/2, movelist->move.string); + } else { + printw(" %s", movelist->move.string); + } + if (i % 20) { + addch(' '); + } else { + addch('\n'); + } + } + refresh(); +}
--- a/src/game.h Mon Jun 02 19:32:37 2025 +0200 +++ b/src/game.h Mon Jun 02 19:40:24 2025 +0200 @@ -30,17 +30,34 @@ #ifndef GAME_H #define GAME_H -#include "chess/chess.h" -#include "terminal-chess.h" +#include "chess/game-info.h" #ifdef __cplusplus extern "C" { #endif +typedef struct { + GameInfo gameinfo; + char* port; + char* serverhost; /* NULL, if we are about to start a server */ + char* continuepgn; + char* analyzepgn; + _Bool singlemachine; + _Bool unicode; +} Settings; + +#define is_server(settings) !((settings)->serverhost) + void game_start(Settings *settings, int opponent); void game_continue(Settings *settings, int opponent, GameState *gamestate); void game_start_singlemachine(Settings *settings); +int server_run(Settings* settings); +int client_run(Settings* settings); + +void dump_moveinfo(GameState *gamestate); +void dump_gameinfo(GameInfo *gameinfo); + #ifdef __cplusplus } #endif
--- a/src/input.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/input.c Mon Jun 02 19:40:24 2025 +0200 @@ -28,7 +28,6 @@ */ #include "input.h" -#include <string.h> #include <ctype.h> int prompt_yesno(char *msg) {
--- a/src/main.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/main.c Mon Jun 02 19:40:24 2025 +0200 @@ -27,9 +27,11 @@ * */ -#include "terminal-chess.h" +#define PROGRAM_VERSION "0.9 beta" + #include "game.h" #include "input.h" +#include "network.h" #include "colors.h" #include <string.h> #include <time.h> @@ -160,46 +162,6 @@ return settings; } -void dump_gameinfo(GameInfo *gameinfo) { - int serverwhite = gameinfo->servercolor == WHITE; - attron(A_UNDERLINE); - printw("Game details\n"); - attroff(A_UNDERLINE); - printw(" Server: %s\n Client: %s\n", - serverwhite?"White":"Black", serverwhite?"Black":"White" - ); - if (gameinfo->timecontrol) { - if (gameinfo->time % 60) { - printw(" Time limit: %ds + %ds\n", - gameinfo->time, gameinfo->addtime); - } else { - printw(" Time limit: %dm + %ds\n", - gameinfo->time/60, gameinfo->addtime); - } - } else { - printw(" No time limit\n"); - } - refresh(); -} - -void dump_moveinfo(GameState *gamestate) { - int i = 1; - for (MoveList *movelist = gamestate->movelist ; - movelist ; movelist = movelist->next) { - if (++i % 2 == 0) { - printw("%d. %s", i/2, movelist->move.string); - } else { - printw(" %s", movelist->move.string); - } - if (i % 20) { - addch(' '); - } else { - addch('\n'); - } - } - refresh(); -} - int main(int argc, char **argv) { srand(time(NULL));
--- a/src/server.c Mon Jun 02 19:32:37 2025 +0200 +++ b/src/server.c Mon Jun 02 19:40:24 2025 +0200 @@ -27,11 +27,14 @@ * */ -#include "terminal-chess.h" +#include "network.h" #include "game.h" +#include "chess/game-info.h" +#include "chess/pgn.h" #include <ncurses.h> #include <errno.h> #include <string.h> +#include <stdlib.h> static int server_open(Server *server, char *port) { printw("\nListening for client...\n");
--- a/src/terminal-chess.h Mon Jun 02 19:32:37 2025 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,68 +0,0 @@ -/* - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. - * - * Copyright 2016 Mike Becker. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. 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. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER OR 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. - * - */ - -#include <stdlib.h> -#include <stdio.h> -#include <ncurses.h> -#include "network.h" -#include "chess/rules.h" - -#ifndef TERMINAL_CHESS_H -#define TERMINAL_CHESS_H - -#define PROGRAM_VERSION "0.9-r72" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - GameInfo gameinfo; - char* port; - char* serverhost; /* NULL, if we are about to start a server */ - char* continuepgn; - char* analyzepgn; - _Bool singlemachine; - _Bool unicode; -} Settings; - -#define is_server(settings) !((settings)->serverhost) - -void dump_gameinfo(GameInfo *gameinfo); -void dump_moveinfo(GameState *gamestate); - -int server_run(Settings* settings); -int client_run(Settings* settings); - -#ifdef __cplusplus -} -#endif - -#endif /* TERMINAL_CHESS_H */ -