Wednesday, January 22, 2014

[Java] Maze

The first project I am going to post is a small maze game I made in Java. (specifically Processing, a tool that adds high level functions to Java) It has a few interesting parts to it to create the visual effects I wanted. Here are a couple screenshots (They may be a bit dark on some monitors):

The player can move a blue flame through the maze, a 30x30 grid of cells which each have 4 open or closed sides,  with the WASD keys. The maze is randomly generated when the program starts. As you might have noticed, the visibility in this maze is quite low; the light from the blue flame only reaches so far and you cannot see around the corners.

The first thing that happens in the program is the generation of the maze. This is done recursively, and starts at the end (this makes it so the end is a dead end). The recursive algorithm I used to generate the maze goes like this:
  1. Mark the current cell as 'visited'
  2. Recurse on all directly adjacent cells that have not been visited in a random order
    1. Remove walls between this and the next cell
  3. Return when all adjacent cells have been visited
Here's a quick example of the algorithm on a 5x5 grid:


The red signifies the places the algorithm has already visited, and the arrows within them show which neighbor they chose next (which cell the recursive function chose at random). The blue cell is the current cell, with the bold outlines showing where it's neighbors are. Because two of it's neighbors have been visited before (East and South of current), the current function only considers the North and West neighbors (green), and visits them in a random order, in this case North and then West. Note that it may never travel from the current cell to the West neighbor, as it may visit it later in the path before it has to return to go down that path, say, if it continues down this path:


In this example, the algorithm chose a path that blocks the other cell we looked at from traveling West before it had the chance to explore it. That function would have had the chance to explore only if the the path had met a dead end somewhere and had returned to that point while that spot remained unvisited. This causes a fork in the path, as seen in the next pictures as the current cell reaches a dead end, and returns to the most recent cell that had an unvisited neighbor:




That's all for this post, in my next post about this little project I will talk about the shadows and how they are computed. If you want to look at my code, the project can be downloaded from this link, however, note that you will need Processing to run it.
















Monday, December 23, 2013

Hello World!

Hello World!  I decided to make this blog to keep track of the projects I have done in the past few years as well as let other people check out the code, give suggestions or just play around with the projects. I will post screenshots, video footage, and code from my more interesting projects and any updates I do to them. I will try to make any code I post as clear as possible and put comments in the important places. I hope my projects are interesting/useful to you!
Happy Holidays!
Sam Blazes