Skip to content

Commit 42820dc

Browse files
committed
Fix the join to also assign the same set id to all cell containing the previous set id
1 parent 355e85b commit 42820dc

File tree

1 file changed

+13
-45
lines changed

1 file changed

+13
-45
lines changed

src/Row.js

Lines changed: 13 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -91,35 +91,21 @@ class Row extends Component {
9191
});
9292
}
9393

94-
joinCellsToSet(cells, prevSetId, newSetId) {
95-
cells.map(cell => {
96-
if (cell.props.setID !== prevSetId) {
94+
joinCellsToSet(cells, currentCellSetId, nextCellSetId) {
95+
// drop walls between current and next cell
96+
cells[this.currentCell].props.walls.right = false;
97+
cells[this.currentCell + 1].props.walls.left = false;
98+
99+
// assign the current set id all cells having the same previous set ID
100+
return cells.map((cell, i) => {
101+
if (cell.props.setID !== nextCellSetId) {
97102
return cell;
98103
}
99104

100-
const walls = {
101-
...cell.props.walls,
102-
right: false,
103-
}
104-
105-
return <Cell setID={newSetId}
105+
return <Cell setID={currentCellSetId}
106106
active={cell.props.active}
107-
walls={walls}/>
108-
})
109-
110-
return cells;
111-
}
112-
113-
joinCellToLastSet(cells) {
114-
const walls = {
115-
...cells[this.currentCell].props.walls,
116-
left: false,
117-
}
118-
return (
119-
<Cell setID={cells[this.currentCell - 1].props.setID}
120-
active={true}
121-
walls={walls}/>
122-
);
107+
walls={cell.props.walls}/>
108+
});
123109
}
124110

125111
joinSomeCells() {
@@ -146,27 +132,14 @@ class Row extends Component {
146132
}
147133

148134
let cells = this.getCellsAndHighlightCurrent();
149-
150-
// if the current cell must be joined to the previous cell.
151-
// Whether it will join or not was decided on the previous tick
152-
// we just do the join in this tick so that it looks better visually
153-
if (
154-
cells[this.currentCell - 1] &&
155-
!cells[this.currentCell - 1].props.walls.right
156-
) {
157-
cells[this.currentCell] = this.joinCellToLastSet(cells);
158-
}
159-
160135
const currentCellSetId = cells[this.currentCell].props.setID;
161136

162137
if (
163138
this.willJoin() &&
164139
this.currentCell < this.props.width - 1 &&
165140
currentCellSetId !== cells[this.currentCell + 1].props.setID
166141
) {
167-
// TODO: send the next set id and the current cell, to just open up the right
168-
// wall for the current cell, but to change the set id for all who share the next cell's set
169-
cells = this.joinCellsToSet(cells, cells[this.currentCell], currentCellSetId);
142+
cells = this.joinCellsToSet(cells, currentCellSetId, cells[this.currentCell + 1].props.setID);
170143
}
171144

172145
// if last set,
@@ -176,13 +149,8 @@ class Row extends Component {
176149
if (cells[this.currentCell + 1] &&
177150
cells[this.currentCell + 1].props.setID !==
178151
cells[this.currentCell].props.setID) {
179-
cells = this.joinCellsToSet(cells, cells[this.currentCell].props.setID, currentCellSetId);
152+
cells = this.joinCellsToSet(cells, currentCellSetId, cells[this.currentCell].props.setID);
180153
}
181-
182-
cells[this.currentCell] =
183-
<Cell setID={cells[this.currentCell].props.setID}
184-
active={true}
185-
walls={cells[this.currentCell].props.walls}/>;
186154
}
187155

188156
this.setState({ cells });

0 commit comments

Comments
 (0)