Android Open Source - Droid-Maze Kruskal Maze Generator






From Project

Back to project page Droid-Maze.

License

The source code is released under:

Apache License

If you think the Android project Droid-Maze listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2011 Massimo Gaddini/*from w  ww.  j  a v a  2 s  .  c om*/
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.  
 */
package com.sgxmobileapps.droidmaze.maze.generator;


import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.TreeSet;

/**
 * @author Massimo Gaddini
 */
public class KruskalMazeGenerator implements MazeGenerator {
    
    class KruskalCell extends MazeCell implements Comparable<KruskalCell> {

        private TreeSet<KruskalCell> mSet;

        KruskalCell(int x, int y) {
            super(x, y);
        }

        public void init() {
            super.init();
            mSet = new TreeSet<KruskalCell>();
            mSet.add(this);
        }

        public boolean joinSet(KruskalCell otherCell) {
            if (!mSet.contains(otherCell)) {
                mSet.addAll(otherCell.mSet);
                
                Iterator<KruskalCell> it = otherCell.mSet.iterator();
                while (it.hasNext()) {
                    KruskalCell cell = it.next();
                    cell.mSet = mSet;
                }
                
                return true;
            }

            return false;
        }

        public int compareTo(KruskalCell another) { 
          if (getX() < another.getX())
            return -1; 
          else if (getX() > another.getX()) 
            return 1; 
          else { 
            if (getY() < another.getY()) 
              return -1; 
            else if (getY() > another.getY()) 
              return 1;
            } 
          return 0; 
        }
         
        @Override 
        public boolean equals(Object obj) { 
          if (this == obj) 
            return true;
         
          if (obj == null) 
            return false;
         
          if (getClass() != obj.getClass()) 
            return false;
         
          KruskalCell other = (KruskalCell) obj;
         
          if ((getX() == other.getX()) && (getY() == other.getY()) ) 
            return true;
 
          return false; 
        } 
    }

    private KruskalCell[][] mGrid   = null;
    List<Integer>           mWalls  = null;
    private Random          mRandom = new Random();
    private int             mHeight = 0;
    private int             mWidth  = 0;

    private void init(int height, int width) {

        mRandom.setSeed(System.currentTimeMillis());

        if ( ( height != mHeight ) || ( width != mWidth )) {
            mGrid = new KruskalCell[height][width];
        }

        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                if (mGrid[i][j] == null) {
                    mGrid[i][j] = new KruskalCell(i, j);
                }

                mGrid[i][j].init();
            }
        }

        Integer[] walls = new Integer[height * ( width - 1 ) + width * ( height - 1 )];
        for (int i = 0; i < walls.length; i++) {
            walls[i] = i;
        }

        mWalls = Arrays.asList(walls);
        Collections.shuffle(mWalls, mRandom);

        this.mHeight = height;
        this.mWidth = width;
    }

    public MazeCell[][] generate(int height, int width) {
        int i, j, is, js;

        init(height, width);

        for (Integer wall : mWalls) {
            i = wall / ( 2 * mWidth - 1 );
            j = wall % ( 2 * mWidth - 1 );

            if (j >= ( mWidth - 1 )) {
                /* south wall */
                is = i + 1;
                j -= ( mWidth - 1 );
                js = j;
            } else {
                is = i;
                js = j + 1;
            }

            if ( ( is >= height ) || ( js >= width )) {
                continue;
            }

            if (mGrid[i][j].joinSet(mGrid[is][js])) {
                mGrid[i][j].openTo(mGrid[is][js]);
                mGrid[is][js].openTo(mGrid[i][j]);
            }
        }

        return mGrid;
    }
}




Java Source Code List

com.sgxmobileapps.droidmaze.datastore.DroidMazeDbAdapter.java
com.sgxmobileapps.droidmaze.datastore.DroidMazeDbMetadata.java
com.sgxmobileapps.droidmaze.game.GameLevelManager.java
com.sgxmobileapps.droidmaze.game.model.GamePlayerProfile.java
com.sgxmobileapps.droidmaze.maze.generator.BacktrackingMazeGenerator.java
com.sgxmobileapps.droidmaze.maze.generator.IterativeBacktrackingMazeGenerator.java
com.sgxmobileapps.droidmaze.maze.generator.KruskalMazeGenerator.java
com.sgxmobileapps.droidmaze.maze.generator.MazeCell.java
com.sgxmobileapps.droidmaze.maze.generator.MazeGenerator.java
com.sgxmobileapps.droidmaze.maze.generator.PrimMazeGenerator.java
com.sgxmobileapps.droidmaze.ui.MainMenuActivity.java
com.sgxmobileapps.droidmaze.ui.MainSplashActivity.java
com.sgxmobileapps.droidmaze.ui.MazeActivity.java
com.sgxmobileapps.droidmaze.ui.animator.MultiSlideMenuAnimator.java
com.sgxmobileapps.droidmaze.ui.shape.ComplexShape.java
com.sgxmobileapps.droidmaze.ui.shape.LevelBarShape.java
com.sgxmobileapps.droidmaze.ui.shape.LoadingShape.java
com.sgxmobileapps.droidmaze.ui.shape.MazeShape.java
com.sgxmobileapps.droidmaze.util.ActivityUtils.java