HTML5 Tic Tac Toe game with local storage and canvas [now with minimax & alpha-beta pruning]

Modified: The algorithm for AI now uses the minimax algorithm with alpha-beta pruning.

Tic Tac Toe – a simple HTML5 game which uses local storage to store player’s and pc’s moves and draws a game with canvas. Here’s source code, I tried to comment as much as I can, but if you don’t understand something, leave a comment and I’ll try to explain.

Local storage stores key/value pairs locally. Like cookies, this data doesn’t get deleted even after you close your browser. So, you can make few moves in the game, close your browser, return to the game and you’ll still see stats and previous moves made by you and AI.

Why use local storage instead of cookies?

Local Storage is basically the same as cookies. Problem with cookies is that they are sent between browser and server with every HTTP request, so it slows our application. Secondly, Cookies are limited to ~4 KB of data when Local Storage can store up to 5MB.

HTML5 Storage support
IE Firefox Safari Chrome Opera iPhone Android
8.0+ 3.5+ 4.0+ 4.0+ 10.5+ 2.0+ 2.0+
Share