/
Form1.cs
1331 lines (1243 loc) · 55.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
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.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Maze_SandBox
{
public partial class Form1 : Form
{
System.Drawing.Graphics g;
int length_x = 15;
int length_y = 15;
int size = 35;
cell[,] maze;
Point current_position;
Point exit_location;
int[] all_steps = { 0, 1, 2, 3, 0, 1, 2, 3 }; // cycled steps sequence
Point[] step_maker = {
new Point(0, -1), // coordinates diviations
new Point(-1, 0), // used to calculate coordinates of the next cell
new Point(0, 1), // depending on which way you go
new Point(1, 0) // 0 - up; 1 - left
}; // 2 - down; 3 - right
public Form1()
{
InitializeComponent();
}
static Color SetTransparency(int A, Color source)
{
return Color.FromArgb(A, source.R, source.G, source.B);
}
private void draw_cell(int i, int j)
{
if (j < 0)
j = length_y - 1;
if (i < 0)
i = length_x - 1;
g = this.CreateGraphics();
int x = size * i + i*2 + 2;
int y = size * j + j*2 + 2;
Pen pencil;
SolidBrush filler;
// --- background drawing
filler = new SolidBrush(Color.White);
g.FillRectangle(filler, x, y, size, size);
// --- Walls drawing
filler = new SolidBrush(Color.Black);
if (maze[i, j].get_side(0) == 1)
g.FillRectangle(filler, x - 2, y - 2, size + 3, 1);
if (maze[i, j].get_side(1) == 1)
g.FillRectangle(filler, x - 2, y - 2, 1, size + 3);
if (maze[i, j].get_side(2) == 1)
g.FillRectangle(filler, x - 2, y + size, size + 3, 1);
if (maze[i, j].get_side(3) == 1)
g.FillRectangle(filler, x + size, y - 2, 1, size + 3);
// --- specific marker drawing
int rank = maze[i, j].get_rank();
switch (rank)
{
case 0:
break;
case 1: // // Growing Tree/Walking Man in-stack cell marker
filler = new SolidBrush(SetTransparency(64, Color.Tomato));
g.FillRectangle(filler, x + size / 2 - size / 4, y + size / 2 - size / 4, size / 2, size / 2);
break;
case 2: // starting cell
pencil = new Pen(Color.Red);
g.DrawRectangle(pencil, x + size / 2 - size / 4, y + size / 2 - size / 4, size / 2, size / 2);
break;
case 3: // exit cell
pencil = new Pen(Color.Orange);
g.DrawRectangle(pencil, x + size / 2 - size / 4, y + size / 2 - size / 4, size / 2, size / 2);
break;
case 4: // wave covered cell
pencil = new Pen(Color.YellowGreen);
g.DrawRectangle(pencil, x + size / 2 - size / 10, y + size / 2 - size / 10, size / 5, size / 5);
break;
case 5: // winner wave cell
filler = new SolidBrush(SetTransparency(64, Color.Red));
g.FillRectangle(filler, x + size / 2 - size / 10, y + size / 2 - size / 10, size / 5, size / 5);
break;
case 6: // Hunt & Kill not-finished cell marker
pencil = new Pen(Color.SkyBlue);
g.DrawRectangle(pencil, x + size / 2 - size / 10, y + size / 2 - size / 10, size / 5, size / 5);
break;
case 7: // Tremo/Hunt & Kill current cell marker
filler = new SolidBrush(SetTransparency(64, Color.Green));
g.FillRectangle(filler, x + size / 2 - size / 4, y + size / 2 - size / 4, size / 2, size / 2);
break;
case 8: // Tremo solution levels
case 9:
case 10:
case 11:
filler = new SolidBrush(SetTransparency(255 - 80*(rank - 8), Color.SkyBlue));
g.FillRectangle(filler, x + size / 2 - size / 4, y + size / 2 - size / 4, size / 2, size / 2);
break;
case 12:// Tremo <Shortest path> Shortest path levels
case 13:
case 14:
case 15:
if (rank > 12)
rank = 13;
filler = new SolidBrush(SetTransparency(255 - 200 * (rank - 12), Color.Red));
g.FillRectangle(filler, x + size / 2 - size / 10, y + size / 2 - size / 10, size / 5, size / 5);
break;
default:
break;
}
}
private void draw_cell(Point position)
{
draw_cell(position.X, position.Y);
}
private void draw_line(int j)
{
g = this.CreateGraphics();
SolidBrush filler = new SolidBrush(this.BackColor);
// clear whole line
g.FillRectangle(filler, 0, (size + 2) * j + 2, length_x * (size + 2) + 2, size);
// redraw all cells one by one
for (int i = 0; i < length_x; i++)
draw_cell(i, j);
}
private void draw()
{
g = this.CreateGraphics();
SolidBrush filler = new SolidBrush(this.BackColor);
g.FillRectangle(filler, 0, 0, length_x * (size + 2) + 2, length_y * (size + 2) + 2);
for (int i = 0; i < length_x; i++)
for (int j = 0; j < length_y; j++)
draw_cell(i, j);
}
private void maze_update()
{
for (int j = 0; j < length_y; j++)
for (int i = 0; i < length_x; i++)
{
maze[i, j].set_rank(0);
if (i < length_x - 1)
if (maze[i + 1, j].get_side(1) == 1)
maze[i, j].set_side(3);
if (i > 0)
if (maze[i - 1, j].get_side(3) == 1)
maze[i, j].set_side(1);
if (j < length_y - 1)
if (maze[i, j + 1].get_side(0) == 1)
maze[i, j].set_side(2);
if (j > 0)
if (maze[i, j - 1].get_side(2) == 1)
maze[i, j].set_side(0);
}
maze[0, 0].set_rank(2);
maze[exit_location.X, exit_location.Y].set_rank(3);
draw();
}
private void maze_init()
{
if (g != null)
g.Clear(this.BackColor);
maze = new cell[length_x, length_y];
for (int i = 0; i < length_x; i++)
for (int j = 0; j < length_y; j++)
{
maze[i, j] = new cell();
if (j == 0)
maze[i, j].set_side(0);
if (i == 0)
maze[i, j].set_side(1);
if (j == length_y - 1)
maze[i, j].set_side(2);
if (i == length_x - 1)
maze[i, j].set_side(3);
}
draw();
}
private void parameters_init()
{
size = Convert.ToInt32(cellSizeBox.Text);
length_x = (this.Width - 220 - 10) / (size+2);
length_y = (this.Height - 60) / (size+2);
if ((exit_location.X == 0) && (exit_location.Y == 0))
{
exit_location.X = length_x - 1;
exit_location.Y = length_y - 1;
}
}
private void Form1_Load(object sender, EventArgs e)
{
solveBox.SelectedIndex = 0;
generateBox.SelectedIndex = 0;
//parameters_init();
textBox_TextChanged(sender, e);
reset_button_Click(sender, e);
}
private void Form1_Resize(object sender, EventArgs e)
{
controlPanel.Location = new Point(this.Width - 220, 5);
textBox_TextChanged(sender, e);
reset_button_Click(sender, e);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (selectCheckBox.Checked)
{
if ((exit_location.X >= length_x) || (exit_location.Y >= length_y))
exit_location.X = exit_location.Y = 0;
maze[exit_location.X, exit_location.Y].set_rank(0);
draw_cell(exit_location.X, exit_location.Y);
exit_location.X = e.X / (size + 2);
exit_location.Y = e.Y / (size + 2);
if ((exit_location.X < length_x) && (exit_location.Y < length_y))
{
maze[exit_location.X, exit_location.Y].set_rank(3);
draw_cell(exit_location.X, exit_location.Y);
statusLabel.Text = "New exit position: X = " + exit_location.X + ", Y = " + exit_location.Y;
}
else
{
exit_location.X = 0;
exit_location.Y = 0;
}
}
}
private void Form1_MouseEnter(object sender, EventArgs e)
{
if (selectCheckBox.Checked)
this.Cursor = Cursors.Cross;
}
private void Form1_MouseLeave(object sender, EventArgs e)
{
if (selectCheckBox.Checked)
this.Cursor = Cursors.Default;
}
private void reset_button_Click(object sender, EventArgs e)
{
if (g != null)
g.Clear(this.BackColor);
current_position = new Point();
parameters_init();
if (speedCheckBox.Checked)
maze_init();
statusLabel.Text = "Layout reseted.";
}
private void generate_button_Click(object sender, EventArgs e)
{
Random rnd = new Random();
current_position.X = rnd.Next(0, length_x); // random starting point
current_position.Y = rnd.Next(0, length_y); // for maze generation
parameters_init();
maze_init();
int delay_gen = Convert.ToInt32(delayBox2.Text);
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
switch (generateBox.SelectedItem.ToString())
{
case "Hunt and Kill algorithm":
Hunt_and_Kill(delay_gen);
break;
case "Walking Man algorithm":
Walking_Man(delay_gen);
break;
case "Growing Tree algorithm":
Growing_Tree(delay_gen);
break;
}
timer.Stop();
double disp = timer.ElapsedMilliseconds / 1000F;
if (!animationCheckBox.Checked)
draw();
statusLabel.Text = generateBox.SelectedItem.ToString();
if (optionsBox1.SelectedItem != null)
statusLabel.Text += " <" + optionsBox1.SelectedItem.ToString() + ">";
statusLabel.Text += ": time elapsed " + disp + " seconds.";
}
private void generateBox_SelectedIndexChanged(object sender, EventArgs e)
{
optionsBox1.Items.Clear();
switch (generateBox.SelectedItem.ToString())
{
case "Hunt and Kill algorithm":
break;
case "Walking Man algorithm":
optionsBox1.Items.Add("Newest");
optionsBox1.Items.Add("Random");
//optionsBox.Items.Add("Newest/Random, 50/50 split");
optionsBox1.SelectedIndex = 0;
break;
case "Growing Tree algorithm":
optionsBox1.Items.Add("Newest (Backtracker)");
optionsBox1.Items.Add("Random (Prim's)");
optionsBox1.Items.Add("One of the newest");
optionsBox1.Items.Add("Newest/Random, 75/25 split");
optionsBox1.Items.Add("Newest/Random, 50/50 split");
optionsBox1.Items.Add("Newest/Random, 25/75 split");
optionsBox1.Items.Add("Middle");
optionsBox1.Items.Add("Oldest");
optionsBox1.SelectedIndex = 0;
break;
}
}
private void solve_button_Click(object sender, EventArgs e)
{
if (!selectCheckBox.Checked)
{
exit_location.X = length_x - 1;
exit_location.Y = length_y - 1;
}
parameters_init();
maze_update();
maze[0, 0].set_rank(2);
maze[exit_location.X, exit_location.Y].set_rank(3);
current_position = new Point();
draw_cell(0, 0);
draw_cell(exit_location);
int delay_solve = Convert.ToInt32(delayBox3.Text);
System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
timer.Start();
switch (solveBox.SelectedItem.ToString())
{
case "Wave solution":
Solve_Wave(delay_solve);
break;
case "Tremo solution":
Solve_Tremo(delay_solve);
if (!animationCheckBox.Checked)
draw();
break;
case "Tremaux's solution":
Solve_Tremaux(delay_solve);
break;
}
timer.Stop();
double disp = timer.ElapsedMilliseconds / 1000F;
//MessageBox.Show("Time elapsed: " + disp + " seconds.");
statusLabel.Text = solveBox.SelectedItem.ToString();
if (optionsBox2.SelectedItem != null)
statusLabel.Text += " <" + optionsBox2.SelectedItem.ToString() + ">";
statusLabel.Text += ": time elapsed " + disp + " seconds.";
}
private void solveBox_SelectedIndexChanged(object sender, EventArgs e)
{
optionsBox2.Items.Clear();
switch (solveBox.SelectedItem.ToString())
{
case "Wave solution":
break;
case "Tremo solution":
optionsBox2.Items.Add("Shortest path");
optionsBox2.Items.Add("Classic");
optionsBox2.SelectedIndex = 0;
break;
case "Tremaux's solution":
break;
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
parameters_init();
mazeSizeBox.Text = length_x + "x" + length_y + " = " + length_x * length_y;
}
private void selectCheckBox_Click(object sender, EventArgs e)
{
if (selectCheckBox.Checked)
selectCheckBox.BackColor = Color.LightBlue;
else
selectCheckBox.BackColor = solve_button.BackColor; // didnt find this color :(
}
private void Hunt_and_Kill(int delay = 0)
{
Random rnd = new Random();
int step = 0;
Point ghost_step = new Point(); // used to "ghost" to the next cell and analyse it
bool dead_end = false;
int sum = 0;
bool step_done = false;
bool gen_complete = false;
byte[] bit_counter = new byte[4]; // used to store information about blicked directions
// "1" - direction is blocked; "0" - direction is free to go
int ban_way = 8; // first run - no ban way
g = this.CreateGraphics();
SolidBrush paint;
int extra_lines = 0;
bool found = false;
while (!gen_complete)
{
// --- WALK
dead_end = false;
while (!dead_end)
{
System.Threading.Thread.Sleep(delay);
maze[current_position.X, current_position.Y].visited = true;
maze[current_position.X, current_position.Y].set_rank(6);
bit_counter = new byte[4];
sum = 0;
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
// set up walls to every nearby visited cells, except for the way back
if ((ghost_step.X >= 0) && (ghost_step.X < length_x) && (ghost_step.Y >= 0) && (ghost_step.Y < length_y))
{
if (maze[ghost_step.X, ghost_step.Y].visited)
{
if (ban_way != t)
maze[current_position.X, current_position.Y].set_side(t);
bit_counter[t] = 1;
}
}
else
bit_counter[t] = 1;
sum += bit_counter[t];
}
while (ban_way == 9) // returning from "hunt" phase
{
step = rnd.Next(0, 4);
ghost_step.X = current_position.X + step_maker[step].X;
ghost_step.Y = current_position.Y + step_maker[step].Y;
if ((ghost_step.X >= 0) && (ghost_step.X < length_x) && (ghost_step.Y >= 0) && (ghost_step.Y < length_y) &&
maze[ghost_step.X, ghost_step.Y].visited)
{
maze[current_position.X, current_position.Y].set_side(step, 0);
break;
}
}
if (sum < 4)
{
step_done = false;
while (!step_done)
{
step = rnd.Next(0, 4);
if (bit_counter[step] != 1)
{
if (animationCheckBox.Checked)
draw_cell(current_position);
current_position.X = current_position.X + step_maker[step].X;
current_position.Y = current_position.Y + step_maker[step].Y;
step_done = true;
ban_way = all_steps[step + 2];
maze[current_position.X, current_position.Y].set_rank(7);
}
}
}
else
{
dead_end = true;
}
if (animationCheckBox.Checked)
draw_cell(current_position);
}
// --- HUNT
found = false;
for (int j = extra_lines; j < length_y; j++)
{
System.Threading.Thread.Sleep(delay*2);
if (animationCheckBox.Checked)
{
if (j > 0)
draw_line(j - 1);
paint = new SolidBrush(SetTransparency(32, Color.LightGreen));
g.FillRectangle(paint, size / 4, (size + 2) * j + 2 + size / 8, length_x * (size + 2) - size / 2, 3 * size / 4);
}
for (int i = 0; i < length_x; i++)
{
if (!maze[i, j].visited)
{
current_position = new Point(i, j);
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
if ((ghost_step.X >= 0) && (ghost_step.X < length_x) && (ghost_step.Y >= 0) && (ghost_step.Y < length_y) &&
maze[ghost_step.X, ghost_step.Y].visited)
{
found = true;
i = length_x;
j = length_y;
break;
}
}
}
else if ((i == length_x - 1) && speedCheckBox.Checked)
{
maze[i, j].set_rank(0);
extra_lines++;
}
else
maze[i, j].set_rank(0);
}
}
if (found)
{
ban_way = 9;
maze[current_position.X, current_position.Y].set_rank(7);
draw_cell(current_position);
System.Threading.Thread.Sleep(delay * 3);
}
else
{
gen_complete = true;
if (animationCheckBox.Checked)
draw_line(length_y - 1);
}
if (animationCheckBox.Checked)
draw_line(current_position.Y);
}
}
private void Walking_Man(int delay = 0)
{
Point[] Storage = new Point[length_x*length_y];
int length = 0;
Random rnd = new Random();
int step = 0;
Point ghost_step = new Point(); // used to "ghost" to the next cell and analyse it
bool dead_end = false;
int sum = 0;
bool step_done = false;
bool gen_complete = false;
string generation_options = optionsBox1.SelectedItem.ToString();
byte[] bit_counter = new byte[4]; // used to store information about blicked directions
// "1" - direction is blocked; "0" - direction is free to go
int ban_way = 8; // first run - no ban way
while (!gen_complete)
{
while (!dead_end)
{
Storage[length++] = current_position;
// if this cell is already visited - it will be deleted from stack later on
// however in-stack marker is cleared right away!
if (maze[current_position.X, current_position.Y].visited)
{
maze[current_position.X, current_position.Y].set_rank(0);
length--; // delete this cell from stack because its already visited
}
else
{
maze[current_position.X, current_position.Y].set_rank(1);
maze[current_position.X, current_position.Y].visited = true;
}
System.Threading.Thread.Sleep(delay);
sum = 0;
bit_counter = new byte[4];
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
//if ghosted cell is outside of the maze - block this way
if ((ghost_step.X < 0) || (ghost_step.X >= length_x) || (ghost_step.Y < 0) || (ghost_step.Y >= length_y))
bit_counter[t] = 1;
else
{
if (maze[ghost_step.X, ghost_step.Y].visited)
{
if ((ban_way != t)&&(ban_way != 9)) // ban_way = 9 if its the beginning of new walk iteration
maze[current_position.X, current_position.Y].set_side(t);
bit_counter[t] = 1;
}
}
sum += bit_counter[t];
}
if (sum < 4)
{
step_done = false;
while (!step_done)
{
step = rnd.Next(0, 4);
if (bit_counter[step] != 1)
{
if (animationCheckBox.Checked)
draw_cell(current_position);
current_position.X = current_position.X + step_maker[step].X;
current_position.Y = current_position.Y + step_maker[step].Y;
step_done = true;
ban_way = all_steps[step + 2];
}
}
if (animationCheckBox.Checked)
draw_cell(current_position);
}
else
{
dead_end = true;
maze[current_position.X, current_position.Y].set_rank(0);
if (animationCheckBox.Checked)
draw_cell(current_position);
}
}
if (length > 1)
{
switch (generation_options)
{
case "Newest":
current_position = Storage[length - 2];
break;
case "Random":
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 2; r++)
Storage[r] = Storage[r + 1];
break;
case "Newest/Random, 50/50 split": // this is not working at this moment
if (rnd.Next(2) == 1)
current_position = Storage[length - 2];
else
{
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 2; r++)
Storage[r] = Storage[r + 1];
}
break;
}
length--;
dead_end = false;
ban_way = 9; // triggers unstuck mechanism
}
else
gen_complete = true;
}
if (length > 0) // clean up call
for (int r = 0; r < length; r++)
{
maze[Storage[r].X, Storage[r].Y].set_rank(0);
if (animationCheckBox.Checked)
draw_cell(Storage[r]);
}
}
private void Growing_Tree(int delay = 0)
{
Point[] Storage = new Point[length_x * length_y];
int length = 0;
Random rnd = new Random();
int step = 0;
Point ghost_step = new Point(); // used to "ghost" to the next cell and analyse it
bool dead_end = false;
int sum = 0;
bool step_done = false;
bool gen_complete = false;
string generation_options = optionsBox1.SelectedItem.ToString();
byte[] bit_counter = new byte[4]; // used to store information about blicked directions
// "1" - direction is blocked; "0" - direction is free to go
int ban_way = 8; // first run - no ban way
while (!dead_end)
{
maze[current_position.X, current_position.Y].set_rank(1);
maze[current_position.X, current_position.Y].visited = true;
Storage[length++] = current_position;
System.Threading.Thread.Sleep(delay);
sum = 0;
bit_counter = new byte[4];
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
if ((ghost_step.X < 0) || (ghost_step.X >= length_x) || (ghost_step.Y < 0) || (ghost_step.Y >= length_y))
bit_counter[t] = 1;
else
{
if (maze[ghost_step.X, ghost_step.Y].visited)
{
if ((ban_way != t))
maze[current_position.X, current_position.Y].set_side(t);
bit_counter[t] = 1;
}
}
sum += bit_counter[t];
}
if (sum < 4)
{
step_done = false;
while (!step_done)
{
step = rnd.Next(0, 4);
if (bit_counter[step] != 1)
{
if (animationCheckBox.Checked)
draw_cell(current_position);
current_position.X = current_position.X + step_maker[step].X;
current_position.Y = current_position.Y + step_maker[step].Y;
step_done = true;
ban_way = all_steps[step + 2];
}
}
if (animationCheckBox.Checked)
draw_cell(current_position);
}
else
{
dead_end = true;
maze[current_position.X, current_position.Y].set_rank(0);
if (animationCheckBox.Checked)
draw_cell(current_position);
}
}
while (!gen_complete)
{
if (length > 0)
{
switch (generation_options)
{
case "Newest (Backtracker)":
current_position = Storage[length - 1];
break;
case "Random (Prim's)":
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 1; r++)
Storage[r] = Storage[r + 1];
break;
case "One of the newest":
if (length > 5)
sum = rnd.Next(5);
else
sum = rnd.Next(length);
sum = length - sum - 1;
current_position = Storage[sum];
for (int r = sum; r < length - 1; r++)
Storage[r] = Storage[r + 1];
break;
case "Newest/Random, 75/25 split":
if (rnd.Next(4) != 0)
current_position = Storage[length - 1];
else
{
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 1; r++)
Storage[r] = Storage[r + 1];
}
break;
case "Newest/Random, 50/50 split":
if (rnd.Next(2) == 0)
current_position = Storage[length - 1];
else
{
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 1; r++)
Storage[r] = Storage[r + 1];
}
break;
case "Newest/Random, 25/75 split":
if (rnd.Next(4) == 0)
current_position = Storage[length - 1];
else
{
sum = rnd.Next(length);
current_position = Storage[sum];
for (int r = sum; r < length - 1; r++)
Storage[r] = Storage[r + 1];
}
break;
case "Middle":
current_position = Storage[length / 2];
for (int r = length/2; r < length - 1; r++)
Storage[r] = Storage[r + 1];
break;
case "Oldest":
current_position = Storage[0];
for (int r = 0; r < length - 1; r++)
Storage[r] = Storage[r + 1];
break;
}
// phase two
Storage[length - 1] = current_position;
System.Threading.Thread.Sleep(delay);
sum = 0;
bit_counter = new byte[4];
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
if ((ghost_step.X < 0) || (ghost_step.X >= length_x) || (ghost_step.Y < 0) || (ghost_step.Y >= length_y))
bit_counter[t] = 1;
else
if (maze[ghost_step.X, ghost_step.Y].visited)
bit_counter[t] = 1;
sum += bit_counter[t];
}
if (sum < 4)
{
step_done = false;
while (!step_done)
{
step = rnd.Next(0, 4);
if (bit_counter[step] != 1)
{
if (animationCheckBox.Checked)
draw_cell(current_position);
current_position.X = current_position.X + step_maker[step].X;
current_position.Y = current_position.Y + step_maker[step].Y;
step_done = true;
ban_way = all_steps[step + 2];
}
}
for (int t = 0; t < 4; t++)
{
ghost_step.X = current_position.X + step_maker[t].X;
ghost_step.Y = current_position.Y + step_maker[t].Y;
if ((ghost_step.X >= 0) && (ghost_step.X < length_x) && (ghost_step.Y >= 0) && (ghost_step.Y < length_y) &&
maze[ghost_step.X, ghost_step.Y].visited && (ban_way != t))
maze[current_position.X, current_position.Y].set_side(t);
}
maze[current_position.X, current_position.Y].set_rank(1);
maze[current_position.X, current_position.Y].visited = true;
Storage[length++] = current_position;
sum = 0; // breaks 'if' chain, please let it live!
}
else
{
maze[current_position.X, current_position.Y].set_rank(0);
length--;
}
if (animationCheckBox.Checked)
draw_cell(current_position);
sum = 0; // breaks 'if' chain, please let it live!
}
else
gen_complete = true;
}
}
private void Solve_Wave(int delay = 0)
{
wave[] legacy = new wave[1];
wave[] switcher; // ised to increase legacy.Length
wave add2legacy;
Point[] Storage = new Point[length_x * length_y];
int length = 0;
legacy[0] = new wave(current_position);
bool step_done = false;
int step;
Point ghost_step = new Point();
byte[] bit_counter = new byte[4];
int ban_way = 8; // first run - not banning any way
bool dead_end = false;
Random rnd = new Random();
int sum = 0;
int finder = -1;
while (true) // it will break as soon as "length" == 0
{
// --- Forking wave
dead_end = false;
while (!dead_end)
{
System.Threading.Thread.Sleep(delay);
if ((maze[current_position.X, current_position.Y].get_rank() != 2) && (maze[current_position.X, current_position.Y].get_rank() != 3))
maze[current_position.X, current_position.Y].set_rank(4);
bit_counter = new byte[4];
if (animationCheckBox.Checked)
draw_cell(current_position);
sum = 0;
for (int i = 0; i < 4; i++)
{
bit_counter[i] = maze[current_position.X, current_position.Y].get_side(i);
if (bit_counter[i] != 1)
{
ghost_step.X = current_position.X + step_maker[i].X;
ghost_step.Y = current_position.Y + step_maker[i].Y;
step = maze[ghost_step.X, ghost_step.Y].get_rank(); // just didnt want to create new variable for this
if ((step == 2) || (step == 4))
bit_counter[i] = 1;
if (maze[ghost_step.X, ghost_step.Y].get_side(all_steps[i + 2]) == 1)
bit_counter[i] = 1; // This aint gonna happen, just making sure to cover it
// "maze_update()" completely removes this situations
}
sum += bit_counter[i];
}
if (ban_way >= 0 && ban_way < 4)
{
bit_counter[ban_way] = 1;
legacy[legacy.Length - 1].add(current_position);
//sum++;
}
if (sum < 3)
Storage[length++] = current_position;
if (sum < 4)
{
step_done = false;
while (!step_done)
{
step = rnd.Next(0, 4);
if (bit_counter[step] != 1)
{
ghost_step.X = current_position.X + step_maker[step].X;
ghost_step.Y = current_position.Y + step_maker[step].Y;
sum = maze[ghost_step.X, ghost_step.Y].get_rank(); // just didnt want to create new variable for this
if (sum == 3)
{
current_position = ghost_step;
step_done = true;
ban_way = all_steps[step + 2];
legacy[legacy.Length - 1].winner = true;
legacy[legacy.Length - 1].add(current_position);
dead_end = true;
}
else
{
current_position = ghost_step;
step_done = true;
ban_way = all_steps[step + 2];
}
}
}
if (animationCheckBox.Checked)
draw_cell(current_position);
}
else
{
dead_end = true;
}
}
// --- creating new wave
if (length == 0)
break;
ban_way = 6;
current_position = Storage[length-- - 1];
add2legacy = null; // if something goes wrong - it will throw an error
for (int i = legacy.Length - 1; i >= 0; i--)
{
finder = legacy[i].find_element(current_position);
if (finder >= 0)
{
add2legacy = legacy[i];
break;
}
}
switcher = legacy;
legacy = new wave[switcher.Length + 1];
for (int i = 0; i < switcher.Length; i++)
legacy[i] = switcher[i];
if (finder >= 0)
legacy[switcher.Length] = new wave(add2legacy, finder);
}
// --- seacrhing for a shortes wave that hit exit cell