This is a game that I get my students to write.  I cannot take credit for it.  It is from the book Foundation Game Design with HTML5 and JavaScript by Rex van der Spuy