From 3b8ecc1d1864f4368e1621284b482996ed87b72f Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Wed, 1 Jun 2022 18:32:49 +0800 Subject: -h, -w, -p arguments --- sirtet.c | 42 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 6 deletions(-) (limited to 'sirtet.c') diff --git a/sirtet.c b/sirtet.c index 6031ec5..2ec0b19 100644 --- a/sirtet.c +++ b/sirtet.c @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include @@ -60,11 +62,7 @@ int clearfull(char** map, int mapH, int mapW) { return (rtc_count * mapW + ctc_count * mapH - rtc_count * ctc_count); } -void sirtet() { - const int H = 8; // canvas height - const int W = 8; // canvas width - const int P = 3; // maximum # of pieces at hand - +void sirtet(int H, int W, int P) { // init memory and game state char** map = malloc(H * sizeof(char*)); for (int r = 0; r < H; r++) { @@ -241,6 +239,38 @@ void sirtet() { } int main(int argc, char *argv[]) { - sirtet(); + int H = 8; // map height + int W = 8; // map width + int P = 3; // max # of pieces at hand + + // handle CLI arguments + int opt = 0; + while (opt != -1) { + opt = getopt(argc, argv, "h:w:p:"); + switch (opt) { + case 'h': + sscanf(optarg, "%d", &H); + if (H < 4) { + printf("Map height cannot be less than 4\n"); + return 1; + } + break; + case 'w': + sscanf(optarg, "%d", &W); + if (W < 4) { + printf("Map width cannot be less than 4\n"); + return 1; + } + break; + case 'p': + sscanf(optarg, "%d", &P); + if (P < 1) { + printf("You must wield at least one piece\n"); + return 1; + } + break; + } + } + sirtet(H, W, P); return 0; } -- cgit v1.2.3