Example: Implementing Sokoban

Armed with addHandler and the dom function from the previous chapter, we are ready for more challenging feats of document manipulation. As an exercise, we will implement the game known as Sokoban. This is something of a classic, but you may not have seen it before. The rules are these: There is a grid made up of walls, empty space, and one or more “exits.” On this grid, there are a number of crates or stones, and a little dude that the player controls. This dude can be moved horizontally and vertically into empty squares and can push the boulders around, provided that there is empty space behind them. The goal of the game is to move a given number of boulders into the exits. It looks something like this:

Level ...

Get Eloquent JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.