-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaze.cs
572 lines (567 loc) · 10.8 KB
/
Maze.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
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
namespace MazeAdvanced
{
/// <summary>
/// Maze 的摘要说明。
/// </summary>
public class Maze
{
public enum Errors
{
Unkonw,
NoError,
NoPath,
StartEndOutOfMaze,
StartEndDisable,
}
public enum GridState
{
Null=int.MinValue,
Disable=0,
Enable=1,
Seeked=2
}
public enum Direction
{
Null=int.MinValue,
Right=0,
Down=1,
Left=2,
Up=3
}
private Direction NextDir(Direction Dir)
{
switch(Dir)
{
case Direction.Right:
return Direction.Down;
case Direction.Down:
return Direction.Left;
case Direction.Left:
return Direction.Up;
case Direction.Up:
return Direction.Null;
default:
return Direction.Null;
}
}
public class Grid
{
public int X;
public int Y;
public Grid PreGrid;
public Grid():this(-1,-1,null){}
public Grid(int X,int Y):this(X,Y,null){}
private Grid(int X,int Y,Grid PreG)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.X=X;
this.Y=Y;
this.PreGrid=PreG;
}
public Grid Copy()
{
return new Grid(this.X,this.Y,this.PreGrid);
}
public Grid CopyWithoutPre()
{
return new Grid(this.X,this.Y);
}
public Grid SuperSet(Grid G)
{
return new Grid(G.X,G.Y,new Grid(this.X,this.Y,this.PreGrid));
}
public Grid NextGrid(Direction Dir)
{
switch(Dir)
{
case Direction.Down:
return new Grid(this.X,this.Y+1);
case Direction.Left:
return new Grid(this.X-1,this.Y);
case Direction.Right:
return new Grid(this.X+1,this.Y);
case Direction.Up:
return new Grid(this.X,this.Y-1);
default:
return this.CopyWithoutPre();
}
}
#region 重载的"=="和"!="
/* public static bool operator ==(Grid GL,Grid GR)
{
int XL,XR,YL,YR;
bool LNull=false;
bool RNull=false;
try
{
XL=GL.X;
YL=GL.Y;
}
catch(NullReferenceException)
{
LNull=true;
}
try
{
XR=GR.X;
YR=GR.Y;
}
catch(NullReferenceException)
{
RNull=true;
}
if(LNull && RNull)
return true;
else if(!LNull && !RNull)
{
if(GL.X==GR.X && GL.Y==GR.Y)
return true;
else
return false;
}
else
return false;
}
public static bool operator !=(Grid GL,Grid GR)
{
int XL,XR,YL,YR;
bool LNull=false;
bool RNull=false;
try
{
XL=GL.X;
YL=GL.Y;
}
catch(NullReferenceException)
{
LNull=true;
}
try
{
XR=GR.X;
YR=GR.Y;
}
catch(NullReferenceException)
{
RNull=true;
}
if(LNull && RNull)
return false;
else if(!LNull && !RNull)
{
if(GL.X==GR.X && GL.Y==GR.Y)
return false;
else
return true;
}
else
return true;
}*/
#endregion
}
private bool GenFinish()
{
for(int i=0;i<this.Height;i+=2)
{
for(int j=0;j<this.Width;j+=2)
{
if(this.Grids[i,j]==GridState.Disable)
return false;
}
}
return true;
}
public bool Generate(int Width,int Height)
{
if(Width%2==0)
Width++;
if(Height%2==0)
Height++;
this.Grids=new GridState[Height,Width];
this.Width=Width;
this.Height=Height;
byte dir;
//this.Grids[0,0]=0;
int currX=0;
int currY=0;
while(true)
{
if(this.GenFinish())
break;
dir=(byte)this.Rnd.Next(4);
if(currX==0)
{
if(currY==0)
{
if(dir%2==0)
dir=0;
else
dir=1;
}
else if(currY==this.Height-1)
{
if(dir%2==0)
dir=0;
else
dir=3;
}
else
{
dir%=3;
if(dir==0)
dir=0;
else if(dir==1)
dir=1;
else
dir=3;
}
}
else if(currX==this.Width-1)
{
if(currY==0)
{
if(dir%2==0)
dir=2;
else
dir=1;
}
else if(currY==this.Height-1)
{
if(dir%2==0)
dir=2;
else
dir=3;
}
else
{
dir%=3;
if(dir==0)
dir=2;
else if(dir==1)
dir=1;
else
dir=3;
}
}
else
{
if(currY==0)
{
dir%=3;
if(dir==0)
dir=1;
else if(dir==1)
dir=0;
else
dir=2;
}
else if(currY==this.Height-1)
{
dir%=3;
if(dir==0)
dir=3;
else if(dir==1)
dir=0;
else
dir=2;
}
else
dir=(byte)this.Rnd.Next(4);
}
if(dir==0)
{
currX+=2;
if(this.Grids[currY,currX]==GridState.Disable)
{
this.Grids[currY,currX]=GridState.Enable;
this.Grids[currY,currX-1]=GridState.Enable;
}
continue;
}
else if(dir==2)
{
currX-=2;
if(this.Grids[currY,currX]==GridState.Disable)
{
this.Grids[currY,currX]=GridState.Enable;
this.Grids[currY,currX+1]=GridState.Enable;
}
continue;
}
else if(dir==1)
{
currY+=2;
if(this.Grids[currY,currX]==GridState.Disable)
{
this.Grids[currY,currX]=GridState.Enable;
this.Grids[currY-1,currX]=GridState.Enable;
}
continue;
}
else if(dir==3)
{
currY-=2;
if(this.Grids[currY,currX]==GridState.Disable)
{
this.Grids[currY,currX]=GridState.Enable;
this.Grids[currY+1,currX]=GridState.Enable;
}
continue;
}
}
this.Player.X=0;
this.Player.Y=0;
return true;
}
public Errors Solve(out Stack S)
{
return Solve(new Grid(0,0),new Grid(this.Width-1,this.Height-1),out S);
}
public Errors Solve(Grid Start,Grid End,out Stack S)
{
S=new Stack();
if(!IsInMaze(Start) || !IsInMaze(End))
return Errors.StartEndOutOfMaze;
if(this[Start]!=GridState.Enable || this[End]!=GridState.Enable)
return Errors.StartEndDisable;
Queue Q=new Queue();
Q.Enqueue(Start);
Direction CurDir;
Grid CurG=new Grid();
Grid tempG=new Grid();
bool Find=false;
while(!Find)
{
if(Q.Count>0)
CurG=(Grid)Q.Dequeue();
else
return Errors.NoPath;
CurDir=Direction.Right;
while(CurDir!=Direction.Null)
{
tempG=CurG.NextGrid(CurDir);
if(this[tempG]==GridState.Enable)
{
if(tempG.X==End.X && tempG.Y==End.Y)
{
Find=true;
break;
}
Q.Enqueue(CurG.SuperSet(tempG));
this[tempG]=GridState.Seeked;
}
CurDir=NextDir(CurDir);
}
}
CurG=CurG.SuperSet(tempG);
while(CurG!=null)
{
S.Push(CurG.CopyWithoutPre());
CurG=CurG.PreGrid;
}
ClearSeek();
return Errors.NoError;
}
private void ClearSeek()
{
int I,J;
for(I=0;I<this.Height;I++)
for(J=0;J<this.Width;J++)
if(this.Grids[I,J]==GridState.Seeked)
this.Grids[I,J]=GridState.Enable;
}
public bool Draw(Graphics Graph)
{
if(Graph==null)
return false;
if(Width==0 || Height==0)
return false;
int I,J;
for(I=0;I<this.Width;I++)
for(J=0;J<this.Height;J++)
{
if(this.Grids[J,I]==GridState.Disable)
Graph.FillRectangle(this.WallBrush,I*this.GridSize,J*this.GridSize,this.GridSize,this.GridSize);
else if(this.Grids[J,I]==GridState.Enable)
Graph.FillRectangle(this.PathBrush,I*this.GridSize,J*this.GridSize,this.GridSize,this.GridSize);
}
return true;
}
public bool DrawAnswer(Stack Answer,Graphics Graph)
{
if(Graph==null)
return false;
foreach(Grid Gd in Answer.ToArray())
Graph.FillRectangle(this.AnswerBrush,Gd.X*this.GridSize,Gd.Y*this.GridSize,this.GridSize,this.GridSize);
return true;
}
public bool ClearAnswer(Stack Answer,Graphics Graph)
{
if(Graph==null)
return false;
foreach(Grid Gd in Answer.ToArray())
Graph.FillRectangle(this.PathBrush,Gd.X*this.GridSize,Gd.Y*this.GridSize,this.GridSize,this.GridSize);
return true;
}
public bool MovePlayer(Direction Dir)
{
int tX=this.Player.X;
int tY=this.Player.Y;
switch(Dir)
{
case Direction.Down:
{
this.Player.Y++;
break;
}
case Direction.Left:
{
this.Player.X--;
break;
}
case Direction.Right:
{
this.Player.X++;
break;
}
case Direction.Up:
{
this.Player.Y--;
break;
}
}
if(this[this.Player]==GridState.Enable)
return true;
else
{
this.Player.X=tX;
this.Player.Y=tY;
return false;
}
}
public bool DrawPlayer(Graphics Graph)
{
if(Graph==null)
return false;
Graph.FillRectangle(this.PlayerBrush,this.Player.X*this.GridSize,this.Player.Y*this.GridSize,this.GridSize,this.GridSize);
return true;
}
public bool ClearPlayer(Graphics Graph)
{
if(Graph==null)
return false;
Graph.FillRectangle(this.PathBrush,this.Player.X*this.GridSize,this.Player.Y*this.GridSize,this.GridSize,this.GridSize);
return true;
}
public Maze() :this(new GridState[0,0]){}
public Maze(int Width,int Height) :this(new GridState[Height,Width]){}
public Maze(GridState[,] Grids)
{
//
// TODO: 在此处添加构造函数逻辑
//
ReLoad(Grids);
}
public void ReLoad(GridState[,] Grids)
{
int I,J;
this.Height=Grids.GetLength(0);
this.Width=Grids.GetLength(1);
this.Grids=new GridState[this.Height,this.Width];
for(I=0;I<this.Grids.GetLength(0);I++)
for(J=0;J<this.Grids.GetLength(1);J++)
this.Grids[I,J]=Grids[I,J];
}
public Maze Copy()
{
return new Maze(this.Grids);
}
public GridState this [Grid Grid]//获取设置Grid处的状态
{
get
{
if(IsInMaze(Grid))
return this.Grids[Grid.Y,Grid.X];
else
return GridState.Null;
}
set
{
if(IsInMaze(Grid))
this.Grids[Grid.Y,Grid.X]=value;
}
}
public GridState this [int X,int Y]
{
get
{
if(IsInMaze(X,Y))
return this.Grids[Y,X];
else
return GridState.Null;
}
set
{
if(IsInMaze(X,Y))
this.Grids[Y,X]=value;
}
}
public bool IsInMaze(Grid Grid)
{
if(Grid.X>=0 && Grid.X<this.Width && Grid.Y>=0 && Grid.Y<this.Height)
return true;
else
return false;
}
public bool IsInMaze(int X,int Y)
{
if(X>=0 && X<this.Width && Y>=0 && Y<this.Height)
return true;
else
return false;
}
public int get_Width()
{
return this.Width;
}
public int get_Height()
{
return this.Height;
}
public Grid get_Player()
{
return Player;
}
public bool set_Player(Grid G)
{
return set_Player(G.X,G.Y);
}
public bool set_Player(int X,int Y)
{
if(IsInMaze(X,Y))
{
Player.X=X;
Player.Y=Y;
return true;
}
else
return false;
}
private GridState[,] Grids;
private int Width;
private int Height;
public ushort GridSize=4;
private Grid Player=new Grid(0,0);
public Brush WallBrush=new SolidBrush(Color.Black);
public Brush PathBrush=new SolidBrush(Color.White);
public Brush AnswerBrush=new SolidBrush(Color.FromArgb(128,255,128));
public Brush PlayerBrush=new SolidBrush(Color.Red);
Random Rnd=new Random();
}
}