shithub: gm4s

Download patch

ref: 671299bd41eb3c05d07c923cd794562bccf48b0f
parent: 53846c7acde9959077f3903c22ad8e323b866736
author: qwx <qwx@sciops.net>
date: Fri Aug 8 06:27:13 EDT 2025

add -b: set initial playfield state from string

--- a/fns.h
+++ b/fns.h
@@ -3,6 +3,7 @@
 void	drop(void);
 void	gameover(void);
 void	step(void);
+void	readboard(char*);
 void	initgame(void);
 void	redraw(void);
 void	quit(void);
--- a/game.c
+++ b/game.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <ctype.h>
 #include "dat.h"
 #include "fns.h"
 #include "/sys/src/games/eui.h"
@@ -223,6 +224,42 @@
 	cur->flags &= ~Fhovering;
 	cur->lastmove = 0;
 	cur->y++;
+}
+
+void
+readboard(char *s)
+{
+	int i, n, b, *bp, max;
+	char c, *p;
+
+	max = nelem(playfield);
+	if((n = strlen(s)) > max){
+		fprint(2, "readboard: string longer than playfield\n");
+		n = Ncol * max;
+	}else if(n < 1)
+		sysfatal("readboard: empty string");
+	i = (n - 1) % Ncol;
+	bp = bfield + Nrow - n / Ncol;
+	if(bp == bfield + nelem(bfield))
+		bp--;
+	b = *bp;
+	for(p=playfield+max-n; p<playfield+nelem(playfield); i--){
+		c = *s++;
+		if(!isdigit(c)){
+			c = 0;
+			b &= ~(1 << i);
+		}else if((c -= '0') <= 0 || c >= NF){
+			c = 0;
+			b &= ~(1 << i);
+		}else
+			b |= 1 << i;
+		*p++ = c;
+		if(i == 0){
+			*bp++ = b;
+			b = 0;
+			i = Ncol;
+		}
+	}
 }
 
 void
--- a/gm4s.c
+++ b/gm4s.c
@@ -115,6 +115,12 @@
 	}
 }
 
+static void
+usage(void)
+{
+	sysfatal("usage: %s [-b STR]", argv0);
+}
+
 void
 threadmain(int argc, char **argv)
 {
@@ -121,6 +127,9 @@
 	ulong k;
 
 	ARGBEGIN{
+	case 'b':
+		readboard(EARGF(usage()));
+		break;
 	}ARGEND
 	if((stepc = chancreate(sizeof(ulong), 1)) == nil
 	|| (evc = chancreate(sizeof(ulong), 0)) == nil)
--