Too Much Forest, Not Enough Trees

(I think I’m done messing with the title!)

My friend and mentor, Chris Gomez (check out his blog, he needs the motivation to keep posting too!) made an interesting comment to me the other day. He told me I should spend some time focusing more on the details of my program and less on the big picture. Andrea’s comment on yesterday’s post helped hammer home this profound lesson on why getting outside opinions is important. In a lot of ways, I thought I had been pretty detail oriented, but when I looked back at it, I could see I had been swept up in the idea of catching my blog up to the current state of my program.

With that in mind, I am going to start looking back through my code and trying to explain what drives the choices I make. I’ll start with something I just did tonight. In backgammon, the first time dice are rolled, each player rolls one die of his or her color. The high die wins and that player moves, using those two dice. Every turn after that, the player rolls both if his or her dice. So on the first roll, there are two colors of dice and on subsequent rolls, the dice are one color, the player’s color. I wanted to capture this functionality to help indicate which player was up.

In order to facilitate this operation, I added a parameter to my rollDirollDicece function called, simply enough, first. If rollDice is told that this is the first roll of the game, it will set the css class of each of the die elements to the appropriate colors. One black and one white. (I just realized that I am concatenating two strings that don’t need to be concatenated and could be just “die0” and “die1”. That is a relic of a previous attempt to set them in a cleaner manner. Whoops!) The rest of the function just sets each of the die values to a random number from 1 to 6 and then tells the view to update the page.

A short aside. I am trying, probably very poorly, to implement an MVC pattern. Hence the view, and obviously there is a model and a controller. I am not sure how successful I have been at this, but I am pretty sure trying can’t hurt!

The way my game starts is when the player (for now you can only play against yourself, hopefully that will change in the future) clicks a button labelled “Reset Board.” That fires my init function that, in part, looks like this: clipOfInitPretty simple. It just passes a 1 into the parameter “first” which makes the if statement evaluate to true and initializes the dice, as it were.

To make sureadvancePlayer that only happens on the first roll,is even simpler. I just don’t pass an argument to rollDice ever again. I am fairly certain this works, although I haven’t tested it. I am relying on my understanding of what JavaScript does when there are “missing” arguments. The values are passed as undefined. The if statement is false and everything just plays out as it otherwise would. The calls to rollDice come from a function named advancePlayer. It’s a little messy, but it works, so I keep it. The highlighted line is where I call rollDice with no argument, forcing the if to fail.

Further down in the mess is another important call related to the color of the dice, changeDiceColor. That does exactly what it sounds like it does, and along with the showDice, it is called whenever the player side changes and the dice are rolled. (You can see the call to showDice at the end of my rollDice function in the first image.) They are pretty straightforward functions reaching into the html to change a value in one case and a css class in the other.diceUpdaters

Usually, my goal is to make small, focused functions like showDice and changeDieColor. Sometimes I just keep cramming functionality in where I know it doesn’t belong just to get things working. I figure I can always come back and separate things out later. I haven’t done that yet, but I could… I promise! It can be hard to hold a lot of discrete parts in my head at the same time and so for the sake of moving forward I do things that I am sure are not best practice. As I get more comfortable with all of this, I hope to be able to make better first-pass decisions and rely less on the idea of fixing things in the future.

In case it wasn’t clear before, I am open to questions and comments and criticism and just about anything else you can think of, so please, feel free to ask or teach or just say hi. The interaction and community are a very powerful motivator for me. Thanks for reading!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s