-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathForm1.cs
1319 lines (1300 loc) · 44.4 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
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
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using Microsoft.Win32;
namespace Sudoku {
public class Form1 : System.Windows.Forms.Form {
private IContainer components;
#region private stuff
private System.Drawing.Printing.PrintDocument printDocument1;
private System.Windows.Forms.PrintPreviewDialog printPreviewDialog1;
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.SaveFileDialog saveFileDialog1;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.MenuItem mnuRestart;
private System.Windows.Forms.MenuItem mnuOpen;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.MenuItem mnuSave;
private System.Windows.Forms.MenuItem mnuNew;
private System.Windows.Forms.MenuItem mnuSaveTemplate;
private System.Windows.Forms.MenuItem mnuTemplateMode;
private System.Windows.Forms.MenuItem mnuCheckSolution;
private System.Windows.Forms.MenuItem mnuEasy;
private System.Windows.Forms.MenuItem mnuHard;
private System.Windows.Forms.MenuItem mnuFile;
private System.Windows.Forms.MenuItem mnuGenerate;
private System.Windows.Forms.MenuItem mnuPrint;
private System.Windows.Forms.MenuItem mnuPrintPreview;
private System.Windows.Forms.MenuItem mnuSaveImage;
private System.Windows.Forms.MenuItem mnuExit;
private System.Windows.Forms.MenuItem mnuUtilities;
private System.Windows.Forms.MenuItem mnuMakePermanent;
private System.Windows.Forms.MenuItem mnuDifficult;
private System.Windows.Forms.MenuItem mnuHelp;
private System.Windows.Forms.MenuItem mnuAbout;
private System.Windows.Forms.MenuItem mnuHow;
private System.Windows.Forms.MenuItem mnuSolve;
private System.Windows.Forms.Label statusBar1;
private System.Windows.Forms.MenuItem mnuMedium;
private System.Windows.Forms.MenuItem mnuClearWrong;
private System.Timers.Timer timer1;
private System.Windows.Forms.Label timer;
private System.Windows.Forms.MenuItem mnuTimer;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem mnuStartTimer;
private System.Windows.Forms.MenuItem mnuStopTimer;
private System.Windows.Forms.MenuItem mnuResetTimer;
private System.Windows.Forms.MenuItem mnuShowHints;
private System.Windows.Forms.MenuItem mnuEditHints;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem mnuClearHints;
private System.Windows.Forms.MenuItem mnuAutoCalcHints;
private System.Windows.Forms.MenuItem menuItem1;
#endregion
#region public Form1(string open)
public Form1(string open) {
InitializeComponent();
InitializeBoardArray();
InitializeGrid();
Bitmap bmp = new Bitmap(ClientSize.Width, ClientSize.Height);
Graphics g = Graphics.FromImage(bmp);
//g.TranslateTransform(-(statusBar1.Height + 1), -(statusBar1.Height + 1));
// white background
DrawSudokuBackground(g);
BackgroundImage = bmp;
_sudoku.CreateSubKey(@"Software\SudoKu");
_sudoku = _sudoku.OpenSubKey(@"Software\SudoKu", true);
if (_sudoku.GetValue("Save", "").ToString() != "" || open != "") {
statusBar1.Text = "Open...";
statusBar1.Update();
if (open == "") {
openFileDialog1.FileName = _sudoku.GetValue("Save", "").ToString();
} else {
openFileDialog1.FileName = open;
}
_filename = openFileDialog1.FileName.Remove(openFileDialog1.FileName.LastIndexOf("."), openFileDialog1.FileName.Length - openFileDialog1.FileName.LastIndexOf("."));
_filename = _filename.Remove(0, _filename.LastIndexOf("\\") + 1);
mnuTemplateMode.Checked = false;
_grid = SudokuReader.Reader.Read(openFileDialog1.FileName);
_checkBoxes = 0;
if (_grid.ACH()) {
mnuAutoCalcHints.Checked = true;
_checkBoxes += 1;
} else
mnuAutoCalcHints.Checked = false;
if (_grid.EH()) {
mnuEditHints.Checked = true;
_checkBoxes += 2;
} else
mnuEditHints.Checked = false;
if (_grid.T()) {
mnuTimer.Checked = true;
_checkBoxes += 4;
} else
mnuTimer.Checked = false;
if (_grid.TM()) {
mnuTemplateMode.Checked = true;
_checkBoxes += 8;
} else
mnuTemplateMode.Checked = false;
if (_grid.E()) {
mnuEasy.Checked = true;
_checkBoxes += 16;
} else
mnuEasy.Checked = false;
if (_grid.M()) {
mnuMedium.Checked = true;
_checkBoxes += 32;
} else
mnuMedium.Checked = false;
if (_grid.H()) {
mnuHard.Checked = true;
_checkBoxes += 64;
} else
mnuHard.Checked = false;
if (mnuAutoCalcHints.Checked)
_grid.CalculateHints();
_Tsec = 0;
_Tmin = 0;
timer.Text = "0:00";
statusBar1.Text = "Ready...";
statusBar1.Update();
}
Invalidate();
}
#endregion
#region void InitializeGrid()
/// <summary>
/// Declares "_grid" a SudokuGrid();
/// </summary>
void InitializeGrid() {
_grid = new SudokuGrid();
}
#endregion
#region Global Variables
string _filename = "MyPuzzle";
int _checkBoxes = 36;
bool _highlightWrong = false;
bool _cellLegal = true;
bool _timing = false;
int _Tmin = 0;
int _Tsec = 0;
ManualResetEvent _blocker = new ManualResetEvent(true);
Pen _penThick = new Pen(Color.Black, 3);
Font _sudukoFont = new Font("Arial", 16, FontStyle.Regular);
Rectangle _board = new Rectangle(0, 0, 0, 0);
Rectangle[,] _boardArray = new Rectangle[9, 9];
Font _hintsFont = new Font("Small Fonts", 6);
SudokuGrid _grid;
int _selectedRow = -1;
int _selectedCol = -1;
Rectangle _selectedRect;
RegistryKey _sudoku = Registry.CurrentUser;
#endregion
#region protected override void Dispose( bool disposing )
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing) {
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion
#region private void InitializeComponent()
/// <summary>
/// Required method for Designer support
/// </summary>
private void InitializeComponent() {
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.printDocument1 = new System.Drawing.Printing.PrintDocument();
this.printPreviewDialog1 = new System.Windows.Forms.PrintPreviewDialog();
this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
this.mnuFile = new System.Windows.Forms.MenuItem();
this.mnuNew = new System.Windows.Forms.MenuItem();
this.mnuOpen = new System.Windows.Forms.MenuItem();
this.mnuSave = new System.Windows.Forms.MenuItem();
this.mnuSaveTemplate = new System.Windows.Forms.MenuItem();
this.mnuSaveImage = new System.Windows.Forms.MenuItem();
this.mnuPrint = new System.Windows.Forms.MenuItem();
this.mnuPrintPreview = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.mnuExit = new System.Windows.Forms.MenuItem();
this.mnuUtilities = new System.Windows.Forms.MenuItem();
this.mnuGenerate = new System.Windows.Forms.MenuItem();
this.mnuSolve = new System.Windows.Forms.MenuItem();
this.mnuTemplateMode = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.mnuMakePermanent = new System.Windows.Forms.MenuItem();
this.mnuCheckSolution = new System.Windows.Forms.MenuItem();
this.mnuRestart = new System.Windows.Forms.MenuItem();
this.mnuClearWrong = new System.Windows.Forms.MenuItem();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.mnuStartTimer = new System.Windows.Forms.MenuItem();
this.mnuStopTimer = new System.Windows.Forms.MenuItem();
this.mnuResetTimer = new System.Windows.Forms.MenuItem();
this.mnuTimer = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.mnuClearHints = new System.Windows.Forms.MenuItem();
this.mnuShowHints = new System.Windows.Forms.MenuItem();
this.mnuEditHints = new System.Windows.Forms.MenuItem();
this.mnuAutoCalcHints = new System.Windows.Forms.MenuItem();
this.mnuDifficult = new System.Windows.Forms.MenuItem();
this.mnuEasy = new System.Windows.Forms.MenuItem();
this.mnuMedium = new System.Windows.Forms.MenuItem();
this.mnuHard = new System.Windows.Forms.MenuItem();
this.mnuHelp = new System.Windows.Forms.MenuItem();
this.mnuHow = new System.Windows.Forms.MenuItem();
this.mnuAbout = new System.Windows.Forms.MenuItem();
this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.statusBar1 = new System.Windows.Forms.Label();
this.timer1 = new System.Timers.Timer();
this.timer = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
this.SuspendLayout();
//
// printDocument1
//
this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
//
// printPreviewDialog1
//
this.printPreviewDialog1.AutoScrollMargin = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.AutoScrollMinSize = new System.Drawing.Size(0, 0);
this.printPreviewDialog1.ClientSize = new System.Drawing.Size(400, 300);
this.printPreviewDialog1.Document = this.printDocument1;
this.printPreviewDialog1.Enabled = true;
this.printPreviewDialog1.Icon = ((System.Drawing.Icon)(resources.GetObject("printPreviewDialog1.Icon")));
this.printPreviewDialog1.Name = "printPreviewDialog1";
this.printPreviewDialog1.Visible = false;
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuFile,
this.mnuUtilities,
this.mnuDifficult,
this.mnuHelp});
//
// mnuFile
//
this.mnuFile.Index = 0;
this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuNew,
this.mnuOpen,
this.mnuSave,
this.mnuSaveTemplate,
this.mnuSaveImage,
this.mnuPrint,
this.mnuPrintPreview,
this.menuItem7,
this.mnuExit});
this.mnuFile.Text = "File";
//
// mnuNew
//
this.mnuNew.Index = 0;
this.mnuNew.Shortcut = System.Windows.Forms.Shortcut.CtrlN;
this.mnuNew.Text = "&New";
this.mnuNew.Click += new System.EventHandler(this.mnuNew_Click);
//
// mnuOpen
//
this.mnuOpen.Index = 1;
this.mnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
this.mnuOpen.Text = "&Open";
this.mnuOpen.Click += new System.EventHandler(this.mnuOpen_Click);
//
// mnuSave
//
this.mnuSave.Index = 2;
this.mnuSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
this.mnuSave.Text = "&Save as";
this.mnuSave.Click += new System.EventHandler(this.mnuSaveAs_Click);
//
// mnuSaveTemplate
//
this.mnuSaveTemplate.Index = 3;
this.mnuSaveTemplate.Shortcut = System.Windows.Forms.Shortcut.CtrlT;
this.mnuSaveTemplate.Text = "Save as &Template";
this.mnuSaveTemplate.Click += new System.EventHandler(this.mnuSaveTemplate_Click);
//
// mnuSaveImage
//
this.mnuSaveImage.Index = 4;
this.mnuSaveImage.Shortcut = System.Windows.Forms.Shortcut.CtrlI;
this.mnuSaveImage.Text = "Save as &Image";
this.mnuSaveImage.Click += new System.EventHandler(this.mnuSaveImage_Click);
//
// mnuPrint
//
this.mnuPrint.Index = 5;
this.mnuPrint.Shortcut = System.Windows.Forms.Shortcut.CtrlP;
this.mnuPrint.Text = "&Print";
this.mnuPrint.Click += new System.EventHandler(this.mnuPrint_Click);
//
// mnuPrintPreview
//
this.mnuPrintPreview.Index = 6;
this.mnuPrintPreview.Shortcut = System.Windows.Forms.Shortcut.CtrlR;
this.mnuPrintPreview.Text = "P&rint Preview";
this.mnuPrintPreview.Click += new System.EventHandler(this.mnuPrintPreview_Click);
//
// menuItem7
//
this.menuItem7.Index = 7;
this.menuItem7.Text = "-";
//
// mnuExit
//
this.mnuExit.Index = 8;
this.mnuExit.Shortcut = System.Windows.Forms.Shortcut.CtrlX;
this.mnuExit.Text = "E&xit";
this.mnuExit.Click += new System.EventHandler(this.mnuExit_Click);
//
// mnuUtilities
//
this.mnuUtilities.Index = 1;
this.mnuUtilities.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuGenerate,
this.mnuSolve,
this.mnuTemplateMode,
this.menuItem3,
this.mnuMakePermanent,
this.mnuCheckSolution,
this.mnuRestart,
this.mnuClearWrong,
this.menuItem1,
this.mnuStartTimer,
this.mnuStopTimer,
this.mnuResetTimer,
this.mnuTimer,
this.menuItem5,
this.mnuClearHints,
this.mnuShowHints,
this.mnuEditHints,
this.mnuAutoCalcHints});
this.mnuUtilities.Text = "Utilities";
//
// mnuGenerate
//
this.mnuGenerate.Index = 0;
this.mnuGenerate.Shortcut = System.Windows.Forms.Shortcut.CtrlG;
this.mnuGenerate.Text = "&Generate";
this.mnuGenerate.Click += new System.EventHandler(this.mnuGenerate_Click);
//
// mnuSolve
//
this.mnuSolve.Index = 1;
this.mnuSolve.Shortcut = System.Windows.Forms.Shortcut.CtrlV;
this.mnuSolve.Text = "Sol&ve";
this.mnuSolve.Click += new System.EventHandler(this.mnuSolve_Click);
//
// mnuTemplateMode
//
this.mnuTemplateMode.Index = 2;
this.mnuTemplateMode.Shortcut = System.Windows.Forms.Shortcut.CtrlM;
this.mnuTemplateMode.Text = "Template &Mode";
this.mnuTemplateMode.Click += new System.EventHandler(this.mnuTemplateMode_Click);
//
// menuItem3
//
this.menuItem3.Index = 3;
this.menuItem3.Text = "-";
//
// mnuMakePermanent
//
this.mnuMakePermanent.Index = 4;
this.mnuMakePermanent.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftP;
this.mnuMakePermanent.Text = "Make Guesses &Permanent";
this.mnuMakePermanent.Click += new System.EventHandler(this.mnuMakePermanent_Click);
//
// mnuCheckSolution
//
this.mnuCheckSolution.Index = 5;
this.mnuCheckSolution.Shortcut = System.Windows.Forms.Shortcut.CtrlC;
this.mnuCheckSolution.Text = "&Check Guesses";
this.mnuCheckSolution.Click += new System.EventHandler(this.mnuCheckGuesses_Click);
//
// mnuRestart
//
this.mnuRestart.Index = 6;
this.mnuRestart.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
this.mnuRestart.Text = "Clear &All Guesses";
this.mnuRestart.Click += new System.EventHandler(this.mnuClearGuesses_Click);
//
// mnuClearWrong
//
this.mnuClearWrong.Index = 7;
this.mnuClearWrong.Shortcut = System.Windows.Forms.Shortcut.CtrlW;
this.mnuClearWrong.Text = "Clear &Wrong Guesses";
this.mnuClearWrong.Click += new System.EventHandler(this.mnuClearWrong_Click);
//
// menuItem1
//
this.menuItem1.Index = 8;
this.menuItem1.Text = "-";
//
// mnuStartTimer
//
this.mnuStartTimer.Index = 9;
this.mnuStartTimer.Text = "Start Timer";
this.mnuStartTimer.Click += new System.EventHandler(this.mnuStartTimer_Click);
//
// mnuStopTimer
//
this.mnuStopTimer.Index = 10;
this.mnuStopTimer.Text = "Stop Timer";
this.mnuStopTimer.Click += new System.EventHandler(this.mnuStopTimer_Click);
//
// mnuResetTimer
//
this.mnuResetTimer.Index = 11;
this.mnuResetTimer.Text = "Reset Timer";
this.mnuResetTimer.Click += new System.EventHandler(this.mnuResetTimer_Click);
//
// mnuTimer
//
this.mnuTimer.Checked = true;
this.mnuTimer.Index = 12;
this.mnuTimer.Shortcut = System.Windows.Forms.Shortcut.CtrlShiftT;
this.mnuTimer.Text = "&Timer";
this.mnuTimer.Click += new System.EventHandler(this.mnuTimer_Click);
//
// menuItem5
//
this.menuItem5.Index = 13;
this.menuItem5.Text = "-";
//
// mnuClearHints
//
this.mnuClearHints.Index = 14;
this.mnuClearHints.Text = "Clear Hints";
this.mnuClearHints.Click += new System.EventHandler(this.mnuClearHints_Click);
//
// mnuShowHints
//
this.mnuShowHints.Index = 15;
this.mnuShowHints.Shortcut = System.Windows.Forms.Shortcut.CtrlH;
this.mnuShowHints.Text = "Show All &Hints";
this.mnuShowHints.Click += new System.EventHandler(this.mnuShowHints_Click);
//
// mnuEditHints
//
this.mnuEditHints.Index = 16;
this.mnuEditHints.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
this.mnuEditHints.Text = "&Edit Hints";
this.mnuEditHints.Click += new System.EventHandler(this.mnuEditHints_Click);
//
// mnuAutoCalcHints
//
this.mnuAutoCalcHints.Index = 17;
this.mnuAutoCalcHints.Text = "Auto Calc Hints";
this.mnuAutoCalcHints.Click += new System.EventHandler(this.mnuAutoCalcHints_Click);
//
// mnuDifficult
//
this.mnuDifficult.Index = 2;
this.mnuDifficult.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuEasy,
this.mnuMedium,
this.mnuHard});
this.mnuDifficult.Text = "Difficulty";
//
// mnuEasy
//
this.mnuEasy.Index = 0;
this.mnuEasy.Shortcut = System.Windows.Forms.Shortcut.F5;
this.mnuEasy.Text = "Easy";
this.mnuEasy.Click += new System.EventHandler(this.mnuEasy_Click);
//
// mnuMedium
//
this.mnuMedium.Checked = true;
this.mnuMedium.Index = 1;
this.mnuMedium.Shortcut = System.Windows.Forms.Shortcut.F6;
this.mnuMedium.Text = "Medium";
this.mnuMedium.Click += new System.EventHandler(this.mnuMedium_Click);
//
// mnuHard
//
this.mnuHard.Index = 2;
this.mnuHard.Shortcut = System.Windows.Forms.Shortcut.F7;
this.mnuHard.Text = "Hard";
this.mnuHard.Click += new System.EventHandler(this.mnuHard_Click);
//
// mnuHelp
//
this.mnuHelp.Index = 3;
this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.mnuHow,
this.mnuAbout});
this.mnuHelp.Text = "Help";
//
// mnuHow
//
this.mnuHow.Index = 0;
this.mnuHow.Shortcut = System.Windows.Forms.Shortcut.F1;
this.mnuHow.Text = "How To Play";
this.mnuHow.Click += new System.EventHandler(this.mnuHow_Click);
//
// mnuAbout
//
this.mnuAbout.Index = 1;
this.mnuAbout.Shortcut = System.Windows.Forms.Shortcut.F2;
this.mnuAbout.Text = "About";
this.mnuAbout.Click += new System.EventHandler(this.mnuAbout_Click);
//
// saveFileDialog1
//
this.saveFileDialog1.DefaultExt = "jpg";
this.saveFileDialog1.Filter = "JPeg Files | *.jpg ||";
//
// statusBar1
//
this.statusBar1.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.statusBar1.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.statusBar1.Location = new System.Drawing.Point(8, 304);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(312, 22);
this.statusBar1.TabIndex = 0;
this.statusBar1.Text = "Ready...";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.SynchronizingObject = this;
this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
//
// timer
//
this.timer.Font = new System.Drawing.Font("Verdana", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.timer.ForeColor = System.Drawing.Color.White;
this.timer.Location = new System.Drawing.Point(256, 304);
this.timer.Name = "timer";
this.timer.Size = new System.Drawing.Size(56, 16);
this.timer.TabIndex = 1;
this.timer.Text = "0:00";
this.timer.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(318, 327);
this.Controls.Add(this.timer);
this.Controls.Add(this.statusBar1);
this.DoubleBuffered = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.KeyPreview = true;
this.MaximizeBox = false;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Sudoku Puzzle";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form1_KeyPress);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
this.ResumeLayout(false);
}
#endregion
#region static void Main(string[] args)
[STAThread]
static void Main(string[] args) {
string openFile;
if (args.Length > 0) {
openFile = args[0];
} else {
openFile = "";
}
try {
Application.Run(new Form1(openFile));
} catch (Exception ex) {
System.Windows.Forms.MessageBox.Show("Error -->\n" + ex.ToString());
}
}
#endregion
#region void InitializeBoardArray()
void InitializeBoardArray() {
_board = ClientRectangle;
_board.Inflate(-statusBar1.Height - 2, -statusBar1.Height - 2);
int spacingX = _board.Width / 9;
int spacingY = _board.Height / 9;
for (int col = 0; col < 9; col++) {
for (int row = 0; row < 9; row++) {
_boardArray[row, col] = new Rectangle(_board.Left + col * spacingX, _board.Top + row * spacingY, spacingX, spacingY);
}
}
}
#endregion
#region void DrawSudokuBackground(Graphics g)
void DrawSudokuBackground(Graphics g) {
_board = ClientRectangle;
_board.Inflate(-statusBar1.Height - 2, -statusBar1.Height - 2);
g.DrawRectangle(_penThick, _board);
int spacingX = _board.Width / 9;
int spacingY = _board.Height / 9;
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
if (r < 3) {
if (c < 3)
g.FillRectangle(Brushes.DeepSkyBlue, _boardArray[r, c]);
else if (c < 6)
g.FillRectangle(Brushes.LightGreen, _boardArray[r, c]);
else
g.FillRectangle(Brushes.DeepSkyBlue, _boardArray[r, c]);
} else if (r < 6) {
if (c < 3)
g.FillRectangle(Brushes.LightGreen, _boardArray[r, c]);
else if (c < 6)
g.FillRectangle(Brushes.DeepSkyBlue, _boardArray[r, c]);
else
g.FillRectangle(Brushes.LightGreen, _boardArray[r, c]);
} else {
if (c < 3)
g.FillRectangle(Brushes.DeepSkyBlue, _boardArray[r, c]);
else if (c < 6)
g.FillRectangle(Brushes.LightGreen, _boardArray[r, c]);
else
g.FillRectangle(Brushes.DeepSkyBlue, _boardArray[r, c]);
}
}
}
}
#endregion
#region void DrawSudokuGrid(Graphics g)
/// <summary>
/// Draw background colors and _grid numbers.
/// </summary>
void DrawSudokuGrid(Graphics g) {
int spacingX = _board.Width / 9;
int spacingY = _board.Height / 9;
if (_highlightWrong) {
for (int r = 0; r < 9; r++) {
for (int c = 0; c < 9; c++) {
if (!_grid.CheckCell(r, c))
if (_grid.IsKnownElement(r, c) == mnuTemplateMode.Checked)
g.FillRectangle(Brushes.Yellow, _boardArray[r, c]);
}
}
} else if (_selectedRow >= 0 && _selectedCol >= 0) {
if (_cellLegal) {
g.FillRectangle(Brushes.White, _boardArray[_selectedRow, _selectedCol]);
} else {
g.FillRectangle(Brushes.Yellow, _boardArray[_selectedRow, _selectedCol]);
}
}
for (int i = 0; i < 10; i++) {
if (i % 3 == 0) {
g.DrawLine(_penThick, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY * i);
g.DrawLine(_penThick, _board.Left + spacingX * i, _board.Top, _board.Left + spacingX * i, _board.Bottom);
} else {
g.DrawLine(Pens.Black, _board.Left, _board.Top + spacingY * i, _board.Right, _board.Top + spacingY * i);
g.DrawLine(Pens.Black, _board.Left + spacingX * i, _board.Top, _board.Left + spacingX * i, _board.Bottom);
}
}
if (_grid.CheckEmpty())
return;
for (int col = 0; col < 9; col++) {
for (int row = 0; row < 9; row++) {
int val = _grid[row, col];
if (val != 0) {
if (_grid.IsKnownElement(row, col) || mnuTemplateMode.Checked) {
g.DrawString(val.ToString(), _sudukoFont, Brushes.Black, _board.Left + col * spacingX + 5, _board.Top + row * spacingY + 5, new StringFormat());
} else {
g.DrawString(val.ToString(), _sudukoFont, Brushes.Red, _board.Left + col * spacingX + 5, _board.Top + row * spacingY + 5, new StringFormat());
}
} else {
DrawGridHints(g, row, col);
}
}
}
}
#endregion
#region void DrawGridHints(Graphics g, int row, int col)
/// <summary>
/// Draw _grid hints.
/// </summary>
void DrawGridHints(Graphics g, int row, int col) {
int spacingX = _board.Width / 9;
int spacingY = _board.Height / 9;
int length;
string hints = "";
string hintss = "";
string hintsss = "";
for (int i = 0; i < 9; i++) {
if (_grid.GridHints[row, col, i] != 0)
hints = hints + _grid.GridHints[row, col, i].ToString() + ",";
}
if (hints.Length > 0) {
hints = hints.Remove(hints.Length - 1, 1);
System.Drawing.Brush brush = Brushes.Maroon;
if (mnuEasy.Checked)//
{
int tempcol;
int temprow;
int tempcell;
int crow = row / 3 * 3;
int ccol = col / 3 * 3;
for (int i = 0; i < 9; i++) {
if (brush != Brushes.Blue && _grid.GridHints[row, col, i] != 0) {
temprow = 0;
tempcol = 0;
tempcell = 0;
for (int j = 0; j < 9; j++) {
if (_grid.GridHints[row, j, i] != 0)
tempcol++;
if (_grid.GridHints[j, col, i] != 0)
temprow++;
}
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (_grid.GridHints[crow + k, ccol + l, i] != 0)
tempcell++;
}
}
if (tempcol == 1 || temprow == 1 || tempcell == 1 || hints.Length == 1) {
hints = (i + 1).ToString();
brush = Brushes.Blue;
}
}
}
}//
if (hints.Length == 1) {
brush = Brushes.Blue;
} else if (hints.Length > 8) {
length = hints.Length - 8;
hintss = hints.Remove(0, 8);
hints = hints.Remove(8, length);
if (hintss.Length > 8) {
hintsss = hintss.Remove(0, 8);
hintss = hintss.Remove(8, 1);
g.DrawString(hintsss.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 2, _board.Top + row * spacingY + 20, new StringFormat());
}
g.DrawString(hintss.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 2, _board.Top + row * spacingY + 11, new StringFormat());
}
g.DrawString(hints.ToString(), _hintsFont, brush, _board.Left + col * spacingX + 2, _board.Top + row * spacingY + 2, new StringFormat());
}
}
#endregion
#region private Rectangle TranslateToRectBounds(Point p, out int selectedRow, out int selectedCol)
private Rectangle TranslateToRectBounds(Point p, out int selectedRow, out int selectedCol) {
int spacingX = _board.Width / 9;
int spacingY = _board.Height / 9;
for (int col = 0; col < 9; col++) {
for (int row = 0; row < 9; row++) {
if (_boardArray[row, col].Contains(p)) {
selectedRow = row;
selectedCol = col;
return _boardArray[row, col];
}
}
}
selectedRow = -1;
selectedCol = -1;
return new Rectangle(0, 0, 0, 0);
}
#endregion
#region private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
DrawSudokuGrid(e.Graphics);
}
#endregion
#region private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
DrawSudokuGrid(e.Graphics);
}
#endregion
#region private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) {
if (_highlightWrong) {
_highlightWrong = false;
Invalidate();
} else {
Invalidate(_selectedRect);
}
_selectedRect = TranslateToRectBounds(new Point(e.X, e.Y), out _selectedRow, out _selectedCol);
if (_selectedRow != -1) {
_cellLegal = _grid.CheckCell(_selectedRow, _selectedCol);
if (_selectedRow >= 0) {
Invalidate(_selectedRect);
}
}
}
#endregion
#region private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
if (_selectedRow >= 0 && _selectedCol >= 0) {
if (_highlightWrong) {
_highlightWrong = false;
Invalidate();
} else {
Invalidate(_selectedRect);
}
//System.Windows.Forms.MessageBox.Show(e.KeyValue.ToString());
if (e.KeyValue == 39) {
_selectedCol++;
if (_selectedCol == 9)
_selectedCol = 0;
} else if (e.KeyValue == 37) {
_selectedCol--;
if (_selectedCol == -1)
_selectedCol = 8;
} else if (e.KeyValue == 40) {
_selectedRow++;
if (_selectedRow == 9)
_selectedRow = 0;
} else if (e.KeyValue == 38) {
_selectedRow--;
if (_selectedRow == -1)
_selectedRow = 8;
} else if (e.KeyValue == 46) {
_grid[_selectedRow, _selectedCol] = 0;
}
_cellLegal = _grid.CheckCell(_selectedRow, _selectedCol);
_selectedRect = _boardArray[_selectedRow, _selectedCol];
Invalidate(_selectedRect);
}
}
#endregion
#region private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
private void Form1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) {
if (_highlightWrong) {
_highlightWrong = false;
Invalidate();
}
if (_grid == null)
return;
else if ((e.KeyChar >= '1' && e.KeyChar <= '9') || e.KeyChar == ' ') {
if (_selectedRow >= 0 && _selectedCol >= 0) {
if (mnuEditHints.Checked) {
if (_grid[_selectedRow, _selectedCol] == 0) {
if (e.KeyChar == ' ') {
_grid.ClearRCHints(_selectedRow, _selectedCol);
} else {
_grid.ChangeHints(_selectedRow, _selectedCol, Convert.ToInt32(e.KeyChar.ToString()));
}
if (mnuAutoCalcHints.Checked) {
Invalidate();
} else {
Invalidate(_selectedRect);
}
}
return;
}
if (e.KeyChar == ' ') {
if (!_grid.IsKnownElement(_selectedRow, _selectedCol) || mnuTemplateMode.Checked)
_grid[_selectedRow, _selectedCol] = 0;
} else {
if (!_grid.IsKnownElement(_selectedRow, _selectedCol) || mnuTemplateMode.Checked)
_grid[_selectedRow, _selectedCol] = Convert.ToInt32(e.KeyChar.ToString());
}
if (mnuTemplateMode.Checked) {
_grid.SetKnownElement(_selectedRow, _selectedCol, _grid[_selectedRow, _selectedCol]);
}
if (mnuAutoCalcHints.Checked) {
_grid.CalculateHints();
}
_cellLegal = _grid.CheckCell(_selectedRow, _selectedCol);
if (mnuAutoCalcHints.Checked) {
Invalidate();
} else {
Invalidate(_selectedRect);
}
if (_grid.GridCompleted())
if (_grid.CheckAll()) {
if (_timing) {
_timing = false;
MessageBox.Show("You solved the sudoku puzzle in " + (_Tmin != 0 ? _Tmin.ToString() + " minute" + (_Tmin == 1 ? "" : "s") : "") + (_Tmin != 0 && _Tsec != 0 ? " and " : "") + (_Tsec != 0 ? _Tsec.ToString() + " second" + (_Tsec == 1 ? "" : "s") : "") + "!", "Congratulations!!!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
} else
MessageBox.Show("You Solved the Sudoku Puzzle!", "Congratulations!!!", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Asterisk);
}
}
}
}
#endregion
#region private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
if (!_grid.CheckEmpty() && MessageBox.Show("Do you want to save this game?", "Save?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) {
statusBar1.Text = "Save as...";
statusBar1.Update();
saveFileDialog1.FileName = _filename;
saveFileDialog1.DefaultExt = "sav";
saveFileDialog1.Filter = "Sudoku Puzzle|*.sav";
saveFileDialog1.AddExtension = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK) {
_sudoku.SetValue("Save", saveFileDialog1.FileName);
SudokuReader.Reader.Save(saveFileDialog1.FileName, _grid, _checkBoxes);
} else {
_sudoku.SetValue("Save", "");
}
statusBar1.Text = "Ready...";
statusBar1.Update();
} else {
_sudoku.SetValue("Save", "");
}
_sudoku.Close();
}
#endregion
#region private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
if (timer.Visible && _timing) {
_Tsec++;
if (_Tsec == 60) {
_Tmin++;
_Tsec = 0;
}
timer.Text = _Tmin + ":" + (_Tsec < 10 ? "0" : "") + _Tsec;
}
}
#endregion
#region private void mnuNew_Click(object sender, System.EventArgs e)
private void mnuNew_Click(object sender, System.EventArgs e) {
if (!_grid.CheckEmpty()) {
_grid.Clear();
_grid.ClearHints();
Invalidate();
}
_Tsec = 0;
_Tmin = 0;
timer.Text = "0:00";
_timing = false;
}
#endregion
#region private void mnuGenerate_Click(object sender, System.EventArgs e)
private void mnuGenerate_Click(object sender, System.EventArgs e) {
Invalidate();
statusBar1.Text = "Generating...";
statusBar1.Update();
mnuTemplateMode.Checked = false;
_grid.Generate();
_grid.ClearHints();
Invalidate();
statusBar1.Text = "Ready...";
statusBar1.Update();
if (mnuTimer.Checked) {
_Tsec = 0;
_Tmin = 0;
timer.Text = "0:00";
_timing = true;
}
}
#endregion
#region private void mnuOpen_Click(object sender, System.EventArgs e)
private void mnuOpen_Click(object sender, System.EventArgs e) {
statusBar1.Text = "Open...";
statusBar1.Update();
openFileDialog1.FileName = _filename;
openFileDialog1.DefaultExt = "sav";
openFileDialog1.Filter = "Sudoku Puzzle|*.sav|Sudoku Template|*.xml|All Files|*";
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
_filename = openFileDialog1.FileName.Remove(openFileDialog1.FileName.LastIndexOf("."), openFileDialog1.FileName.Length - openFileDialog1.FileName.LastIndexOf("."));
_filename = _filename.Remove(0, _filename.LastIndexOf("\\") + 1);
mnuTemplateMode.Checked = false;
_grid = SudokuReader.Reader.Read(openFileDialog1.FileName);
_checkBoxes = 0;
if (_grid.ACH()) {
mnuAutoCalcHints.Checked = true;
_checkBoxes += 1;
} else
mnuAutoCalcHints.Checked = false;
if (_grid.EH()) {
mnuEditHints.Checked = true;
_checkBoxes += 2;
} else
mnuEditHints.Checked = false;
if (_grid.T()) {
mnuTimer.Checked = true;
_checkBoxes += 4;