By Sumit Kumar


2019-09-11 07:56:50 8 Comments

enter image description here

Anyone can help me to draw this image on canvas with algorithm or code. I have created maze on canvas using YouTube tutorial but it doesn't look like above image.

After developing with help of tutorial, i am getting below image. enter image description here

Edit 2: Anyone guide me to move ball smoothly by detecting obstacles from following code. Thanks in advance.

Code

public class MazaGame extends View {
    private Cell[][] cells;
    private static final int COLS = 7, ROWS = 10;
    private static final float WALL_THICKNESS = 4;
    private float cellSize,hMargin,vMargin;
    private Paint wallPaint;
    private Random random;

    public MazaGame(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        wallPaint = new Paint();
        wallPaint.setColor(Color.BLACK);
        wallPaint.setStrokeWidth(WALL_THICKNESS);

        random = new Random();

        createMaze();
    }

    private Cell getNeighbor(Cell cell){
        ArrayList<Cell> neighbors = new ArrayList<>();

        //left neighbor
        if(cell.col >0)
            if(!cells[cell.col-1][cell.row].visited)
                neighbors.add(cells[cell.col-1][cell.row]);

        //right neighbor
        if(cell.col < COLS-1)
            if(!cells[cell.col+1][cell.row].visited)
                neighbors.add(cells[cell.col+1][cell.row]);

        //top neighbor
        if(cell.row >0)
            if(!cells[cell.col][cell.row-1].visited)
                neighbors.add(cells[cell.col][cell.row-1]);

        //bottom neighbor
        if(cell.row < ROWS-1)
            if(!cells[cell.col][cell.row+1].visited)
                neighbors.add(cells[cell.col][cell.row+1]);

        if(neighbors.size()>0) {
            int index = random.nextInt(neighbors.size());
            return neighbors.get(index);
        }
        return null;
    }

    private void removeWall(Cell current, Cell next){
        if(current.col == next.col && current.row == next.row+1){
            current.topWall = false;
            next.bottomWall = false;
        }

        if(current.col == next.col && current.row == next.row-1){
            current.bottomWall = false;
            next.topWall = false;
        }

        if(current.col == next.col+1 && current.row == next.row){
            current.leftWall = false;
            next.rightWall = false;
        }

        if(current.col == next.col-1 && current.row == next.row){
            current.rightWall = false;
            next.leftWall = false;
        }
    }

    private void createMaze() {
        Stack<Cell> stack = new Stack<>();
        Cell current, next;

        cells = new Cell[COLS][ROWS];

        current = cells[0][0];
        current.visited = true;

        for (int x = 0; x < COLS; x++) {
            for (int y = 0; y < ROWS; y++) {
                cells[x][y] = new Cell(x, y);
            }
        }
        do {
            next = getNeighbor(current);
            if (next != null) {
                removeWall(current, next);
                stack.push(current);
                current = next;
                current.visited = true;
            } else
                current = stack.pop();
        }while (!stack.empty());
    }
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.RED);

        int width = getWidth();
        int height = getHeight();

        if(width/height < COLS/ROWS)
            cellSize = width/(COLS+1);
        else
            cellSize = height/(ROWS+1);

        hMargin = (width-COLS*cellSize)/2;
        vMargin = (height-ROWS*cellSize)/2;

        canvas.translate(hMargin,vMargin);

        for(int x=0;x<COLS;x++){
            for(int y=0;y<ROWS;y++){
                if(cells[x][y].topWall)
                    canvas.drawLine(
                            x*cellSize,
                            y*cellSize,
                            (x+1)*cellSize,
                            y*cellSize,
                            wallPaint);

                if(cells[x][y].leftWall)
                    canvas.drawLine(
                            x*cellSize,
                            y*cellSize,
                            x*cellSize,
                            (y+1)*cellSize,
                            wallPaint);

                if(cells[x][y].bottomWall)
                    canvas.drawLine(
                            x*cellSize,
                            (y+1)*cellSize,
                            (x+1)*cellSize,
                            (y+1)*cellSize,
                            wallPaint);

                if(cells[x][y].rightWall)
                    canvas.drawLine(
                            (x+1)*cellSize,
                            y*cellSize,
                            (x+1)*cellSize,
                            (y+1)*cellSize,
                            wallPaint);
            }
        }
    }

    private class Cell{
        boolean
            topWall = true,
            rightWall = true,
            bottomWall = true,
            leftWall = true,
            visited = false;

        int col,row;

        public Cell(int col, int row) {
            this.col = col;
            this.row = row;
        }
    }
}


public void newTry(SensorEvent event) {

    x = x - (int) event.values[0];
    y = y + (int) event.values[1];


    // right side x -1 left side 1
    //up side y -1 down 1


    Log.d("newTry: x", "" + (int) event.values[0]);
    Log.d("newTry: y", "" + (int) event.values[1]);

    //Make sure we do not draw outside the bounds of the view.
    //So the max values we can draw to are the bounds + the size of the circle
    if (x <= 0 + CIRCLE_RADIUS) {
        x = 0 + CIRCLE_RADIUS;
    }
    if (x >= viewWidth - CIRCLE_RADIUS) {
        x = cellSize - CIRCLE_RADIUS;
    }
    if (y <= 0 + CIRCLE_RADIUS) {
        y = 0 + CIRCLE_RADIUS;
    }
    if (y >= viewHeight - CIRCLE_RADIUS) {
        y = viewHeight - CIRCLE_RADIUS;
    }

    xPosition = x;
    yPosition = y;

    oldXPosition = x;
    oldYPosition = y;
    //int cellx = (int) (xPosition / cellSize);
    //int celly = (int) (yPosition / cellSize);
    boolean move = canBallMove(xPosition, yPosition, event);
    if (move)
        invalidate();
    else{
      xPosition =  oldXPosition;
      yPosition = oldYPosition;
    }
}

private boolean canBallMove(float newX, float newY, SensorEvent event) {
    boolean isMove = false;
    int oldCellX = (int) ((xPosition + CIRCLE_RADIUS) / (cellSize+1));
    int oldCellY = (int) ((yPosition + CIRCLE_RADIUS) / (cellSize+1));
    Log.d("newTry:old x", "" + oldCellX);
    Log.d("newTry:old y", "" + oldCellY);
    if(oldCellX >0 && oldCellY>0) {
        if ((int) event.values[0] < 0) {
            isMove = cells[oldCellX - 1][oldCellY - 1].rightWall;
        }
        if ((int) event.values[0] > 0) {
            isMove = cells[oldCellX - 1][oldCellY - 1].leftWall;
        }
        if ((int) event.values[1] < 0) {
            isMove = cells[oldCellX - 1][oldCellY - 1].topWall;
        }
        if ((int) event.values[1] > 0) {
            isMove = cells[oldCellX - 1][oldCellY - 1].bottomWall;
        }
        player = cells[oldCellX - 1][oldCellY - 1];
    }
    return isMove;
}

above code doesn't detect obstacle and ball move is not smooth. Any help will great.

Can i modify this code to generate https://i.stack.imgur.com/xQwvt.jpg maze image.

0 comments

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Maze drawing with canvas

31 Answered Questions

[SOLVED] Android Drawing Separator/Divider Line in Layout?

  • 2011-02-19 08:59:28
  • Androider
  • 643486 View
  • 684 Score
  • 31 Answer
  • Tags:   android layout draw

9 Answered Questions

[SOLVED] Representing and solving a maze given an image

1 Answered Questions

[SOLVED] Generating a maze with Javascript & Canvas

1 Answered Questions

Java simple maze advice

  • 2017-02-09 14:26:58
  • Kristo
  • 237 View
  • 1 Score
  • 1 Answer
  • Tags:   java maze

1 Answered Questions

[SOLVED] Undo/Redo not working in android Canvas

14 Answered Questions

[SOLVED] Programming theory: Solve a maze

  • 2010-06-22 22:17:13
  • Max Ruf
  • 75303 View
  • 67 Score
  • 14 Answer
  • Tags:   algorithm maze

1 Answered Questions

[SOLVED] Best way to determine screen dimensions for signature capture stroke width?

3 Answered Questions

[SOLVED] simple maze game in java

  • 2013-04-16 07:04:02
  • maggie
  • 9058 View
  • 0 Score
  • 3 Answer
  • Tags:   java maze

Sponsored Content