[Java] I tried to make a maze by the digging method ♪

This is the first post! !!

I have a maze like this.

実行結果.png

The reason I decided to make it

I used to make a maze game using the stick-down method with HTML5 and JS and exhibited it at a school festival. However, the maze created by the stick-down method has a very simple solution, so I wanted to try it with a digging method with a complicated solution.

What is the digging method?

** ■ Very roughly speaking, the digging method is such an algorithm ** (Maybe it's wrong: sweat_drops :)

Create an odd-numbered map with a size of 5 or more both vertically and horizontally. Fill everything with a wall Randomly select places with odd X and Y coordinates other than the edges of the map and make holes. ★ Check if there are still walls with odd X and Y coordinates (this time the upper left corner is 0,0) other than the edge. ** If there is: ** Randomly select a place where the X and Y coordinates are both odd and already have holes, other than the edge of the map. | Check if there is still a wall 2 squares on all sides | ** If there is: ** Digging 2 squares at random from the direction of digging | If not **: Return to ** ★ ** If not: ** Maze completed

…… But it is difficult to implement it obediently, so I will devise various things.

contents

Main class

MainClass.java


package automaze;
public class MainClass {
	public static void main(String[] args) {
		Maze maze=new Maze(21,21);//This time, I will make a 21x21 maze as a trial.
		maze.show();
	}
}

Maze class

Maze.java


package automaze;

public class Maze {
	private int pointX; //A mark for placing and erasing blocks.
	private int pointY;
	private int width; //Width and height.
	private int height;
	private byte[][] map; //Array to store the map
	public Maze(int w, int h) { //Constructor
		width = w;
		height = h;
		if (w % 2 != 0 && h % 2 != 0 && 5 <= w && 5 <= h) {
			map = new byte[width][height];
			make();
		} else {
			System.out.println("Create an odd number of 5 or more both vertically and horizontally.");
		}
	}

	int randomPos(int muki) { //x,Returns odd random coordinates for both y coordinates
		int result = 1 + 2 * (int) Math.floor((Math.random() * (muki - 1)) / 2);
		return result;
	}

	private void make() { //Create a map

		pointX = randomPos(width);
		pointY = randomPos(height);

		for (int y = 0; y < height; y++) { //Fill everything with a wall.
			for (int x = 0; x < width; x++) {
				map[x][y] = 1;
			}
		}
		map[pointX][pointY] = 0;
		dig();

	}

	private void dig() {
		if (isAbleContinueDig() && map[pointX][pointY] == 0) {
			map[pointX][pointY] = 0;
			int direction = (int) Math.floor(Math.random() * 4);
			switch (direction) {
			case 0:
				if (pointY != 1) {
					if (map[pointX][pointY - 2] == 1) {
						map[pointX][pointY - 1] = 0;
						pointY -= 2;
						break;//u
					}
				}
			case 1:
				if (pointY != height - 2) {
					if (map[pointX][pointY + 2] == 1) {
						map[pointX][pointY + 1] = 0;
						pointY += 2;
						break;//d
					}
				}
			case 2:
				if (pointX != 1) {
					if (map[pointX - 2][pointY] == 1) {
						map[pointX - 1][pointY] = 0;
						pointX -= 2;
						break;//l
					}
				}
			case 3:
				if (pointX != width - 2) {
					if (map[pointX + 2][pointY] == 1) {
						map[pointX + 1][pointY] = 0;
						pointX += 2;
						break;//r
					}
				}
			}
			map[pointX][pointY] = 0;
			dig();
		} else if (isAbleDig()) {
			pointX = randomPos(width);
			pointY = randomPos(height);
			dig();
		}

	}

	private boolean isAbleDig() { //See if there is still a place to dig
		boolean result;
		int cnt = 0;
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				if (x % 2 != 0 && y % 2 != 0) {

					if (map[x][y] != 0) {
						cnt++;
					}
				}
			}
		}
		if (cnt == 0) {
			result = false;
		} else {
			result = true;
		}
		return result;
	}

	private boolean isAbleContinueDig() {//Determine if there is any space left to dig in all directions

		if (pointY != 1) {
			if (map[pointX][pointY - 2] == 1) {
				return true;
			}
		}
		if (pointY != height - 2) {
			if (map[pointX][pointY + 2] == 1) {
				return true;
			}
		}
		if (pointX != 1) {
			if (map[pointX - 2][pointY] == 1) {
				return true;
			}
		}
		if (pointX != width - 2) {
			if (map[pointX + 2][pointY] == 1) {
				return true;
			}
		}
		return false;
	}

	public void show() {
		for (int y = 0; y < map[0].length; y++) {
			System.out.println("");
			for (int x = 0; x <map.length; x++) {

				if (map[x][y] == 1) {
					System.out.print("##");
				} else {
					System.out.print("  ");
				}
			}
		}
	}
	public byte[][] getMaze() {
		return map;
	}

}

What I noticed when it was completed

I wonder if I can make it a little shorter. Also, maybe because I'm calling the digging function recursively, I get a stackoverflow error just by trying to create a slightly larger maze. I will edit this article as soon as I fix it. I am vaguely thinking that it seems quite so if I use a while statement.

What you might do if you feel like it

--Start and goal are set automatically --Find the shortest path --Make it playable as a game.

My environment

OS :Windows10 IDE:Eclipse 2020-03

Recommended Posts

[Java] I tried to make a maze by the digging method ♪
I tried to make a login function in Java
I tried to explain the method
[Small story] I tried to make the java ArrayList a little more convenient
I tried to make a client of RESAS-API in Java
[Java] I tried to make a rock-paper-scissors game that beginners can run on the console.
I tried to make Basic authentication with Java
java I tried to break a simple block
I did Java to make (a == 1 && a == 2 && a == 3) always true
I tried hitting a Java method from ABCL
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I tried to break a block with java (1)
I tried to make a talk application in Java using AI "A3RT"
A story when I tried to make a video by linking Processing and Resolume
I tried to decorate the simple calendar a little
I tried to create a Clova skill in Java
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
I tried to implement the Euclidean algorithm in Java
I tried to make a program that searches for the target class from the process that is overloaded with Java
I tried to operate home appliances by holding a smartphone over the NFC tag
I tried to implement the like function by asynchronous communication
I tried to create a java8 development environment with Chocolatey
I tried to modernize a Java EE application with OpenShift.
[JDBC] I tried to access the SQLite3 database from Java.
I tried to summarize the basics of kotlin and java
I want to make a list with kotlin and java!
I want to call a method and count the number
I just wanted to make a Reactive Property in Java
I want to make a function with kotlin and java!
I tried using Hotwire to make Rails 6.1 scaffold a SPA
I tried to make Java Optional and guard clause coexist
Create a method to return the tax rate in Java
I tried to build the environment little by little using docker
I tried to convert a string to a LocalDate type in Java
Make a digging maze with Ruby2D
How to make a Java container
I tried to interact with Java
I tried the Java framework "Quarkus"
I tried to summarize Java learning (1)
I tried to summarize Java 8 now
How to make a Java array
I tried to make a product price comparison tool of Amazon around the world with Java, Amazon Product Advertising API, Currency API (2017/01/29)
I tried Tribuo published by Oracle. Tribuo --A Java prediction library (v4.0)
I tried to illuminate the Christmas tree in a life game
[Unity] I tried to make a native plug-in UniNWPathMonitor using NWPathMonitor
Connecting to a database with Java (Part 1) Maybe the basic method
I tried to translate the error message when executing Eclipse (Java)
I tried to understand how the rails method "redirect_to" is defined
I made a method to ask for Premium Friday (Java 8 version)
I tried to make an Android application with MVC now (Java)
I tried to understand how the rails method "link_to" is defined
I tried to summarize the methods of Java String and StringBuilder
I tried to move the Java compatible FaaS form "Fn Project"
I tried to display the calendar on the Eclipse console using Java.
I tried to make a group function (bulletin board) with Rails
[VBA] I tried to make a tool to convert the primitive type of Entity class generated by Hibernate Tools to the corresponding reference type.
How to make a Java calendar Summary
I tried to make it possible to set the delay for the UDP client of Android by myself
I tried to summarize the methods used
I tried the new era in Java
[Java] How to use the toString () method