I then began working on the game logic, starting on communicating with the PHP backend. Using an OO architecture was a must, as was easily displaying content, as I had a feeling from the very beginning simply
echoing to the page would not provide the layout control I needed.
I devised two ways of accomplishing this goal: treat the page as a form,
- As stated,
POSTing to the page in a form-like manner required a page reload order to process the guess and display new content. I was not particularly fond of this method, as it would get annoying fast and require keeping track of the game state by storing the values in
$_SESSIONon object destruction (and subsequently restore them on object creation).
As this was my first big (or even real) project using PHP, I did learn some things in the process.
- If something can break, it will break.
Too many times I had things break that should not have broken. For example, once my letter white list, designed to not make players type certain characters (spaces and dashes) broke while I worked on code that did not relate to or interact with it. Yes, random breakage can happen in any code base, but it seemed to happen more frequently with Hangman.
- PHP is confusing and convoluted as all get out.
Look, I have read many blog posts on this topic. Some say PHP is bad, others good, and still others say it is getting better. As you know, I am of the opinion PHP is absolutely crazy. I have never had so much trouble figuring out what function to call, what syntax is correct (is it
=>?), nor why all the aforementioned random breaking kept occurring. Could I use PHP in further programming? Probably so, but I will make sure to have plenty of candy and breaks to relieve the intense stress and pain when writing PHP. 😛
- Just like with Tic-tac-toe, Hangman is simple to play but hard to convert to code. Though not near as complex as Tic-tac-toe, yet again it is proven that creating a game, even something as simple as Hangman and Tic-tac-toe, is more complex than first imagined. The primary issue for me was determining the current score, e.g., correct or incorrect answer, continue guessing or game over. Things your brain already understands has to be reimplemented in code, and that was probably the most challenging part of this project.
If you are wondering if you can play my Hangman game, the answer is yes! I have open sourced it on GitHub under the MIT License and hosted a copy on my website. If you like to try it out youself,, it should run under practically any PHP environment as it uses no libraries or platform specific code. Developed under PHP 5.4 and tested under PHP 5.6 and PHP 7.0. Do note Hangman, like Tic-tac-toe before it, is not super-mobile-optimized, and layout issues are likely to be present. 🙂
Next possible code project: convert the game to be HTML5 powered.