日常の進捗

主に自分のための,行為とその習慣化の記録

Mod:Coding Challenge #10.1: Maze Generator with p5.js - Part 1

p5.jsで書かれていたものをProcessingで書いてみた。なんとなく。先取りして迷路作るところまで行けた。グリッドに切り分けたセルごとに、上下左右の4つの壁を設定して、Booleanで壁があるかないかをプロパティで持たせてる。変数名とかは自分がわかりやすいように少し変えた。

float cols, rows;
float grid = 40;
ArrayList<Cell> cells = new ArrayList<Cell>();
void setup() {
  size(800, 800);
  colorMode(HSB, 360, 100, 100);
  cols = floor(width/grid);
  rows = floor(height/grid);
  for (int j = 0; j < rows; j++) {
    for (int i = 0; i < cols; i++) {
      Cell cell = new Cell(i, j);
      cells.add(cell);
    }
  }
}

void draw() {
  background(0, 0, 25);
  for (Cell cell : cells) {
    cell.draw();
  }
}

class Cell {
  float i, j;
  Boolean[] walls = new Boolean[4];
  Cell(float _i, float _j) {
    i = _i;
    j = _j;
    for (int k = 0; k < walls.length; k++) {
      if (random(1) < 0.5) {
        walls[k] = true;
      } else {
        walls[k] = false;
      }
    }
  }
  void draw() {
    float x = i * grid;
    float y = j * grid;
    stroke(0, 0, 100);
    if (walls[0]) {
      line(x, y, x+grid, y);
    }
    if (walls[1]) {
      line(x+grid, y, x+grid, y+grid);
    }
    if (walls[2]) {
      line(x+grid, y+grid, x, y+grid);
    }
    if (walls[3]) {
      line(x, y, x, y+grid);
    }
  }
}

リファレンス