-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
630 lines (564 loc) · 19.1 KB
/
Copy pathMainForm.cs
File metadata and controls
630 lines (564 loc) · 19.1 KB
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
/// <summary>
/// Korean Word Search Application
/// 12/25/2015 Print Preview Added
/// 11/02/2015 Created by //AJ
/// </summary>
using System;
using System.Data;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace KoWordSearch
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
#region [ Members ]
/// <summary>
/// Matrix Data Table
/// </summary>
private BindingSource m_MatrixBs = new BindingSource();
private DataTable m_MatrixTbl = new DataTable("MatrixTbl");
/// <summary>
/// Vocabulary Data Table
/// </summary>
private BindingSource m_VocabBs = new BindingSource();
private DataTable m_VocabTbl = new DataTable("VocabTbl");
private string m_VocabFile = Application.StartupPath + @"\Vocabulary\Vocab-0001.txt";
public const string VOC_KOWORD = "KoWord";
private const string VOC_KOWORDLEN = "KoWordLen";
public const string VOC_ENWORD = "EnWord";
private const string VOC_ENWORDLEN = "EnWordLen";
private const string VOC_ORIENT = "Orient";
private const string VOC_X = "X";
private const string VOC_Y = "Y";
private const string VOC_ISPICKED = "IsPicked";
private const string CRLF = "\x000d\x000a";
private Random m_Ran = new Random(0);
private int m_TriesMaximum = 4000;
/// <summary>
/// Configuration Parameters
/// </summary>
private string CONFIG_FILE = Application.StartupPath + @"\" + "config.ini";
private const string CFG_VOCABULARY_FILE = "Vocabulary_File";
private const string CFG_MATRIX_ROWS = "Matrix_Rows";
private const string CFG_MATRIX_COLS = "Matrix_Cols";
private const string CFG_TRIES_MAXIMUM = "Tries_Maximum";
private const string CFG_LIST_LANGUAGE = "List_Language";
private const string CFG_WINDOW_WIDTH = "Window_Width";
private const string CFG_WINDOW_HEIGHT = "Window_Height";
#endregion
#region [ Constructor and Configuration ]
public MainForm()
{
// The InitializeComponent() call is required for Windows Forms designer support.
InitializeComponent();
m_VocabTbl.Columns.Add(new DataColumn(VOC_KOWORD, typeof(string)));
m_VocabTbl.Columns.Add(new DataColumn(VOC_KOWORDLEN, typeof(int)));
m_VocabTbl.Columns.Add(new DataColumn(VOC_ENWORD, typeof(string)));
m_VocabTbl.Columns.Add(new DataColumn(VOC_ENWORDLEN, typeof(int)));
m_VocabTbl.Columns.Add(new DataColumn(VOC_ORIENT, typeof(bool)));
m_VocabTbl.Columns.Add(new DataColumn(VOC_X, typeof(int)));
m_VocabTbl.Columns[VOC_X].DefaultValue = 0;
m_VocabTbl.Columns.Add(new DataColumn(VOC_Y, typeof(int)));
m_VocabTbl.Columns[VOC_Y].DefaultValue = 0;
m_VocabTbl.Columns.Add(new DataColumn(VOC_ISPICKED, typeof(bool)));
m_VocabTbl.Columns[VOC_ISPICKED].DefaultValue = false;
ConfigLoad();
}
/// <summary>
/// Saves the current configuration for the User
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SaveMnuClick(object sender, EventArgs e)
{
ConfigSave();
}
/// <summary>
/// Saves current configuration when the User quits and closes the application
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
ConfigSave();
}
/// <summary>
/// Saves the configuration
/// </summary>
private void ConfigSave()
{
try
{
StringBuilder sb = new StringBuilder("[Vocabulary]" + CRLF);
sb.Append(CFG_VOCABULARY_FILE + "=" + Application.StartupPath + @"\Vocabulary\Vocab-0001.txt" + CRLF);
sb.Append("[Matrix]" + CRLF);
sb.Append(CFG_MATRIX_COLS + "=" + this.MatrixColsNud.Value.ToString() + CRLF);
sb.Append(CFG_MATRIX_ROWS + "=" + this.MatrixRowsNud.Value.ToString() + CRLF);
sb.Append(CFG_TRIES_MAXIMUM + "=" + m_TriesMaximum.ToString() + CRLF);
sb.Append(CFG_LIST_LANGUAGE + "=" + this.EnglishRadio.Checked.ToString() + CRLF);
sb.Append("[Display]" + CRLF);
sb.Append(CFG_WINDOW_WIDTH + "=" + this.Width.ToString() + CRLF);
sb.Append(CFG_WINDOW_HEIGHT + "=" + this.Height.ToString() + CRLF);
if (File.Exists(CONFIG_FILE))
{
File.Delete(CONFIG_FILE);
}
File.WriteAllText(CONFIG_FILE, sb.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Error Save Configuration File" + CRLF + ex.Message, "Save Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
/// <summary>
/// Loads the Configuration Settings
/// </summary>
private void ConfigLoad()
{
bool isIniLoaded = false;
if (File.Exists(CONFIG_FILE))
{
try
{
string[] inis = File.ReadAllLines(CONFIG_FILE);
foreach (string ini in inis)
{
string[] lrs = ini.Split("=".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (lrs.Length > 1)
{
if (lrs[0].Trim() == CFG_VOCABULARY_FILE)
{
m_VocabFile = lrs[1].Trim();
if (m_VocabFile.Length > 0)
{
VocabLoad();
}
}
else if (lrs[0].Trim() == CFG_MATRIX_COLS)
{
try
{
int cols = Convert.ToInt32(lrs[1].Trim());
this.MatrixColsNud.Value = cols;
}
catch
{
// default per design mode
}
}
else if (lrs[0].Trim() == CFG_MATRIX_ROWS)
{
try
{
int rows = Convert.ToInt32(lrs[1].Trim());
this.MatrixRowsNud.Value = rows;
}
catch
{
// default per design mode
}
}
else if (lrs[0].Trim() == CFG_TRIES_MAXIMUM)
{
try
{
int tries = Convert.ToInt32(lrs[1].Trim());
m_TriesMaximum = tries;
}
catch
{
// default per design mode
}
}
else if (lrs[0].Trim() == CFG_LIST_LANGUAGE)
{
try
{
bool test = Convert.ToBoolean(lrs[1].Trim());
this.EnglishRadio.Checked = test;
}
catch
{
// default per design mode
}
}
else if (lrs[0].Trim() == CFG_WINDOW_WIDTH)
{
try
{
this.Width = Convert.ToInt32(lrs[1].Trim());
}
catch
{
// default per design mode
}
}
else if (lrs[0].Trim() == CFG_WINDOW_HEIGHT)
{
try
{
this.Height = Convert.ToInt32(lrs[1].Trim());
}
catch
{
// default per design mode
}
}
}
}
isIniLoaded = true;
}
catch (Exception ex)
{
MessageBox.Show("Error Loading Configuration File" + CRLF + ex.Message, "Load Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
if (!isIniLoaded)
{
// Initialize a new Configuration file
ConfigSave();
}
}
/// <summary>
/// Rebuilds the Matrix when the user changes the Columns count
/// </summary>
private void ColumnsNudValueChanged(object sender, EventArgs e)
{
ColumnsBuild();
}
/// <summary>
/// Rebuilds the Matrix Columns
/// </summary>
private void ColumnsBuild()
{
m_MatrixBs.DataSource = null;
m_MatrixTbl.Columns.Clear();
this.MatrixDgv.Columns.Clear();
for (int cc = 0; cc < this.MatrixColsNud.Value; cc++)
{
string colName = "X" + cc.ToString("00");
m_MatrixTbl.Columns.Add(new DataColumn(colName, typeof(string)));
this.MatrixDgv.Columns.Add(colName, colName);
this.MatrixDgv.Columns[cc].DataPropertyName = colName;
this.MatrixDgv.Columns[cc].Width = 60;
this.MatrixDgv.Columns[cc].DefaultCellStyle = this.MatrixDgv.DefaultCellStyle;
}
m_MatrixTbl.AcceptChanges();
m_MatrixBs.DataSource = m_MatrixTbl.DefaultView;
this.MatrixDgv.DataSource = m_MatrixBs;
}
/// <summary>
/// Rebuilds the Matrix when the User changes the Rows count
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RowsNudValueChanged(object sender, EventArgs e)
{
RowsBuild();
}
/// <summary>
/// Rebuilds the Matrix Rows
/// </summary>
private void RowsBuild()
{
m_MatrixTbl.Rows.Clear();
m_MatrixTbl.AcceptChanges();
for (int rr = 0; rr < this.MatrixRowsNud.Value; rr++)
{
DataRow dr = m_MatrixTbl.NewRow();
m_MatrixTbl.Rows.Add(dr);
}
m_MatrixTbl.AcceptChanges();
}
/// <summary>
/// Selects a Random Hangul Sylable from the Unicode Characters
/// </summary>
/// <returns></returns>
private string HangulRandom()
{
return ((char)m_Ran.Next(0xAC00, 0xD7A4)).ToString();
}
/// <summary>
/// Loads the Vacabulary data from the selected vocabulary file
/// </summary>
private void VocabLoad()
{
m_VocabTbl.Rows.Clear();
if (!File.Exists(m_VocabFile))
{
return;
}
string[] vocablines = File.ReadAllLines(m_VocabFile);
foreach (string vocab in vocablines)
{
if (vocab.Trim().Length == 0)
{
break;
}
string[] voWord = vocab.Split(",".ToCharArray());
DataRow dr = m_VocabTbl.NewRow();
dr[VOC_KOWORD] = voWord[0].Trim();
dr[VOC_KOWORDLEN] = voWord[0].Trim().Length;
dr[VOC_ENWORD] = voWord[1].Trim();
dr[VOC_ENWORDLEN] = voWord[1].Trim().Length;
dr[VOC_X] = 0;
dr[VOC_Y] = 0;
if (m_Ran.Next(0, 2) == 0)
{
dr[VOC_ORIENT] = true;
}
else
{
dr[VOC_ORIENT] = false;
}
m_VocabTbl.Rows.Add(dr);
}
m_VocabTbl.AcceptChanges();
m_VocabBs.DataSource = m_VocabTbl.DefaultView;
this.VocabDgv.DataSource = m_VocabBs;
VocabListBox_SetLanguage();
int iMatrixMinimum = MatrixMinimum();
this.StatusLb.Text = "Vocabulary Loaded. Minimum Matrix Size = " + iMatrixMinimum.ToString() + " x " + iMatrixMinimum.ToString();
}
/// <summary>
/// Determines the minimum size for the matrix based on the vocabulary
/// The minimum size is the longest vocabulary word's length
/// </summary>
/// <returns></returns>
private int MatrixMinimum()
{
int iMaxWordLength = 0;
foreach (DataRowView drv in m_VocabTbl.DefaultView)
{
if (Convert.ToInt32(drv[VOC_KOWORDLEN]) > iMaxWordLength)
{
iMaxWordLength = Convert.ToInt32(drv[VOC_KOWORDLEN]);
}
}
return iMaxWordLength;
}
/// <summary>
/// Places the vocabulary words into the Matrix
/// </summary>
private void VocabLayout()
{
ColumnsBuild();
RowsBuild();
int iTries = 0;
int iGaveUp = 0;
string sPrefix = "";
for (int ii = 0; ii < m_VocabTbl.DefaultView.Count; ii++)
{
sPrefix = m_VocabTbl.DefaultView[ii][VOC_KOWORD].ToString() + " -> ";
m_VocabTbl.DefaultView[ii].BeginEdit();
iTries = 0;
if (Convert.ToBoolean(m_VocabTbl.DefaultView[ii][VOC_ORIENT]) == true)
{
// Horizontal
bool placed = false;
while (!placed)
{
iTries++;
if (iTries > m_TriesMaximum)
{
iGaveUp++;
break;
}
this.StatusLb.Text = sPrefix + iTries.ToString();
this.statusStrip1.Refresh();
// select a random row
int rowRan = m_Ran.Next(0, Convert.ToInt32(this.MatrixRowsNud.Value));
// select a random column that allows the word to fit.
int colRan = m_Ran.Next(0, Convert.ToInt32(Convert.ToInt32(this.MatrixColsNud.Value) - Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN])) + 1);
// Test for collision
for (int xx = 0; xx < Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]); xx++)
{
if ((m_MatrixTbl.DefaultView[rowRan][colRan + xx].ToString().Length == 0)
|| (m_MatrixTbl.DefaultView[rowRan][colRan + xx].ToString() == m_VocabTbl.DefaultView[ii][VOC_KOWORD].ToString()[xx].ToString()))
{
// cell is either empty or contains the same letter that we want to put there
// no collision
if (xx == (Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]) - 1))
{
placed = true;
}
}
else
{
// we have a collision
break;
}
}
if (placed)
{
for (int xx = 0; xx < Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]); xx++)
{
m_VocabTbl.DefaultView[ii][VOC_X] = colRan;
m_VocabTbl.DefaultView[ii][VOC_Y] = rowRan;
m_MatrixTbl.DefaultView[rowRan].BeginEdit();
m_MatrixTbl.DefaultView[rowRan][colRan + xx] = m_VocabTbl.DefaultView[ii][VOC_KOWORD].ToString()[xx];
m_MatrixTbl.DefaultView[rowRan].EndEdit();
}
m_MatrixTbl.AcceptChanges();
}
}
}
else
{
// Vertical
bool placed = false;
while (!placed)
{
iTries++;
if (iTries > m_TriesMaximum)
{
iGaveUp++;
break;
}
this.StatusLb.Text = sPrefix + iTries.ToString();
this.statusStrip1.Refresh();
// select a random row that will allow the word to fit vertically
int rowRan = m_Ran.Next(0, Convert.ToInt32(this.MatrixRowsNud.Value) - Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]));
// select a random column
int colRan = m_Ran.Next(0, Convert.ToInt32(this.MatrixColsNud.Value));
// Test for collision
for (int yy = 0; yy < Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]); yy++)
{
if ((m_MatrixTbl.DefaultView[rowRan + yy][colRan].ToString().Length == 0)
|| (m_MatrixTbl.DefaultView[rowRan + yy][colRan].ToString() == m_VocabTbl.DefaultView[ii][VOC_KOWORD].ToString()[yy].ToString()))
{
// cell is either empty or contains the same letter that we want to put there
// no collision
if (yy == (Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]) - 1))
{
placed = true;
}
}
else
{
// we have a collision
break;
}
}
if (placed)
{
for (int yy = 0; yy < Convert.ToInt32(m_VocabTbl.DefaultView[ii][VOC_KOWORDLEN]); yy++)
{
m_VocabTbl.DefaultView[ii][VOC_X] = colRan;
m_VocabTbl.DefaultView[ii][VOC_Y] = rowRan;
m_MatrixTbl.DefaultView[rowRan].BeginEdit();
m_MatrixTbl.DefaultView[rowRan + yy][colRan] = m_VocabTbl.DefaultView[ii][VOC_KOWORD].ToString()[yy];
m_MatrixTbl.DefaultView[rowRan].EndEdit();
}
m_MatrixTbl.AcceptChanges();
}
}
}
m_VocabTbl.DefaultView[ii].EndEdit();
}
if (iGaveUp > 0)
{
this.StatusLb.Text = sPrefix + "I gave up trying to place " + iGaveUp.ToString() + " words!";
}
else
{
this.StatusLb.Text = "Done = " + iTries.ToString() + " tries";
this.statusStrip1.Refresh();
}
}
/// <summary>
/// Generate the Final Word Search
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void RunTBtnClick(object sender, EventArgs e)
{
//VocabLoad();
//VocabLayout();
foreach (DataRowView drv in m_MatrixTbl.DefaultView)
{
drv.BeginEdit();
foreach (DataColumn dc in m_MatrixTbl.Columns)
{
if (drv[dc.ColumnName].ToString().Trim().Length == 0)
{
drv[dc.ColumnName] = HangulRandom();
}
}
drv.EndEdit();
}
m_MatrixTbl.AcceptChanges();
}
/// <summary>
/// Loads the Vocabulary List and Places the words in the Matrix
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PreviewTBtnClick(object sender, EventArgs e)
{
VocabLoad();
VocabLayout();
}
/// <summary>
/// Displays the File Open Dialog for the User to select a Vocabulary file.
/// If selected, then loads the file into the vocabulary table
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LoadVocabFileMnuClick(object sender, EventArgs e)
{
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.DefaultExt = "txt";
ofDlg.InitialDirectory = Path.GetDirectoryName(m_VocabFile);
try
{
if (ofDlg.ShowDialog() == DialogResult.OK)
{
m_VocabFile = ofDlg.FileName;
VocabLoad();
}
}
catch (Exception ex)
{
MessageBox.Show("Error Loading Vocabulary File" + CRLF + ex.Message, "Load Vocabulary Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
ofDlg.Dispose();
}
#endregion
/// <summary>
/// Changes the Vocabulary List to the Language the User selects
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void LanguageRadio_CheckedChanged(object sender, EventArgs e)
{
VocabListBox_SetLanguage();
}
/// <summary>
/// Sets the Vocabulary List Language to the current selected Language
/// </summary>
private void VocabListBox_SetLanguage()
{
this.VocabListBox.DataBindings.Clear();
((ListBox)this.VocabListBox).DataSource = m_VocabTbl.DefaultView;
((ListBox)this.VocabListBox).ValueMember = VOC_ISPICKED;
if (this.EnglishRadio.Checked)
{
((ListBox)this.VocabListBox).DisplayMember = VOC_ENWORD;
}
else
{
((ListBox)this.VocabListBox).DisplayMember = VOC_KOWORD;
}
}
private void PrintTBtn_Click(object sender, EventArgs e)
{
PrintPreviewDLG dlg = new PrintPreviewDLG(m_MatrixTbl, m_VocabTbl);
dlg.ShowDialog();
}
}
}