Walking Maze C # version (i)

Source: Internet
Author: User
Maze class related

Using System;
Using System.Drawing;
Using System.Drawing.Drawing2D;
Using System.Collections;

Namespace Mazedemo
{
<summary>
Maze class
</summary>
public class Cmaze
{
bool[,] mg; Map lattice
Stack stack; Stack
Point In_p; Entry point
Point out_p; Exit point
Point start_p; Draw the starting point of the fan time
Size boxsize; Size of each lattice
int step_count; How many steps to take

Public Cmaze ()
{
Stack=new Stack ();
This.start_p=new Point (0,0);
This.boxsize=new Size (50,50);
step_count=0;
}

Public Cmaze (bool[,] _mg): this ()
{
THIS.MG=_MG;
}

Public Cmaze (bool[,] _mg,point _in,point _out): this ()
{
THIS.MG=_MG;
this.in_p=_in;
This.out_p=_out;
Stack Way=this. Test (this.in_p,_in);
Stack. Push (New Ccoor (This.in_p,way));
this.step_count++;
}


<summary>
The starting coordinates of the window when the maze is drawn
</summary>
Public Point StartPoint
{
Set{this.start_p=value;}
Get{return this.start_p;}
}

<summary>
How many steps to take in the current maze
</summary>
public int Stepcount
{
Get{return This.step_count;}
}

<summary>
Labyrinth Lattice Size
</summary>
Public Size Boxsize
{
Set{this.boxsize=value;}
Get{return This.boxsize;}
}

<summary>
Number of stack data
</summary>
public int Stackcount
{
Get{return This.stack.Count;}
}

<summary>
Draw a Maze
</summary>
<param name= "G" ></param>
public void Drawbox (Graphics g)
{
for (int i=0;i<mg. GetLength (0); i++)
{
for (int j=0;j<mg. GetLength (1); j + +)
{
Point Pp=new Point ((J*boxsize.width) +startpoint.x, (i*boxsize.height) +startpoint.y); Position
SolidBrush Brush;
Rectangle rect=new Rectangle (pp,boxsize);

if (Mg[i,j])
Brush=new SolidBrush (Color.green);
Else
Brush=new SolidBrush (color.red);
G.fillrectangle (Brush,rect);
}
}
}

<summary>
Draw the line you are taking
</summary>
<param name= "G" ></param>
public void DrawPath (Graphics g)
{
IEnumerator myenumerator = stack. GetEnumerator ();
while (Myenumerator.movenext ())
{
Ccoor c=new Ccoor ();
C= (Ccoor) myenumerator.current;
Point Pp=new Point ((C.currentpoint.y*boxsize.width) +startpoint.x, (c.currentpoint.x*boxsize.height) +StartPoint.Y) ;
SolidBrush brush=new SolidBrush (Color.Blue);
Rectangle rect=new Rectangle (pp,boxsize);
G.fillrectangle (Brush,rect);
}
}

<summary>
To draw a workable path to the current position
</summary>
<param name= "G" ></param>
public void Drawnextpath (Graphics g)
{
Ccoor c= (Ccoor) This.stack.Peek ();
Stack S=c.waypath;
IEnumerator Myenumerator=s.getenumerator ();
while (Myenumerator.movenext ())
{
Point p= (Point) myenumerator.current;
Point Pp=new Point ((P.y*boxsize.width) +startpoint.x, (p.x*boxsize.height) +startpoint.y);
SolidBrush brush=new SolidBrush (Color.yellow);
Rectangle rect=new Rectangle (pp,boxsize);
G.fillrectangle (Brush,rect);
}
}

<summary>
To judge whether the maze is finished.
</summary>
<returns></returns>
public bool Isend ()
{
Ccoor coor= (Ccoor) This.stack.Peek (); Current location information
if (coor. currentpoint.x==this.out_p.x && coor. CURRENTPOINT.Y==THIS.OUT_P.Y)
return true;
Else
return false;
}

<summary>
Walk a lattice in a maze
</summary>
<returns> Digital Status </returns>
public int Step ()
{
Ccoor coor= (Ccoor) This.stack.Peek (); Current location information
Whether to reach the exit
if (!) ( Coor. Currentpoint.x==this.out_p.x&&coor. CURRENTPOINT.Y==THIS.OUT_P.Y))
{
Stack Ss=coor. Waypath;
if (ss. count==0)
{
This.stack.Pop ();
return 0;
}
Point p= (Point) SS. Pop (); The next location where the current position can continue to move
if (P.X==THIS.OUT_P.X&AMP;&AMP;P.Y==THIS.OUT_P.Y)
{
This.stack.Push (New Ccoor (P,new stack ()));
return 0;
}
Stack St=this. Test (P,coor. Currentpoint); To get all the removable positions for the next removable position
if (St. count==0)
{
return 0;
}
Ccoor newcoor=new Ccoor (p,st); Create a new location information
This.stack.Push (Newcoor); Push into stack
this.step_count++; Steps plus 1.
return 0;
}
Else
return 1;
}

<summary>
Take the Maze
</summary>
public void Run ()
{
while (this. Step ()!=1);
}

<summary>
Back to the beginning of the maze
</summary>
public void Reset ()
{
This.stack.Clear ();
Stack Way=this. Test (this.in_p,this.in_p);
Stack. Push (New Ccoor (This.in_p,way));
This.step_count=1;
}

<summary>
Detecting viable routes
Probe sequence right-> before-> left->
Left
/// |
Back--+--> before
/// |
Right
</summary>
<param name= "P" > Query four weeks from the current point whether there is a feasible route </param>
<param name= "Perv_p" > Previous route </param>
<returns> feasible Route Stack </returns>
Public Stack Test (Point p,point perv_p)
{
Stack stack_way=new stack (); The point of the feasible position stack
int x,y;

After
x=p.x;
Y=p.y-1;
This. Signpost (x,y,stack_way,perv_p);

Left
X=p.x-1;
Y=P.Y;
This. Signpost (x,y,stack_way,perv_p);

Ago
x=p.x;
y=p.y+1;
This. Signpost (x,y,stack_way,perv_p);

Right
x=p.x+1;
Y=P.Y;
This. Signpost (x,y,stack_way,perv_p);

return stack_way;
}

<summary>
To determine if this direction is feasible, it is possible to press the information onto the stack, which is called only in the test () function.
</summary>
<param name= "x" >x coordinates </param>
<param name= "y" >y coordinates </param>
<param name= "s" > Stack </param>
<param name= "Perv_p" > The direction of the time </param>
private void signpost (int x,int y,stack s,point perv_p)
{
if ((x>=0 && x<this.mg.getlength (0)) && (y>=0 && y<this.mg.getlength (1)))
{
if (this.mg[x,y]&&! ( X==PERV_P.X&AMP;&AMP;Y==PERV_P.Y))
S.push (new Point (X,y));
}
}

<summary>
Maze diagram
</summary>
<returns> Character Map </returns>
public override string ToString ()
{
String Str= "";
for (int i=0;i<mg. GetLength (0); i++)
{
for (int j=0;j<mg. GetLength (1); j + +)
{
if (This.mg[i,j])
str+= "-";
Else
str+= "";
}
str+= "\ n";
}
return str;
}
}

<summary>
Current coordinate information, and the available direction coordinates
</summary>
public class Ccoor
{
Private point curr_p; Current coordinates
Private Stack Way; coordinate of the direction to go

Public Ccoor ()
{
//...
}

Public Ccoor (Point P,stack W)
{
Curr_p=p;
Way=w;
}

Public Point Currentpoint
{
Get{return this.curr_p;}
Set{this.curr_p=value;}
}

Public Stack Waypath
{
Set{this.way=value;}
Get{return This.way;}
}
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.