-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathForm1.cs
325 lines (224 loc) · 7.71 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
namespace Sudoku
{
public partial class Form1 : Form
{
Sudoku S;
private SoundPlayer SquarePlace;
private SoundPlayer PuzzleSolved;
private SoundPlayer PuzzleFine;
private SoundPlayer MoveNo;
private Communicator C;
public Form1()
{
InitializeComponent();
S = new Sudoku();
Application.DoEvents();
S.GenerateGame();
LoadSounds();
S.PlaySound += new Sudoku.SudokuEvent(S_PlaySound);
S.RequestRepaint += new Sudoku.SudokuEvent2(S_RequestRepaint);
C = new Communicator();
C.StatusChange += new Communicator.MethodDelegate(C_StatusChange);
}
void C_StatusChange()
{
this.Text = C.Status;
}
void S_RequestRepaint()
{
pictureBox1.Invalidate();
}
private void LoadSounds()
{
SquarePlace = new SoundPlayer("square.wav");
PuzzleSolved = new SoundPlayer("warm-interlude.wav");
PuzzleFine = new SoundPlayer("fine.wav");
MoveNo = new SoundPlayer("no.wav");
Application.DoEvents();
/*
SquarePlace.Load();
PuzzleSolved.Load();
PuzzleFine.Load();
MoveNo.Load();
* */
}
void S_PlaySound(Sudoku.SudokuSound S)
{
/*
switch (S)
{
case Sudoku.SudokuSound.Square:
// SquarePlace.Play();
break;
case Sudoku.SudokuSound.Solved:
PuzzleSolved.Play();
break;
case Sudoku.SudokuSound.No:
MoveNo.Play();
break;
case Sudoku.SudokuSound.Fine:
PuzzleFine.Play();
break;
case Sudoku.SudokuSound.Stop :
PuzzleSolved.Stop();
break;
}
*/
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
S.Draw(e.Graphics, 0);
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
S.SetLocation(e.Location);
pictureBox1.Invalidate();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
S.KeyPress((char)e.KeyValue);
if ((int)e.KeyValue == 27) S.Deselect();
if ((int)e.KeyValue == 46) S.DeleteCurrentSquare();
pictureBox1.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
pictureBox1.Invalidate();
}
private void pictureBox1_SizeChanged(object sender, EventArgs e)
{
pictureBox1.Invalidate();
}
private void newToolStripMenuItem1_Click(object sender, EventArgs e)
{
S.GenerateGame();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
S.RenderMessage("Sudoku 1.0", "Ghaith Tarawneh ([email protected])", false);
}
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
Clipboard.SetText(S.GetGameString());
}
private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
{
bool result = S.SetGameString(Clipboard.GetText());
if (!result)
{
Application.DoEvents();
MessageBox.Show("The clipboard does not contain a valid puzzle.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void solveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (S.checkSolvable(Sudoku.SolveMethods.All) == false)
{
Application.DoEvents();
MessageBox.Show("This puzzle cannot be solved!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
S.SolvePuzzle(Sudoku.SolveMethods.All);
pictureBox1.Invalidate();
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
S.SolveStep(Sudoku.SolveMethods.All);
pictureBox1.Invalidate();
}
private void checkSolvabilityToolStripMenuItem_Click(object sender, EventArgs e)
{
if (S.isSolved()) return;
int error_count = S.ComputeErrors();
if (error_count == 0)
{
S.RenderMessage("Everything's cool dude!", "Press any key to continue", false);
pictureBox1.Invalidate();
Application.DoEvents();
// PuzzleFine.Play();
}
else
{
Application.DoEvents();
S.ShowErrors = true;
pictureBox1.Invalidate();
}
}
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
openFileDialog1.FileName = "";
DialogResult R = openFileDialog1.ShowDialog();
if (R != DialogResult.OK) return;
bool result = S.LoadFile(openFileDialog1.FileName);
if (!result)
{
Application.DoEvents();
MessageBox.Show("Error loading puzzle file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void checkSolutionsCountToolStripMenuItem_Click(object sender, EventArgs e)
{
if (S.isSolved()) return;
Sudoku.SolutionStepList L = S.ComputePossibleSteps(Sudoku.SolveMethods.All);
S.RenderMessage(L.Count().ToString() + " possible deductions!", "Press any key to continue", false);
}
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
DialogResult R = saveFileDialog1.ShowDialog();
if (R != DialogResult.OK) return;
bool result = S.SaveFile(saveFileDialog1.FileName);
if (!result)
{
Application.DoEvents();
MessageBox.Show("Error saving puzzle file!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Leave(object sender, EventArgs e)
{
}
private void Form1_Deactivate(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void Form1_Activated(object sender, EventArgs e)
{
timer1.Enabled = false;
pictureBox1.Invalidate();
}
private void menuStrip1_MouseHover(object sender, EventArgs e)
{
}
private void menuStrip1_MouseEnter(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void menuStrip1_MouseLeave(object sender, EventArgs e)
{
timer1.Enabled = false;
pictureBox1.Invalidate();
}
}
}