Started adding parsing of command-line options

This commit is contained in:
AnnaSnoeijs 2025-06-06 21:31:59 +02:00
parent 89712fc9dc
commit bf1ae99a9a
4 changed files with 56 additions and 15 deletions

View file

@ -17,6 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <stdio.h> #include <stdio.h>
#include <getopt.h>
#include "macros.h" #include "macros.h"
#include "config.h" #include "config.h"
#include "logic.h" #include "logic.h"
@ -25,8 +26,33 @@
#define VERSION 0.0.3 #define VERSION 0.0.3
int main(){ int main( int argc, char *argv[] ){
// First the boilerplate stuffs // 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( printf(
"AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n" "AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n"
"AnnaConnect comes with ABSOLUTELY NO WARRANTY\n" "AnnaConnect comes with ABSOLUTELY NO WARRANTY\n"
@ -63,6 +89,6 @@ int main(){
int column = askColumn( playboard, move ); int column = askColumn( playboard, move );
playMove( &playboard, move, column ); playMove( &playboard, move, column );
calcWins( &wins, playboard ); calcWins( &wins, playboard );
updateBoard( wins, playboard, column ); updateBoard( wins, playboard, move, column );
} }
} }

5
ui.h
View file

@ -9,12 +9,13 @@ extern void initBoard( void );
extern void updateBoard( extern void updateBoard(
const wins_t wins, const wins_t wins,
const board_t board, const board_t board,
const move_t move,
const int column const int column
); );
extern int askColumn( extern int askColumn(
const board_t board const board_t board,
,const move_t move const move_t move
); );
extern move_t askMove( void ); extern move_t askMove( void );

View file

@ -132,14 +132,27 @@ void initBoard( void ){
void updateBoard( void updateBoard(
const wins_t wins, const wins_t wins,
const board_t board, const board_t board,
const move_t move,
const int column const int column
){ ){
for(int row = 0; row < board.height[ column ]; row++){ int height = board.height[ column ];
mvaddstr( switch( move ){
BOARD_Y + BOARD_DY * ( BOARD_HEIGHT - row ), case PUT:
BOARD_X + 1 + BOARD_DX * ( column + 1 ), mvaddstr(
board.column[ column ] & 1 << row ? "1" : "0" 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]; char num[4];
sprintf( num, "%3d", wins.count0.vertical ); sprintf( num, "%3d", wins.count0.vertical );
@ -205,8 +218,8 @@ void updateBoard(
} }
int askColumn( int askColumn(
const board_t board const board_t board,
,const move_t move const move_t move
){ ){
int column = 0; int column = 0;
#ifdef ARROWS #ifdef ARROWS

View file

@ -69,6 +69,7 @@ void initBoard( void ){
void updateBoard( void updateBoard(
const wins_t wins, const wins_t wins,
const board_t board, const board_t board,
const move_t move,
const int column const int column
){ ){
int height = board.height [ column ]; int height = board.height [ column ];
@ -111,8 +112,8 @@ void updateBoard(
} }
int askColumn( int askColumn(
const board_t board const board_t board,
,const move_t move const move_t move
){ ){
int column; int column;
for(;;){ for(;;){