Small Steps

I would like to be posting (at least) three times per week, and I’m trying to make that happen. Sometimes though, my life is just too chaotic. I don’t think I have talked about my personal life much, if at all. During the day, I am a mild-mannered stay-at-home dad of two beautiful but rambunctious children. Occasionally that day stretches into the evening. Parenting is a complex topic that I could go on and on about, but it’s, um… out of scope for this blog. The short version is, it is a wonderful experience that leaves me feeling scatterbrained and exhausted some days, and it’s part of the reason why I can’t always get a post up.

That said, I am still working and learning and doing programming-type things. Andrea’s post yesterday got me to check out even more online resources for the burgeoning developer. I jumped right into freeCodeCamp, and have completed a few of the challenges. I tend to need a lot of review of topics, and like Andrea, I find doing the same exercises over and over can be a little brain-numbing (and counter-productive).

When I finally got a relatively quiet block of time, I decided to work on my backgammon game a bit. I had identified a task that I thought I could get through quickly.  It picks up right where my last post left off, with my rollDice function. Last time, I managed to get the dice to change color properly, but the game wasn’t actually choosing a starting player, that was hard-coded to be the “black” player and the game just moved on from there. I figured it was time to change that!

Here is what it looked like when I started:


Actually, there was an iteration before that. One that just rolled the dice and was missing the if (first) loop. Knowing how I ended up structuring the function, I can already see a problem with what I had above. Two (maybe more?) problems actually. It is already becoming a messy bit of code. I don’t have all the lingo and philosophy down, so I can’t say what exactly bothers me about functions getting larger and more packed. Maybe I want more “abstraction” or “encapsulation” or some other concept that hasn’t osmotically entered my lexicon. But, you’ll see, it gets messier and cleaner.

I knew I needed to compare the dice and select a winner. Sounds simple. I’ve got the dice, do a comparison, pick the winner, change the window dressing, done! interRollDice

This is the messier phase. Less readable. Crammed in a comparison. Remembered I needed to reject a roll of doubles. Tacked that on. You can tell I was running on pure horsepower by the “&&” in that if statement. I can’t even remember if this worked. I went through one version that did the comparison but through sloppy syntax, it just made every roll a “white” win. Sometimes I work way harder than I need to. After a few deep breaths and a snack, I saw a better way. Better, in this case, meaning more readable. It’s possible that what I did next was actually a step backward, but I’m knew enough not to know any better!


I realized that what I was trying to do still fit under the mantle of the “first roll” and so I nested it into the already existing if (first) loop. I then slammed the check for a double roll into the existing dice comparison loop. I keep reading about recursion and so I wanted to try that. (I hear it makes your code more impressive!). I changed the passed argument from “1” to “true” to make it more explicit. The best part, it works! I think. I really need to start using some type of testing so I can say, “no, really, it works!” but for now I have yet to run into doubles on a first roll, so it seems to be working.

I can’t actually say how long that all took me, I should start keeping track, that would be a useful statistic. It probably took me longer than it should have, but that’s okay. I’m one step closer to finished. I am hoping to wrap this up and submit it to the One Game A Month challenge. It’s almost playable at this point, I really just need to set up the bearing-off stage and figure out how to know when someone has won. I’d also like to clean up the presentation a little. “Just a few more hours” of coding, really!