Hello my lovable minions, today let’s talk about board games.
Well, not really, but let’s briefly discuss that I made one. This was an all-important C++ refresher that I wrote in preparation for a small interview the other day where I knew I’d be queried on some C, so it seemed only practical to do some for the first time in a long time. Nothing particularly complicated, but a good refresher in pointers, memory management and classes/program structure. The one zip file I’ll provide includes both the source and a compiled exe, since C programs are so small anyway, and it’s only command line — download here.
A brief overview of what it is… The default application with no parameters will run a human vs human game on a 3×3 grid – classic ‘Toe. There are two parameters available, -ai which sets the second player to a very dumb AI (more on that later), and a numerical value between 3 and 9 which dictates the size of the grid on which you play (as well as the win score, obviously). More information is included in the README in the zip.
The AI is an implementation of whatever popped in to my head… It’d be very easy to look up existing solutions (and I since have, Minimax et al) and implement one but I wanted to see what I could come up with alone. Turns out it’s not very good, but it does work at least. In essence, the first AI move it tries to secure one of the desirable squares – the middle or corners. Each subsequent move is based on the previous, where it takes the previous cell it filled and searches vertically, horizontally and diagonally – if applicable – until it finds a totally empty row (i.e, unobstructed by opponent pieces). It then sets about filling this row from the start to the end. In the event that all the paths from the previous move are blocked, it just does a random search for a cell that has an unobstructed path, and if it can’t find that (which should basically mean the game is now unwinnable) it chooses one entirely at random and fills it.
If you get the first move it’s fairly trivial to beat – you could be one move away from winning and it won’t try to block you, just keep on doing its own thing. But if it moves first you will have to block it in some form, and it’s behavior from them on may cause problems for you. Hardly the best AI ever written, but not bad for something I threw together.
In implementation terms there are three classes: Game, Player and AIPlayer (extends Player). The only new thing AIPlayer introduces is overriding the virtual method “YourMove()”, where the human player class would prompt for and process input, as well as adding a few private methods to work some stuff out. The Game class obviously holds most of the interesting bits, although there still isn’t much to see. The game board is stored in a simple char array of (size*size) elements, and each square in turn is set to the active players symbol (x or o).
So that’s that really. Simple C++ Tic Tac Toe that I’m sure might help someone trying to learn – not that I’m guaranteeing everything is best practice or even well written, but you should at least get the idea!
— B