/* SPDX-License-Identifier: GPL-2.0-only */ /* * AnnaConnect is a silly little text-based connect 4 game c: * Copyright (C) 2025 Anna Snoeijs. anna.snoeijs@hotmail.com * * This program 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 2 of the License * (at your option) any later version. * * This program 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 . */ #include #include #include #include #ifndef NO_OPTIONS #include #endif /* ! NO_OPTIONS */ #include #include "macros.h" #include "config.h" #include "logic.h" #include "types.h" #include "ui.h" #define VERSION 0.3.1 #ifndef GITHASH #define FULLVERSION VERSION #else #define FULLVERSION VERSION~git.GITHASH #endif #define VERSIONSTRING \ "AnnaConnect version "XSTR(FULLVERSION)", Copyright (C) Anna Snoeijs\n" #ifndef NO_OPTIONS // The LISENCE file // Not null-terminated so need pointers for both start and end extern char _binary_LICENSE_start[]; extern char _binary_LICENSE_end[]; #endif /* ! NO_OPTIONS */ int main( #ifndef NO_OPTIONS // [[maybe_unused]] is unsupported in C99, so this will do int argc, char *argv[] #endif /* ! NO_OPTIONS */ ){ // Initialise variables board_t playboard = { .player = 0, .rows = BOARD_HEIGHT, .columns = BOARD_WIDTH, .count0 = {0}, .count1 = {0}, }; wins_t *wins = malloc( sizeof(wins_t) ); #ifndef NO_OPTIONS FILE *outputfile = NULL; char *filename = NULL; bool randomMoves = 0; // Parse options for(;;){ // Allowed options static struct option long_options[] = { { "help", no_argument, NULL, 'h' }, { "license", no_argument, NULL, 'l' }, { "version", no_argument, NULL, '\0' }, { "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 = (char)getopt_long( argc, argv, "hlc:r:o:", long_options, &option_index ); if( c == -1 ) break; // Handle option switch( c ){ case '\0': // Any option without shorthand switch( option_index ){ 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, "%s: unhandled option \'--%s\' \n", argv[0], long_options[ option_index ].name ); break; } break; case 'h': // --help printf( "Usage: %s [OPTIONS]\n\n", argv[0] ); printf( " -h --help Print this help text\n" " -l --license Print the LICENSE file\n" " --version Print the version string\n" "\n" " -c n --columns n Set the amount of columns\n" " -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 reference implementation of calcWins()\n" " --random-moves Play random moves instead of asking for input\n" ); return 0; case 'l': // --license for( char *p = _binary_LICENSE_start; p != _binary_LICENSE_end; p++ ){ putchar( *p ); } return 0; case 'c': // --columns playboard.columns = (columnsint_t)atoi(optarg); break; case 'r': // --rows playboard.rows = (rowsint_t)atoi(optarg); break; case 'o': // filename = strdup(optarg); filename = calloc( strlen( optarg ) + 1, sizeof(char) ); if( filename != NULL ){ strcpy( filename, optarg ); outputfile = fopen( filename, "w" ); }else{ fprintf( stderr, "ERR: COULD NOT ALLOCATE FILENAME\n" ); } break; } } // Check for unhandled options if( optind < argc ) { fprintf( stderr, "non-option ARGV-elements: " ); while( optind < argc ) printf( "%s ", argv[optind++] ); putchar( '\n' ); } if( playboard.rows < 1 ){ fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE AT LEAST 1\n" ); return -1; } #define ROWOVERFLOW (rowsint_t)( sizeof(column_t) * CHAR_BIT - 1 ) if( playboard.rows >= ROWOVERFLOW ){ fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE LESS THAN %d\n", ROWOVERFLOW ); return -1; } #undef ROWOVERFLOW #endif /* ! NO_OPTIONS */ // Start the actual program printf( VERSIONSTRING "AnnaConnect comes with ABSOLUTELY NO WARRANTY\n" "This is free software, and you are welcome to redistribute it\n" "under certain condtions." #ifndef NO_OPTIONS " See `%s --license`\n", argv[0] #else /* NO_OPTIONS */ "\n" #endif /* NO_OPTIONS */ ); // board, innit? // Allocate the board playboard.height = calloc( playboard.columns, sizeof(rowsint_t) ); playboard.column = calloc( playboard.columns, sizeof(column_t) ); if( playboard.height == NULL || playboard.column == NULL ){ fprintf( stderr, "ERROR: COULD NOT ALLOCATE BOARD\n" ); return 1; } // 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 #ifndef NO_OPTIONS if( outputfile != NULL ){ fprintf( outputfile, "# File generated by " VERSIONSTRING "# Here's the board size\n" "--rows %d\n" "--columns %d\n" "# Now follows the moves taken, zero indexed\n" , playboard.rows, playboard.columns ); } #endif /* ! NO_OPTIONS */ for(;; playboard.player = !playboard.player ){ columnsint_t column; #ifndef NO_OPTIONS if( randomMoves ){ column = randomColumn( playboard ); }else{ column = askColumn( playboard ); }; #else /* NO_OPTIONS */ column = askColumn( playboard ); #endif /* NO_OPTIONS */ if( column == QUITCOLUMN ) break; playMove( &playboard, column ); calcWins( wins, &playboard, column ); updateBoard( playboard, column ); #ifndef NO_OPTIONS if( outputfile != NULL ){ fprintf( outputfile, "%d\n", column ); } #endif /* ! NO_OPTIONS */ } #ifndef NO_OPTIONS 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 ); } #endif /* ! NO_OPTIONS */ exit_ui(); }