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!

So close…

I have made a lot of small steps in the last few days. My game board is mostly functional now. At some point yesterday, I realized that I needed a punch list to help me focus and so I sat down (ok, I was already sitting) and wrote one. It looked something like this:

  • dice roller
  • check for legal move
  • check for bearing off
  • check for game over
  • jail rules
  • correctly stack checkers
  • optional stuff
    • score
    • player login
    • starting variations
    • A.I.
    • Hints & Help

A sizable but manageable list. I found that it helped to have smaller tasks to do so I could stop thinking about the big picture. I could pick an item and work on it exclusively and not worry about how I am going to do, well, anything else. I also kind of gave up on keeping my code “clean” and just started writing. Rather than getting bogged down on two levels, figuring out how to make it work and how to keep it tidy, I could just make it work.  As a consequence, I am much further along that I was two days ago, and my pace has picked up considerably. My punch list now looks like this:

  • dice roller  (partially, need to cycle through dice and deal with doubles)
  • check for legal move
  • check for bearing off
  • check for game over
  • jail rules
  • correctly stack checkers (technically finished, but I now need to correctly unstack them!)
  • input options (mouse/touch)
  • optional stuff
    • score
    • player login
    • starting variations
    • A.I.
    • Hints & Help

Here’s an example of some jail rules:


White has captured a black piece here…


… and Black has deployed her jailed piece and captured a White piece.

Finally, code samples!

I guess I have put it off long enough. At some point I knew I would have to show other pfilledBoardeople my code, as embarrassingly naive and novice as it is. So let’s get it out of the way in one fell swoop. Here is everything I have done. It is sloppy, messy, not well commented, hacked together and sometimes difficult for no reason. And I’m okay with that.

My earliest work is in the index.html and the top of the css file. It is basically me working through the battleship game in the Head First book, with some added flourishes. The meat of my own work is in the two backgammon files. That is where, so far, I have made my most creative mistakes.

Here is my board with some, uh…, test features turned on. The board itself is an image and the playing field is composed of several tables that I have aligned to the board. Then each celcellCSSl of the table has one of three css classes applied to it. .fillWhite uses an image, confusingly enough, an orange checker. .fillBlack, even more confusingly, makes the entire cell white (note to self, change that to a black fill… done). Finally there is .empty that, for testing purposes makes the cell grey.


After moving some of the checkers a few times.

Aside from setting up the board, the user can also move checkers using an input box. This is another stepping stone/test function while I work out the real input scheme. I just recently got the move function to turn the origin cell to empty instead of just “adding” a new checker to the board in the destination position. I also have the moves hardcoded to 2 spots and flipping between the players every move. It’s confusing, but it works for now.

There are a lot of things to talk about with even just this early stage and simplistic implementation. Design and decision choices. A couple questions I can answer easily together. Why am I using a table for the board? Why am I using an input box to move the checkers? Because I ripped those ideas right from the battleship game. I, essentially, don’t know of another way to do it. In fact, this is a good time to talk about my overarching design philosophy for this particular project. I aim to get it done and make it work however I can. I can always rewrite, but for now I just want to see this through.

I do try to keep my functions small and simple whenever I have the brainpower to do so. One place that shows well is in the functions to fill and empty cells. I pass in the minimum amount of information the need and update the view. Here they are:


That’s it. I have run into some issues. To clear the entire board with the emptyCell function, I need to feed it the id of every cell. That means either storing all of the cell values in an array or building a function to create those values and feed them to emptyCell. Also, who knows how many other creative solutions I haven’t thought of. What I have decided to do instead is utilize jQuery, with mixed success. I’ll figure it out. At least half of the “fun” of coding for me has been troubleshooting. I see a lot of troubleshooting in my future!

Ready, set, go!

There are a lot of ways to learn to code. There are a lot of ways to actually write code. There are a lot of things to think about. It can be… it is overwhelming. “Start there,” was my mandate, and with that in mind, I decided I need to quickly find the lowest resistance approaches.

I knew immediately that books, as a first step, were not going to work. There are just too many, with varying levels of prior experience assumed and no quick and easy way to determine if they even offered what I would need. I would soon circle back to this avenue, but for now I knew it would only bog me down.

Formal education. I bring this up even though by this stage I had already discarded it as a method for me because I think that under other circumstances, this would have been more attractive. That is probably a topic for later discussion, but my timeline ruled this out too.

The most attractive path for me, then, was informal education. For this I turned to the internet. One website in particular came up repeatedly. Codecademy. It turns out, this is where I would start. I should give a special mention for another website that shares a lot of the same features of Codecademy, and that primed me to learn in this fashion. Anyone who has investigated interactive, gamified learning on the internet has surely run into Khan Academy.

A few years ago, a friend asked me to help her son with his math homework. I figured I could handle pre-algebra, even though it has been a long time since I cracked open a math textbook. But just in case, I started looking for online resources. If nothing else, I could crib the methods to help me teach the material. That led me to Khan Academy, and I raced through the material, eventually progressing further than I ever got at school. I never did finish the lessons in Linear Algebra (sorry Matt, I really wanted to be able to help!), but Khan Academy is still one of my most used bookmarks.

Codecademy follows a similar model. Focused lessons with a gradual progression, and rewarding badges and challenges. Compared to Khan Academy, Codecademy is almost Spartan, but that is actually one of its major plusses. There aren’t a lot of distractions or roadblocks or hurdles. Sign up and start learning. The front page emphasizes a somewhat holistic approach to learning, with the more general Web Developer lessons at the top and then language specific courses below.

For me, Codecademy was the perfect introduction to Web Development. Self-paced, with easy navigation and just enough positive reinforcement to keep me motivated without being patronizing or cloying. I spent around three weeks sprinting through lessons whenever I had the time. Pretty soon I was feeling comfortable with the three main “languages” in their core web dev section; HTML, CSS and JavaScript. Not an expert, by any means. In fact, probably just enough knowledge to be dangerous, but I knew that I still didn’t know enough to know what I didn’t know!

It was time for the next step, but once again, I wasn’t sure what that step should be. Books have been a big part of my life since early childhood. The feel of holding a book and flipping through the pages is a very relaxing and comforting act for me. I still didn’t know how to pick and choose, and I didn’t need a dozen books of varying usefulness sitting on my shelves. And that’s how I ended up in a bookstore, monopolizing the twelve feet or so of programming shelf space. Once again, however, I realized that I still didn’t know what to look for, but I at least felt like I was getting closer…

Next time, I’ll talk about finding the right tools and, big surprise, the right books.

How did I get here?

Over the years, I have dabbled with coding. I have started and stopped learning C++, WPF, .net, even python. They were interesting and difficult and in a lot of ways fascinating, but I didn’t have the drive to push through, hunker down and learn them. Sure, I have plenty of excuses. I had a full-time job running my own electrical contracting business. Then my son was born and I became a stay-at-home parent, which, it turns out, is even more full-time than a full-time job. More recently, my daughter was born, and so my free-time has grown into an even more precious commodity.

So, why now? The brutal, up-front, honest answer is, I need marketable skills, because I need to work. For the last few months, I have been deciding what I want to do with my life and realizing the hole I have dug myself into by not finishing college and not seeking a career before now. I realized that I need a job that has some level of creativity and will keep me mentally stimulated. I’d also love to be able to help people. And then there is the variable I call “time to acquire.” How long will it take to go from zero to having a job in the field?

Most of the things that piqued my interest had long or uber-competitive processes. Firefighter/Paramedic, nursing, teaching, engineering. No matter how “good” I got at the skills required, I was going to need formal education. This led me to consider a new strategy. Get an intermediate job and pursue an education. This is still the most reasonable approach I can think of, but I had one more idea.

My brother and my best friend both work as professional software developers. They have both encouraged me, for years, to learn how to code. They have both been in positions to interview and hire developers. I wondered what they thought my chances would be? Could someone with no formal education in the field, but a command the material get a job as a developer? So I discussed it with them. “Yes, but,” was the answer they both gave, immediately. This was good, I could work with this. Over the course of the next few days, we talked about it more in-depth. Then we got to work on a plan, and it wasn’t at all what I expected.

Both of my new career counselors have spent a lot of time neck-deep in the Microsoft development world. Not exclusively, they are both knowledge seekers first and try to keep their toolboxes current. However, their years of experience gave me certain expectations about what advice they would give me. I was dead wrong. Both of them recommended that I give web and mobile development a shot.

“You mean, HTML and CSS?” I asked.

“Yes, and JavaScript. There will be more, but start there.”

Start there. So I did.

Next time, I’ll talk about how I narrowed down the confusing forest of choices and focused on some very select trees.