AnnaConnect/connect4.c

172 lines
5 KiB
C

/* 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 <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <getopt.h>
#include "macros.h"
#include "config.h"
#include "logic.h"
#include "types.h"
#include "ui.h"
#define VERSION 0.1.1
#ifndef GITHASH
#define FULLVERSION VERSION
#else
#define FULLVERSION VERSION~git.GITHASH
#endif
#define VERSIONSTRING \
"AnnaConnect version "XSTR(FULLVERSION)", Copyright (C) Anna Snoeijs\n"
// The LISENCE file
// Not null-terminated so need pointers for both start and end
extern char _binary_LICENSE_start[];
extern char _binary_LICENSE_end[];
int main( int argc, char *argv[] ){
// Initialise variables
wins_t wins = {
.count0 = {
.total = 0,
.vertical = 0,
.horizontal = 0,
.diagonalUp = 0,
.diagonalDown = 0,
},
};
wins.count1 = wins.count0;
board_t playboard = {
.player = 0,
.rows = BOARD_HEIGHT,
.columns = BOARD_WIDTH
};
// 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' },
{ 0 }
};
// Index of current option
int option_index = 0;
// Get next option
char c = getopt_long( argc, argv, "hlc:r:", 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;
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"
);
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 = atoi(optarg);
break;
case 'r': // --rows
playboard.rows = atoi(optarg);
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 ( sizeof(column_t) * CHAR_BIT )
if( playboard.rows >= ROWOVERFLOW ){
fprintf( stderr,
"ERR: AMOUT OF ROWS MUST BE LESS THAN %d\n", (int)ROWOVERFLOW
);
return -1;
}
#undef ROWOVERFLOW
// 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. See `%s --license`\n", argv[0]
);
// board, innit?
playboard.height = calloc( playboard.columns, sizeof(int) );
playboard.column = calloc( playboard.columns, sizeof(int) );
wins.win0 = calloc( playboard.columns, sizeof(int) );
wins.win1 = calloc( playboard.columns, sizeof(int) );
wins.same.vertical2 = calloc( playboard.columns, sizeof(int) );
wins.same.horizontal2 = calloc( playboard.columns, sizeof(int) );
wins.same.diagonalUp2 = calloc( playboard.columns, sizeof(int) );
wins.same.diagonalDown2 = calloc( playboard.columns, sizeof(int) );
wins.same.vertical4 = calloc( playboard.columns, sizeof(int) );
wins.same.horizontal4 = calloc( playboard.columns, sizeof(int) );
wins.same.diagonalUp4 = calloc( playboard.columns, sizeof(int) );
wins.same.diagonalDown4 = calloc( playboard.columns, sizeof(int) );
initBoard( playboard );
// Begin loopin
for(;; playboard.player = !playboard.player ){
move_t move = askMove();
int column = askColumn( playboard, move );
playMove( &playboard, move, column );
calcWins( &wins, playboard );
updateBoard( wins, playboard, move, column );
}
}