From f660377768067993b4379af6e4a1a6a472948682 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 18:33:56 +0200 Subject: [PATCH 1/2] Add compiler flags --- makefile | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index 5d10e86..28eb98f 100644 --- a/makefile +++ b/makefile @@ -4,7 +4,7 @@ ARCH = native # Optimization level -O = 3 +O = 2 # Flags for the compiler FLAGS = -std=c23 @@ -18,7 +18,13 @@ FLAGS += -pedantic FLAGS += -Wall FLAGS += -Wextra FLAGS += -Werror -# This flag is because of the compiler otherwise giving too much warning +FLAGS += -Wconversion +FLAGS += -Wmissing-prototypes +FLAGS += -Wlogical-op +FLAGS += -Wdisabled-optimization +FLAGS += -fanalyzer +# These flags are because of the compiler otherwise giving too much warning +FLAGS += -Wno-sign-conversion #FLAGS += -Wformat-truncation=0 # Compile flag for defining GITHASH to put at the end of the version string @@ -49,7 +55,7 @@ MSG_CLEANING_OBJ = Cleaning $(OBJDIR): MSG_CLEANING_OUT = Cleaning $(OUTDIR): # Compile for all UI targets -all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf) +all: $(addprefix $(OUTDIR)/connect4_,ncurses.elf vt100.elf stub.elf) # Compile and run the default UI target run: $(OUTDIR)/connect4_$(UI_TARGET).elf From bb344cb956bf70befbcddac32aee2eb4358355c4 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Thu, 14 Aug 2025 18:38:22 +0200 Subject: [PATCH 2/2] Push version 0.3.0 --- connect4.c | 87 ++++++++++++++------- logic.c | 213 +++++++++++++++++++++++++++++++++++++++++---------- logic.h | 42 +++++++++- types.h | 29 ++----- ui.h | 1 - ui_ncurses.c | 21 +++-- ui_stub.c | 26 +++++++ 7 files changed, 311 insertions(+), 108 deletions(-) create mode 100644 ui_stub.c diff --git a/connect4.c b/connect4.c index 9ed5cf1..b054145 100644 --- a/connect4.c +++ b/connect4.c @@ -28,7 +28,7 @@ #include "types.h" #include "ui.h" -#define VERSION 0.2.0 +#define VERSION 0.3.0 #ifndef GITHASH #define FULLVERSION VERSION @@ -49,10 +49,14 @@ int main( int argc, char *argv[] ){ board_t playboard = { .player = 0, .rows = BOARD_HEIGHT, - .columns = BOARD_WIDTH + .columns = BOARD_WIDTH, + .count0 = {0}, + .count1 = {0}, }; FILE *outputfile = NULL; char *filename = NULL; + wins_t *wins = malloc( sizeof(wins_t) ); + bool randomMoves = 0; // Parse options for(;;){ // Allowed options @@ -63,12 +67,14 @@ int main( int argc, char *argv[] ){ { "columns", required_argument, NULL, 'c' }, { "rows", required_argument, NULL, 'r' }, { "output", required_argument, NULL, 'o' }, + { "slow-calcWins", no_argument, NULL, '\0' }, + { "random-moves", no_argument, NULL, '\0' }, { 0 } }; // Index of current option int option_index = 0; // Get next option - char c = getopt_long( argc, argv, "hlc:r:o:", + char c = (char)getopt_long( argc, argv, "hlc:r:o:", long_options, &option_index ); if( c == -1 ) break; @@ -79,6 +85,13 @@ int main( int argc, char *argv[] ){ case 2: // --version printf( VERSIONSTRING ); return 0; + case 6: // --slow-calcWins + free( wins ); + wins = NULL; + break; + case 7: // --random-moves + randomMoves = 1; + break; default: fprintf( stderr, @@ -100,6 +113,9 @@ int main( int argc, char *argv[] ){ " -r n --rows n Set the amount of rows\n" "\n" " -o file --output file Ouput moves taken in game to file\n" +"\n" +" --slow-calcwins Use the 32bit reference implementation of calcWins()\n" +" --random-moves Play random moves instead of asking for input\n" ); return 0; case 'l': // --license @@ -150,36 +166,29 @@ int main( int argc, char *argv[] ){ "under certain condtions. See `%s --license`\n", argv[0] ); // board, innit? - wins_t wins = { - .count0 = { - .total = 0, - .vertical = 0, - .horizontal = 0, - .diagonalUp = 0, - .diagonalDown = 0, - }, - }; - wins.count1 = wins.count0; // Allocate the board - playboard.height = calloc( playboard.columns, sizeof(int) ); - playboard.column = calloc( playboard.columns, sizeof(column_t) ); - wins.win0 = calloc( playboard.columns, sizeof(column_t) ); - wins.win1 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.vertical2 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.horizontal2 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.vertical4 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.horizontal4 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.diagonalUp4 = calloc( playboard.columns, sizeof(column_t) ); - wins.same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) ); + playboard.height = calloc( playboard.columns, sizeof(rowsint_t) ); + playboard.column = calloc( playboard.columns, sizeof(column_t) ); + // Initialize wins struct if used + if( wins != NULL ){ + wins->win0 = calloc( playboard.columns, sizeof(column_t) ); + wins->win1 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.vertical2 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.horizontal2 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.diagonalUp2 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.diagonalDown2 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.vertical4 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.horizontal4 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.diagonalUp4 = calloc( playboard.columns, sizeof(column_t) ); + wins->same.diagonalDown4 = calloc( playboard.columns, sizeof(column_t) ); + } initBoard( playboard ); // Begin loopin if( outputfile != NULL ){ fprintf( outputfile, "# File generated by " VERSIONSTRING - "# First define the config\n" + "# Here's the board size\n" "--rows %d\n" "--columns %d\n" "# Now follows the moves taken, zero indexed\n" @@ -189,16 +198,36 @@ int main( int argc, char *argv[] ){ ); } for(;; playboard.player = !playboard.player ){ - columnsint_t column = askColumn( playboard ); + columnsint_t column; + if( randomMoves ){ + column = randomColumn( playboard ); + }else{ + column = askColumn( playboard ); + }; if( column == QUITCOLUMN ) break; playMove( &playboard, column ); - calcWins( &wins, playboard, column ); - updateBoard( wins, playboard, column ); + calcWins( wins, &playboard, column ); + updateBoard( playboard, column ); if( outputfile != NULL ){ fprintf( outputfile, "%d\n", column ); } } if( outputfile != NULL ){ + fprintf( outputfile, + "# Wins\n" + "{\n" + "total,%d,%d\n" + "horizontal,%d,%d\n" + "vertical,%d,%d\n" + "diagonalUp,%d,%d\n" + "diagonalDown,%d,%d\n" + "}\n", + playboard.count0.total, playboard.count1.total, + playboard.count0.horizontal, playboard.count1.horizontal, + playboard.count0.vertical, playboard.count1.vertical, + playboard.count0.diagonalUp, playboard.count1.diagonalUp, + playboard.count0.diagonalDown, playboard.count1.diagonalDown + ); fclose( outputfile ); printf( "Output is written to %s\n", filename ); } diff --git a/logic.c b/logic.c index aa36dff..093fd60 100644 --- a/logic.c +++ b/logic.c @@ -16,11 +16,13 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ +#include +#include #include "logic.h" #include "macros.h" static column_t heightMask( const rowsint_t a ){ - return ( 1 << a ) - 1; + return ( 1 << (column_t)a ) - (column_t)1; } static column_t safeHeightMask( const rowsint_t a ){ @@ -31,6 +33,51 @@ static column_t bottomHeightMask( const rowsint_t a, const rowsint_t b ){ return safeHeightMask( bottom( a, b ) ); } +static void updateTotal( wincount_t *count ){ + count->total = + count->horizontal + + count->vertical + + count->diagonalUp + + count->diagonalDown; +} + +// popcount for a column +static winint_t popcnt_column( column_t column ){ + winint_t i = (winint_t)stdc_count_ones( column ); + return i; +} + +// Reset a wincount +static void resetcount( wincount_t *count ){ + count->total = 0; + count->horizontal = 0; + count->vertical = 0; + count->diagonalUp = 0; + count->diagonalDown = 0; +} + +// Returns 0 if counts are the same +winint_t countcmp( const board_t a, const board_t b ){ + winint_t i = 0; + if( a.count0.total != b.count0.total ) i+=1; + if( a.count1.total != b.count1.total ) i+=2; + return i; +} + +// return a random valid column +columnsint_t randomColumn( const board_t board ){ + columnsint_t i = 0; + do{ + if( i == board.columns ) return QUITCOLUMN; + }while( board.height[ i++ ] == board.rows ); + columnsint_t column; + do{ + column = (columnsint_t)rand() % board.columns; + }while( board.height[ column ] == board.rows ); + return column; +} + +// Play a move void playMove( board_t *boardptr, const columnsint_t column @@ -40,17 +87,107 @@ void playMove( boardptr->height [ column ]++; } +// Reference implementation of calcWins() +// Not intended for use in the main program, only for testing purposes +// Has silly behaviour for unknown reasons +void calcWins_slow( board_t *boardptr ){ + resetcount( &boardptr->count0 ); + resetcount( &boardptr->count1 ); + for( columnsint_t column = 0; column < boardptr->columns; column++ ){ + boardptr->count0.vertical += popcnt_column( + ~(boardptr->column[ column ] + | boardptr->column[ column ] >> 1 + | boardptr->column[ column ] >> 2 + | boardptr->column[ column ] >> 3 + )&safeHeightMask( boardptr->height[ column ] - 3 ) + ); + boardptr->count1.vertical += popcnt_column( + boardptr->column[ column ] + & boardptr->column[ column ] >> 1 + & boardptr->column[ column ] >> 2 + & boardptr->column[ column ] >> 3 + ); + }; + for( columnsint_t column = 0; column < boardptr->columns - 3; column++ ){ + boardptr->count0.horizontal += popcnt_column( + ~(boardptr->column[ column ] + | boardptr->column[ column + 1 ] + | boardptr->column[ column + 2 ] + | boardptr->column[ column + 3 ] + ) + & safeHeightMask( boardptr->height[ column ] ) + & safeHeightMask( boardptr->height[ column + 1 ] ) + & safeHeightMask( boardptr->height[ column + 2 ] ) + & safeHeightMask( boardptr->height[ column + 3 ] ) + ); + boardptr->count1.horizontal += popcnt_column( + boardptr->column[ column ] + & boardptr->column[ column + 1 ] + & boardptr->column[ column + 2 ] + & boardptr->column[ column + 3 ] + ); + boardptr->count0.diagonalUp += popcnt_column( + ~(boardptr->column[ column ] + | boardptr->column[ column + 1 ] >> 1 + | boardptr->column[ column + 2 ] >> 2 + | boardptr->column[ column + 3 ] >> 3 + ) + & safeHeightMask( boardptr->height[ column ] ) + & safeHeightMask( boardptr->height[ column + 1 ] - 1 ) + & safeHeightMask( boardptr->height[ column + 2 ] - 2 ) + & safeHeightMask( boardptr->height[ column + 3 ] - 3 ) + ); + boardptr->count1.diagonalUp += popcnt_column( + boardptr->column[ column ] + & boardptr->column[ column + 1 ] >> 1 + & boardptr->column[ column + 2 ] >> 2 + & boardptr->column[ column + 3 ] >> 3 + ); + boardptr->count0.diagonalDown += popcnt_column( + ~(boardptr->column[ column ] + | boardptr->column[ column + 1 ] << 1 + | boardptr->column[ column + 2 ] << 2 + | boardptr->column[ column + 3 ] << 3 + | 0x07 // A line diagonal down don't start at one of the bottom 3 + ) + & bottomHeightMask( + bottom( boardptr->height[ column ], + boardptr->height[ column + 1 ] + 1 + ), + bottom( boardptr->height[ column + 2 ] + 2, + boardptr->height[ column + 3 ] + 3 + ) + ) + ); + boardptr->count1.diagonalDown += popcnt_column( + boardptr->column[ column ] + & boardptr->column[ column + 1 ] << 1 + & boardptr->column[ column + 2 ] << 2 + & boardptr->column[ column + 3 ] << 3 + ); + }; + updateTotal( &boardptr->count0 ); + updateTotal( &boardptr->count1 ); +} + +// Less slow implementation of calcWins(); +// Only works if called after avery move void calcWins( wins_t *wins, - const board_t board, + board_t *boardptr, const columnsint_t column ){ + // If no remembering between runs, run the reference implementation + if( wins == NULL ) { + calcWins_slow( boardptr ); + return; + } // First the simplest win, the humble tower // Check for lil towers wins->same.vertical2 [ column ] = - ~( board.column [ column ] - ^ board.column [ column ] >> 1 ) - & safeHeightMask( board.height [ column ] - 1 ); + ~( boardptr->column [ column ] + ^ boardptr->column [ column ] >> 1 ) + & safeHeightMask( boardptr->height [ column ] - 1 ); // Actually check for wins column_t newcolumn = wins->same.vertical2 [ column ] @@ -61,11 +198,11 @@ void calcWins( if( ( newcolumn ^ wins->same.vertical4 [ column ] - ) & board.column [ column ] + ) & boardptr->column [ column ] ){ - wins->count1.vertical ++; + boardptr->count1.vertical ++; } else { - wins->count0.vertical ++; + boardptr->count0.vertical ++; } wins->same.vertical4 [ column ] = newcolumn; } @@ -73,36 +210,36 @@ void calcWins( // First connect 2 for( columnsint_t i = clipped_subtract( column, 1 ); - i < bottom( column + 1, board.columns - 1 ); + i < bottom( column + 1, boardptr->columns - 1 ); i++ ){ wins->same.horizontal2 [ i ] = - ~( board.column [ i ] - ^ board.column [ i + 1 ] ) + ~( boardptr->column [ i ] + ^ boardptr->column [ i + 1 ] ) & bottomHeightMask( - board.height [ i ], - board.height [ i + 1 ] + boardptr->height [ i ], + boardptr->height [ i + 1 ] ); wins->same.diagonalUp2 [ i ] = - ~( board.column [ i ] - ^ board.column [ i + 1 ] >> 1 ) + ~( boardptr->column [ i ] + ^ boardptr->column [ i + 1 ] >> 1 ) & bottomHeightMask( - board.height [ i ], - board.height [ i + 1 ] - 1 + boardptr->height [ i ], + boardptr->height [ i + 1 ] - 1 ); wins->same.diagonalDown2 [ i ] = - ~( board.column [ i ] - ^ board.column [ i + 1 ] << 1 ) + ~( boardptr->column [ i ] + ^ boardptr->column [ i + 1 ] << 1 ) & bottomHeightMask( - board.height [ i ], - board.height [ i + 1 ] + 1 + boardptr->height [ i ], + boardptr->height [ i + 1 ] + 1 ) - & ~1; // A diagonal line down ain't starts at the floor innit? + & ~(column_t)1; // A diagonal line down ain't starts at the floor innit? } // Then stitch the twos together and count for( columnsint_t i = clipped_subtract( column, 3 ); - i < bottom( column + 1, board.columns - 3 ); + i < bottom( column + 1, boardptr->columns - 3 ); i++ ){ newcolumn = @@ -113,11 +250,11 @@ void calcWins( if( ( newcolumn ^ wins->same.horizontal4 [ i ] - ) & board.column [ i ] + ) & boardptr->column [ i ] ){ - wins->count1.horizontal++; + boardptr->count1.horizontal++; } else { - wins->count0.horizontal++; + boardptr->count0.horizontal++; } wins->same.horizontal4 [ i ] = newcolumn; } @@ -129,11 +266,11 @@ void calcWins( if( ( newcolumn ^ wins->same.diagonalUp4 [ i ] - ) & board.column [ i ] + ) & boardptr->column [ i ] ){ - wins->count1.diagonalUp++; + boardptr->count1.diagonalUp++; } else { - wins->count0.diagonalUp++; + boardptr->count0.diagonalUp++; } wins->same.diagonalUp4 [ i ] = newcolumn; } @@ -145,23 +282,15 @@ void calcWins( if( ( newcolumn ^ wins->same.diagonalDown4 [ i ] - ) & board.column [ i ] + ) & boardptr->column [ i ] ){ - wins->count1.diagonalDown++; + boardptr->count1.diagonalDown++; } else { - wins->count0.diagonalDown++; + boardptr->count0.diagonalDown++; } wins->same.diagonalDown4 [ i ] = newcolumn; } } - wins->count0.total = - wins->count0.vertical - + wins->count0.horizontal - + wins->count0.diagonalUp - + wins->count0.diagonalDown; - wins->count1.total = - wins->count1.vertical - + wins->count1.horizontal - + wins->count1.diagonalUp - + wins->count1.diagonalDown; + updateTotal( &boardptr->count0 ); + updateTotal( &boardptr->count1 ); } diff --git a/logic.h b/logic.h index 488894c..8fb6e7b 100644 --- a/logic.h +++ b/logic.h @@ -1,16 +1,54 @@ /* SPDX-License-Identifier: GPL-2.0-only */ #pragma once +#include #include "config.h" #include "types.h" +// Types for using a bit more ram to avoid duplicated computations +typedef struct { + column_t *vertical2; + column_t *horizontal2; + column_t *diagonalUp2; + column_t *diagonalDown2; + column_t *vertical4; + column_t *horizontal4; + column_t *diagonalUp4; + column_t *diagonalDown4; +} directions_t; + +typedef struct { + column_t *win0; + column_t *win1; + directions_t same; +} wins_t; + +extern winint_t countcmp( + const board_t a, + const board_t b +); + +// Get a random column in the board +extern columnsint_t randomColumn( + const board_t board +); + +// Put a piece in the board extern void playMove( board_t *boardptr, const columnsint_t column ); +// Calculate wins extern void calcWins( - wins_t *wins, - const board_t board, + // Null pointer on systems where the extra ram is not available + wins_t *wins, + board_t *boardptr, const columnsint_t column ); + +// Reference implementation for calculating wins, +// slow AF especially on big boards +extern void calcWins_slow( + board_t *boardptr +); diff --git a/types.h b/types.h index df3c0a4..028565d 100644 --- a/types.h +++ b/types.h @@ -23,25 +23,6 @@ typedef uint_fast8_t column_t; #define WININT_FORMAT "%3d" -typedef struct { - bool player; - rowsint_t *height; - column_t *column; - rowsint_t rows; - columnsint_t columns; -} board_t; - -typedef struct { - column_t *vertical2; - column_t *horizontal2; - column_t *diagonalUp2; - column_t *diagonalDown2; - column_t *vertical4; - column_t *horizontal4; - column_t *diagonalUp4; - column_t *diagonalDown4; -} directions_t; - typedef struct { winint_t total; winint_t horizontal; @@ -51,9 +32,11 @@ typedef struct { } wincount_t; typedef struct { + bool player; + rowsint_t *height; + column_t *column; + rowsint_t rows; + columnsint_t columns; wincount_t count0; wincount_t count1; - column_t *win0; - column_t *win1; - directions_t same; -} wins_t; +} board_t; diff --git a/ui.h b/ui.h index fbff018..4192fb8 100644 --- a/ui.h +++ b/ui.h @@ -9,7 +9,6 @@ extern void initBoard( ); extern void updateBoard( - const wins_t wins, const board_t board, const columnsint_t column ); diff --git a/ui_ncurses.c b/ui_ncurses.c index 8da8914..bf77740 100644 --- a/ui_ncurses.c +++ b/ui_ncurses.c @@ -137,7 +137,6 @@ void initBoard( const board_t board ){ } void updateBoard( - const wins_t wins, const board_t board, const columnsint_t column ){ @@ -148,61 +147,61 @@ void updateBoard( board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0" ); char num[4]; - snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.vertical ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count0.vertical ); mvaddstr( SCOREBOARD_Y + 3, SCOREBOARD_X + 17, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.vertical ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count1.vertical ); mvaddstr( SCOREBOARD_Y + 3, SCOREBOARD_X + 22, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.horizontal ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count0.horizontal ); mvaddstr( SCOREBOARD_Y + 4, SCOREBOARD_X + 17, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.horizontal ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count1.horizontal ); mvaddstr( SCOREBOARD_Y + 4, SCOREBOARD_X + 22, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalUp ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalUp ); mvaddstr( SCOREBOARD_Y + 5, SCOREBOARD_X + 17, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalUp ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalUp ); mvaddstr( SCOREBOARD_Y + 5, SCOREBOARD_X + 22, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.diagonalDown ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count0.diagonalDown ); mvaddstr( SCOREBOARD_Y + 6, SCOREBOARD_X + 17, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.diagonalDown ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count1.diagonalDown ); mvaddstr( SCOREBOARD_Y + 6, SCOREBOARD_X + 22, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count0.total ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count0.total ); mvaddstr( SCOREBOARD_Y + 8, SCOREBOARD_X + 17, num ); - snprintf( num, sizeof(num), WININT_FORMAT, wins.count1.total ); + snprintf( num, sizeof(num), WININT_FORMAT, board.count1.total ); mvaddstr( SCOREBOARD_Y + 8, SCOREBOARD_X + 22, diff --git a/ui_stub.c b/ui_stub.c new file mode 100644 index 0000000..e8d397f --- /dev/null +++ b/ui_stub.c @@ -0,0 +1,26 @@ +#include "ui.h" +#include "logic.h" +#include + +void initBoard( + __attribute__((unused)) const board_t board +){ + return; +} + +void updateBoard( + __attribute__((unused)) const board_t board, + __attribute__((unused)) const columnsint_t column +){ + return; +} + +columnsint_t askColumn( + const board_t board +){ + return randomColumn( board ); +} + +void exit_ui(void){ + return; +}