Here comes the Boom…..Part 1.

So having gotten the Hangman,  I got my next assigned project to work on for the next several days.  The idea is to make a Minesweeper like clone.  That seemed easy enough.  Start with a table to create the grid,  create a loop that then assigns a ‘bomb’ class to each square that has a bomb (randomly with:

[code language=”javascript”]

var X = Math.floor(Math.random() * 7 + 1);

var Y = Math.floor(Math.random() * 7 + 1);

[/code]

Then do another loop to go around the bomb square and place a number depending on how many bombs that square is touching.  This proved to be a snag as I could get the loop to go around the squares,  but it was replacing the numbers rather than adding to them.  After a very intimate session with Google,  I finally found the answer:

[code language=”javascript”]

$.fn.counts = function(row, column) {

for (i = row – 1; i <= row + 1; i++) {

for (p = column – 1; p <= column + 1; p++) {

if ($(“#field tr:nth-child(” + i + “) td:nth-child(” + p + “)”).html() == “X”) {

return;

}

else {

$(“#field tr:nth-child(” + i + “) td:nth-child(” + p + “)” ).html(function(m, val) {

return + val + 1;

});

$(“#field tr:nth-child(” + i + “) td:nth-child(” + p + “)” ).css(‘backgroundColor’, ‘green’);

$(“#field tr:nth-child(” + i + “) td:nth-child(” + p +”)” ).removeClass(“space”).addClass(“numbered”);

}

}

}

}

[/code]

(Sorry for the poor formatting,  still have figure out to properly place code in this blog.  It looks A LOT better in my Sublime. )

The loop tells it to go around the ‘Bomb’ squares.  If it has an X (meaning Bomb),  then do nothing to that square.  Else return a value of +1 into that square and change the class of “space” (meaning empty square) to be “numbered”,  a square with a number in it.

So now I have Bombs,  and numbers calculated around the bombs so you know when you are getting close.  The idea was to create a click function that would blow you up if you click on a bomb,  open up a square if it is a number, and if it is a space then do a loop that would keep opening squares until it opened up a numbered square.

 

Concept-wise this proved a good strategy,  but one I tried to put it into practice I ran into many problems.  I created the click for the bombs and numbered squares easily.  The loop for opening squares is where things went awry.  I started with the loop I used above for assigning numbers,  only to check the squares around each and then open them if they are blank or numbered.  Upon first trying this,  things looked like they were working.  But the celebration got cut short after a few more rounds of testing.  It would in fact go through the squares,  but each time it would jump to the new square and apply the ‘Click’,  it seemed to abandon the initial click function.  So instead of opening all squares it would only open the one.

 

I then tried to fix this using a ‘While’ loop,  thinking up some logic of “While the square next to it was empty keep opening.  After several hours of playing with this,  and talking with my support trainers,  we determined the original way that I had planned on doing it would be the best and that we should continue debugging to find out what is wrong.  That is when I got a nice lesson in line by line debugging.  Basically throwing in a debugger at the beginning of the code and stepping through it.  Upon each step,  using the console to verify information (variables, functions, etc.), and even copy and pasting snips of code from the editor (sublime) into the console to see if it in fact runs or errors.

 

This time-consuming process allowed me find one problem,  that the ‘clicks’ went out-of-bounds (off the game board) and thus caused it to hit into an infinite loop.  After setting up a function to check to make sure each ‘click’ was in the boundaries,  things started improving.  Once I fixed that,  and I took at the extra check of “If has class space” (because that was already being handled by the ‘click’),  things worked beautifully.

 

I added a bit more of code such as ability to Command-Click on Macs to mark spots.  Sorry PC people, didn’t include other clicks in this release.  And started the work on the win condition.  It came time to show my work and though it looked pretty good,  there was still a glaring issue.  The fact that I was using Classes and putting the data directly into the cells,  made it not much of a game since you could see where everything was.  The plan was going to be make all the squares black (since the text was black, it would then hide the items) for a simple solution and a more complex solution would be to include a graphic file that would layer on top and be ‘hidden’ on ‘click’.  After showing my progress,  they gave me my next task that would be a better way of handling the last touches of the game.  That is to put the data into an array and use that as reference,  thus only populating the board squares when the click function runs on them.

So tune-in next time when I rebuild the entire game using an array of data.

For this session you can see what I built here: Minesweeper Version 1

Find a tree, we’re hav’n a hang’n!

So the Tic-Tac-Toe game went really well.  After I got over the hiccup it was all smooth sailing and so the next challenge presented itself.  Take what I learned and create a Hangman style game.  And then there I was again confronted with the blank page.  This time I decided to try a new tactic,  throw something up really quick so that way it is no longer a blank page!  So first things first,  I build a couple divs,  add a bit of styling with CSS,  and poof now I have a frame to build something in.  And it’s not as intimidating as a blank page.  Now I can start to plan out the hangman game.

First I look at the components needed to make the game.  First is to have a list of words that the player has to guess,  create an array of words and done.  Next I’ll need the underlines that shows how many letters are there.  For this I decided to do a loop that appends the underlines per the word counts.  Off to a good start so far.  Then the difficult part hit,  the guessing of the letters.  I knew of a few options available such as str.indexOf() and .charAt(),  but that usually just finds the first instance.  For Hangman I’m going to need it to scan the entire word,  and place the letter in the appropriate space.  It took a good deal of searching and trying different things,  but finally found the solution:

// if (theword.indexOf(letter) > -1) {

//       for (var i = 0; i <= theword.length; i++) {

//                    if (theword.charAt(i) == letter) {

//                               $(“#” + i).html(letter);
console.log(“Found it”);
}
}
}

Basically it goes like this:  Take the letter the user entered,  and if it is greater than -1 (remember things start at Zero),  then start looking through the word and if the Character at the point in the loop (i) matches the user’s letter,  then bam you found it.

So now it can find the letters the user enters,  but another problem popped up.  The user can put it in either upper or lower case.  The easy solution to this was to convert it all to uppercase using the dun dun DUUUUuun .toUpperCase() function.

The final steps was to then give the user the ability to guess the word as well as give them a set number or guesses they can make on the letters.  These were pretty easy so I also added a little extra in that the user can see letters they already guessed.  Having come to the end of this session,  I turned it in and excitedly waited for my next challenge.

If you would like to check out the Hangman game (even though it’s not very pretty),  you can do so here:  Hangman

The journey thus far….

So after completing all the online courses I could find (Code Academy, Coursera, TreeHouse, Lynda.com, Udemy, Khan Academy, and even the ‘Hour of Code’ challenge on Code.org),  I found myself in an infinite loop (ha!).  I keep learning the same things over and over,  in different languages,  but I still haven’t really applied it and built anything.  It’s like learning the ability to ask where the bathroom is in many languages,  but never going to another country to ask.

Even though I have multitudes of ideas in my head,  once I get the blank page in front of me the ‘writers block’ takes over and nothing happens.  That is until I met a couple of guys (Eli and David) which were starting their own programming course.  What immediately drew me in was their focus on not just running through the same course work found in other places,  but doing it a project based fashion.  Basically it starts off with a bit of a test,  as their site has several challenges such as ‘Change the background color of this menu’,  ‘Make this section look good using the Bootstrap’, and ‘Align this stuff up using CSS’.

Having done a lot of this already,  I had breezed through it.  That is when the real challenge began.  First up,  was opening the blank page,  that endless black hole of despair that has eaten up so much of my staring at the deep void of its existence.  From here they quickly showed me a few tricks and then said, ‘Okay,  now build a Tic-Tac-Toe game and we’ll meet again in a few days to see what you did’.

Then something amazing happened,  I started writing code.  My mind first worked through the logic of what Tic-Tac-Toe means to a computer.  It needs 3×3 of squares,  which can be selected back and forth by X’s and O’s and once 3 of those line up ‘Bam, Winner!’  I then started off with the front end framework.  Using some HTML and CSS,  I built out the game board and make it look just like I wanted.  Then I started adding the click functions to turn each square into either an X or O depending on the turn.

It seemed to be going great but then the first major problem happened.  As squares were clicked,  the board then when crazy and squares started moving around all over the place.  Well come to find out that I made my first rookie mistake: not getting enough information and making assumptions.  You would think with my years of Product Management that my first thing would be to detail out the entire plan and make sure of the constraints against building it.  But alas excitement of actually building it got the better of me.  I had made an assumption that because we last left off building everything using divs and CSS for aligning them up,  that the board should be built this way.  Once we touched base again,  it was made clear that no you can build it using anything HTML or CSS can do.  And so I started the reconstruction by building it all into a 3×3 table.  From there everything started working out great.  So good in fact that I decided to go above and beyond by adding a scoreboard so you can see how many games each player has won,  along with a ‘Play Again’ button that would clear the board for another game.  All in all I’m pretty proud of my very first development project.

If you want to play some Tic-Tac-Toe you can try it out here: Tic-Tac-Toe

There is more to come so stay tuned!!

~B

Hello world!

Welcome to my little corner of the net.  I decided to start keeping track of everything I’ve learned from being a Product Manager as well as stuff I learn on my journey learning how to program.  Mostly this is for me, just to keep track of my progress,  allow me to reflect on things I’ve accomplished,  as well as have a place to go when I need to remember that certain process or bit of code.  And hey, it helps other people out in the process then it makes it all the more worth it.

So welcome and here is looking forward to a great 2014!!!