18
18
#include <signal.h> // defines: signal, SIGINT
19
19
20
20
#define SIZE 4
21
+ #define VERSION "1.0"
21
22
22
23
// this function receives 2 pointers (indicated by *) so it can set their values
23
24
void getColors (uint8_t value , uint8_t scheme , uint8_t * foreground , uint8_t * background )
@@ -425,6 +426,12 @@ int test()
425
426
return !success ;
426
427
}
427
428
429
+ int version ()
430
+ {
431
+ printf ("2048.c version %s\n" , VERSION );
432
+ return EXIT_SUCCESS ;
433
+ }
434
+
428
435
void signal_callback_handler (int signum )
429
436
{
430
437
printf (" TERMINATED \n" );
@@ -434,6 +441,48 @@ void signal_callback_handler(int signum)
434
441
exit (signum );
435
442
}
436
443
444
+ struct CliOption
445
+ {
446
+ char * name ;
447
+ char * description ;
448
+ };
449
+ struct CliOption cli_options [] = {
450
+ {"--help" , "Show this help message." },
451
+ {"--version" , "Show version number." },
452
+ {"bluered" , "Use a blue-to-red color scheme (requires 256-color terminal support). If unsupported, it will fall back to black-to-white." },
453
+ {"blackwhite" , "The default color scheme is black-to-white (requires 256-color terminal support)." },
454
+ {"test" , "Run the test suite." },
455
+ };
456
+
457
+ char * parseArgs (int argc , char * argv [], int * status_code )
458
+ {
459
+ if (argc == 1 )
460
+ {
461
+ return "blackwhite" ; // default color scheme
462
+ }
463
+ else if (argc == 2 )
464
+ {
465
+ return argv [1 ];
466
+ }
467
+ else
468
+ {
469
+ printf ("Invalid number of arguments\n" );
470
+ * status_code = EXIT_FAILURE ;
471
+ return "--help" ;
472
+ }
473
+ }
474
+
475
+ int help (int success ) {
476
+ printf ("Usage: 2048 [OPTION]\n" );
477
+ printf ("Play the game 2048 in the console\n\n" );
478
+ printf ("Options:\n" );
479
+ for (int i = 0 ; i < sizeof (cli_options ) / sizeof (cli_options [0 ]); i ++ )
480
+ {
481
+ printf (" %-12s %s\n" , cli_options [i ].name , cli_options [i ].description );
482
+ }
483
+ return success ;
484
+ }
485
+
437
486
int main (int argc , char * argv [])
438
487
{
439
488
uint8_t board [SIZE ][SIZE ];
@@ -442,18 +491,31 @@ int main(int argc, char *argv[])
442
491
char c ;
443
492
bool success ;
444
493
445
- if (argc == 2 && strcmp (argv [1 ], "test" ) == 0 )
494
+ int status_code = EXIT_SUCCESS ;
495
+ char * option = parseArgs (argc , argv , & status_code );
496
+ if (strcmp (option , "--help" ) == 0 || strcmp (option , "-h" ) == 0 )
497
+ {
498
+ return help (status_code );
499
+ }
500
+ else if (strcmp (option , "--version" ) == 0 || strcmp (option , "-v" ) == 0 )
501
+ {
502
+ return version ();
503
+ }
504
+ else if (strcmp (option , "test" ) == 0 )
446
505
{
447
506
return test ();
448
507
}
449
- if (argc == 2 && strcmp (argv [ 1 ] , "blackwhite" ) == 0 )
508
+ else if (strcmp (option , "blackwhite" ) == 0 )
450
509
{
451
510
scheme = 1 ;
452
511
}
453
- if (argc == 2 && strcmp (argv [ 1 ] , "bluered" ) == 0 )
512
+ else if (strcmp (option , "bluered" ) == 0 )
454
513
{
455
514
scheme = 2 ;
456
- }
515
+ } else {
516
+ printf ("Invalid option: %s\n" , option );
517
+ return help (EXIT_FAILURE );
518
+ }
457
519
458
520
// make cursor invisible, erase entire screen
459
521
printf ("\033[?25l\033[2J" );
0 commit comments