1

So, I am creating a maze using some algorithm and in this maze I have cells which are basically div elements.

Initially all the cells have borders but as I proceed through the algorithm, I remove certain borders to achieve the maze. Now the problem is that while doing so, the corners of the borders don't join up properly, which can be seen in the image below. enter image description here

Here I have removed the right border for cell[0] and the left border for cell[1], and removed the bottom border for cell[0] and the top border for cell[2].

I just want to fix the corner of the borders, but I am unable to do so, I would be really thankful to any help.

cells = document.querySelectorAll(".cell"); //Removing the right-left border cells[0].style["border-right"] = "none"; cells[1].style["border-left"] = "none"; //Removing the top-down border cells[0].style["border-bottom"] = "none"; cells[2].style["border-top"] = "none";
*{ margin: 0; padding: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .row{ display: flex; justify-content: center; align-items: center; } .cell{ height: 50px; width: 50px; border-right: 3px solid black; border-bottom: 3px solid black; } .row .cell:first-of-type{ border-left: 3px solid black; } .row:first-of-type .cell{ border-top: 3px solid black; }
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <div class="row"> <div class="cell first"></div> <div class="cell second"></div> </div> <div class="row"> <div class="cell third"></div> <div class="cell forth"></div> </div> </body> </html>

Full code:

const nrows = 20; const ncols = 20; for (let i=0; i<nrows; i++){ let row = document.createElement("div"); row.classList.add("row"); document.body.appendChild(row); for (let j=0; j<ncols; j++){ let col = document.createElement("div"); col.classList.add("cell"); row.appendChild(col); } } class Cell{ constructor(i, j){ this.r = i; this.c = j; this.neighbors = []; this.walls = [true, true, true, true]; //Top Right Bottom Left this.visited = false; } show(){ if (!this.walls[0]) cellElements[this.r*ncols + this.c].style["border-top"] = "none"; if (!this.walls[1]) cellElements[this.r*ncols + this.c].style["border-right"] = "none"; if (!this.walls[2]) cellElements[this.r*ncols + this.c].style["border-bottom"] = "none"; if (!this.walls[3]) cellElements[this.r*ncols + this.c].style["border-left"] = "none"; if (this.visited) cellElements[this.r*ncols + this.c].style.background = "greenyellow"; } createNeighbors(){ if (this.r > 0) this.neighbors.push(grid[(this.r-1)*ncols + this.c]) //Top if (this.c < ncols-1) this.neighbors.push(grid[this.r*ncols + (this.c+1)]) //Right if (this.r < nrows-1) this.neighbors.push(grid[(this.r+1)*ncols + this.c]) //Bottom if (this.c > 0) this.neighbors.push(grid[this.r*ncols + (this.c-1)]) //Left } } let cellElements = document.querySelectorAll(".cell"); let grid = []; for (let i=0; i<nrows; i++){ for (let j=0; j<ncols; j++){ grid.push(new Cell(i,j)); } } for (let i=0; i<nrows; i++){ for (let j=0; j<ncols; j++){ grid[i*ncols + j].createNeighbors(); } } let current = grid[0]; let stack = []; //Maze Generator Algorithm function BacktrackingDFS(){ cellElements[current.r*ncols + current.c].style.background = "darkgreen"; if (!current.visited){ current.visited = true; stack.push(current); } let unvisited = []; for (let neigh of current.neighbors){ if (!neigh.visited){ unvisited.push(neigh); } } if (unvisited.length > 0){ let randomIndex = Math.floor(Math.random()*(unvisited.length)); let next = unvisited[randomIndex]; //Remove Walls if(current.r - next.r === 1){ //TOP current.walls[0] = false; next.walls[2] = false; //Fixing Corners }else if(current.c - next.c === -1){ //RIGHT current.walls[1] = false; next.walls[3] = false; }else if(current.r - next.r === -1){ //BOTTOM current.walls[2] = false; next.walls[0] = false; //Fixing Corners }else if(current.c - next.c === 1){ //LEFT current.walls[3] = false; next.walls[1] = false; } current = next; }else if(stack.length > 0){ current = stack.pop(); }else{ console.log("FINISHED!"); clearInterval(drawLoop); } } let drawLoop = setInterval(function(){ for (let i=0; i<nrows; i++){ for (let j=0; j<ncols; j++){ grid[i*ncols + j].show(); } } BacktrackingDFS(); },100);
*{ margin: 0; padding: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .row{ display: flex; justify-content: center; align-items: center; } .cell{ height: 20px; width: 20px; border-bottom: 2px solid black; border-right: 2px solid black; } .row .cell:first-of-type{ border-left: 2px solid black; } .row:first-of-type .cell{ border-top: 2px solid black; }
<!DOCTYPE html> <html> <head> <title>Maze Generator</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <script type="text/javascript" src="app.js"></script> </body> </html>

4 Answers 4

2

Have your cells overlap the same amount as your border size.

cells = document.querySelectorAll(".cell"); //Removing the right-left border cells[0].style["border-right"] = "none"; cells[1].style["border-left"] = "none"; //Removing the top-down border cells[0].style["border-bottom"] = "none"; cells[2].style["border-top"] = "none";
*{ margin: 0; padding: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .row{ display: flex; justify-content: center; align-items: center; } .cell{ height: 50px; width: 50px; margin-left: -3px; border-right: 3px solid black; border-bottom: 3px solid black; } .row .cell:first-of-type{ border-left: 3px solid black; } .row:first-of-type .cell{ border-top: 3px solid black; }
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <div class="row"> <div class="cell first"></div> <div class="cell second"></div> </div> <div class="row"> <div class="cell third"></div> <div class="cell forth"></div> </div> </body> </html>

Sign up to request clarification or add additional context in comments.

2 Comments

This fix is very specific to this scenario, it doesn't work in my actual code. Here's the link to my actual code: codepen.io/ssmkhrj/pen/YzwVmGB If you are moving everything to the left, then the whole design of my maze gets disturbed, it has somewhere thick and somewhere thin lines. @JerMah
Your link is 404 @SomShekharMukherjee
1

Add a filler to fill the gap:

.cell::before { position: absolute; content: ""; /*Make sure all values below match your border size and color*/ left: -3px; bottom: -3px; width: 3px; height: 3px; border-bottom: 3px solid black; } 

Also need to position .cell to anchor the filler:

.cell { /* ... */ position: relative; } 

Added note

In case of your sandbox example, just update this CSS, no need to change HTML or JS:

*{ margin: 0; padding: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .row{ display: flex; justify-content: center; align-items: center; } .cell{ height: 50px; width: 50px; border-right: 2px solid black; border-bottom: 2px solid black; } .row .cell:first-of-type{ border-left: 2px solid black; } .row:first-of-type .cell{ border-top: 2px solid black; } .cell::before { position: absolute; content: ""; /*Make sure all values below match your border size and color*/ left: -2px; bottom: -2px; width: 2px; height: 2px; border-bottom: 2px solid black; } .cell{ position: relative; } 

17 Comments

Can you please make your changes here. I tried adding your changes but it still doesn't fix it. Can you please add it yourself and see. It's a request.
I did, it works but I can't save cause I dont have account. Just add the css , and change all 3px to 2px
I have made the changes, but see still the corners aren't perfectly aligned! If possible you can copy that code that makes changes and paste it here.
I changed filler from background to border and it looks ok to me. added to answer
I added height: 2.5px and it worked for me, but I can't understand why? It would be really nice if you could help me!
|
0

I added a pseudo element to the cell that didn't had the border. Does this help you ?

cells = document.querySelectorAll(".cell"); //Removing the right-left border cells[0].style["border-right"] = "none"; cells[1].style["border-left"] = "none"; //Removing the top-down border cells[0].style["border-bottom"] = "none"; cells[2].style["border-top"] = "none";
*{ margin: 0; padding: 0; box-sizing: border-box; } body{ height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .row{ display: flex; justify-content: center; align-items: center; } .cell{ height: 50px; width: 50px; border-right: 3px solid black; border-bottom: 3px solid black; } .row .cell:first-of-type{ border-left: 3px solid black; } .row:first-of-type .cell{ border-top: 3px solid black; } .cell.forth { position: relative; } .cell.forth::before { content: ''; display: block; position: absolute; top: -3px; left: -3px; width: 3px; height: 3px; background-color: black; }
<!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <div class="row"> <div class="cell first"></div> <div class="cell second"></div> </div> <div class="row"> <div class="cell third"></div> <div class="cell forth"></div> </div> </body> </html>

3 Comments

This might be helpful, but the problem with this is, you are using the forth cell which I won't have access to in my actual algorithm in runtime, I want a fix that uses only the cells for which the borders are being changed. I hope you're getting my point.
You can add a class with Javascript and do the width/height of the cell + border and then moving from the pixels of border (3px here) negatively to fix. Would that help you ?
Here's my complete code, can you please insert your fix here, it would be really helpful.
-1

So, finally after scratching my head for almost 10 hours I got the fix.

Changes in CSS

.bottomRightCorner::before { position: absolute; content: ""; right: 0; bottom: 0; width: 2px; height: 2px; background: black; } .cell{ position: relative; } 

Adding a method named fix in the Cell class, and every time while calling .show(), also calling .fix().

Changes in Cell Class

fix(){ if (!this.walls[1] && !this.walls[2]) cellElements[this.r*ncols + this.c].classList.add("bottomRightCorner"); } 

Changes in drawLoop

let drawLoop = setInterval(function(){ for (let i=0; i<nrows; i++){ for (let j=0; j<ncols; j++){ grid[i*ncols + j].show(); } } for (let i=0; i<nrows; i++){ for (let j=0; j<ncols; j++){ grid[i*ncols + j].fix(); } } BacktrackingDFS(); },100); 

Comments