Walking the Maze C # Edition (1)

zhaozj2021-02-16  53

// Maze related

Using system.drawing; using system.drawing.drawing2d; using system.collections;

Namespace Mazedemo {///

/// Maze /// public class cmaze {bool [,] mg; // map lattice stack stack; // stack Point in_p; // Entrance point Point Out_p ; // Export point Point Start_p; // Draw the starting point size boxsize; / / Each grid Int Step_count; // How many steps

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 ;}

///

/// Draw the start coordinate ////////////////////////////////////////////////////////////////bit {= value;} get {return this.start_p;}}

///

/// Current labyrinth How many steps /// public int adjust {get {return this.step_count;}}

///

/// Maze lattice size /// public size boxsize {set {this.boxsize = value;} get {return this.boxsize;}}

///

/// Stack data number /// public int stackcount {get {return this.stack.count;}}

///

/// Draw Maze /// /// public void DrawBox (graphics g) {for (int i = 0; i < mg.getLength (0); i ) {for (int J = 0; j

IF (mg [i, j]) brush = new solidbrush; Else Brush = new solidbrush (color.red); g.fillRectangle (brush, rect);}}} ///

// / Drawing Walk /// /// 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);}}

///

/// Draw the feasible path of the current location /// /// 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 (pY * BoxSize.Width) StartPoint.X, (pX * BoxSize.Height) StartPoint.Y); SolidBrush brush = new SolidBrush (Color.Yellow); Rectangle rect = new Rectangle (pp, boxSize); g.FillRectangle Brush, Rect);}}

///

/// Judging whether the labyrinth is finished /// /// public bool isend () {ccoor coor = (ccoor) this.stack.peek () ; // Current location information IF (COOR.CURRENTPOINT.X == this.out_p.x && coor.cRRRENTPOINT.Y == this.out_p.y) Return True; else returnaf false;}

///

/// A grid /// /// digital status public int adjust () {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 (); // Current location can continue to move the next position IF (PX == this.out_p.x && p.y == this.out_p.y) {this.stack.push (new ccoor (p, new stack ()); return 0;} st = this.test (p, coor .Currentpoint); // Get all removable position IF of the next movable position (st.count == 0) {RETURN 0;} ccoor newcoor = new ccoor (p, st); // Establish a new location information this .stack.push (newcoor); // Pressing stack this.step_count ; // walking step plus 1 return 0;} else returnit 1;} /// /// Walking /// Public void Run () {while (this.step ()! = 1);

///

/// Reply to the labyrinth starting point /// 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;}

///

/// detection feasible route /// detection order right-> before-> left -> after / // /// | ///// -> front /// | /// right /// Whether there is a feasible route from the current point /// Previous route < / param> /// Viable route stack Public Stack Test (Point P, POINT Perv_P) {stack stack_way = new stack (); // This point is possible to position stack int x, y;

// After x = p.x; y = p.y-1; this.signpost (x, y, stack_way, perv_p);

// Left x = PX-1; y = py; this.signpost (x, y, stack_way, perv_p); // front x = px; 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;}

///

/// Determines if the direction is feasible, feasible, press the information into the stack, only call /// /// x in the TEST () function Coordinate /// y coordinate /// stack /// The direction of the time Private void sign (int x, int y, stack s, point perv_p) {if ((x> = 0 && x = 0 && y <.mg.getlength (1))) {IF (THIS.MG [X, Y] &&! (x = PERV_P.X && Y == Perv_p.y)) S.push (New Point (x, y) }}

///

/// Maze simple map /// /// Character map public override string toString () {string str = ""; for (int i = 0 i

///

/// Current coordinate information, and travel direction coordinate /// public class ccoor {private point curr_p; // Current coordinate private stack way; // can go direction coordinate

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 {returnid this.way;}}}}}}

转载请注明原文地址:https://www.9cbs.com/read-19130.html

New Post(0)