Back to the Hangman Game

I’ve sort of taken a break from Ruby for the Holidays. Lots going on, not much time to sit down and try to concentrate. I have taken some time to play around with a little javascript and html but I’m ready to dive back in to learning Ruby.

I had a nice Hangman game working (actually it was just the word guessing part, without the hangman part. The important thing is it was working!). The assignment is to create the game, and then find a way to save a game.

Once I got the game running, I started trying to figure out how I could save it. It makes sense to use Pstore to save the game object. But what pieces exactly do I need to save?

I decided to refactor the code, breaking it up into smaller classes so that the essential parts would be in one class that I could save. And of course things started breaking.

I started out with a GameEngine class and a HangMan class.  This iteration of the game was working fine.

The aim is to get the essential things into one single class. The essential things being:

  • the word to guess
  • the number of turns taken so far
  • letters guessed so far
  • maybe also the blank word that shows the player’s progress?  Or maybe this could be figured upon loading the saved game

I have since broken out a few more classes successfully. The RandomWord class picks a random word. Tonight I started working on a FeedBack class for all the things that are printed out to show the player their progress. This class should:

  • create the blank word (ie, “_ _ _ _ _ _”) which represents the word to be guessed
  • print out the player’s status:  any progress on the word (ie “a _ _ _ _ y”), the number of turns taken, letters guessed so far, etc.

I have had trouble getting all the pieces working together again, as all the references have changed.  What I’ve done is to create a test file which runs RandomWord and FeedBack just to try to get those two classes working again.

I’m thinking about also writing some bona fide tests with Rspec as I go along, but I’m not sure if I should concentrate on that.right now or just try to get the code running.

Leave a Reply

Your email address will not be published. Required fields are marked *