Current location - Training Enrollment Network - Mathematics courses - How java generates a maze
How java generates a maze
//Author: zhongZw?

Parcel? cn . zhongw . model;

Import? Java . util . ArrayList;

Import? Java . util . random;

Public? Class? MazeModel? {

Private? int? Width? =? 0;

Private? int? Height? =? 0;

Private? Random? rnd? =? New? random();

Public? MazeModel()? {

This. Width? =? 50; ? //Maze width?

This. Height? =? 50; ? //Maze height?

}

Public? int? getWidth()? {

Return? Width;

}

Public? Invalid? setWidth(int? Width)? {

This. Width? =? Width;

}

Public? int? getHeight()? {

Return? Height;

}

Public? Invalid? setHeight(int? Height)? {

This. Height? =? Height;

}

Public? MazeModel(int? Width? int? Height)? {

super();

This. Width? =? Width;

This. Height? =? Height;

}

Public? Array list? & lt? MazePoint? & gt? getMaze()? {

Array list? & lt? MazePoint? & gt? Maze? =? New? Array list? & lt? MazePoint? & gt? ();

For what? (int? h? =? 0; ? h? & lt? Height; ? h++)? {

For what? (int? W? =? 0; ? W? & lt? Width; ? w++)? {

MazePoint? Point? =? New? MazePoint(w,h);

Maze.add (point);

}

}

Return? CreateMaze;

}

Private? Array list? & lt? MazePoint? & gt? CreateMaze(ArrayList? & lt? MazePoint? & gt? Maze)? {

int? top? =? 0;

int? x? =? 0;

int? y? =? 0;

Array list? & lt? MazePoint? & gt? Team? =? New? Array list? & lt? MazePoint? & gt? ();

team.add(maze.get(x? +? y? *? Width));

What time? (top? & gt=? 0)? {

int[]? Val. =? New? int[]? {

- 1,? - 1,? - 1,? - 1

};

int? Times? =? 0;

Bull? Flag? =? Fake;

MazePoint? pt? =? (MazePoint)? team . get(top);

x? =? pt . getx();

y? =? pt . gety();

pt.visted? =? True;

ro 1:? What time? (Time? & lt? 4)? {

int? dir? =? rnd . nextint(4);

What if? (val[dir]? ==? dir)

Continue;

other

val[dir]? =? dir

Switch? (dir)? {

Case? 0:? //? Left?

What if? ((x? -? 1)? & gt=? 0? & amp& amp? maze.get(x? -? 1? +? y? *? Width). Have you visited it? ==? False)? {

maze.get(x? +? y? *? Width). set left();

maze.get(x? -? 1? +? y? *? Width). setRight();

team.add(maze.get(x? -? 1? +? y? *? Width));

top++;

Flag? =? True;

Break? ro 1;

}

Break;

Case? 1:? //? Right?

What if? ((x? +? 1)? & lt? Width? & amp& amp? maze.get(x? +? 1? +? y? *? Width). Have you visited it? ==? False)? {

maze.get(x? +? y? *? Width). setRight();

maze.get(x? +? 1? +? y? *? Width). set left();

team.add(maze.get(x? +? 1? +? y? *? Width));

top++;

Flag? =? True;

Break? ro 1;

}

Break;

Case? 2:? //? Up there?

What if? ((y? -? 1)? & gt=? 0? & amp& amp? maze.get(x? +? (y? -? 1)? *? Width). Have you visited it? ==? False)? {

maze.get(x? +? y? *? Width). setUp();

maze.get(x? +? (y? -? 1)? *? Width). setDown();

team.add(maze.get(x? +? (y? -? 1)? *? Width));

top++;

Flag? =? True;

Break? ro 1;

}

Break;

Case? 3:? //? Below?

What if? ((y? +? 1)? & lt? Height? & amp& amp? maze.get(x? +? (y? +? 1)? *? Width). Have you visited it? ==? False)? {

maze.get(x? +? y? *? Width). setDown();

maze.get(x? +? (y? +? 1)? *? Width). setUp();

team.add(maze.get(x? +? (y? +? 1)? *? Width));

top++;

Flag? =? True;

Break? ro 1;

}

Break;

}

Times? +=? 1;

}

What if? (! Flag)? {

Team.remove (top);

top? -=? 1;

}

}

Return? Labyrinth;

}

}

Maze [java]? View the plain? copy

//Author: zhongZw?

//E-mail: zhong3 17@ 126.com?

Parcel? cn . zhongw . model;

Import? Java . util . *;

Import? Java . lang . *;

Public? Class? MazePoint? {

Private? int? Left? =? 0;

Private? int? Right? =? 0;

Private? int? Up? =? 0;

Private? int? Come down? =? 0;

Private? int? x;

Private? int? y;

Public? Bull? Visited;

Public? MazePoint(int? x,? int? y)? {

This. x? =? x;

This. y? =? y;

}

Public? int? getLeft()? {

Return? Left;

}

Public? Invalid? setLeft()? {

This, on the left? =? 1;

}

Public? int? getRight()? {

Return? Right;

}

Public? Invalid? setRight()? {

This, right? =? 1;

}

Public? int? getUp()? {

Return? Upward;

}

Public? Invalid? setUp()? {

Here. Get up? =? 1;

}

Public? int? getDown()? {

Return? Downward;

}

Public? Invalid? setDown()? {

Put this down. =? 1;

}

Public? int? getX()? {

Return? x;

}

Public? Invalid? setX(int? x)? {

This. x? =? x;

}

Public? int? getY()? {

Return? y;

}

Public? Invalid? setY(int? y)? {

This. y? =? y;

}

}