Simple Java 2d array maze sample

前端 未结 3 1123
轻奢々
轻奢々 2021-02-03 15:46

I am working or understanding how to create a simple java 2d maze that should look like this:

int [][] maze = 
{ {1,1,1,1,1,1,1,1,1,1,1         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-03 16:15

    If I understand your question correctly, what I would do is: 1. create a board of a specific size (change all the coordinates to your desired number - in your example '1'). I wouldn't use a recursive function, because you will probably end up drawing the whole board (think about what will make the recursion stop).

    you can create a function that receives a starting coordination, an ending coordination, and the array (the board). pseudo code of the function: set a variable for the next direction of painting (set it to the starting coordination). paint the next coordination 0. while the next coordination != to the ending coordination: paint the next coordination 0. use Random to set the coordination to one of the 4 directions.

    you should add limits (if the next coordination is a painted one/the border of the maze etc... chose a different coordination). good luck!

提交回复
热议问题