Added command-line option for setting size of board

This commit is contained in:
AnnaSnoeijs 2025-06-06 21:31:59 +02:00
parent e47d63b430
commit 72b6290798
6 changed files with 101 additions and 74 deletions

View file

@ -17,6 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
#include "macros.h"
@ -25,7 +26,7 @@
#include "types.h"
#include "ui.h"
#define VERSION 0.0.5
#define VERSION 0.1.0
#define VERSIONSTRING \
"AnnaConnect version "XSTR(VERSION)", Copyright (C) Anna Snoeijs\n"
@ -36,19 +37,37 @@ 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' },
{ "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, "hl", long_options, &option_index );
char c = getopt_long( argc, argv, "hlc:r:", long_options, &option_index );
if( c == -1 ) break;
// Handle option
switch( c ){
@ -86,6 +105,12 @@ int main( int argc, char *argv[] ){
putchar( *p );
}
return 0;
case 'c': // --columns
playboard.columns = atoi(optarg);
break;
case 'r': // --rows
playboard.rows = atoi(optarg);
break;
}
}
// Check for unhandled options
@ -95,6 +120,14 @@ int main( int argc, char *argv[] ){
printf( "%s ", argv[optind++] );
putchar( '\n' );
}
if( playboard.rows < 1 ){
fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE AT LEAST 1\n" );
return -1;
}
if( playboard.rows >= 32 ){
fprintf( stderr, "ERR: AMOUT OF ROWS MUST BE LESS THAN 32\n" );
return -1;
}
// Start the actual program
printf(
VERSIONSTRING
@ -103,30 +136,19 @@ int main( int argc, char *argv[] ){
"under certain condtions. See `%s --license`\n", argv[0]
);
// board, innit?
initBoard();
board_t playboard = {
.player = 0,
.column = {0},
.height = {0},
};
wins_t wins = {
.count0 = {
.total = 0,
.vertical = 0,
.horizontal = 0,
.diagonalUp = 0,
.diagonalDown = 0,
},
.count1 = {
.total = 0,
.vertical = 0,
.horizontal = 0,
.diagonalUp = 0,
.diagonalDown = 0,
},
.win0 = {0},
.win1 = {0},
};
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();