The artificial intelligence chooses from an opening book for the first move, then builds the complete game tree for subsequent moves. it should therefore always choose the "least bad" move, assuming a perfect opponent.
The code is available at ??. I used Underscore.js for syntactic sugar, and jQuery and jQuery UI for the eye-candy.