-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cpp
1867 lines (1587 loc) · 47.5 KB
/
generator.cpp
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2006 Jason Katz-Brown and John O'Laughlin.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include <fstream>
#include <iostream>
#include <math.h>
#include "datamanager.h"
#include "evaluator.h"
#include "generator.h"
#include "gaddag.h"
#include "board.h"
#include "boardparameters.h"
#include "gameparameters.h"
#include "lexiconparameters.h"
// #define DEBUG_GENERATOR
using namespace std;
using namespace Quackle;
Generator::Generator()
{
}
Generator::Generator(const GamePosition &position)
: m_position(position)
{
}
Generator::~Generator()
{
}
void Generator::kibitz(int kibitzLength, int flags)
{
// don't just record best move, unless kibitz length is one
setrecordall(kibitzLength > 1);
// perform actual kibitz
findstaticbest(!(flags & CannotExchange));
m_kibitzList.clear();
if (kibitzLength <= 1)
{
m_kibitzList.push_back(best);
return;
}
filterOutDuplicatePlays();
MoveList::sort(m_moveList, MoveList::Equity);
int i = 0;
MoveList::iterator end(m_moveList.end());
for (MoveList::iterator it = m_moveList.begin(); it != end; ++it, ++i)
{
if (i >= kibitzLength)
break;
m_kibitzList.push_back(*it);
}
}
void Generator::filterOutDuplicatePlays()
{
map<int, bool> oneTilePlayMap;
for (MoveList::iterator it = m_moveList.begin(); it != m_moveList.end();)
{
LetterString usedTiles = (*it).usedTiles();
if (usedTiles.size() == 1)
{
const LetterString &tiles = (*it).tiles();
int actualTileIndex = 0;
for (LetterString::const_iterator letterIt = tiles.begin(); letterIt != tiles.end(); ++letterIt, ++actualTileIndex)
if ((*letterIt) != QUACKLE_PLAYED_THRU_MARK)
break;
const int row = (*it).startrow + ((*it).horizontal? 0 : actualTileIndex);
const int column = (*it).startcol + ((*it).horizontal? actualTileIndex : 0);
int key = row + QUACKLE_MAXIMUM_BOARD_SIZE * column + (QUACKLE_MAXIMUM_BOARD_SIZE * QUACKLE_MAXIMUM_BOARD_SIZE) * String::front(usedTiles);
if (oneTilePlayMap.find(key) == oneTilePlayMap.end())
{
oneTilePlayMap[key] = true;
++it;
}
else
{
it = m_moveList.erase(it);
}
}
else
{
++it;
}
}
}
void Generator::allCrosses()
{
vector<int> vrows, vcols, hrows, hcols;
for (int i = 0; i < board().height(); i++) {
for (int j = 0; j < board().width(); j++) {
vrows.push_back(i);
vcols.push_back(j);
hrows.push_back(i);
hcols.push_back(j);
}
}
// check the appropriate crosses
for (unsigned int i = 0; i < vrows.size(); i++) {
int row = vrows[i];
int col = vcols[i];
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
board().setVCross(row, col, 0);
}
else {
LetterString pre;
if (row > 0) {
for (int i = row - 1; i >= 0; i--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(i, col))) {
i = -1;
}
else {
LetterString newpre;
newpre += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(i, col));
newpre += pre;
pre = newpre;
}
}
}
LetterString suf;
if (row < board().height() - 1) {
for (int i = row + 1; i < board().height(); i++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(i, col))) {
i = board().height();
}
else {
suf += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(i, col));
}
}
}
#ifdef DEBUG_GENERATOR
UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(pre) << " / " << QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
#endif
if (pre.empty() && suf.empty()) {
board().setVCross(row, col, Utility::Max);
}
else {
board().setVCross(row, col, fitbetween(pre, suf));
}
#ifdef DEBUG_GENERATOR
UVcout << "board().vcross[" << row << "][" << col << "] = " << cross2string(board().vcross(row, col)) << endl;
#endif
}
}
for (unsigned int i = 0; i < hrows.size(); i++) {
int row = hrows[i];
int col = hcols[i];
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
board().setHCross(row, col, 0);
}
else {
LetterString pre;
if (col > 0) {
for (int i = col - 1; i >= 0; i--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, i))) {
i = -1;
}
else {
LetterString newpre;
newpre += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(row, i));
newpre += pre;
pre = newpre;
}
}
}
LetterString suf;
if (col < board().width() - 1) {
for (int i = col + 1; i < board().width(); i++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, i))) {
i = board().width();
}
else {
suf += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(row, i));
}
}
}
if (pre.empty() && suf.empty()) {
board().setHCross(row, col, Utility::Max);
}
else {
board().setHCross(row, col, fitbetween(pre, suf));
}
#ifdef DEBUG_GENERATOR
UVcout << "board().hcross[" << row << "][" << col << "] = " << cross2string(board().hcross(row, col)) << endl;
#endif
}
}
}
void Generator::makeMove(const Move &move, bool regenerateCrosses)
{
if (move.action != Move::Place)
return;
if (!regenerateCrosses)
{
board().makeMove(move);
return;
}
// mark which squares need their crosses checked
vector<int> hrows;
vector<int> hcols;
vector<int> vrows;
vector<int> vcols;
hrows.reserve(2 * QUACKLE_MAXIMUM_BOARD_SIZE);
hcols.reserve(2 * QUACKLE_MAXIMUM_BOARD_SIZE);
vrows.reserve(2 * QUACKLE_MAXIMUM_BOARD_SIZE);
vcols.reserve(2 * QUACKLE_MAXIMUM_BOARD_SIZE);
if (move.horizontal) {
int row = move.startrow;
int endcol = move.startcol + move.tiles().length() - 1;
if (move.startcol > 0) {
hrows.push_back(row);
hcols.push_back(move.startcol - 1);
}
if (endcol < board().width() - 1) {
hrows.push_back(row);
hcols.push_back(endcol + 1);
}
for (int col = move.startcol; col <= endcol; col++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
int upempty = -1;
for (int hookrow = row - 1; hookrow >= 0; hookrow--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(hookrow, col))) {
upempty = hookrow;
hookrow = -1;
}
}
if (upempty >= 0) {
vrows.push_back(upempty);
vcols.push_back(col);
}
int downempty = board().height();
for (int hookrow = row + 1; hookrow < board().height(); hookrow++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(hookrow, col))) {
downempty = hookrow;
hookrow = board().height();
}
}
if (downempty < board().height()) {
vrows.push_back(downempty);
vcols.push_back(col);
}
}
}
}
else {
int col = move.startcol;
int endrow = move.startrow + move.tiles().length() - 1;
if (move.startrow > 0) {
vrows.push_back(move.startrow - 1);
vcols.push_back(col);
}
if (endrow < board().height() - 1) {
vrows.push_back(endrow + 1);
vcols.push_back(col);
}
for (int row = move.startrow; row <= endrow; row++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
int upempty = -1;
for (int hookcol = col - 1; hookcol >= 0; hookcol--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, hookcol))) {
upempty = hookcol;
hookcol = -1;
}
}
if (upempty >= 0) {
hrows.push_back(row);
hcols.push_back(upempty);
}
int downempty = board().width();
for (int hookcol = col + 1; hookcol < board().width(); hookcol++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, hookcol))) {
downempty = hookcol;
hookcol = board().width();
}
}
if (downempty < board().width()) {
hrows.push_back(row);
hcols.push_back(downempty);
}
}
}
}
board().makeMove(move);
// check the appropriate crosses
for (unsigned int i = 0; i < vrows.size(); i++) {
int row = vrows[i];
int col = vcols[i];
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
board().setVCross(row, col, 0);
}
else {
LetterString pre;
if (row > 0) {
for (int i = row - 1; i >= 0; i--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(i, col))) {
i = -1;
}
else {
LetterString newpre;
newpre += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(i, col));
newpre += pre;
pre = newpre;
}
}
}
LetterString suf;
if (row < board().height() - 1) {
for (int i = row + 1; i < board().height(); i++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(i, col))) {
i = board().height();
}
else {
suf += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(i, col));
}
}
}
#ifdef DEBUG_GENERATOR
UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(pre) << " / " << QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
#endif
if ((pre.empty()) && (suf.empty())) {
board().setVCross(row, col, Utility::Max);
}
else {
board().setVCross(row, col, fitbetween(pre, suf));
}
#ifdef DEBUG_GENERATOR
UVcout << "board().vcross[" << row << "][" << col << "] = " << cross2string(board().vcross(row, col)) << endl;
#endif
}
}
for (unsigned int i = 0; i < hrows.size(); i++) {
int row = hrows[i];
int col = hcols[i];
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, col))) {
board().setHCross(row, col, 0);
}
else {
LetterString pre;
if (col > 0) {
for (int i = col - 1; i >= 0; i--) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, i))) {
i = -1;
}
else {
LetterString newpre;
newpre += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(row, i));
newpre += pre;
pre = newpre;
}
}
}
LetterString suf;
if (col < board().width() - 1) {
for (int i = col + 1; i < board().width(); i++) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(row, i))) {
i = board().width();
}
else {
suf += QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(row, i));
}
}
}
#ifdef DEBUG_GENERATOR
UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(pre) << " / " << QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
#endif
if ((pre.empty()) && (suf.empty())) {
board().setHCross(row, col, Utility::Max);
}
else {
board().setHCross(row, col, fitbetween(pre, suf));
}
#ifdef DEBUG_GENERATOR
UVcout << "board().hcross[" << row << "][" << col << "] = " << cross2string(board().hcross(row, col)) << endl;
#endif
}
}
}
void Generator::readFromDawg(int index, unsigned int &p, Letter &letter, bool &t, bool &lastchild, bool &british, int &playability) const
{
index *= 7;
p = (QUACKLE_LEXICON_PARAMETERS->dawgAt(index) << 16) + (QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 1) << 8) + (QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 2));
letter = QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 3);
t = (letter & 32);
lastchild = (letter & 64);
british = !(letter & 128);
letter = (letter & 31) + QUACKLE_FIRST_LETTER;
playability = (QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 4) << 16) + (QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 5) << 8) + (QUACKLE_LEXICON_PARAMETERS->dawgAt(index + 6));
}
bool Generator::checksuffix(int i, const LetterString &suffix) {
unsigned int p;
Letter c;
bool t;
bool lastchild;
bool british;
int playability;
readFromDawg(i, p, c, t, lastchild, british, playability);
Letter sc = suffix[0];
//#ifdef DEBUG_BOARD
//UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(suffix) << " i: " << i << ", p: " << p << ", c: " << QUACKLE_ALPHABET_PARAMETERS->userVisible(c) << ", sc: " << QUACKLE_ALPHABET_PARAMETERS->userVisible(sc) << endl;
//#endif
if (c == sc) {
if (suffix.length() == 1) {
return t;
}
else {
if (p != 0) {
return checksuffix(p, String::allButFront(suffix));
}
else {
return false;
}
}
}
else if (true) {
//else if (c < sc) {
if (lastchild) {
return false;
}
else {
return checksuffix(i + 1, suffix);
}
}
else {
return false;
}
}
int Generator::gaddagFitbetween(const LetterString &pre, const LetterString &suf)
{
// UVcout << "fit "
// << QUACKLE_ALPHABET_PARAMETERS->userVisible(pre)
// << "_"
// << QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
int crosses = 0;
/* process the suffix once */
const GaddagNode *sufNode = QUACKLE_LEXICON_PARAMETERS->gaddagRoot();
int sufLen = suf.length();
for (int i = sufLen - 1; i >= 0; --i) {
sufNode = sufNode->child(suf[i]);
if (!sufNode) { // this can only happen if an illegal word is on the board
return crosses;
}
}
int preLen = pre.length();
for (const GaddagNode* node = sufNode->firstChild(); node; node = node->nextSibling()) {
Letter childLetter = node->letter();
if (childLetter == QUACKLE_GADDAG_SEPARATOR) {
break;
}
const GaddagNode *n = node;
for (int i = preLen - 1; i >= 0; --i) {
n = n->child(pre[i]);
if (!n) {
break;
}
}
if (n && n->isTerminal()) {
crosses |= Utility::Bits[(int)(childLetter)];
}
}
return crosses;
}
int Generator::fitbetween(const LetterString &pre, const LetterString &suf)
{
if (QUACKLE_LEXICON_PARAMETERS->hasGaddag()) {
return gaddagFitbetween(pre, suf);
}
//UVcout << QUACKLE_ALPHABET_PARAMETERS->userVisible(pre) << "_" <<
// QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
int crosses = 0;
for (Letter c = QUACKLE_FIRST_LETTER; c <= QUACKLE_ALPHABET_PARAMETERS->lastLetter(); c++) {
/*
UVcout << "Let's check " <<
QUACKLE_ALPHABET_PARAMETERS->userVisible(pre) <<
QUACKLE_ALPHABET_PARAMETERS->userVisible(c) <<
QUACKLE_ALPHABET_PARAMETERS->userVisible(suf) << endl;
*/
if (checksuffix(1, pre + c + suf)) {
// subtract first letter because crosses hold values starting from zero
crosses |= Utility::Bits[c - QUACKLE_FIRST_LETTER];
//UVcout << " that's a word" << endl;
// UVcout << c;
}
}
//UVcout << " crosses: " << crosses << " " << cross2string(crosses) << endl;
return crosses;
}
UVString Generator::counts2string()
{
UVString ret;
for (Letter i = 0; i < QUACKLE_ALPHABET_PARAMETERS->lastLetter(); i++)
for (int j = 0; j < m_counts[(int)(i)]; j++)
ret += QUACKLE_ALPHABET_PARAMETERS->userVisible(i);
return ret;
}
UVString Generator::cross2string(int cross)
{
UVString ret;
for (int i = 0; i < QUACKLE_ALPHABET_PARAMETERS->length(); i++)
if (cross & Utility::Bits[i])
ret += QUACKLE_ALPHABET_PARAMETERS->userVisible(QUACKLE_FIRST_LETTER + i);
return ret;
}
/*
Gen(pos, word, rack, arc): pos = offset from anchor
IF a letter, L, is already on this square THEN
GoOn(pos, L, word, rack, NextArc(arc, L), arc)
ELSE IF letters remain on the rack THEN
FOR each letter on the rack, L, allowed on this square
GoOn(pos, L, word, rack - L, NextArc(arc, L), arc)
IF the rack contains a BLANK THEN
FOR each letter the BLANK could be, L, allowed...
GoOn(pos, L, word, rack - BLANK, NextArc(arc, L), arc)
GoOn(pos, L, word, rack, NewArc, OldArc):
IF pos <= 0 THEN // moving left
word <- L + word
IF L on OldArc & no letter directly left THEN Record
IF NewArc != 0 THEN
IF room to the left THEN Gen(pos - 1, word, rack, NewArc)
NewArc <- NextArc(NewArc, ^)
IF NewArc != 0, no letter directly left & room to the right THEN
Gen(1, word, rack, NewArc)
ELSE IF pos > 0 THEN // moving right
word <- word + L
IF L on OldArc & no letter directly right THEN Record
IF NewArc != & room to the right THEN
Gen(pos + 1, word, rack, NewArc)
*/
void Generator::gordongoon(int pos, char L, LetterString word, const GaddagNode *node)
{
//UVcout << "gordongoon(" << pos << ", " << L << ", " << word << ", " << newarc << ", " << oldarc << ")" <<
// " horiz: " << m_gordonhoriz << endl;
if (pos <= 0) {
int currow = m_anchorrow;
int curcol = m_anchorcol;
int leftrow = m_anchorrow;
int leftcol = m_anchorcol;
if (m_gordonhoriz) {
curcol += pos;
leftcol += pos - 1;
}
else {
currow += pos;
leftrow += pos - 1;
}
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(currow, curcol))) {
L = QUACKLE_PLAYED_THRU_MARK;
}
LetterString newWord;
newWord += L;
newWord += word;
bool emptyleft = true;
bool roomtoleft = true;
bool atboardedge = false;
if ((leftcol >= 0) && (leftrow >= 0)) {
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(currow, curcol)) && QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(leftrow, leftcol))) {
roomtoleft = false;
}
if (pos < -m_leftlimit) {
roomtoleft = false;
}
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(leftrow, leftcol))) {
emptyleft = false;
}
}
else {
atboardedge = true;
}
if (node->isTerminal() && (roomtoleft) && (m_laid > 0)) {
// UVcout << "found a word or something " << word << " at " << pos << endl;
Move move;
move.action = Move::Place;
move.setTiles(newWord);
if (m_gordonhoriz) {
move.startrow = m_anchorrow;
move.startcol = m_anchorcol + pos;
}
else {
move.startrow = m_anchorrow + pos;
move.startcol = m_anchorcol;
}
move.horizontal = m_gordonhoriz;
move.score = board().score(move, &move.isBingo);
move.equity = equity(move);
if (m_recordall) {
m_moveList.push_back(move);
}
if (MoveList::equityComparator(best, move)) {
best = move;
}
// UVcout << "found a move: " << move << " score: " << move.score << ", equity: " << move.equity <<
// " outputted by leftmoving loop" << endl;
}
if (node != 0) {
if (roomtoleft && pos != -m_leftlimit && !atboardedge) {
gordongen(pos - 1, newWord, node);
}
// UVcout << "looking for the delimiter" << endl;
node = node->child(QUACKLE_GADDAG_SEPARATOR);
// UVcout << "after all that, delimiter's newarc is " << newarc << endl;
int rightrow = m_anchorrow;
int rightcol = m_anchorcol;
if (m_gordonhoriz) {
rightcol++;
}
else {
rightrow++;
}
bool atrightedge = false;
if ((rightrow > board().height() - 1) || (rightcol > board().width() - 1)) {
atrightedge = true;
}
if ((node != 0) && emptyleft && !atrightedge) {
gordongen(1, newWord, node);
}
}
}
else {
// UVcout << "looking to the right" << endl;
int currow = m_anchorrow;
int curcol = m_anchorcol;
int rightrow = m_anchorrow;
int rightcol = m_anchorcol;
if (m_gordonhoriz) {
curcol += pos;
rightcol += pos + 1;
}
else {
currow += pos;
rightrow += pos + 1;
}
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(currow, curcol))) {
word += QUACKLE_PLAYED_THRU_MARK;
}
else {
word += L;
}
bool roomtoright = true;
bool atboardedge = false;
// UVcout << "rightsquare: " << (char)(rightcol + 'A') << rightrow + 1 << endl;
if ((rightcol <= board().width() - 1) && (rightrow <= board().height() - 1)) {
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(rightrow, rightcol))) {
roomtoright = false;
// UVcout << "can't record " << word << " here because of the " << board().letter(rightrow, rightcol) << endl;
}
else {
// UVcout << "yay! " << (char)(rightcol + 'A') << rightrow + 1 << " is empty!" << endl;
// UVcout << board() << endl;
}
}
else {
// UVcout << "at board edge so i can maybe record " << word << endl;
atboardedge = true;
}
if (node->isTerminal() && (roomtoright) && (m_laid > 0)) {
// UVcout << "found a word or something " << word << " at " << pos << endl;
Move move;
move.action = Move::Place;
move.setTiles(word);
if (m_gordonhoriz) {
move.startrow = m_anchorrow;
move.startcol = m_anchorcol - word.length() + pos + 1;
}
else {
move.startrow = m_anchorrow - word.length() + pos + 1;
move.startcol = m_anchorcol;
}
move.horizontal = m_gordonhoriz;
move.score = board().score(move, &move.isBingo);
move.equity = equity(move);
if (m_recordall) {
m_moveList.push_back(move);
}
if (MoveList::equityComparator(best, move)) {
best = move;
}
// UVcout << "found a move: " << move << " score: " << move.score << ", equity: " << move.equity <<
// " outputted by rightmoving loop" << endl;
}
// UVcout << "newarc is " << newarc << endl;
if (node != 0) {
if (!atboardedge) {
gordongen(pos + 1, word, node);
}
else {
// UVcout << "didn't go ahead because we were at board edge" << endl;
}
}
else {
// UVcout << "didn't go ahead because newarc was zero" << endl;
}
}
}
void Generator::gordongen(int pos, const LetterString &word, const GaddagNode *node)
{
// UVcout << "gordongen(" << pos << ", " << word << ", " << i << ")" << " horiz: " << m_gordonhoriz << endl;
int currow = m_anchorrow;
int curcol = m_anchorcol;
if (m_gordonhoriz) {
curcol += pos;
}
else {
currow += pos;
}
int cross;
if (m_gordonhoriz) {
cross = board().vcross(currow, curcol);
}
else {
cross = board().hcross(currow, curcol);
}
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(currow, curcol))) {
// UVcout << "gordongen sez a letter (" << board().letter(currow, curcol) << ") already on this square" << endl;
Letter boardc = QUACKLE_ALPHABET_PARAMETERS->clearBlankness(board().letter(currow, curcol));
const GaddagNode *child = node->child(boardc);
if (child) {
gordongoon(pos, board().letter(currow, curcol), word, child);
}
}
else {
for (const GaddagNode* child = node->firstChild(); child; child = child->nextSibling()) {
Letter childLetter = child->letter();
if ((m_counts[(int)(childLetter)] <= 0)
|| !(cross & Utility::Bits[(int)(childLetter)])) {
continue;
}
if (childLetter == QUACKLE_GADDAG_SEPARATOR) {
// UVcout << "ran into a delimiter" << endl;
break;
}
m_counts[(int)(childLetter)]--;
m_laid++;
// UVcout << " yeah that'll work" << endl;
gordongoon(pos, childLetter, word, child);
m_counts[(int)(childLetter)]++;
m_laid--;
}
if (m_counts[(int)(QUACKLE_BLANK_MARK)] >= 1) {
for (const GaddagNode* child = node->firstChild(); child; child = child->nextSibling()) {
Letter childLetter = child->letter();
// UVcout << "childLetter is " << (char)(arcc + 'A') << endl;
if (childLetter == QUACKLE_GADDAG_SEPARATOR) {
// UVcout << "ran into a delimiter" << endl;
break;
}
if (cross & Utility::Bits[(int)(childLetter)]) {
m_counts[(int)(QUACKLE_BLANK_MARK)]--;
m_laid++;
// UVcout << " yeah that'll work" << endl;
gordongoon(pos, QUACKLE_ALPHABET_PARAMETERS->setBlankness(childLetter), word, child);
m_counts[(int)(QUACKLE_BLANK_MARK)]++;
m_laid--;
}
}
}
}
}
void Generator::extendright(const LetterString &partial, int i,
int row, int col, int edge, int righttiles, bool horizontal)
{
if (i == 0) { // is this really correct?
return;
}
unsigned int p;
Letter c;
bool t;
bool lastchild;
bool british;
int playability;
readFromDawg(i, p, c, t, lastchild, british, playability);
int rowpos = row;
int rownext = row;
int colpos = col;
int colnext = col;
int dirpos;
int edgeDirpos;
if (horizontal) {
colpos = col + righttiles;
colnext = col + righttiles + 1;
dirpos = colpos;
edgeDirpos = board().width() - 1;
}
else {
rowpos = row + righttiles;
rownext = row + righttiles + 1;
dirpos = rowpos;
edgeDirpos = board().height() - 1;
}
#ifdef DEBUG_GENERATOR
UVcout << "extendright(" << QUACKLE_ALPHABET_PARAMETERS->userVisible(partial) << ", " << i << ", " << counts2string() << ", " << rowpos << ", " << colpos << ", " << horizontal << ")" << endl;
#endif
if (!QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(rowpos, colpos))) {
if ((m_counts[(int)(c)] >= 1)) {
int cross;
if (horizontal) {
cross = board().vcross(rowpos, colpos);
}
else {
cross = board().hcross(rowpos, colpos);
}
if (cross & Utility::Bits[(int)(c - QUACKLE_FIRST_LETTER)]) {
if (t) {
bool couldend = true;
if (dirpos < edgeDirpos) {
if (QUACKLE_ALPHABET_PARAMETERS->isSomeLetter(board().letter(rownext, colnext))) {
couldend = false;
}
}
if (couldend) {
Move move;
move.action = Move::Place;
move.setTiles(partial + c);
if (horizontal) {
move.startrow = row;
move.startcol = col - (partial.length() - righttiles);
}
else {
move.startrow = row - (partial.length() - righttiles);
move.startcol = col;
}
move.horizontal = horizontal;
move.score = board().score(move, &move.isBingo);
move.equity = equity(move);
// i added this because m_laid is wrong and i don't want to break anything by fixing it :)
// will need to remember to add this bit to the DAGGAD code when we start using it again
int laid = move.wordTilesWithNoPlayThru().length();
bool onetilevert = (!move.horizontal) && (laid == 1);
bool ignore = onetilevert && (board().hcross(row, col) != Utility::Max);
if (1 || !ignore)
{
if (m_recordall) {
m_moveList.push_back(move);
}
if (MoveList::equityComparator(best, move)) {
best = move;
}
#ifdef DEBUG_GENERATOR
UVcout << "found a move: " << move << " laid: " << m_laid << ", score: " << move.score << ", equity: " << move.equity << endl;
#endif
}
}
}
if (dirpos < edgeDirpos) {
m_counts[(int)(c)]--;
m_laid++;
extendright(partial + c, p, row, col,
0, righttiles + 1, horizontal);
m_counts[(int)(c)]++;
m_laid--;
}
}