current cell (maze generator)

edited August 2016 in Questions about Code

trying to recreate daniel's maze generator from his p5 video into processing. stuck at the part where he creates "var current" and sets an object to current. i cant figure out what variable to make current. btw using 2d array for grid.

[mod edit, copied video link from comment]

Answers

  • apparently it was posted in p5.js, swapped it to "how to..." dunno how it got to "p5.js" using processing. watching the video where he uses p5. just confused on what variable he makes "current" in the video. >.<

  • edited August 2016

    Oops! It's about p5 not p5.js. Sorry! b-(
    So the most correct section would be "Library Questions" then. Since it's about the video library.

  • yes i guess so, just dl'd processing not to long ago and still learning about it. and havent programmed in a while so this is my coming back to programming lol. hopefully someone can help me figure it out =D

  • You can raise the chances by posting the link for the video and your own attempt about it.

  • edited August 2016

    BtW, https://www.Reddit.com/r/processing/new/ is also a nice place to get help. O:-)

  • during first like.... 2 minutes

    https://youtube.com/watch?v=D8UgRyRnvXU&list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH&index=11

    //global initated variables
    int w = 40;
    int cols, rows;
    Cell [][] grid;
    
    void setup() {
      //draws the window
      size(400, 400);
    
      //sets number of columns = to width divided by object width
      cols = width/w;
    
      //sets number of rows = to height divided by object width
      rows = height/w;
    
      //sets a grid array to columns and rows (I.E. (5,3) would be column 5 row 3
      grid = new Cell[cols][rows];
    
      //creates the cells inside of the grid
      for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
          grid[i][j] = new Cell(i, j);
        }
      }
    
    }
    
    void draw() {
      //sets background color
      background(0);
    
      //displays the grid
      for (int i=0; i<grid.length; i++) {
        for (int j=0; j<grid.length; j++) {
          grid[i][j].display();
        }
      }
    }
    
    class Cell {
      //Cell Class variables
      int x, y;
      boolean walls[] = {true, true, true, true};
    
      //creates temporary variables when Cell is called to create grid
      Cell(int i, int j) {
        x = i*w;
        y = j*w;
      }
    
      void display() {
        //trbl = top right bottom left
    
        //creates walls for top side of cells
        noFill();
        stroke(255);
        if (walls[0]) {
          line(x, y, x+w, y);
        }
    
        //creates walls for right side of cells
        if (walls[1]) {
          line(x+w, y, x+w, y+w);
        }
    
        //creates walls for botttom side of cells
        if (walls[2]) {
          line(x+w, y+w, x, y+w);
        }
    
        //creates walls for left side of cells
        if (walls[3]) {
          line(x, y+w, x, y);
        }
      }
    }
    
  • edited August 2016

    That video is indeed using p5.js library, not Processing Java! 8-|

  • yes i know, im trying to make it in java lol. i got stuck at when he makes the variable "current" idk what type of variable to make it. thats the problem lol

  • edited August 2016

    I don't see any variable called current from the code you've posted.
    In Java, all variables have to be declared w/ the datatype it's meant to store.

  • in 1:50 to 2 minutes he declares "var current;"

    after that he makes it current.visited = true;

    i just cant figure out what variable datatype current is suppose to be

    (current being the current cell)

  • edited August 2016

    If you can't figure out the datatype of variable visited, how come you've hit the bull's eye about variable walls'? boolean walls[] = {true, true, true, true}; :>

  • edited August 2016

    Just a second... you mean variable current, which doesn't show up in your code here? Maybe it's of datatype Cell? :-/ Gotta watch that video in order to make sure then... :(|)

  • yeah, current is in his video, im trying to figure out what variable type to declare it lol. sorry. its almost 4 am and ive been drinking, so not the best way to word things on forums lol...

  • @ 1:55 he states: "So this variable current is the current Cell."
    Therefore current is of datatype Cell: Cell current;. :-B

  • yeah that works. but when i try to make it into a boolean its all like no. lol. like he does "current.visited = true;" and im trying to get that to work. blah maybe i need to sleep on it lol

  • edited August 2016

    Since variable current is of datatype Cell, it can only access members from that class.
    I can't spot any variable called visited inside your Cell definition btW. 8-X

  • yeah ive tried putting boolean visited inside of the cell class, but it just says "global variable 'visited' does not exist" when i do current.visited = true;

    i think i have to do an if statement or something. idk i appreciate the help though!

  • ok so progress, i got it to kind of work, but i need current to equal the current cell. i did "current = grid" just to get something working but how would i make it just the specific cell?

  • You should keeping reposting your current sketch. It's hard to guess what's your current situation otherwise.

  • yeah sorry, been busy lol. so i got it to work kind of. i know the problem is "current = grid;" but ive tried multiple combinations that i can think of and none of them select just the first cell. i havent messed with arrays in a while, and this is my first project in... 4 years? lol. so little bit rusty

    //global initated variables
    int w = 40;
    int cols, rows;
    Cell [][] grid;
    
    Cell current[][];
    
    void setup() {
      //draws the window
      size(400, 400);
    
      //sets number of columns = to width divided by object width
      cols = width/w;
    
      //sets number of rows = to height divided by object width
      rows = height/w;
    
      //sets a grid array to columns and rows (I.E. (5,3) would be column 5 row 3
      grid = new Cell[cols][rows];
    
      //creates the cells inside of the grid
      for (int j = 0; j < rows; j++) {
        for (int i = 0; i < cols; i++) {
          grid[i][j] = new Cell(i, j);
        }
      }
    
    
    
    
      current = grid;
    
    
    
    
    
    
    
    
    }
    
    void draw() {
      //sets background color
      background(0);
    
    
    
      //displays the grid
      for (int i=0; i<grid.length; i++) {
        for (int j=0; j<grid.length; j++) {
          grid[i][j].display();
        }
      }
    
    }
    
    class Cell {
      //Cell Class variables
      int x, y;
      boolean walls[] = {true, true, true, true};
      boolean visited = false;
    
    
      //creates temporary variables when Cell is called to create grid
      Cell(int i, int j) {
        x = i*w;
        y = j*w;
      }
    
      void display() {
        //trbl = top right bottom left
    
        //creates walls for top side of cells
        noFill();
        stroke(255);
        if (walls[0]) {
          line(x, y, x+w, y);
        }
    
        //creates walls for right side of cells
        if (walls[1]) {
          line(x+w, y, x+w, y+w);
        }
    
        //creates walls for botttom side of cells
        if (walls[2]) {
          line(x+w, y+w, x, y+w);
        }
    
        //creates walls for left side of cells
        if (walls[3]) {
          line(x, y+w, x, y);
        }
    
        //colors current cell
        if(current == grid) {
        fill(255,0,255,100);
        rect(x,y, w,w);
      }
      }
    }
    
  • to make it clear ( just incase of confusion) trying to make "current" the current cell, which should start at top left cell.

  • edited August 2016 Answer ✓
    • As stated, variable current points to the current active Cell.
    • And all of Cell objects are stored in your grid[][] 2D array.
    • But variable current is meant to point to just 1 Cell, not all of them!
    • Therefore variable current should be declared as datatype Cell, not Cell[][]. :-B
  • edited August 2016

    And given you're using a 2D array, it's advisable to read about it: O:-)
    https://Processing.org/tutorials/2darray/

  • i had looked at it the other night, but i had a few to drink so half the time just skipping things lol. thank you though. i think i got it ( it selected the cell) but mind if i keep this open for a sec just in case?

  • eh i think that got it. i can always post again if i get stuck. thank you. (btw i realized at the bottom i had

    if(current == grid) {
       fill(255,0,255,100);
       rect(x,y, w,w);
    }
    

    and it should have been

    if(visited) {
       fill(255,0,255,100);
       rect(x,y, w,w);
    }
    

    so it was always going to make it purple. but what you told me helped a lot. hopefully i can move on! thank you

Sign In or Register to comment.