All Projects

JS Blackjack

This began as an extra credit project for my COMP101 (Problem Solving with Computing) class taken in 2014. We were learning the basics of programming using JavaScript in that class. Actually, we weren’t even using it the way it is traditionally used (along with html/css) but instead were using an online IDE that was for learning the basics and only wrote text out to the console. Each week during the lecture, the professor would build a little more on a text-based blackjack game and told us in the final couple weeks that we could earn extra credit if we finished the project by adding an AI opponent to complete the game. I decided to take it to the next level by making a full-fledged HTML version with graphics and sound effects. It took me three or four days to complete and I had a lot of fun coding it.

A few years later:

With my COMP321 (Application Server Programming) class wrapping up, I got the inspiration to improve the blackjack game by adding some server-side functionality in the form of a high score list. The list is a simple text file that is read and written to using php. I realize that a database would probably be a better option for this, but I wanted to keep it simple for now. When the main script is invoked, a php function is called (using AJAX) to retreive the lowest score on the list which is stored in a variable. The score is simply the number of consecutive wins acheived. This number is tracked by the JavaScript and whenever the player loses, the number is checked against the lowest number from the high-score list. If it is greater than or equal to that number, the php function is called again to make sure no changes occurred on the high-score list during the play session and if still valid, the user is prompted to enter their name for the high-score list. Should the player choose to enter their name, another php function is called which parses the text file, adds the new player with score to the end, and compared using usort in descending order, and pop() is called on the array to discard the last (lowest) score. A two-dimensional array of associative arrays is used during this process (which is why the user-defined sort needs to be used). After the array is built, the entire text file is overwritten with the new ordering of high-scores.

At the time of posting this (during my website rebuild) (November 2018):

This project was long before I got up to speed on the latest best practices for JavaScript.  I did not know any frameworks at the time and it wasn’t even object-oriented.  I was one huge file full of functions.  Lots of spaghetti-code.  The last I remembered, it was running smoothly with no indication of bugs.

 

All Projects

Leave a Reply

Your email address will not be published.