Racket Gin Rummy in ClojureScript
20 Mar 2012A ClojureScript implementation of a simple card game. The game can be played here.
The Racket programming environment comes with a couple of example games written in Scheme. My favorite of these games is Gin Rummy. The object of this game is to make a hand of 10 cards, consisting of a set of four cards and two sets of three cards. A set of cards are cards of the same rank (i.e. three Jacks) or subsequent cards of the same suit (i.e. 8,9,10,J of hearts). An ace can be low or high. The game has two players with ten cards each. In the middle are the deck of cards faced down and a discard pile, with cards face up. Players take turns and can choose to pick to top card of the discard pile or a face down card from the deck. The chosen card has to be exchanged for a card in the players hand, which is placed on top of the discard pile. For more information see the Racket documentation.
cljs-gin is an implementation of Gin Rummy in ClojureScript. The games runs completely in the browser. The Clojure code for the opponent is from a program that takes control of the mouse to play the game against the original Scheme client. This code worked without change in ClojureScript, but its naive algorithm from the Scheme source has been optimized for speed in the browser. The cards are draggable div elements, using the Dragger class from the google closure library.
The game is hosted on Amazon S3: Gin Rummy
The development version has a view of the table state, including the cards of the opponent. This is a consequence of the whole application living at the client. However the code is written to support communication with a server for a remote opponent in the future.
The code is on Github.