From bf1ae99a9a8967297728eb28302c00a015f654e1 Mon Sep 17 00:00:00 2001 From: AnnaSnoeijs Date: Fri, 6 Jun 2025 21:31:59 +0200 Subject: [PATCH] Started adding parsing of command-line options --- connect4.c | 32 +++++++++++++++++++++++++++++--- ui.h | 5 +++-- ui_ncurses.c | 29 +++++++++++++++++++++-------- ui_vt100.c | 5 +++-- 4 files changed, 56 insertions(+), 15 deletions(-) diff --git a/connect4.c b/connect4.c index 089064a..0c3d690 100644 --- a/connect4.c +++ b/connect4.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ #include +#include #include "macros.h" #include "config.h" #include "logic.h" @@ -25,8 +26,33 @@ #define VERSION 0.0.3 -int main(){ - // First the boilerplate stuffs +int main( int argc, char *argv[] ){ + // Parse options + for(;;){ + static struct option long_options[] = { + { "help", no_argument, NULL, 'h' }, + { "license", no_argument, NULL, 'l' }, + { "version", no_argument, NULL, '\0' }, + { 0 } + }; + int option_index = 0; + char c = getopt_long( argc, argv, "hl", long_options, &option_index ); + + if( c == -1 ) break; + + switch( c ){ + case 'h': + printf( "Usage: connect4 [OPTIONS]\n\n" ); + for( int i = 0; long_options[i].name != NULL; i++ ){ + if( long_options[i].val != '\0' ) + printf( " -%c --", long_options[i].val ); + else printf( " --" ); + printf( long_options[i].name ); + putchar( '\n' ); + } + return 0; + } + } printf( "AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n" "AnnaConnect comes with ABSOLUTELY NO WARRANTY\n" @@ -63,6 +89,6 @@ int main(){ int column = askColumn( playboard, move ); playMove( &playboard, move, column ); calcWins( &wins, playboard ); - updateBoard( wins, playboard, column ); + updateBoard( wins, playboard, move, column ); } } diff --git a/ui.h b/ui.h index 5b8024b..0fbbacd 100644 --- a/ui.h +++ b/ui.h @@ -9,12 +9,13 @@ extern void initBoard( void ); extern void updateBoard( const wins_t wins, const board_t board, + const move_t move, const int column ); extern int askColumn( - const board_t board - ,const move_t move + const board_t board, + const move_t move ); extern move_t askMove( void ); diff --git a/ui_ncurses.c b/ui_ncurses.c index 88d0cfe..ea2268b 100644 --- a/ui_ncurses.c +++ b/ui_ncurses.c @@ -132,14 +132,27 @@ void initBoard( void ){ void updateBoard( const wins_t wins, const board_t board, + const move_t move, const int column ){ - for(int row = 0; row < board.height[ column ]; row++){ - mvaddstr( - BOARD_Y + BOARD_DY * ( BOARD_HEIGHT - row ), - BOARD_X + 1 + BOARD_DX * ( column + 1 ), - board.column[ column ] & 1 << row ? "1" : "0" - ); + int height = board.height[ column ]; + switch( move ){ + case PUT: + mvaddstr( + BOARD_Y + BOARD_DY * ( BOARD_HEIGHT - height - 1 ), + BOARD_X + 1 + BOARD_DX * ( column + 1 ), + board.column[ column ] & 1 << ( height - 1 ) ? "1" : "0" + ); + break; + default: + for( int row = 0; row < height; row++ ){ + mvaddstr( + BOARD_Y + BOARD_DY * ( BOARD_HEIGHT - row ), + BOARD_X + 1 + BOARD_DX * ( column + 1 ), + board.column[ column ] & 1 << row ? "1" : "0" + ); + } + break; } char num[4]; sprintf( num, "%3d", wins.count0.vertical ); @@ -205,8 +218,8 @@ void updateBoard( } int askColumn( - const board_t board - ,const move_t move + const board_t board, + const move_t move ){ int column = 0; #ifdef ARROWS diff --git a/ui_vt100.c b/ui_vt100.c index 05e5a39..a23742f 100644 --- a/ui_vt100.c +++ b/ui_vt100.c @@ -69,6 +69,7 @@ void initBoard( void ){ void updateBoard( const wins_t wins, const board_t board, + const move_t move, const int column ){ int height = board.height [ column ]; @@ -111,8 +112,8 @@ void updateBoard( } int askColumn( - const board_t board - ,const move_t move + const board_t board, + const move_t move ){ int column; for(;;){