WebAssembly has been there for quite some time. While I understand the overall idea, I never had a chance to build anything production-ready so far. I had a chance to play around with WebAssembly during our company hackathon and one of the engineers explained in detail how WebAssembly works.
I wanted to build something with WebAssembly and wanted that to be a real-world program instead of a simple hello world. While doing something with image processing might have been useful, I wanted something small to start with. Two-player games with computer AI seemed like a good idea and tic tac toe is felt easier to build.
A quick search in Github for tic tac toe gave this result – https://github.com/ryancesiel/tictactoe-minimax/. While the code wasn’t the best (especially the way char board is handled across all the functions), it was short, easy to understand & modify and didn’t give me any nightmares about compiling the code. As you can see, I have had nightmares with simple code compilation.
In the first version, I thought I had to export Game::minimax function and I had no clue how to expose a function under a class. So, ended up creating a simple function which called this function and had a simple name – get_next_move()
The whole thing works like a charm. You can try to play it here https://sp2hari.com/work/tic-tac-toe/. As always, the entire code is published in my Github here at https://github.com/sp2hari/tic-tac-toe
In short, this was a fun experiment to work on instead of the usual hello world or the Fibonacci. The next step is to build something to do with image processing or code auto-completion using WebAssembly. Super excited about both the ideas.
Hacks/Pending issues with the script
Issue #1 – Passing the integer array and using it in C++
Module.HEAPF32.set(typed_gameboard, buffer >> 2)
But for some reason, I wasn’t able to get it right for no value. With most of the example on the internet for float arrays, I didn’t have too much time to mess with this.
Issue #2 – Passing a 2D array of N rows and M columns, instead of a 1D array of N*M size
Issue #3 – Calling functions from a class.
If you have any solutions for any one of the above issues, please respond in the comments below. I’ll be forever thankful!