Skip to content

Two player tic tac toe, using highly optimized bitwise operations. Built with vanilla JS and SCSS

Notifications You must be signed in to change notification settings

sayandcode/ticTacToe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ticTacToe

Minimalist Tic Tac Toe game built using Vanilla Javascript.

Note from the developer

I built this game before I had any knowledge of bundlers and JS frameworks. It was a good excercise in DOM Manipulation, and using SCSS to style.

One interesting thing that came out of this is that when I was looking into ways to determine a game win state, I started thinking how wasteful it was to run multiple loops to check each cell, for every move, in order to determine whether the previous move resulted in a win.

That's when I came across this answer in stack overflow. It stored the state of the board in an (N+1)^2-bit variable, where N represents the number of positions in any row of the tic-tac-toe board. With inspiration from this answer, and help from an answer to a subsequent post of my own, I have implemented such a strategy in this web game.

Instead of running multiple loops, this app simply runs two bitwise shift operators, in order to determine whether the current board position is a win. I find that the solution suggested in this answer is quite ingenious and Trentium deserves the full credit to this. I had run into some trouble with storing the numbers in an ordinary JS number type(bitwise shifts were altering the 2s complelement representation, resulting in data loss), but that was solved by using BigInts, albeit at a slight performance drop at higher board sizes.

Overall I am very happy with my results, given the level of knowledge I had at that point. If I had to redo it, I would probably segment the code into separate files, move debugging tools into a need-to-use basis file, and work on improving performance at higher board sizes.

Onward and Upward.

About

Two player tic tac toe, using highly optimized bitwise operations. Built with vanilla JS and SCSS

Topics

Resources

Stars

Watchers

Forks