Wednesday, 28 October 2015

Tic Tac Toe 3DS

This is something I've been meaning to post for a while, but it's been sat in the Drafts folder gathering digital dust.

Following the release of IronHax and TubeHax for 3DS prompted me into looking at 3DS Homebrew. I had previously attempted to create some DS homebrew but was quickly turned off by the nonsense of C and C++.

Initially I looked at a "Hello World" example and wrote it line for line, but it would not compile, giving an error that an escape command was attempting to do something unexpected. After several attempts that did not rectify the problem, I instead just copy-pasted the example and it compiled fine despite the code being identical. Code gonna code, I guess.

After examining some more text based functionality, I then turned my hand to rendering. However the example project would not compile no matter what I tried, citing that something was missing. Being unable to figure out what it was, I then took the source code for a finished game and attempted to compile that and was successful. Now the question for me was - what do I attempt to develop?

I wanted something simple. As much as everybody loves to dive in and try and make something like one of their favourite games, attempting to make Pokémon at this point isn't really an uphill battle but more of brick wall that will eventually topple backwards and squash you. I decided to look at mobile games for inspiration, and then realised I had made Tic Tac Toe for Android with relative ease, so decided to do that.

I had originally copied the bulk of the code into the main.cpp file (like the example game had done), but after receiving errors about things not being declared in the scope, I decided it was better to separate it into it's own class. This got rid of most of the scope errors, as I declared everything in the header first, but then there were issues with syntax where Java likes arrays to be int[] val and C++ prefers int val[]. After eventually getting no compile errors (Yay!) with the ported logic, it was time to turn my attention to input and rendering.

Most of the stuff I had studied so far had referred to button inputs. But, I wanted to utilise the touch screen - this was an Android port after all. In the Android version, each box in the grid is actually an invisible button which has code that runs following a touch event, returning whether the button was touched or not. This meant bringing the button code over too. Soon after, everything compiled with no errors once more.
The main screen of Tic Tac Toe - Screenshot captured using Citra
The 3DS has some strange ways of displaying stuff, and that's not just because of the 3D because it happens on the touch screen too. Everything is rotated 90 degrees. In order to account for this, there is a tool which allows you to create a normal image and rotate it whilst exporting a .bin file. This .bin file when compiled will produce .o and .h files that can then be referenced in code to draw them.

Computer player wins
Drawing the backgrounds with the simplest of draw functions was simple enough, but when touching the screen, the O or X would appear in a different "button"'s place. This was not a result of the draw code being inaccurate, but the buttons not being set up with the screen rotation into account. Soon after figuring out where the buttons should really have been, the game was up and running.
A winner is you!
Some things to take away from this project - this was the first C++ project I'd actually completed, bar basic Hello World stuff running in console windows. In terms of improving it - the Android version featured a "Two Player Mode" which is essentially a case of foregoing the CPU turn and checking input again, but I didn't feel it was necessary to bring it across. It would be pretty trivial to do so, however.

The source code and .3dsx file are located here.