]> mj.ucw.cz Git - misc.git/commitdiff
Digit: Calculating possible moves...
authorMartin Mares <mj@ucw.cz>
Fri, 2 Sep 2011 16:19:58 +0000 (18:19 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 2 Sep 2011 16:19:58 +0000 (18:19 +0200)
ucw/digit.c

index 6331782901ba67e8ef6a0fc9103926075941adc0..7cc9867d0a823143f090a72b4a530f140f320b93 100644 (file)
@@ -10,7 +10,8 @@
 #include <string.h>
 
 #define MATCHES 5
-#define SIZE (2*MATCHES+3)
+#define SIZE (2*MATCHES+5)
+/* +5, because possible_moves() sometimes needs to represent a disconnected 5-match configuration with 1 gap */
 
 typedef char conf[SIZE][SIZE];
 
@@ -191,6 +192,7 @@ static void generate(void)
                  struct state *t = lookup(to);
                  if (!t)
                    {
+                     ASSERT(wi < MAXSTATES);
                      t = &states[wi++];
                      norm(to, t->c);
                      t->matches = s->matches+1;
@@ -212,8 +214,55 @@ static void generate(void)
     }
 }
 
+static void possible_moves(void)
+{
+  puts("\n=== POSSIBLE MOVES ===\n\n");
+
+  for (int i=0; i<wi; i++)
+    states[i].from = 0;
+
+  for (int i=0; i<wi; i++)
+    {
+      struct state *s = &states[i];
+      if (s->matches < MATCHES)
+       continue;
+      conf c;
+      memcpy(c, s->c, sizeof(conf));
+
+      printf("### State %d ###\n", s->id);
+      print(s->c);
+
+      int neighbors = 0;
+      for (int j=0; j<SIZE; j++)
+       for (int k=0; k<SIZE; k++)
+         if (s->c[j][k])
+           {
+             c[j][k] = 0;
+             for (int p=0; p<SIZE; p++)
+               for (int q=0; q<SIZE; q++)
+                 if (!c[p][q] && (p != j || q != k) && has_neighbor(c, p, q))
+                   {
+                     c[p][q] = 1;
+                     struct state *t = lookup(c);
+                     if (t && t != s && t->from != s->id)
+                       {
+                         t->from = s->id;
+                         // printf("-> state %d\n", t->id);
+                         // print(t->c);
+                         neighbors++;
+                       }
+                     c[p][q] = 0;
+                   }
+             c[j][k] = 1;
+           }
+
+      printf("=> %d neighbors\n\n", neighbors);
+    }
+}
+
 int main(void)
 {
   generate();
+  possible_moves();
   return 0;
 }