Last weekend I created my first standalone project that I felt was worth showing off. It’s a multiple choice game with random elements that takes place in JavaScript alerts. It’s pretty simple, but it’s also pretty cool.  Go ahead and play it, then come back for the breakdown.

Zombor: A Game by Dick Ward

The Cool Bits

This game came after I finished a Pluralsight tutorial and wanted to add more complexity to it.  The version at the end of the tutorial would randomly select a weapon and then would roll a random number to see if the user would survive. I decided I wanted to add some complexity, like hit points for the zombie, weapon differences,  and additional random elements. One of my favorites comes when the user selects to fight the zombies and then picks a knife as the weapon.

if (weapon.toLowerCase() === ‘knife’){
for (i = 3; i > 0; ){
random = Math.random()
alert (`You attack the zombie with your ${weapon.toLowerCase()}.`)

if (random > 0.75){
alert (‘Critical Hit! Oh man, you slashed that zombor\’s face right off his heeead’)
i = 0
}

else if (random > 0.25 && i > 1) {
alert (`You have hit the zombie with your ${weapon.toLowerCase()}! `)
i–
alert (`It will take ${i} more hits to kill the zombie!`)
}
else if (random > .25 && i === 1){
alert (‘You finish off the zombie with your super cool knifey moves. Such a badass. You.’)
i = 0
}

else {
alert (`You swing the knife and it pierces the zombie\’s flesh.`)
alert (‘And it sticks there.’)
alert (‘Aaaaand, the zombie eats your stupid head.’)
alert (‘You’)
alert (‘Are’)
alert (‘Dead’)
i = 0

So the way this works is pretty cool. The loop starts with the variable i assigned to the number three. This is my way of doing some behind-the-scenes hitpoints for the zombie. If a random number equals less than .25 they instantly lose, and if it equals less than .75 they instantly win. But when it’s between those two, it registers a hit and tells they player that they need to hit the zombie i more times before it dies.

Not only does this give a cool countdown for successful hits, it also gives the player another chance to either critically hit or critically fail. Using this loop, a player can hit the zombie twice, but still critically fail at the end. I really like this sort of random nature.

Where I Got Stuck

There are two big places where I got stuck in this game, and the first was the both the most obvious and the most time consuming to solve. Basically, I had a curly bracket out of place. It’s an easy problem to have when writing a lot of code – and for me this is a lot. I have for loops inside of if statements inside of functions, and that got confusing fast.  After wrestling with why the logic of my finale else statement wasn’t working, I took a hard look at what my program was doing, spoke some of it aloud, and found my extra bracket.  Boom, solved.The second problem was that my random number generator was only creating a single random number and then using that through the entirety of the program, meaning that the cool knife fight from above would only go one way. My thinking was that each time I called the ‘random’ variable, it would generate a new random number.

var random = Math.random()

The above code doesn’t tell ‘random’ to get a new random number every time – it simply tells ‘random’ to be whatever number Math.random generates at that instance. To fix that in the knife fight, I put another random = Math.random() at the top of the for loop. This means that every time the program goes back up to the top of the loop, it generates a new random number for the outcome.

I banged my head against the wall for a while on this one, and then on my commute to work I figured it out. I ended up having to record myself explaining the solution so that I could play it back when I got home.

Oh, and mad props if you got the reference 🙂