summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-06-01 18:32:49 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-06-01 18:34:43 +0800
commit3b8ecc1d1864f4368e1621284b482996ed87b72f (patch)
tree29b2cdeba87ffdce58371e0fecdaa53f8e9232d2
parent6912d144a5a7ae5abf2480a2a9a8842a4907edf4 (diff)
-h, -w, -p arguments
-rw-r--r--sirtet.c42
1 files changed, 36 insertions, 6 deletions
diff --git a/sirtet.c b/sirtet.c
index 6031ec5..2ec0b19 100644
--- a/sirtet.c
+++ b/sirtet.c
@@ -1,5 +1,7 @@
#include <stdlib.h>
#include <stdbool.h>
+#include <stdio.h>
+#include <getopt.h>
#include <time.h>
#include <locale.h>
#include <ncurses.h>
@@ -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;
}