O'Reilly logo

Eloquent JavaScript by Marijn Haverbeke

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 Input ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required