Critique My Code Please {Contains Java}

Discuss game development design and post your game ideas

Post » Tue Dec 17, 2013 1:11 am

Anywho, I was trying to warn in my topic line that I am posting Java Syntax and not a CapX file.

I have this idea for a game. I needed a maze generation algorithm. I was trying to prototype one in Java because I'm more comfortable in that language and can work through the logic. I was trying to do something that didn't require recursion or a lot of loops and variable to make it easier to transfer to C2. That proved harder than I thought it would. I kind of stood stumped at the screen for a while.

I came up with the code below. It completely sucks for good maze generation but it seems to create the effect I was actually after. It was a happy mistake. I wanted to create a maze-like top down view environment but include larger rooms or caverns. This seemed to actually randomly create it rather well. I ran the algorithm a crap ton of times and leveled off the choke point in the loop to where it seems to always create a path through at any dimension.

I have to design a checking algorithm yet that will ensure there is a path through, and a lot of other stuff for the game, but I wanted opinions of the algorithm itself before I put it together in C2. I'll have a couple of small extra steps in C2 to make a really rough prototype.

Let me know what you think.

I threw the code up on my blog quick essentially echoing what I said here. Anyway, here's a link to the article because it has syntax highlighting so it'll be easier to read. The same code is copied below to. Link

Here's the code:

import java.util.Random;

public class Main {

     //These variables define the size of the maze.
     //Change only these to change the size of the maze
     private static final int ROWSIZE = 40;
     private static final int COLUMNSIZE = 40;
     public static void main(String[] args) {
          //variable declarations
          int[][] maze;
          int randX = 0;
          int randY = 0;
          int choke = 0;
          int checkSurroundings = 0;
          Random rand = new Random();
          maze = new int[ROWSIZE][COLUMNSIZE];
          //Initialize maze array to all zeros (make every space a non-moveable area)
          for (int x = 0;x < ROWSIZE; x++){
               for (int y = 0; y < COLUMNSIZE; y++){
                    maze[x][y] = 0;
          //Define outside walls
          //Construct 2 specific:
          //We will use the number two to define all outside walls of maze so the game engine
          //knows where to put these special tiles
          for (int x = 0; x < ROWSIZE; x++){
               maze[x][0] = 2;
               maze[x][COLUMNSIZE - 1] = 2;
          for (int x = 0; x < COLUMNSIZE; x++){
               maze[0][x] = 2;
               maze[ROWSIZE - 1][x] = 2;
          //Maze Generation
          randX = rand.nextInt(ROWSIZE - 10) + 5;
          maze[randX][0] = 1; //Mark beginning
          maze[randX][1] = 1;
          randX = rand.nextInt(ROWSIZE - 10) + 5;
          maze[randX][COLUMNSIZE - 1] = 1; //Mark ending
          maze[randX][COLUMNSIZE - 2] = 1;
          do {
               randX = rand.nextInt(ROWSIZE - 2) + 1;
               randY = rand.nextInt(COLUMNSIZE - 2) + 1;
               checkSurroundings = maze[randX - 1][randY] + maze[randX + 1][randY] + maze[randX][randY - 1] + maze[randX][randY + 1];          
               if (checkSurroundings < 4){
                    maze[randX][randY] = 1;
                    choke = 0;
               } else {
                    choke ++;
          } while (choke < 4);
          //Print results of the maze array
          for (int x = 0; x < ROWSIZE; x++){
               for (int y = 0; y < COLUMNSIZE; y++){
                    if (maze[x][y] == 0 || maze[x][y] == 2)
                         System.out.printf("%d ", maze[x][y]);
                    if (maze[x][y] == 1)
                         System.out.printf("# ");
Come visit and play Blast Box!
Posts: 205
Reputation: 1,349

Post » Wed Dec 18, 2013 9:41 am

Well, like they say: "If it's not broken, don't fix it"

If it works like you intend, then it's fine.

the code is clean and easily be implemented in construct. The only thing I could recommend is to replace some numbers with constant variables like you did for ROWSIZE and COLUMNSIZE, this way you can easily modify the maze properties (proximity of starting point from borders, surrounding check radius, choke size) by just changing a couple variables
Posts: 132
Reputation: 2,637

Return to Game Development, Design & Ideas

Who is online

Users browsing this forum: No registered users and 0 guests