My coding journey: Have you ever played Hangman?

Siobhan Baker
5 min readDec 4, 2018

--

Rediscovering childhood games | Image: Pexels

I began week three at 8th Light by publishing my blogpost synopsis of week two, still buzzing from the joy of getting the coinchanger program to run. The remainder of week three proved pretty challenging, as we attempted to build a well known childhood game — Hangman!

Building a Hangman game…

Monday (PM)

Cue Monday afternoon, and the first challenge was to spike a Hangman game in Ruby. A spike means getting all your ideas out in code in one large “brain dump” if you will, without testing, and trying to get it to work. I spent Monday afternoon on this, trying to get the program to take the user’s guess and check whether that letter matched any of the letters in the word. If so, the program should then tell the user where in the word that letter could be found.

Whilst I was able to get this logic to work, it couldn’t handle the same letter being present in the word more than once, as it would return only the position of the first match.

Tuesday

I continued on Tuesday morning trying to make it so that my code could deal with matching multiples of the same letter, but struggled to figure out how to do this. In the afternoon, we participated in a group code along with one of our mentors Andrew, thinking about how Hangman is played and as such what it is that our program would need to do.

Thinking through the Hangman game with one of our mentors.

This session was super helpful, and after brainstorming the steps on the wall, we began with a few initial tests in Ruby which really made sense of how we might approach building our game. We worked together with Andrew to create our first tests, building a game which was able to map the guessed letter and if matched, show where it is in the word. In essence :-

A guess of ‘j’ when the word is ‘hangman’ would return:

_ _ _ _ _ _ _

Whilst a guess of ‘a’ when the word is ‘hangman’ would return:

_ a _ _ _ a _

Given the above match of ‘a’ occurring more than once in the word, during our code along session we’d already gotten further than I’d been able to in my spike! I continued most of the afternoon thinking through small tests that I could run as I slowly added to the code from our session to expand to a fully working game. The first test I attempted was to add a counter which could track the number of guesses the user had made.

Wednesday

This was the day that I’d looked forward to since the week began. I had hoped that by this point I would have successfully spiked the game and would move to starting to build the program in pairs using TDD. But that’s not quite how the day progressed. Having struggled the previous two days to get a complete working game, I scrapped that and began to pair work on building Hangman using TDD, continuing on from the code along we’d started on Tuesday.

After a brief spell in trying to pair, I continued most of the afternoon working individually trying to think through the guess counter. I began to understand that what I needed was to create two separate methods, one for incorrect guesses and one for correct guesses, to make the distinction in the next actions which needed to be taken. The guesses count could then live within the incorrect guess method. It would count the number of wrong guesses the user had made, minus a life each time they guessed wrongly and when their lives had run out could end the game to reveal the answer. I mobbed with a mentor and fellow apprentice Wednesday afternoon to finish up the lives counter, and by the end of the day had a working game.

Thursday

Having completed the core goals, I moved to stretch goals one and two, managing to get these working quite quickly in the morning. I installed a gem called CleanWords to generate random words which my user could guess. Great, right? Except now all my RSpec tests were failing as I couldn’t predict what the word was and therefore couldn’t be certain what the output was to use in my expect(the output of running a particular method).to eq (this output)tests!

Luckily, there’s a way to fix this. Stub it out!

Snipped and borrowed from https://blog.morizyun.com/blog/mock-stub-outline-rspec-ruby/index.html

Using stub I was able to fix my word to ‘snack’ and run my tests again to make sure the program was continuing to behave as expected by typing:

describe Word dolet(:word_gen) { CleanWords::Random.new } before do allow_any_instance_of(CleanWords::Random).to receive(:fetch).and_return(“snack”) end

A very handy piece of code!

Friday

On the final day of the week I focused on stretch goal three, allowing the user to play again with a new word. I also tidied up my game instructions and the User Interface in the console, using unicode characters to add some emojis and make the game experience more fun.

Highlights from week three…

Highlights this week:

  • discovering stub to set a fixed word which I could use to test my program
  • finding out how to add bullet points and emojis to my console display using unicode characters
  • playing my Hangman game at the end of the week and trying to guess the word! Not once did I get it right! 😆😂

Things I’ve learned:

  • how to add bullet points to my string \u2022
  • how to use more than one class for a program and get the classes to interact with one another to run the game
  • how to use stub for RSpec tests

Things I’ve struggled with:

  • building a Hangman game in spike mode and getting it to work

Things I’m curious about:

  • how I might build a hangman game which draws the hangman image whilst the user plays ⬇️
Image credit: https://github.com/ironhack-labs/lab-canvas-hangman

What I want to focus on:

  • continuing to understand classes

--

--

Siobhan Baker

Apprentice at 8th Light | Speaker | Advocate for leadership routes for black tech/arts professionals, and for bringing the Arts to STEM | bit.ly/sbakerGitHub