Workshop › Sokoban › Step 6 of 29
loadLevel turns the text into state: it walks every character with its column x and row y, and puts every wall's
key into a Set.
```js
let level = 0
let walls
function loadLevel(index) {
level = index
walls = new Set()
LEVELS[level].forEach((line, y) => {
;[...line].forEach((ch, x) => {
if (ch === '#') walls.add(key(x, y))
})
})
}
loadLevel(0)
```level is the index of the current level, walls the Set of wall keys.new Set() makes an empty Set, so every load starts clean.forEach((line, y) => ...) runs the function for every row, with the row's text and its number.[...line] spreads a row into characters; the inner forEach gives each character and its column.; at the start stops JavaScript from gluing a line that starts with [ to the line before.walls.add(key(x, y)) adds the tile's name to the Set.const key, write let level = 0 and let walls, and leave an empty line.key line, leave an empty line and write loadLevel.loadLevel(0) above draw(). Press Run.Skills: Arrays and objects, Game states
← A name for every tile Draw the walls →