-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
493 lines (454 loc) · 16.7 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
using System;
using System.Linq;
using System.Timers;
using System.Drawing;
using System.Windows.Forms;
namespace Skyscrapers
{
/// <summary>
/// The form1 class.
/// </summary>
public partial class Form1 : Form
{
// Constants Labels text
private const string TitleLabelText = "SKYSCRAPERS";
private const string AllRightsLabelText = "© 2021 All Rights Reserved.";
// Constants Buttons text
private const string RulesButtonText = "Rules";
private const string StartButtonText = "Start";
private const string NewGameButtonText = "New Game";
private const string ResetButtonText = "Reset";
private const string HintButtonText = "Hint";
private const string SolveButtonText = "Solve";
private const string ExitButtonText = "Exit";
// Constants messages
private const string RulesMessage =
"The rules are simple.\n" +
"The objective is to place skyscrapers in all cells on the grid according to the rules:\n" +
"The height of the skyscrapers is from 1 to the size of the grid i.e. 1 to 4 for a 4x4 puzzle.\n" +
"You cannot have two skyscrapers with the same height on the same row or column.\n" +
"The numbers on the sides of the grid indicate how many skyscrapers would you see if you look at the row placed in front of the number.\n" +
"Place numbers in each cell to indicate the height of the skyscrapers.\n" +
"Have Fun! :)";
private const string EndGameMessage =
"Congratulations! You finished the puzzle!";
private const string SolvedByComputerMessage =
"Puzzle was solved by the Computer!";
private const string ComputerCouldNotSolveMessage =
"There is not an available solution for this specific puzzle!";
// Constants colors
private Color TitleColor = Color.Fuchsia;
private Color TimerColor = Color.DarkMagenta;
private Color EdgesColor = Color.Orchid;
private Color ChosenCellColor = Color.Gray;
private Color ResetCellColor = Color.Pink;
/// <summary>
/// Gets or sets the board.
/// </summary>
private Board Board { get; set; }
private System.Timers.Timer GameClock;
private int m, s;
/// <summary>
/// Initializes a new instance of the <see cref="Form1"/> class.
/// </summary>
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Form1_S the load.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void Form1_Load(object sender, EventArgs e)
{
Board = new Board(Board.BoardSize);
GameClock = new System.Timers.Timer();
InitClock();
InitWindow();
}
/// <summary>
/// Inits the window.
/// </summary>
private void InitWindow()
{
InitLabels();
InitButtons();
ResetCellsTextAndColor();
ResetEdegesLabels();
ResetCellChoice();
}
/// <summary>
/// Inits the labels.
/// </summary>
private void InitLabels()
{
labelSkyScrapers.ForeColor = TitleColor;
labelSkyScrapers.Text = TitleLabelText;
allRightsLabel.Text = AllRightsLabelText;
}
/// <summary>
/// Inits the buttons.
/// </summary>
private void InitButtons()
{
buttonRules.Text = RulesButtonText;
buttonStart.Text = StartButtonText;
buttonNewGame.Text = NewGameButtonText;
buttonReset.Text = ResetButtonText;
buttonHint.Text = HintButtonText;
buttonSolve.Text = SolveButtonText;
buttonExit.Text = ExitButtonText;
}
/// <summary>
/// Inits the clock.
/// </summary>
private void InitClock()
{
/* This function resests the current Game clock and creates an interval call
* for the the UpdateTime function.
*/
displayClock.BackColor = TimerColor;
displayClock.Text = "00:00";
// Which function to remove from timer:
GameClock.Elapsed -= UpdateTime;
// Time between calls:
GameClock.Interval = 1000;
// Which function to add to timer:
GameClock.Elapsed += UpdateTime;
// Turn clock to 0 minutes and 0 seconds:
m = 0; s = 0;
// Stops Gameclock:
GameClock.Stop();
}
/// <summary>
/// button2_S the click.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(RulesMessage);
}
/// <summary>
/// label1_S the click.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void label1_Click(object sender, EventArgs e)
{
ResetCellChoice();
// Casting object to Label:
Label chosenCell = sender as Label;
// Change the color of the clicked cell:
chosenCell.BackColor = ChosenCellColor;
}
/// <summary>
/// Resets the cell choice.
/// </summary>
private void ResetCellChoice()
{
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell"))
{
cell.BackColor = ResetCellColor;
}
}
}
/// <summary>
/// Clicks the start button.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickStartButton(object sender, EventArgs e)
{
EnableCells(true);
ResetCellChoice();
// Enables all control buttons:
buttonStart.Enabled = false;
buttonNewGame.Enabled = true;
buttonReset.Enabled = true;
buttonHint.Enabled = true;
buttonSolve.Enabled = true;
// Enables clicks on board cells:
guiPanel.Enabled = true;
// Starts timer:
GameClock.Start();
// Updates the text in the hint button with the amount of hints left:
buttonHint.Text = Board.UpdateHintButtonText();
// Reveals the edges:
Setedges(sender, e);
}
/// <summary>
/// Updates the time.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void UpdateTime(object sender, ElapsedEventArgs e)
{
Invoke(new Action(() =>
{
s += 1;
if (s == 60)
{
s = 0;
m += 1;
}
if (m == 100)
{
m = 0;
}
// Updates the clock on the screen, using format 00:00 :
displayClock.Text = string.Format("{0}:{1}", m.ToString().PadLeft(2, '0'), s.ToString().PadLeft(2, '0'));
}));
}
/// <summary>
/// Set the edges.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void Setedges(object sender, EventArgs e)
{
foreach (var edge in guiPanel.Controls.OfType<Label>().ToList())
{
if (edge.Name.StartsWith("edge"))
{
// Use edge's name to find which edge to relate to:
int row = edge.Name[4] - 48;
int col = edge.Name[6] - 48;
edge.Text = Board.GetEdges()[row, col].ToString();
}
}
}
/// <summary>
/// Form1_S the key press.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{ // if key between 1-4 (in ASCII) pressed (in this case, 4x4 board)
if (e.KeyChar >= 49 && e.KeyChar <= 48 + Board.GetSolvingdBoard().GetLength(0))
{
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{ // Checks where is the chosen cell using its color, and update its text
if (cell.Name.StartsWith("cell") && cell.BackColor == ChosenCellColor)
{
cell.Text = e.KeyChar.ToString();
Board.UpdateSolvingBoard(cell.Name, cell.Text);
if (Board.CheckSolution())
{
GameFinishedActions();
}
}
}
}
else // Different keyboard button pressed - clear chosen cell.
{
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell") && cell.BackColor == ChosenCellColor)
{
cell.Text = "";
Board.UpdateSolvingBoard(cell.Name, cell.Text);
}
}
}
}
/// <summary>
/// Games the finished actions.
/// </summary>
private void GameFinishedActions()
{
ConfigEndGameButtons(); // Changing the buttons to disable to press (reset, hint, solve)
ResetCellChoice(); // Clears cells choice
GameClock.Stop(); // Stops highscore
MessageBox.Show(EndGameMessage);
}
/// <summary>
/// Configs the end game buttons.
/// </summary>
private void ConfigEndGameButtons()
{ // Changing the buttons to disable to press (reset, hint, solve)
guiPanel.Enabled = false;
buttonStart.Enabled = false;
buttonReset.Enabled = false;
buttonHint.Enabled = false;
buttonSolve.Enabled = false;
}
/// <summary>
/// Clickeds the exit.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickedExit(object sender, EventArgs e)
{
GameClock.Stop();
Close();
}
/// <summary>
/// Clickeds the hint.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickedHint(object sender, EventArgs e)
{
ResetCellChoice();
// get row,col of the hinted cell using Board.GetHint:
(int, int) cell_index = Board.GetHint();
// Creates string that represents the ending of the cell name e.g: "2x3":
string ending = cell_index.Item1.ToString() + "x" + cell_index.Item2.ToString();
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell") && cell.Name.EndsWith(ending))
{ // Using the hinted cell index (row,col) to update the text in the cell (
// (using the updated currently solving board):
cell.Text = Board.GetSolvingdBoard()[cell_index.Item1, cell_index.Item2].ToString();
cell.Enabled = false;
buttonHint.Text = Board.UpdateHintButtonText();
}
}
if (Board.GetCountHints() == 0)
{ // NO MORE HINTS AVAILABLE
buttonHint.Enabled = false;
}
if (Board.CheckSolution())
{ // HINT SOLVED THE PUZZLE!
GameFinishedActions();
}
}
/// <summary>
/// Clickeds the reset.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickedReset(object sender, EventArgs e)
{
ResetCellChoice();
Board.BackToResetBoard();
ResetCellsTextAndColor();
}
/// <summary>
/// Resets the cells text and color.
/// </summary>
private void ResetCellsTextAndColor()
{
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell"))
{ // Find the index of the cell using its name in the GUI:
int row = cell.Name[4] - 48;
int col = cell.Name[6] - 48;
cell.BackColor = ResetCellColor;
// Cell is not empty:
if (Board.GetResetBoard()[row, col] == 0)
{
cell.Text = "";
}
else
{
// Cell is taken by a hint:
cell.Text = Board.GetResetBoard()[row, col].ToString();
}
}
}
}
/// <summary>
/// Enables the cells.
/// </summary>
/// <param name="enable">If true, enable.</param>
private void EnableCells(bool enable)
{
// Enables clicks of the board cells
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell"))
{
cell.Enabled = enable;
}
}
}
/// <summary>
/// Resets the edeges labels.
/// </summary>
private void ResetEdegesLabels()
{
foreach (var edge in guiPanel.Controls.OfType<Label>().ToList())
{
if (edge.Name.StartsWith("edge"))
{
edge.BackColor = EdgesColor;
edge.Text = "";
}
}
}
/// <summary>
/// Clickeds the new game.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickedNewGame(object sender, EventArgs e)
{
Board = new Board(4);
ClickedReset(sender, e);
ResetEdegesLabels();
EnableCells(false);
ConfigNewGameButtons();
InitClock();
}
/// <summary>
/// Clickeds the solve.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The e.</param>
private void ClickedSolve(object sender, EventArgs e)
{
ClickedReset(sender,e);
if (Board.SolveSkyScrapers(0))
{
for (int i = 0; i < Board.GetSolvingdBoard().GetLength(0); i++)
{
for (int j = 0; j < Board.GetSolvingdBoard().GetLength(1); j++)
{
string ending = i + "x" + j;
foreach (var cell in guiPanel.Controls.OfType<Label>().ToList())
{
if (cell.Name.StartsWith("cell") && cell.Name.EndsWith(ending))
{
cell.Text = Board.GetSolveddBoard()[i, j].ToString();
System.Threading.Thread.Sleep(100);
guiPanel.Refresh();
}
}
}
}
SolvedActions();
}
else
MessageBox.Show(ComputerCouldNotSolveMessage);
}
/// <summary>
/// Solveds the actions.
/// </summary>
private void SolvedActions()
{
EnableCells(false);
ConfigEndGameButtons();
GameClock.Stop();
MessageBox.Show(SolvedByComputerMessage);
}
/// <summary>
/// Configs the new game buttons.
/// </summary>
private void ConfigNewGameButtons()
{
buttonHint.Text = HintButtonText;
buttonHint.Enabled = false;
buttonReset.Enabled = false;
buttonSolve.Enabled = false;
buttonStart.Enabled = true;
buttonExit.Enabled = true;
buttonRules.Enabled = true;
}
}
}