Greg Hewgill (ghewgill) wrote,
Greg Hewgill

mahjongg widget

I'm back to writing more widgets using the Yahoo! Widgets framework. This one is Mahjongg, the terribly addictive tile removal solitaire game. It includes an option to generate either a random initial game, or one that is guaranteed to have a solution. It also (optionally) times your game and keeps track of your fastest time.

Interestingly, the most difficult part of writing this was writing the algorithm that generates a solvable game. The general idea is to work backwards: Start with a blank board and place pairs of matching tiles on the board (in legal positions) until you reach the starting point. Then the solution is just doing the same thing the other way - ie. forwards, like you would play it. The problem is there are ways that the game generator can sort of paint itself into a corner. For example, if you have two tiles left to place on the board (which would be the first move of a solution), you can't place them side by side on one end of a row, because the player won't be able to remove them that way. If the game generator encounters such a situation, it just gives up and tries again from scratch. On average, the generator has to start over once or twice per valid generated game.

Tags: widgets

  • 2013 in review

    2013 is the year when everything changed. The biggest event was the birth of our daughter Lily. She was born prematurely in Shanghai while we…

  • 2012 in review

    2012 has been fairly quiet. Maybe it just seems that way because I haven't actually written anything new in this blog since last year's annual…

  • new photo galleries

    I've been busy processing photo galleries from the last year (or two) and putting them online for your perusal. Vancouver 2010 Northland…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded