Android Open Source - hiddenmarble Maze Helper






From Project

Back to project page hiddenmarble.

License

The source code is released under:

Apache License

If you think the Android project hiddenmarble 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

package com.mygdx.hiddenmarble.utils;
//from   w w w  . j ava  2 s  .  c  o  m
import java.io.Serializable;

import maze.ImmutablePosition;
import maze.Maze;
import maze.Position;
import maze.RecursiveBacktracker;
import maze.TileMaze;

import com.badlogic.gdx.math.MathUtils;

/**
 * Creates mazes. The mazes will have predetermined exit positions but will vary
 * otherwise.
 */
public final class MazeHelper {
    private static final int SMALL_WIDTH = 4;
    private static final int SMALL_HEIGHT = 5;

    private static final int MEDIUM_WIDTH = 5;
    private static final int MEDIUM_HEIGHT = 6;

    private static final int LARGE_WIDTH = 7;
    private static final int LARGE_HEIGHT = 8;

    private MazeHelper() {
    }

    /**
     * Creates a 4 x 5 maze (9 x 11 in tile representation).
     * 
     * @return maze definition (tile maze object and start/exit positions)
     */
    public static MazeDef getSmallMaze() {
        return getMaze(SMALL_WIDTH, SMALL_HEIGHT);
    }

    /**
     * Creates a 5 x 6 maze (11 x 13 in tile representation).
     * 
     * @return maze definition (tile maze object and start/exit positions)
     */
    public static MazeDef getMediumMaze() {
        return getMaze(MEDIUM_WIDTH, MEDIUM_HEIGHT);
    }

    /**
     * Creates a 7 x 8 maze (15 x 17 in tile representation).
     * 
     * @return maze definition (tile maze object and start/exit positions)
     */
    public static MazeDef getLargeMaze() {
        return getMaze(LARGE_WIDTH, LARGE_HEIGHT);
    }

    /**
     * Creates a maze with the specified dimensions.
     * 
     * @param  width the width of the maze
     * @param  height the height of the maze
     * @return maze definition
     */
    private static MazeDef getMaze(int width, int height) {
        Position start = new ImmutablePosition(MathUtils.random(0, width - 1), 0);
        Position startTile = getTileAt(start);
        Position bottom = new ImmutablePosition(width / 2, height - 1);
        Position bottomTile = getTileAt(bottom);
        Position exitTile = new ImmutablePosition(bottomTile.getX(), bottomTile.getY() + 1);
        Maze maze = new RecursiveBacktracker(width, height);
        maze.generate();
        return new MazeDef(new TileMaze(maze), startTile, exitTile);
    }

    /** Converts a wall-based maze position into a tile-based one. */
    private static Position getTileAt(Position p) {
        return new ImmutablePosition(p.getX() * 2 + 1, p.getY() * 2 + 1);
    }

    /** Contains a tile maze object and start/exit positions. */
    public static class MazeDef implements Serializable {
        private static final long serialVersionUID = 7280561842114088170L;
        
        public final TileMaze maze;
        public final Position start;
        public final Position exit;

        public MazeDef(TileMaze maze, Position start, Position exit) {
            this.maze = maze;
            this.start = start;
            this.exit = exit;
        }
    }
}




Java Source Code List

com.mygdx.hiddenmarble.android.AndroidLauncher.java
com.mygdx.hiddenmarble.desktop.DesktopLauncher.java
com.mygdx.hiddenmarble.entities.AbstractDynamicEntity.java
com.mygdx.hiddenmarble.entities.AbstractEntity.java
com.mygdx.hiddenmarble.entities.Borders.java
com.mygdx.hiddenmarble.entities.DefaultBorders.java
com.mygdx.hiddenmarble.entities.DefaultMarble.java
com.mygdx.hiddenmarble.entities.DefaultMazeBox.java
com.mygdx.hiddenmarble.entities.DynamicEntity.java
com.mygdx.hiddenmarble.entities.Entity.java
com.mygdx.hiddenmarble.entities.Marble.java
com.mygdx.hiddenmarble.entities.Material.java
com.mygdx.hiddenmarble.entities.MazeBox.java
com.mygdx.hiddenmarble.entities.MazeFixtureDef.java
com.mygdx.hiddenmarble.ui.GameScreen.java
com.mygdx.hiddenmarble.ui.HiddenMarble.java
com.mygdx.hiddenmarble.ui.WorldRenderer.java
com.mygdx.hiddenmarble.utils.Assets.java
com.mygdx.hiddenmarble.utils.BodyHelper.java
com.mygdx.hiddenmarble.utils.MazeHelper.java
com.mygdx.hiddenmarble.utils.SaveState.java
com.mygdx.hiddenmarble.utils.Serialization.java
com.mygdx.hiddenmarble.utils.SpriteHelper.java
com.mygdx.hiddenmarble.world.GameWorldAdapter.java
com.mygdx.hiddenmarble.world.GameWorldListener.java
com.mygdx.hiddenmarble.world.GameWorld.java