mardi 9 mai 2017

Randomly generating a puzzle grid

We're working on a school project to make a maze where you cross a grid with some walls, covering every tile once on your way to the finish tile, and you lose when you get stuck or get to the finish tile without filling the grid. We've developed and test grid by manually using some types from an enum and setting them into a list. Here's an image of our prototype grid

I've been wondering however if it would be possible to randomly generate the grid based on an input of dimensions for the size of the square playing field. I am however unable to find a way to create a complicated grid or prevent it from looping out of control.

Any Ideas for an algorithm?

Here is the class that generates the grid so far:

public int gridWidth, gridHeight;
public int gridsize;
public int finish;
private Color color1, color2;
public Tile[] data;
private Image BrickMario;
private int[] level;

public Grid(int gridsize, int w, int h, int[] level) {

    this.gridWidth = w;
    this.gridHeight = h;
    this.gridsize = gridsize;
    this.level = level;

    finish = gridsize - 1;

    data = new Tile[this.gridsize * this.gridsize];

    for (int i = 0; i <= this.gridsize * this.gridsize - 1; i++) {

        if (i % this.gridsize == 0) {
            data[i] = new Tile(0, 50+(this.gridHeight / this.gridsize) * (i / this.gridsize),
                    this.gridWidth / this.gridsize, this.gridHeight / this.gridsize, color1, color2);
        } else {
            data[i] = new Tile((i % this.gridsize) * (this.gridWidth / this.gridsize),
                    50 + ((i - (i % this.gridsize)) / this.gridsize) * (this.gridHeight / this.gridsize),
                    this.gridWidth / this.gridsize, this.gridHeight / this.gridsize, color1, color2);
        }
    }
    BrickMario = new ImageIcon(getClass().getResource("BrickMario.png")).getImage();
}

public void drawblock(Graphics g, int x, int y, int w, int h, Color color1, Color color2) {
    g.setColor(color2);
    g.fillRect(x, y, w, h);
    g.setColor(color1);
    g.drawRect(x, y, w, h);

}

public void drawFinish(Graphics g, int x, int y, int w, int h) {
    g.setColor(Color.BLACK);
    for (int i = 0; i < 25; i++) {
        if (i % 2 == 0) {
            g.fillRect(x + (w / 5) * (i % 5), y + (h / 5) * ((i - (i % 5)) / 5), (w / 5) + 2, h / 5);
        }
    }
}

public void drawPlayer(Graphics g, int x, int y, int r, Color color) {
    g.setColor(color);
    g.fillOval(x + r / 6, y + r / 6, 2 * r / 3, 2 * r / 3);
}

public void draw(Graphics g) {
    for (int i = 0; i <= gridsize * gridsize - 1; i++) {
        drawblock(g, data[i].tileX, data[i].tileY, data[i].tileWidth, data[i].tileHeight, data[i].color1,
                data[i].color2);
    }
    drawFinish(g, data[finish].tileX, data[finish].tileY, data[finish].tileWidth, data[finish].tileHeight);
    for (int i = 0; i < this.gridsize * this.gridsize; i++) {
        if (level[i] == 1) {
            g.drawImage(BrickMario, data[i].tileX, data[i].tileY, this.gridWidth / gridsize,
                    this.gridHeight / this.gridsize, this);
        }
    }
}

private static final long serialVersionUID = 8730416137113078832L;




Aucun commentaire:

Enregistrer un commentaire