This repository has been archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
534 lines (448 loc) · 16.2 KB
/
Form1.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using ANDREICSLIB;
using ANDREICSLIB.ClassExtras;
using ANDREICSLIB.Licensing;
using ANDREICSLIB.NewControls;
using HistogramOCRTrainer;
namespace Word_Find_Solver
{
public partial class Form1 : Form
{
#region licensing
private const String HelpString = "";
private readonly String OtherText =
@"�" + DateTime.Now.Year +
@" Andrei Gec (http://www.andreigec.net)
Licensed under GNU LGPL (http://www.gnu.org/)
Zip Assets � SharpZipLib (http://www.sharpdevelop.net/OpenSource/SharpZipLib/)
";
#endregion
#region locks
public static bool AllowLookupEvents = true;
public static bool AllowTbChangeEvent = true;
#endregion locks
private void Form1_Load(object sender, EventArgs e)
{
Licensing.LicensingForm(this, menuStrip1, HelpString, OtherText);
Grid.Baseform = this;
Grid.InitPanel(grid);
Grid.InitWords();
InitGrid(8, 8);
}
public enum SortMode
{
Invalid, Length, Score, Alphabet
}
public SortMode CurrentSortMode = SortMode.Score;
public bool SortModeDesc = true;
public void ChangeSortMode(SortMode sm)
{
if (sm == CurrentSortMode)
{
SortModeDesc = !SortModeDesc;
return;
}
SortModeDesc = true;
CurrentSortMode = sm;
}
/// <summary>
/// get results and order based on search
/// </summary>
/// <returns></returns>
private void ApplySort()
{
switch (CurrentSortMode)
{
case SortMode.Invalid:
return;
case SortMode.Length:
if (SortModeDesc)
results = results.OrderByDescending(s => s.Word.Length).ToList();
else
results = results.OrderBy(s => s.Word.Length).ToList();
break;
case SortMode.Score:
if (SortModeDesc)
results = results.OrderByDescending(s => s.WordScore).ToList();
else
results = results.OrderBy(s => s.WordScore).ToList();
break;
case SortMode.Alphabet:
if (SortModeDesc)
results = results.OrderByDescending(s => s.Word).ToList();
else
results = results.OrderBy(s => s.Word).ToList();
break;
default:
return;
}
}
public Form1()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void ResizeWindow()
{
int w = MinimumSize.Width;
int h = MinimumSize.Height;
var lastTb = Grid.GetLastTextbox();
int lx = lastTb.Location.X;
lx += 340;
int ly = Grid.GetLastTextbox().Location.Y;
ly += 380;
if (lx > w)
w = lx;
if (ly > h)
h = ly;
Size = new Size(w, h);
}
private void InitGrid(int width, int height)
{
Grid.InitGrid(width, height);
ResizeWindow();
}
private void InitGrid(string[] rows)
{
Grid.InitGrid(rows);
ResizeWindow();
}
private void ApplySolve()
{
if (string.IsNullOrEmpty(manualentry.Text) == false)
{
var op = GetFindMode();
Grid.FindAndHighlight(manualentry.Text, op);
}
else if (foundwordsLB.SelectedItems.Count == 1 && foundwordsLB.SelectedItems[0].Tag is Grid.FoundWord)
{
var sel = (Grid.FoundWord)foundwordsLB.SelectedItems[0].Tag;
Grid.FindAndHighlight(sel);
}
}
private List<Grid.FoundWord> results = null;
private void Solve(bool refreshResults)
{
if (results == null || refreshResults)
results = Grid.Solve(GetFindMode(), bestonlyCB.Checked, top100onlyCB.Checked ? 100 : -1);
foundwordsLB.Items.Clear();
ApplySort();
foreach (var r in results)
{
var lvi = new ListViewItem();
lvi.Text = r.Word;
lvi.Name = lvi.Text;
lvi.SubItems.Add(r.WordScore.ToString(CultureInfo.InvariantCulture));
lvi.Tag = r;
foundwordsLB.Items.Add(lvi);
}
}
public void LoadGridFromFile(String filename)
{
AllowLookupEvents = AllowTbChangeEvent = false;
var fs = new FileStream(filename, FileMode.Open);
var sr = new StreamReader(fs);
var f = sr.ReadToEnd();
sr.Close();
fs.Close();
var rows = f.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
InitGrid(rows);
Solve(true);
ApplySolve();
AllowLookupEvents = AllowTbChangeEvent = true;
}
private void LoadFromImage(string fn)
{
AllowLookupEvents = AllowTbChangeEvent = false;
Bitmap b;
try
{
b = new Bitmap(fn);
string[] rows = null;
//crop a bit if wanted
if (whenLoadingImageIgnoreTopBitOfImageToolStripMenuItem.Checked)
{
var rect = new Rectangle(0, 100, b.Width, b.Height - 100);
b = b.Clone(rect, PixelFormat.DontCare);
}
b = BitmapExtras.ChangeAllNonWhitePixelsToColour(b, Color.Black);
if (tesseractOCRToolStripMenuItem.Checked)
rows = OCR.LoadImage(b).Item3;
else
{
if (File.Exists("hist.hist") == false)
{
MessageBox.Show(
"error, histogram file 'hist.hist' does not exist.\r\nYou can generate manually via the histogram OCR trainer");
return;
}
var h = HistogramOCR.DeSerialise("hist.hist");
rows = h.PerformOCR(b, 100);
}
InitGrid(rows);
}
catch (Exception ex)
{
MessageBox.Show("error:" + ex);
AllowLookupEvents = AllowTbChangeEvent = true;
return;
}
Solve(true);
ApplySolve();
AllowLookupEvents = AllowTbChangeEvent = true;
}
private Grid.FindMode GetFindMode()
{
if (crosswordRB.Checked)
return Grid.FindMode.Crossword;
if (lastletterllRB.Checked)
return Grid.FindMode.AllDirFromLast;
if (anywhereRB.Checked)
return Grid.FindMode.Anywhere;
return Grid.FindMode.NotSet;
}
private void loadwordgrid_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.CurrentDirectory;
ofd.Filter = "|*.txt";
var res = ofd.ShowDialog();
if (res != DialogResult.OK)
return;
LoadGridFromFile(ofd.FileName);
}
private void limitToTop100ResultsToolStripMenuItem_Click(object sender, EventArgs e)
{
top100onlyCB.Checked = !top100onlyCB.Checked;
Solve(true);
ApplySolve();
}
#region search type
private void crosswordRB_CheckedChanged(object sender, EventArgs e)
{
Solve(top100onlyCB.Checked);
ApplySolve();
}
private void lastletterllRB_CheckedChanged(object sender, EventArgs e)
{
Solve(top100onlyCB.Checked);
ApplySolve();
}
private void anywhereRB_CheckedChanged(object sender, EventArgs e)
{
Solve(top100onlyCB.Checked);
ApplySolve();
}
#endregion search type
#region sort type
private void sortlengthbutton_Click(object sender, EventArgs e)
{
ChangeSortMode(SortMode.Length);
Solve(top100onlyCB.Checked);
ApplySolve();
}
private void sortscorebutton_Click(object sender, EventArgs e)
{
ChangeSortMode(SortMode.Score);
Solve(false);
ApplySolve();
}
private void sortnamebutton_Click(object sender, EventArgs e)
{
ChangeSortMode(SortMode.Alphabet);
Solve(false);
ApplySolve();
}
#endregion sort type
private void savebutton_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Choose a file name for the new grid";
sfd.Filter = "|*.txt";
sfd.InitialDirectory = Environment.CurrentDirectory;
var res = sfd.ShowDialog();
if (res != DialogResult.OK)
return;
Grid.SaveGridToFile(sfd.FileName);
}
private void clearbutton_Click(object sender, EventArgs e)
{
Grid.Clear();
foundwordsLB.Items.Clear();
}
private void createbutton_Click(object sender, EventArgs e)
{
try
{
int w = int.Parse(createwidthTB.Text);
int h = int.Parse(createheightTB.Text);
InitGrid(w, h);
}
catch (Exception ex)
{
MessageBox.Show("error creating grid:" + ex);
}
return;
}
private void createwidthTB_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = TextboxExtras.HandleInput(TextboxExtras.InputType.Create(false, true), e.KeyChar, createwidthTB);
}
private void createheightTB_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = TextboxExtras.HandleInput(TextboxExtras.InputType.Create(false, true), e.KeyChar, createheightTB);
}
private void randomlettersbutton_Click(object sender, EventArgs e)
{
Grid.Clear();
Grid.RandomiseLetters();
Solve(true);
ApplySolve();
}
private void loadfromimageB_Click(object sender, EventArgs e)
{
var ofd = new OpenFileDialog();
ofd.InitialDirectory = Environment.CurrentDirectory;
ofd.Filter = "|*.*";
ofd.Title = "choose file to load letters from";
var res = ofd.ShowDialog();
if (res != DialogResult.OK)
return;
LoadFromImage(ofd.FileName);
}
private void histogramOCRToolStripMenuItem_Click(object sender, EventArgs e)
{
tesseractOCRToolStripMenuItem.Checked = histogramOCRToolStripMenuItem.Checked;
histogramOCRToolStripMenuItem.Checked = !histogramOCRToolStripMenuItem.Checked;
}
private void tesseractOCRToolStripMenuItem_Click(object sender, EventArgs e)
{
histogramOCRToolStripMenuItem.Checked = tesseractOCRToolStripMenuItem.Checked;
tesseractOCRToolStripMenuItem.Checked = !tesseractOCRToolStripMenuItem.Checked;
}
private void foundwordsLB_SelectedIndexChanged(object sender, EventArgs e)
{
if (AllowLookupEvents)
{
AllowLookupEvents = false;
if (foundwordsLB.SelectedItems.Count == 1 && foundwordsLB.SelectedItems[0].Tag is Grid.FoundWord)
{
manualentry.Text = "";
ApplySolve();
}
AllowLookupEvents = true;
}
}
private void manualentry_TextChanged(object sender, EventArgs e)
{
if (AllowLookupEvents)
{
AllowLookupEvents = false;
foundwordsLB.SelectedItems.Clear();
var mk = manualentry.Text.ToUpper();
var i = foundwordsLB.Items.IndexOfKey(mk);
if (i != -1)
{
foundwordsLB.Items[i].Selected = true;
foundwordsLB.EnsureVisible(i);
}
ApplySolve();
AllowLookupEvents = true;
}
}
public void GridTBChangeEvent(object sender, EventArgs e)
{
if (AllowTbChangeEvent)
{
AllowTbChangeEvent = false;
var tb = sender as TextBox;
Grid.SetGridPointValue(tb, tb.Text, false);
Solve(true);
ApplySolve();
AllowTbChangeEvent = true;
}
}
public void changeMultiplierToolStripMenuItem_Click(object sender, EventArgs e)
{
var tb = (TextBox)ContextMenuStripExtras.GetContextParent(changeMultiplierToolStripMenuItem, typeof(TextBox));
var gp = Grid.GetPointFromTextBox(tb);
if (gp == null)
{
MessageBox.Show("error setting multiplier value");
}
if (gp.C == 0)
return;
var items = new List<MassVariableEdit.TextBoxItems>
{
new MassVariableEdit.TextBoxItems("Enter a letter multiplier for this grid square", "1",
KeypressEvent, AcceptFinalTextBoxTextH,"Must be >=0 and a number"),
new MassVariableEdit.TextBoxItems("Enter an extra word bonus multiplier. eg 1=2x score",
"0",KeypressEvent, AcceptFinalTextBoxTextH, "Must be >=0 and a number")
};
var mve = new MassVariableEdit();
var res = mve.ShowDialog("Enter the values", items);
if (res == null)
return;
var lm1 = res[0].Item2;
int lm11;
int.TryParse(lm1, out lm11);
var wm1 = res[1].Item2;
int wm11;
int.TryParse(wm1, out wm11);
gp.LetterMultiplier = lm11;
gp.WordMultiplierExtra = wm11;
Solve(true);
//set font
if (lm11 != 1 || wm11 != 0)
tb.Font = new Font(tb.Font.FontFamily, tb.Font.Size, FontStyle.Bold);
else
tb.Font = new Font(tb.Font.FontFamily, tb.Font.Size, FontStyle.Regular);
}
private bool AcceptFinalTextBoxTextH(string s)
{
try
{
var i = int.Parse(s);
if (i < 0)
return false;
return true;
}
catch (Exception)
{
}
return false;
}
private bool KeypressEvent(char keyChar, Control control)
{
return TextboxExtras.HandleInput(TextboxExtras.InputType.Create(false, true), keyChar, control);
}
private void GridLetterContext_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
var tb = (TextBox)ContextMenuStripExtras.GetContextParent(GridLetterContext, typeof(TextBox));
var gp = Grid.GetPointFromTextBox(tb);
if (gp == null)
{
MessageBox.Show("error setting multiplier value");
}
if (gp.C == 0)
e.Cancel = true;
}
private void onlyReturnTheHighestScoringOfDuplicateWordsToolStripMenuItem_Click(object sender, EventArgs e)
{
bestonlyCB.Checked =
!bestonlyCB.Checked;
Solve(true);
ApplySolve();
}
}
}