DEV Community

Julie Martin
Julie Martin

Posted on

Do the Thing: How I Wrote a PHP Trivia Game

Back in 2016, I was getting increasingly frustrated at work. I just wasn't technical enough. Since the web application the company I worked at was built in PHP, I decided that I should learn PHP and maybe I could be a more technical resource for the clients I spoke with on a regular basis.

In order to take the PHP classes I wanted to take, I had to take three other classes first! I had to take HTML 5, SQL Essentials and an introduction to Java programming class.

I started out with an HTML 5/CSS 3 class, primarily because I already knew HTML and CSS (well, older versions) and because I thought I'd be able to pass the class without issue. It was my first time going to school while working full-time, so I thought it best to just update my skills with HTML and CSS if nothing else.

A month into my class, I got laid off. Largely, I felt relieved and decided to use some of my severance to pay for my next two classes in the diploma. Why not update my skills while I had the time, right?

HTML went great and I passed with flying colours, but I'd kind of known that would happen. It was the next two classes, SQL and Java, that I was worried about.

First came SQL Essentials. It was my first time doing anything of note with a database. I'd never written a query before and a database was some sort of black box to me. By the time the class was over, I was at ease using MySQL on the command line and could build a database with different tables and columns and rows. I couldn't yet do anything of note with the data I put into the database, mind you, but it was there!

Then, it was time for Java. An 80-hour course, twice a week for three hours each evening, I put in at least double that amount of time in at home as I struggled to learn and understand programming ideas and concepts. Java was my first real programming language and where I learned about if statements and for loops and while loops and what the difference is. I learned about operands. I learned to sanitize the user's input. I finally learned how to retrieve some of that database information I'd safely tucked away in a database.

It was one of the hardest things I'd ever done. Part of it was because I'd never really programmed before, but part of it was because I managed to get a new job right at the busiest part of my semester. I'd work all day, then code for another 6-8 hours at night, hacking away at my Java code, trying to understand what on earth I was doing wrong. (If my new job hadn't been a remote job, there's no way I would have made it through my trial period!)

Finally, with almost no break in between the end of Java and the start of my next class, I was about to start PHP. It was the promised land! This is what I'd been wanting to learn for at least a year and had finally completed all the requirements for over the last 9 months.

The first class almost broke me. My brain still hadn't fully absorbed all the programming concepts from Java. I still hadn't adapted to a programmer's way of thinking. I had no idea how to change my brain to think programmatically. In the first PHP class, we did some quick exercises with brand-new (to us!) PHP commands and I was hopelessly lost by the break.

I didn't give up. I would spend ages working on my assignments and would read a lot of code to better understand why a for loop worked best in one spot, or how a switch statement might be a good idea in this other spot. I tinkered with examples from class to see what would happen to the code when I changed it. Most of all, I kept repeating to myself to think programmatically. Every exercise, every challenge, I'd ask myself how I could do it more efficiently, without repeating myself.

I definitely improved and though I did have some trouble with object-oriented programming, I made it through PHP I and was headed for PHP II.

Finally, this was the class for which I'd been waiting. This was the class that would teach me about git (through GitHub), Docker, Composer and more. It's where I would learn how to use and build APIs, where I would learn how to use Postman to test those APIs, where I would learn to integrate things like Stripe and PHPMailer for my final project. My final project was an ecommerce website, where there was a catalogue, where users could add to their cart, where they could check out with Stripe (in test mode), where they could view their order history. I definitely didn't finish the entire thing, but I finished enough of it to pass the class with a fairly high grade.

I felt as though I could do anything after that point. That January, I took the final class in my diploma program, JavaScript & AJAX. It was a relatively simple class that most people had taken before they took either PHP class, so while the syntax was different, it wasn't all that different from what I'd done before.

So it was sometime in early 2018 that I said to myself that I needed to do something with my PHP and SQL skills or I'd lose them. It's just like learning a spoken or written language like French or Italian or Mandarin. You have to use them or lose them.

I reasoned I could build some kind of trivia game. I liked trivia, so why not? It's not like I don't know how to store and retrieve things in a database, right? Right. It's not like I can't build a website, right? Right. And with JavaScript & AJAX going well, I could even make the site look somewhat neat and use both client-side and server-side validation for stuff.

I poked around at some PHP and MySQL stuff for a while to see if I could actually pull out the information and match it to what a user typed in. And, to my utter surprise, I did it. And then I realized I should do it with the PHP Data Object (PDO) method instead of the mysqli method. So I redid it. And it worked again! Realizing I could do the very basis of what my game would consist of gave me the confidence to move forward with my concept. If nothing else, I'd have fun playing it.

Of course, I needed some kind of story around the trivia portion. I'd always loved the old-school Bulletin Board System (BBS) games I'd grown up with, like Trade Wars 2002, Sky Mountain and Legend of the Red Dragon. So, bearing that in mind, I started working on River of Kurn. It's a text-based game written in PHP and MySQL in which you wake up next to a lake and must progress down the river (by answering trivia questions!) in order to find your way back home.

That was almost two years ago. This past September, I launched an alpha version of my game on an AWS instance.

  • registration & forgot password systems
  • leaderboards to show everyone how far along the river you are
  • daily maintenance routines, which include daily backups to my AWS S3 bucket and replenishment of user energy based on whether or not users performed certain tasks the previous day
  • admin statistics on questions asked and whether or not they were answered correctly
  • admin statistics on food gathered, fished and eaten while in the game
  • admin statistics on food eaten by bears in the game!
  • skillups for food and fishing skills, which then affect your foraging and fishing success rates
  • over 1500 questions on subjects like various Star Trek series, American presidents, Astronomy and Superman (almost all of which are written by me)
  • anti-repeat question check that uses session data to prevent getting repeat questions within 100 questions while the session is active
  • a new admin-only tool to easily update my main question table and my stats tables, making adding questions super easy and fast

Of course, this is still an alpha version. I'm hoping for a beta version to be ready in March or April. Things to be added by beta include, but aren't limited to:

  • more questions! 😄
  • mobile-friendliness
  • nearby player check, opening the door to potential user cooperation, even if they're not online when you are
  • trials: in order to get to the end of the river, you'll have to go through three trials. The Rapids, The Whirlpool and the Waterfall. None of these have been coded yet. ;)
  • invite a friend, to make it easier to have a friend join in and maybe grant inviter and invitee a small reward for doing so
  • minor random events/mini-games to give players some choices that have a bit more weight
  • potential "currency" to allow you to skip questions you don't know the answer to
  • sooooo many bug fixes!

Having said all of that, this has been one of the most gratifying experiences of my life. To look back at the last couple of years of poking away at this project, plus the 18 months of my diploma program before that, and be able to say "Wow, I made something on my own!" is just amazing. Words fail me. Even more fantastic is to see other people playing it, of their own accord. There are actually people playing my game who I haven't begged to play! It really is unlike anything else I've ever done and gives me a sense of satisfaction the likes of which I have never known before.

So what have I learned? Ultimately, it boils down to this: do the thing. Should I have taken on a much simpler idea? Probably. Would I have learned as much? Probably not. Would I be having as much fun as I currently am? Definitely not.

Do the thing. It'll mean a lot of learning, a lot of frustration, many hours on Google and sifting through StackOverflow questions. You will absolutely think that you've lost your mind on multiple occasions.

But it's worth it. Trust me. Do the thing.

(And, of course, if you'd like to test things out, head on over to River of Kurn to take a look at what I've accomplished!)

Top comments (0)