new parameter: number of players
authorLaurent Mazet <mazet@softndesign.org>
Sun, 9 Jun 2024 17:10:02 +0000 (19:10 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Sun, 9 Jun 2024 17:10:02 +0000 (19:10 +0200)
scrabble.c

index a1303926c79185e368420305a45485a4488b0a3c..e62c28162fbcab24e0d36608267f82eb10eab4bb 100644 (file)
@@ -25,6 +25,7 @@ char *version = "0.1";
 
 char *boardname = "15x15-7";
 char *language = "fr";
+int nbplayers = 2;
 
 int xoffset = 4;
 int yoffset = 3;
@@ -47,10 +48,11 @@ char *help =
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-b board] [-h] [-l lang] [-v level]\n", progname);
+    fprintf (fd, "usage: %s [-b board] [-h] [-l lang] [-n nb] [-v level]\n", progname);
     fprintf (fd, " -b: board (%s)\n", boardname);
     fprintf (fd, " -h: help message\n");
     fprintf (fd, " -l: language (%s)\n", language);
+    fprintf (fd, " -n: number of players (%d)\n", nbplayers);
     fprintf (fd, " -v: verbose level (%d)\n", verbose);
     fprintf (fd, "%s version %s\n", progname, version);
 
@@ -97,6 +99,14 @@ int main (int argc, char *argv[])
                 return usage (1);
             }
             break;
+        case 'n':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no number of players\n", progname));
+                return usage (1);
+            }
+            nbplayers = atoi (arg);
+            break;
         case 'v':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -121,6 +131,10 @@ int main (int argc, char *argv[])
         VERBOSE (ERROR, fprintf (stderr, "language not found\n"));
         return 1;
     }
+    if ((nbplayers < 1) || (nbplayers > 4)) {
+        VERBOSE (ERROR, fprintf (stderr, "incorrect number of player [1, 4] (%d)\n", nbplayers));
+        return 1;
+    }
 
     srand (time (NULL));
 
@@ -274,6 +288,8 @@ int main (int argc, char *argv[])
 /* test: scrabble.exe -h | grep usage */
 /* test: scrabble.exe -l 2>&1 | grep 'no language' */
 /* test: scrabble.exe -l aa 2>&1 | grep 'not found' */
+/* test: scrabble.exe -n 2>&1 | grep 'no number' */
+/* test: scrabble.exe -n 5 2>&1 | grep 'incorrect' */
 /* test: scrabble.exe -v 2>&1 | grep missing */
 /* test: scrabble.exe _ 2>&1 | grep invalid */
 /* test: echo vlvjxdvjvjvcivkkvkvctJcLccLLLLLLLLJJJJJJJJq | scrabble.exe */