Entries in Hangman (1)

Sunday
Jul242011

Hangman!!

So, the week wasn't very saturated with learning and/or coding, like the previous one.  Instead I've managed to pull the so far biggest code of my whole study, the 'Hangman' game in C++. :)

Two things.  First, I grow suspicious that it was mentally important to get this game done to get me going forward.  I've never ever tried anything like this before, even during my 'Java days'.  Second, this wasn't really hard!!  Yes, it took about three hours to quickly assemble the code, polish it, and fix bugs.  It'll now take a few more re-runs to check for any other problems, but so far the code seems to be working just fine.

Here's how it went.  First, I've created a very simple dictionary, just a simple text file with words for each letter of the alphabet.  So, I've got 26 words this way.  On a side note, one feature of my code is that if I now do any changes of the dictionary (f.ex. add more words), I do not have to re-write anything, it'll take whatever size there is, and will choose a random word out of that size.  What can I say?  I love 'vector.h'  :)

Then, I was pretty much following my own flow and wrote pieces of code as needed.  I didn't make too many functions (which I definitely could do), and I believe I could've managed to do without the multitude of imports, unusually high in this case, but since stability and speed are not affected, it's all right.

One key thing the whole structure of my program is based on is the entities separation.  I have one array of letters of the word for computer to refer to (it's always open, no hidden letters), and another array initially 'starred' (********), this one is for the player, and as the letters are guessed it reveals the corresponding fields.  This made it very clear and easy to keep track of what the computer 'knows' and what the user sees about the same word.

I've also implemented the alphabet array where I mark what letters the user has already checked, and this allows for warnings like 'You have already used this letter', thus a smarter program.

As I said I am yet to play with the newly created game, but even at this point it produces a very solid and fun result!!  It's been a great day today!