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/>.
*/
#include <stdio.h>
#include <getopt.h>
#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 );
}
}

5
ui.h
View file

@ -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 );

View file

@ -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

View file

@ -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(;;){