I Ported Sudoku to Your URL Bar and It Plays like a Dream

February 21, 2019

Sudoku powered by the dark arts of window.location.hash, which means zero page refreshes are required. Unfortunately this means that the browser history gets spammed.

Secret Sudoku

Playable right here, Secret Sudoku unlocked a series of unique experiences for me. It’s not every day that you find yourself brute forcing a 19th-century newspaper puzzle. Or, better yet, getting Travis CI to do it for you.

test('app plays without error', () => {
    const game = main(this, sudoku);
    game.startGame();
    for (let i = 0; i < 20000; i++) {
        // 0-81, 0-9
        if (game.play(Math.floor(Math.random() * 82), Math.floor(Math.random() * 10)) === true) {
            /* This test will usually complete the game six times over,
               adjust the difficulty randomly on completion */
            game.startGame(Math.floor(Math.random() * 6));
        };
    }
});

It turns out that all you need are some emotes, a few eventListeners, and a dream. Many thanks to Rob McGuire-Dale and his wonderful Sudoku generator/solver. And to Mathew Rayfield for showing that you can animate URLs.

// Main render functions
this.squaresFilled = () => this.board.join('').match(/[^.]/g).length
this.completeness = () => `${this.squaresFilled()}/81`;
this.start = () => `Secret_Sudoku_~_${this.completeness()}_~_row:`;
this.render = (extra = '') => {
    window.location.hash = `${this.start()}${this.rowMap[this.row]}__${this.prettifyRow(this.currRow(), this.cursor)}${extra}`;
}

Secret Sudoku is the perfect project to work on to help me relax in the evenings. It makes programming feel far more fun than logic abstraction should. Taking pull-requests today and every day hereafter.

Repository

Join the newsletter

Join 200+ people signed up to my newsletter on programming and personal growth!


Andrew Healey

Hey, I'm Andrew Healey. I'm a software engineer in London, UK. I write about things I wanted to read when I was a beginner.