DEV Community

Cover image for HANGMAN - Bollywood & Hollywood Movies
nptl
nptl

Posted on

HANGMAN - Bollywood & Hollywood Movies

Why Hangman

Back in school, whenever we were stuck with a boring teacher or a boring lesson, we'd turn to playing the guessing game. In our slightly different version of Hangman we guessed only Bollywood movie names. It was one of my favourite games growing up.

Then one day in college while I was smoking up for the first time, I started playing another version of Hangman called Hangaroo with a couple of friends. I was pleasantly surprised that I was guessing almost everything correctly. I remember asking my friends in total amazement if that sudden rush of brains was due to Marie Jane, to which they laughingly, but convincingly said yes. And I fell in love with that game all over again. No wonder, on being tasked to create a game as a part of my Computer Science skill path on Codecademy, I chose Hangman! In my version of Hangman, you can choose a category out of these 3 to play - Bollywood Movies, Hollywood Movies & Famous Tourist Places, and you can change the category after any round.

The Code

Alt Text

I wrote the code on replit which I've connected to a github repo. The current code of the game that you'll find here is the second iteration. The first time I nearly finished writing the code without much pre-planning, I got stuck in the complicated structure I had created and it got too difficult for me to debug. So I scrapped it altogether, planned better and then wrote again. What I've done basically is - identify the most common element which will be displayed to the player often and break it down into components which will be varying with different situations.

Every time the player makes a guess, the following gets printed:

  • The hangman picture
  • A message from the hanging man
  • The status of the word/phrase being guessed
  • Missed characters so far
  • Optional final message - if game over or round won

Alt Text

So I created a class and defined methods for each component of the above output, in addition to various other methods to choose a word randomly from a long list, to check if the guess is correct, to print the final response, etc., so that I could refactor the code as much as possible.

Final Touches

Then at last, for garnishing, I added some ASCII art to open the game with. Checkout this dope page where you can create big ASCII letters in different fonts.
Alt Text

Code on Github

You can find the code for the game here on github.
Check it out and let me know your thoughts in comments.

Top comments (0)