summaryrefslogtreecommitdiff
path: root/lisp/ouat/scrabble-move-finding.lisp
blob: b80a76224aa69698e1a0c23a9dde2db8db819a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
(in-package :ouat)

(defstruct blank-state
  idx
  limit
  rack-pos
  seq-idx
  assignment)

(defmacro def-move-finder (fn-name layout tiles
                           lexicon rules result-type debug-p)
 `(defun ,fn-name (board unsorted-rack bag)
   ,(if debug-p
       `(declare (optimize debug))
       `(declare (optimize (speed 3) (safety 0) (space 1) (debug 0))))
   (declare (ignore board bag)
            (type (simple-array tile) ,tiles))
   (let* ((rack (rack-sort unsorted-rack))
          (seq (unique-rack-perms rack))
          (elts (perm-seq-elts seq))
          (dawg (lexicon-dawg ,lexicon))
          (nodes (dawg-nodes dawg))
          (nth-child-letter-table (dawg-nth-child-letter-table dawg))
          (letter2child-table (dawg-letter2child-table dawg))
          (rack-pos-dawg-idxs (make-array (i1+ (rack-size rack))
                                          :element-type 'fixnum))
          (max-rack-pos (i1- (rack-size rack)))
          (seq-idx 0)
          (blank-states (make-array (i1+ (rack-size rack))))
          (move-over-blank-p nil)
          (blank-state nil)
          (num-blanks-active 0)
          (blank-id ,(tiles-blank (symbol-value tiles)))
          (blank-tile (aref ,tiles blank-id))
          (blank-quantity (tile-quantity blank-tile))
          (blank-designations (make-array blank-quantity :element-type 'fixnum))
          (word (make-array (the fixnum (rack-size rack)) :element-type 'fixnum))
          (letter-muls ,(layout-letter-muls (symbol-value layout)))
          (word-muls ,(layout-word-muls (symbol-value layout)))
          (center-row ,(layout-center-row (symbol-value layout)))
          (center-col ,(layout-center-col (symbol-value layout)))
          (bingo-bonus ,(rules-bingo-bonus (symbol-value rules)))
          (elt nil)
          (rack-pos nil)
          (letter-idx nil)
          (dawg-idx nil)
          (node 0)
          (skip-seq-idx nil)
          (tile-id nil)
          (letter-id +empty+)
          (length 0)
          (result ,(if (eq result-type :TOP-VALUE)
                       `MOST-NEGATIVE-FIXNUM
                      `nil)))

    (declare (ignore blank-designations)
             (type (simple-array perm-elt) elts)
             (type (simple-array fixnum) nodes)
             ;(type (simple-array fixnum) nth-child-letter-table)
             (type (simple-array (unsigned-byte 8)) nth-child-letter-table)
             (type (simple-array (unsigned-byte 32)) letter2child-table)
             (type fixnum node)
             (type (simple-array fixnum) rack-pos-dawg-idxs))

    ,(when debug-p
      `(format t "rack has ~a permutations~%"
               (length elts)))

    (loop while (i< seq-idx (length elts)) do
      (labels ((node-has-child (child-letter)
                 (logbitp (the fixnum child-letter) node))

               (find-child-idx (child-letter)
                 (when (node-has-child child-letter)
                   (i+ dawg-idx
                       (node-letter-offset node child-letter))))

               (fast-child-idx (idx child-letter)
                 (aref letter2child-table (i+ (i* 32 idx) child-letter)))
               
               (fast-nth-child-letter (idx n)
                 (aref nth-child-letter-table (i+ (i* 32 idx) n)))

               (num-children ()
                 (declare (type (simple-vector 28) *mask*)
                          (type (simple-vector #.(ash 1 13))
                                *one-bits-per-13*))
                 (let ((first-13 (ilogand node (mask 13)))
                       (second-13 (iash node -13)))
                   (i+ (aref *one-bits-per-13* first-13)
                       (aref *one-bits-per-13* second-13))))

               (trimmed ()
                 (declare (sb-ext:muffle-conditions
                           sb-ext:compiler-note))
                 (subseq word 0 length))
               
               (blank-ps ()
                 (let ((bps (make-array length :initial-element nil)))
                   (loop
                      for idx below num-blanks-active
                      for bs across blank-states
                      for rack-pos = (blank-state-rack-pos bs)
                      do (setf (aref bps rack-pos) t))
                   bps))

               (process-word (start-col)
                 (let* ((word-mul-product 1)
                        (bps (blank-ps))
                        (bingo-p (i= length (rack-capacity rack)))
                        (score
                         (loop
                            with sum = 0
                            with row = center-row
                            for col from start-col
                            for idx below length
                            for tile-idx = (aref word idx)
                            for tile = (aref ,tiles tile-idx)
                            for tile-score = (tile-score tile)
                            for letter-mul = (aref letter-muls row col)
                            for word-mul = (aref word-muls row col)
                            do
                              (setf word-mul-product
                                    (i* word-mul-product word-mul))
                              (unless (aref bps idx)
                                (iincf sum (i* letter-mul tile-score)))
                            finally (return sum)))
                          (score (i* score word-mul-product))
                          (score (if bingo-p
                                     (i+ score bingo-bonus)
                                     score)))
                   (macrolet ((make-this-move ()
                                `(make-move
                                  :action :place
                                  :bingo-p bingo-p
                                  :score score
                                  :direction :horizontal
                                  :start-row center-row
                                  :start-col start-col
                                  :word (trimmed)
                                  :blank-ps bps
                                  :tiles (trimmed)
                                  :already-on-board-ps
                                  (make-array length
                                              :element-type 'boolean
                                              :initial-element nil))))
                     ,(when (eq :TOP-VALUE result-type)
                       `(setf result (imax result score)))
                     ,(when (eq :TOPS result-type)
                       `(let ((top-score (if result
                                             (move-score
                                              (first result))
                                             MOST-NEGATIVE-FIXNUM)))
                          (cond ((i> score top-score)
                                 (setf result (list (make-this-move))))
                                ((i= score top-score)
                                 (push (make-this-move) result)))))
                     ,(when (eq :ALL result-type)
                       `(push (make-this-move) result)))))
                         
               (process-word-in-all-placements ()
                 #+nil
                 ,(when debug-p
                   `(format t "processing word in all placements~%"))
                 (loop
                    with min-col = (i- center-col (i1- length))
                    for start-col from min-col to center-col do
                      (process-word start-col)))

               (blank-hop ()
                 ,(when debug-p
                   `(format t "do the blank-hop!~%"))
                 (setf seq-idx skip-seq-idx
                       elt (aref elts (if (i< seq-idx (length elts))
                                          seq-idx
                                          0)))
                 (loop
                    named :blank-hopping
                    while (iplusp num-blanks-active) do
                      (let* ((next-rack-pos (if (i< seq-idx (length elts))
                                                (perm-elt-change-pos elt)
                                                0))
                             (bs (aref blank-states (i1- num-blanks-active))))
                        ,(when debug-p
                          `(format t "num-blanks-active: ~a~%~
                                      next-rack-pos: ~a~%~
                                      (blank-state-rack-pos bs): ~a~%"
                                   num-blanks-active next-rack-pos
                                   (blank-state-rack-pos bs)))
                        (if (i<= next-rack-pos (blank-state-rack-pos bs))
                            (progn  
                              ,(when debug-p
                                `(format t "moving over blank (~a <= ~a)~%~
                                           (blank-state-idx bs): ~a  ~
                                           (blank-state-limit bs): ~a~%"
                                         next-rack-pos (blank-state-rack-pos bs)
                                         (blank-state-idx bs) (blank-state-limit bs)))
                              (if (i< (blank-state-idx bs) (blank-state-limit bs))
                                  (let ((pe (aref elts (blank-state-seq-idx bs))))
                                    ,(when debug-p
                                      `(format t " advancing blank...~%"))
                                    (iincf (blank-state-idx bs))
                                    (setf seq-idx (blank-state-seq-idx bs)
                                          move-over-blank-p t
                                          rack-pos (blank-state-rack-pos bs)
                                          length (i1+ rack-pos)
                                          dawg-idx (aref rack-pos-dawg-idxs rack-pos)
                                          node (aref nodes dawg-idx)
                                          letter-idx (perm-elt-pos-value pe)
                                          tile-id (aref (rack-tiles rack) letter-idx)
                                          skip-seq-idx (perm-elt-next pe))
                                    (return-from :blank-hopping))
                                  (progn
                                    ,(when debug-p
                                      `(format t " deleting blank...~%"))
                                    (idecf num-blanks-active))))
                            (progn
                              (setf seq-idx skip-seq-idx)
                              (return-from :blank-hopping))))))
               .
               ,(when debug-p
                 `((word-text ()
                     (apply #'concatenate
                            (cons 'string
                                  (loop
                                     for idx below length 
                                     for tile-idx = (aref word idx)
                                     for tile = (and (iplusp tile-idx)
                                                     (aref ,tiles tile-idx))
                                     collect (if tile
                                                 (tile-text tile)
                                                 "_")))))
                   (node-string (bits)
                     (apply #'concatenate
                            (cons 'string
                                  (loop
                                     for letter fixnum below 26
                                     for tile = (aref ,tiles (i1+ letter))
                                     when (logbitp letter bits)
                                     collect (tile-text tile)))))
                   (nth-child-letter (n)
                     ,(when debug-p
                       `(format t "looking for ~ath child-letter at ~a (~a)~%"
                                n dawg-idx (node-string node)))
                     (loop
                        with found = 0
                        for letter below 26 ;; ick!
                        when (node-has-child letter)
                        do (if (i= found n)
                               (return-from
                                nth-child-letter
                                 (i1+ letter))
                               (iincf found)))
                     ,(when debug-p
                       `(format t "did not find ~ath child-letter at ~a (~a)~%"
                                n dawg-idx (node-string node)))))))
        
        (declare (dynamic-extent #'node-has-child
                                 #'fast-child-idx
                                 #'find-child-idx
                                 #'fast-nth-child-letter
                                 #'num-children
                                 #'trimmed
                                 #'blank-ps
                                 #'process-word
                                 #'process-word-in-all-placements
                                 #'blank-hop))
        ,(when debug-p
          `(declare (dynamic-extent #'word-text
                                    #'node-string
                                    #'nth-child-letter)))

        (unless move-over-blank-p
            (setf elt (aref elts seq-idx)
                  rack-pos (perm-elt-change-pos elt)
                  letter-idx (perm-elt-pos-value elt)
                  dawg-idx (aref rack-pos-dawg-idxs rack-pos)
                  node (aref nodes dawg-idx)
                  skip-seq-idx (perm-elt-next elt)
                  tile-id (aref (rack-tiles rack) letter-idx)
                  letter-id +empty+
                  length (i1+ rack-pos)))
          
        ,(when debug-p
          `(format t "====================~%~
                      START of the loop~%~
                      seq-idx: ~a~%~
                      num-blanks-active: ~a~%~
                      move-over-blank-p: ~a~%~
                      rack-pos: ~a~%~
                      ====================~%"
                   seq-idx num-blanks-active move-over-blank-p
                   rack-pos))

        (setf blank-state
              (if (izerop num-blanks-active)
                  nil
                  (aref blank-states (i1- num-blanks-active))))

        ,(when debug-p
               `(format t "seq-idx: ~a~%" seq-idx))
        (when (and (i= tile-id blank-id)
                   (not move-over-blank-p))
          ,(when debug-p
                 `(format t "making new blank-state~%"))
          (setf (aref blank-states num-blanks-active)
                (make-blank-state :idx 0
                                  :limit (i1- (num-children))
                                  :rack-pos rack-pos
                                  :seq-idx seq-idx)
                blank-state (aref blank-states
                                  num-blanks-active))
          (iincf num-blanks-active))

        (setf move-over-blank-p nil)
        
        (if (and blank-state (i= tile-id blank-id))
            (progn
              ,(when debug-p
                `(format t "fast-nth: ~a slow-nth: ~a~%"
                         (fast-nth-child-letter
                              dawg-idx
                              (blank-state-idx blank-state))
                         (nth-child-letter
                              (blank-state-idx blank-state))))
              ,(when debug-p
                `(assert (i= (nth-child-letter
                              (blank-state-idx blank-state))
                             (fast-nth-child-letter
                              dawg-idx
                              (blank-state-idx blank-state)))))
              (setf letter-id (fast-nth-child-letter
                               dawg-idx
                               (blank-state-idx blank-state))
                    #+nil (nth-child-letter (blank-state-idx blank-state))
                    (blank-state-assignment blank-state) letter-id))
            (setf letter-id tile-id))
        
        ,(when debug-p
          `(format t "b-s: ~a~%" blank-state))

        (setf (aref word rack-pos) letter-id)

        ,(when debug-p
          `(format t "letter-id: ~a  tile-id: ~a~%"
                   (tile-name (aref ,tiles letter-id))
                   (tile-name (aref ,tiles tile-id))))

        (let (#+nil (child-idx (find-child-idx (i1- letter-id)))
              (child-idx (fast-child-idx dawg-idx letter-id)))
          ,(when debug-p
            `(let ((slow-child-idx (find-child-idx (i1- letter-id))))
               (when (iplusp child-idx)
                 (assert (equal child-idx slow-child-idx)))))
          ,(when debug-p
            `(format t "child-idx: ~a~%" child-idx))
          (if (iplusp child-idx)
              (let* ((child (aref nodes child-idx))
                     (new-dawg-idx (child-pointer child))
                     (whole-word-p (child-terminates child)))
                (progn
                  ,(when debug-p
                    `(progn
                       (if whole-word-p
                           (format t "~a" (word-text))
                           (format t "~a" (string-downcase
                                             (word-text))))))

                  (when whole-word-p
                    (process-word-in-all-placements))

                  (if (or (izerop new-dawg-idx)
                          (i= rack-pos max-rack-pos))
                      (progn
                        ,(when debug-p
                          `(if (izerop new-dawg-idx)     
                               (format t " has no extensions~%")
                               (format t " is as long as we have ~
                                           letters for~%")))

                        (blank-hop))

                      (progn
                        ,(when debug-p
                          `(format t " has extensions: ~a~%"
                                   (node-string (aref nodes
                                                      new-dawg-idx))))
                        (iincf seq-idx)
                        (setf (aref rack-pos-dawg-idxs
                                    (i1+ rack-pos))
                               new-dawg-idx)))))
              (progn
                ,(when debug-p
                  `(progn
                     (format t "start of new progn at the bottom~%~
                                blank-state: ~a~%"
                             (when (iplusp num-blanks-active)
                               (aref blank-states
                                     (i1- num-blanks-active))))))
                (blank-hop))))))
    result)))

(def-move-finder findloud *standard* *english*
                 *twl* *vanilla* :all t)
(def-move-finder findquiet *standard* *english*
                 *twl* *vanilla* :all nil)
(def-move-finder topsquiet *standard* *english*
                 *twl* *vanilla* :tops nil)
(def-move-finder topvaluequiet *standard* *english*
                 *twl* *vanilla* :top-value nil)

(defmacro o (rack)
  (let ((s (gensym "s")))
    (if (symbolp rack)
      (setf s (symbol-name rack))
      (setf s rack))
    `(findloud nil (new-rack ,s) nil)))

(defmacro oq (rack)
  (let ((s (gensym "s")))
    (if (symbolp rack)
      (setf s (symbol-name rack))
      (setf s rack))
    `(findquiet nil (new-rack ,s) nil)))

(defmacro tq (rack)
  (let ((s (gensym "s")))
    (if (symbolp rack)
      (setf s (symbol-name rack))
      (setf s rack))
    `(topsquiet nil (new-rack ,s) nil)))

(defmacro os (rack)
  `(mapcar (lambda (move) (move-string move *english*)) (o ,rack)))

(defmacro oqs (rack)
  `(mapcar (lambda (move) (move-string move *english*)) (oq ,rack)))

(defmacro tqs (rack)
  `(mapcar (lambda (move) (move-string move *english*)) (tq ,rack)))

(defmacro experimental-def-move-finder (fn-name layout tiles
                                        lexicon rules result-type debug-p)
 `(defun ,fn-name (board unsorted-rack bag)
   ,(if debug-p
       `(declare (optimize debug))
       `(declare (optimize (speed 3) (safety 0) (space 1) (debug 0))))
   (declare (ignore board bag)
            (type (simple-array tile) ,tiles))
   (let* ((sq-infos (board-sq-infos board))
          (rack (rack-sort unsorted-rack))
          (seq (unique-rack-perms rack))
          (elts (perm-seq-elts seq))
          (dawg (lexicon-dawg ,lexicon))
          (nodes (dawg-nodes dawg))
          (nth-child-letter-table (dawg-nth-child-letter-table dawg))
          (letter2child-table (dawg-letter2child-table dawg))
          (rack-pos-dawg-idxs (make-array (i1+ (rack-size rack))
                                          :element-type 'fixnum))
          (max-rack-pos (i1- (rack-size rack)))
          (seq-idx 0)
          (blank-states (make-array (i1+ (rack-size rack))))
          (move-over-blank-p nil)
          (blank-state nil)
          (num-blanks-active 0)
          (blank-id ,(tiles-blank (symbol-value tiles)))
          (blank-tile (aref ,tiles blank-id))
          (blank-quantity (tile-quantity blank-tile))
          (blank-designations (make-array blank-quantity :element-type 'fixnum))
          (word (make-array (the fixnum (rack-size rack)) :element-type 'fixnum))
          (letter-muls ,(layout-letter-muls (symbol-value layout)))
          (word-muls ,(layout-word-muls (symbol-value layout)))
          (center-row ,(layout-center-row (symbol-value layout)))
          (center-col ,(layout-center-col (symbol-value layout)))
          (bingo-bonus ,(rules-bingo-bonus (symbol-value rules)))
          (elt nil)
          (rack-pos nil)
          (letter-idx nil)
          (dawg-idx nil)
          (node 0)
          (skip-seq-idx nil)
          (tile-id nil)
          (letter-id +empty+)
          (length 0)
          (result ,(if (eq result-type :TOP-VALUE)
                       `MOST-NEGATIVE-FIXNUM
                      `nil)))

    (declare (ignore blank-designations)
             (type (simple-array perm-elt) elts)
             (type (simple-array fixnum) nodes)
             ;(type (simple-array fixnum) nth-child-letter-table)
             (type (simple-array (unsigned-byte 8)) nth-child-letter-table)
             (type (simple-array (unsigned-byte 32)) letter2child-table)
             (type fixnum node)
             (type (simple-array fixnum) rack-pos-dawg-idxs))

    ,(when debug-p
      `(format t "rack has ~a permutations~%"
               (length elts)))

    (loop while (i< seq-idx (length elts)) do
      (labels ((node-has-child (child-letter)
                 (logbitp (the fixnum child-letter) node))

               (find-child-idx (child-letter)
                 (when (node-has-child child-letter)
                   (i+ dawg-idx
                       (node-letter-offset node child-letter))))

               (fast-child-idx (idx child-letter)
                 (aref letter2child-table (i+ (i* 32 idx) child-letter)))
               
               (fast-nth-child-letter (idx n)
                 (aref nth-child-letter-table (i+ (i* 32 idx) n)))

               (num-children ()
                 (declare (type (simple-vector 28) *mask*)
                          (type (simple-vector #.(ash 1 13))
                                *one-bits-per-13*))
                 (let ((first-13 (ilogand node (mask 13)))
                       (second-13 (iash node -13)))
                   (i+ (aref *one-bits-per-13* first-13)
                       (aref *one-bits-per-13* second-13))))

               (trimmed ()
                 (declare (sb-ext:muffle-conditions
                           sb-ext:compiler-note))
                 (subseq word 0 length))
               
               (blank-ps ()
                 (let ((bps (make-array length :initial-element nil)))
                   (loop
                      for idx below num-blanks-active
                      for bs across blank-states
                      for rack-pos = (blank-state-rack-pos bs)
                      do (setf (aref bps rack-pos) t))
                   bps))

               (process-word (start-col)
                 (let* ((word-mul-product 1)
                        (bps (blank-ps))
                        (bingo-p (i= length (rack-capacity rack)))
                        (score
                         (loop
                            with sum = 0
                            with row = center-row
                            for col from start-col
                            for idx below length
                            for tile-idx = (aref word idx)
                            for tile = (aref ,tiles tile-idx)
                            for tile-score = (tile-score tile)
                            for letter-mul = (aref letter-muls row col)
                            for word-mul = (aref word-muls row col)
                            do
                              (setf word-mul-product
                                    (i* word-mul-product word-mul))
                              (unless (aref bps idx)
                                (iincf sum (i* letter-mul tile-score)))
                            finally (return sum)))
                          (score (i* score word-mul-product))
                          (score (if bingo-p
                                     (i+ score bingo-bonus)
                                     score)))
                   (macrolet ((make-this-move ()
                                `(make-move
                                  :action :place
                                  :bingo-p bingo-p
                                  :score score
                                  :direction :horizontal
                                  :start-row center-row
                                  :start-col start-col
                                  :word (trimmed)
                                  :blank-ps bps
                                  :tiles (trimmed)
                                  :already-on-board-ps
                                  (make-array length
                                              :element-type 'boolean
                                              :initial-element nil))))
                     ,(when (eq :TOP-VALUE result-type)
                       `(setf result (imax result score)))
                     ,(when (eq :TOPS result-type)
                       `(let ((top-score (if result
                                             (move-score
                                              (first result))
                                             MOST-NEGATIVE-FIXNUM)))
                          (cond ((i> score top-score)
                                 (setf result (list (make-this-move))))
                                ((i= score top-score)
                                 (push (make-this-move) result)))))
                     ,(when (eq :ALL result-type)
                       `(push (make-this-move) result)))))
                         
               (process-word-in-all-placements ()
                 #+nil
                 ,(when debug-p
                   `(format t "processing word in all placements~%"))
                 (loop
                    with min-col = (i- center-col (i1- length))
                    for start-col from min-col to center-col do
                      (process-word start-col)))

               (blank-hop ()
                 ,(when debug-p
                   `(format t "do the blank-hop!~%"))
                 (setf seq-idx skip-seq-idx
                       elt (aref elts (if (i< seq-idx (length elts))
                                          seq-idx
                                          0)))
                 (loop
                    named :blank-hopping
                    while (iplusp num-blanks-active) do
                      (let* ((next-rack-pos (if (i< seq-idx (length elts))
                                                (perm-elt-change-pos elt)
                                                0))
                             (bs (aref blank-states (i1- num-blanks-active))))
                        ,(when debug-p
                          `(format t "num-blanks-active: ~a~%~
                                      next-rack-pos: ~a~%~
                                      (blank-state-rack-pos bs): ~a~%"
                                   num-blanks-active next-rack-pos
                                   (blank-state-rack-pos bs)))
                        (if (i<= next-rack-pos (blank-state-rack-pos bs))
                            (progn  
                              ,(when debug-p
                                `(format t "moving over blank (~a <= ~a)~%~
                                           (blank-state-idx bs): ~a  ~
                                           (blank-state-limit bs): ~a~%"
                                         next-rack-pos (blank-state-rack-pos bs)
                                         (blank-state-idx bs) (blank-state-limit bs)))
                              (if (i< (blank-state-idx bs) (blank-state-limit bs))
                                  (let ((pe (aref elts (blank-state-seq-idx bs))))
                                    ,(when debug-p
                                      `(format t " advancing blank...~%"))
                                    (iincf (blank-state-idx bs))
                                    (setf seq-idx (blank-state-seq-idx bs)
                                          move-over-blank-p t
                                          rack-pos (blank-state-rack-pos bs)
                                          length (i1+ rack-pos)
                                          dawg-idx (aref rack-pos-dawg-idxs rack-pos)
                                          node (aref nodes dawg-idx)
                                          letter-idx (perm-elt-pos-value pe)
                                          tile-id (aref (rack-tiles rack) letter-idx)
                                          skip-seq-idx (perm-elt-next pe))
                                    (return-from :blank-hopping))
                                  (progn
                                    ,(when debug-p
                                      `(format t " deleting blank...~%"))
                                    (idecf num-blanks-active))))
                            (progn
                              (setf seq-idx skip-seq-idx)
                              (return-from :blank-hopping))))))
               .
               ,(when debug-p
                 `((word-text ()
                     (apply #'concatenate
                            (cons 'string
                                  (loop
                                     for idx below length 
                                     for tile-idx = (aref word idx)
                                     for tile = (and (iplusp tile-idx)
                                                     (aref ,tiles tile-idx))
                                     collect (if tile
                                                 (tile-text tile)
                                                 "_")))))
                  (node-string (bits)
                    (apply #'concatenate
                           (cons 'string
                                 (loop
                                    for letter fixnum below 26
                                    for tile = (aref ,tiles (i1+ letter))
                                    when (logbitp letter bits)
                                    collect (tile-text tile)))))
                   (nth-child-letter (n)
                     ,(when debug-p
                       `(format t "looking for ~ath child-letter at ~a (~a)~%"
                                n dawg-idx (node-string node)))
                     (loop
                        with found = 0
                        for letter below 26 ;; ick!
                        when (node-has-child letter)
                        do (if (i= found n)
                               (return-from
                                nth-child-letter
                                 (i1+ letter))
                               (iincf found)))
                     ,(when debug-p
                       `(format t "did not find ~ath child-letter at ~a (~a)~%"
                                n dawg-idx (node-string node)))))))
        
        (declare (dynamic-extent #'node-has-child
                                 #'fast-child-idx
                                 #'find-child-idx
                                 #'fast-nth-child-letter
                                 #'num-children
                                 #'trimmed
                                 #'blank-ps
                                 #'process-word
                                 #'process-word-in-all-placements
                                 #'blank-hop))
        ,(when debug-p
          `(declare (dynamic-extent #'word-text
                                    #'node-string
                                    #'nth-child-letter)))

        (unless move-over-blank-p
            (setf elt (aref elts seq-idx)
                  rack-pos (perm-elt-change-pos elt)
                  letter-idx (perm-elt-pos-value elt)
                  dawg-idx (aref rack-pos-dawg-idxs rack-pos)
                  node (aref nodes dawg-idx)
                  skip-seq-idx (perm-elt-next elt)
                  tile-id (aref (rack-tiles rack) letter-idx)
                  letter-id +empty+
                  length (i1+ rack-pos)))
          
        ,(when debug-p
          `(format t "====================~%~
                      START of the loop~%~
                      seq-idx: ~a~%~
                      num-blanks-active: ~a~%~
                      move-over-blank-p: ~a~%~
                      rack-pos: ~a~%~
                      ====================~%"
                   seq-idx num-blanks-active move-over-blank-p
                   rack-pos))

        (setf blank-state
              (if (izerop num-blanks-active)
                  nil
                  (aref blank-states (i1- num-blanks-active))))

        ,(when debug-p
               `(format t "seq-idx: ~a~%" seq-idx))
        (when (and (i= tile-id blank-id)
                   (not move-over-blank-p))
          ,(when debug-p
                 `(format t "making new blank-state~%"))
          (setf (aref blank-states num-blanks-active)
                (make-blank-state :idx 0
                                  :limit (i1- (num-children))
                                  :rack-pos rack-pos
                                  :seq-idx seq-idx)
                blank-state (aref blank-states
                                  num-blanks-active))
          (iincf num-blanks-active))

        (setf move-over-blank-p nil)
        
        (if (and blank-state (i= tile-id blank-id))
            (progn
              ,(when debug-p
                `(format t "fast-nth: ~a slow-nth: ~a~%"
                         (fast-nth-child-letter
                              dawg-idx
                              (blank-state-idx blank-state))
                         (nth-child-letter
                              (blank-state-idx blank-state))))
              ,(when debug-p
                `(assert (i= (nth-child-letter
                              (blank-state-idx blank-state))
                             (fast-nth-child-letter
                              dawg-idx
                              (blank-state-idx blank-state)))))
              (setf letter-id (fast-nth-child-letter
                               dawg-idx
                               (blank-state-idx blank-state))
                    #+nil (nth-child-letter (blank-state-idx blank-state))
                    (blank-state-assignment blank-state) letter-id))
            (setf letter-id tile-id))
        
        ,(when debug-p
          `(format t "b-s: ~a~%" blank-state))

        (setf (aref word rack-pos) letter-id)

        ,(when debug-p
          `(format t "letter-id: ~a  tile-id: ~a~%"
                   (tile-name (aref ,tiles letter-id))
                   (tile-name (aref ,tiles tile-id))))

        (let (#+nil (child-idx (find-child-idx (i1- letter-id)))
              (child-idx (fast-child-idx dawg-idx letter-id)))
          ,(when debug-p
            `(let ((slow-child-idx (find-child-idx (i1- letter-id))))
               (when (iplusp child-idx)
                 (assert (equal child-idx slow-child-idx)))))
          ,(when debug-p
            `(format t "child-idx: ~a~%" child-idx))
          (if (iplusp child-idx)
              (let* ((child (aref nodes child-idx))
                     (new-dawg-idx (child-pointer child))
                     (whole-word-p (child-terminates child)))
                (progn
                  ,(when debug-p
                    `(progn
                       (if whole-word-p
                           (format t "~a" (word-text))
                           (format t "~a" (string-downcase
                                             (word-text))))))

                  (when whole-word-p
                    (process-word-in-all-placements))

                  (if (or (izerop new-dawg-idx)
                          (i= rack-pos max-rack-pos))
                      (progn
                        ,(when debug-p
                          `(if (izerop new-dawg-idx)     
                               (format t " has no extensions~%")
                               (format t " is as long as we have ~
                                           letters for~%")))

                        (blank-hop))

                      (progn
                        ,(when debug-p
                          `(format t " has extensions: ~a~%"
                                   (node-string (aref nodes
                                                      new-dawg-idx))))
                        (iincf seq-idx)
                        (setf (aref rack-pos-dawg-idxs
                                    (i1+ rack-pos))
                               new-dawg-idx)))))
              (progn
                ,(when debug-p
                  `(progn
                     (format t "start of new progn at the bottom~%~
                                blank-state: ~a~%"
                             (when (iplusp num-blanks-active)
                               (aref blank-states
                                     (i1- num-blanks-active))))))
                (blank-hop))))))
    result)))