ASCII Hangman

Back in late 2007 I was fresh out of my freshman year of a computer science degree, and thought I knew C++ well enough to build another game. The “Haunted House” game took forever to build and was full of bugs at first, but that was a full semester of classes ago. Now I was ready to make another game, also I was bored and wanted to stay sharp, so I built an ASCII-art Hangman game.

(Update June 2013: The original source code is now available on GitHub! Feel free to download and play with it!)

Thank God I was so thorough in my commenting at the time, because going back through my code in 2007 would have been much difficult if I hadn’t been taught from the start to comment properly. Here’s some notes at the top of my driver file:

Types of things being practiced:
Parsing strings for proper input
Parsing strings for numbers
Using and modifying files
Using linked sorted/unsorted lists
Using linked sorted/unsorted lists that are templated
Using linked sorted/unsorted lists containing other linked lists
Creating ADTs
Clean navigation between game modes
Random drawing
Preventing same item to be drawn randomly twice
User generated content

As seems to be a theme in my projects, I am really afraid of string parsing, and for that reason seem to practice it constantly. Again, as noted in the Haunted House piece, I still had no idea the STL even existed! So I had data structures like templated sorted linked lists built entirely by my own hands. Sheesh!

If you examine the banner image carefully, you’ll see that the user is given 1 of 6 menu choices upon game start: Play Game (versus Computer), Play Game vs Human, Add New Word (to the dictionary), Help, and Quit. I’ll start from the top.

I used a string to gather all user input to avoid the system crashing when given a string when prompted for a number. I thought it worth noting that I was unfamiliar with the atoi() function at the time, and manually parsed numerical values from my strings (which, if my memory serves me right, was not too easy).

At game start, a variety of information is loaded from text files. The “Categories” text file lists names of other text files that contain words related to certain categories. I never really got around to loading a wealth of categories, so my test data consisted of “Movies”, “People”, “Words” with about 5 words in each accompanying text file. When the user wishes to play, the game randomly selects a category, then randomly selects a word from that category and “anonymizes” it to show to the user. Thus, a word like “Brobdingnagian” becomes “_ _ _ _ _ _ _ _ _ _ _ _ _ _ “:

As you can see, in the top of the screen I track Lives, Score, and the # of misses the player has left before they lose another life. After a letter is guessed, it is listed to the player so they don’t make repeat guesses (even though a repeat guess does not hurt them). This is where I started approaching the game with a design philosophy, what game designers call “Pen and Paper” design, where you flesh something out on paper before writing a line of code.

Here’s my initial design:

Pretty simple, right? I had, at least defined a definite order of game progression, the # of tries a player would get, and that I would want to track how many guesses a player has left so they wouldn’t need to wonder whether the little stick guy is wearing shoes or not.

As the player progresses through the game, they accumulate points for finishing words. When they get a word right, a new category and word are loaded, with the system favoring words that have not yet been seen by the player.

When they choose to quit, the system checks a high scores list it loaded in at game start to see if the player has a new high score. If so, they are informed as much and prompted to enter their name in. Old high scores are bumped off the list to make room.

Something I learned very early through my Hallway Testing was that people, including myself want to type stuff like “I, John Doe, am the greatest Hangman player evar!!! :)” into the high score. Heck, if you give someone free reign to type something, they’ll type crap like that no matter what. So, I had to clean the text. I partially used the string cleaning functions I had in place for the Haunted House project, removing numbers and non-character symbols. I just had to limit the player input this time around. See how the game just took the “Andy” part of “Andy G’s Blog”? This is a small, yet significant detail in design, in my opinion.

Okay, jumping out of order while I’m on the topic of High Scores. I had the idea from the start to have some sort of continuing state from game to game, so I came up with the whole idea to track scores and write them to a text file to be loaded in at next game start. This way people could brag about being the best Hangman player to have played my game. So, after pressing ‘3’ at game menu, they see this:

Reliably inserting into and deleting from a sorted linked list created by hand was problematic for me at first, so it was actually a little while later before I ironed out all the bugs in the highscores functionality. Therefore, only my test scores and my roommate Kyle are represented in the table today. Another shout out to Kyle for being a patient tester for every game I’ve made to this point.

The linked list that tracks highscores is A) Sorted by score and B) only permitted to be 7 entries long. So after a new name is inserted, the last entry must be chopped off. As a newbie dealing with pointers, this inevitably led to some invalid pointer reference issues.

It was easy to implement because I had fleshed out exactly how scores and the leaderboard should work during my pen and paper design sessions. Here’s my design notes exactly as I wrote them three years ago:

(click to enlarge)

So now I get to tell you about the two most exciting parts of the project: First, a 2-player game, and second: editing the existing dictionary to add new words and categories.

First, Versus Play!

I always, always made sure I would ask the user if their input was right before I would accept it. Because there’s always a pretty good chance a fast typer hits enter just as he/she realizes they made a mistake. One simple redundancy check for accurate input ensures that the user doesn’t have to quit out of versus mode and get back in just to get the word or category right (which would disrupt gameplay and not be very fun).

I sort of blacklisted all symbols that weren’t letters from the words to make anonymizing and displaying them easier for myself, but if I went back and did it today, the punctuation would stay in, and just not be anonymized. andy gs blog makes less sense than Andy G’s Blog.

Anyway, I digress. In this mode one player gets to enter a category, then a word for that category, and the system does the rest… Player one just needs to make sure Player two isn’t looking at the screen or something while they enter in the word!

After a valid word has been entered, player two plays the game exactly as if they were playing single player mode. If they get it right, player two wins. Looking back at my old code, I literally copy-pasted the code from playing a normal game into playing a versus game. Ach! Nowadays that code, or most of it, would get thrown in its own function to be used by both modes! I think at the time, I was on the cusp of understanding this, but didn’t quite make it.

Instead, the copied code was modified a bit so that the player had no score, still 6 guesses, and only 1 life. It looks great, and it’s easy to transition into if one was familiar with single player-mode, but there is some code waste there that could have been solved by a simple function that took a mode variable as a parameter.

Next comes the final point I wanted to practice at the time: User-generated content. Initially I wanted to have the entire dictionary editable, a player could insert, update, and delete. Instead, the problem seemed too difficult for me at the time, so I settled on just allowing the player to create a new word under a current category, or create a new category with a new word. Indeed, the insert, update, delete part doesn’t sound all that hard today, but back then I just had a little trouble trying to get how I could do it all into my head:

It was cool to see my new content added to the game dynamically:

After this, the user was told to enter in a new word, like so:

Now, I thought about listing out all words in alphabetical order for the person adding words, but at the time rethought to myself that a person could just do that and look at all the existing words to cheat. But this is a bit of a contradiction, right? If you’re giving someone administrative privileges to add words and categories, they should also be able to view and delete existing words and categories!

I ensured that duplicate words couldn’t be entered:

So that’s the gist of the program! Or at least as much as I remember about it. In retrospect it sounds like a conglomeration of a handful of easy concepts that came together to make a really cool project!

Still, I can’t say I was the BEST with pointers or dynamically allocated memory, as there persists a memory leak in the program, who knows, perhaps I’ll sift through the code and fix it!

I’m ready and willing to provide all the source code and/or executable to anyone who is interested. Just contact me at my e-mail address listed on my “About Me” page.

Advertisements

Tags:

20 Responses to “ASCII Hangman”

  1. Paul Says:

    Hi,

    I’m a french 1st Year student in an engineering school specialized in car and planes conception, and during the semester I have to create a Hangman program. Yours seems to be very interesting and although my are not compltely the sames, i’ll be very pleased if you can send me the source code.
    It should give me ideas for my project and help me when I’m blocked.

    Best Regards

    Paul (paulbourgoin@hotmail.fr)

  2. Paul Says:

    *my OBJECTIVES are not completly the sames

    • gieseanw Says:

      Hi Paul,
      I’ll see if I can dig up the source code and send it your way. I just ask that you don’t plagiarize and copy-paste (too much) for your project.

      I must warn you again, I was very early in my Computer Science career when I wrote this, and there is a memory leak somewhere because I had decided to write all my data structures myself instead of using the C++ STL.

      -Andy

  3. Paul Says:

    Hi Andy,

    Thanks for replying, anyway, it doesn’t matter if you don’t find the code because I almost finished to write mine.
    I am just curious to see how you wrote it, and if mabe I missed things or if you had better ideas, my objective is not to copy-past but to learn C++.
    So, if you can send me it, it’will be perfect, but It’s not a vital needs.

  4. Emma Carter Says:

    The game looks great! I’m studying c++ reading the book Accelerated C++ and I’d like to start writing simple ascii games to improve my skills and just for fun, I don’t find any tutorials to do that with ascii characters, if you could pls send me the source code I would use it as a reference (not copy and paste) and I’m sure I can learn something new.

    Thank you anyway!

    Emma

  5. Jacob Says:

    Hi andy,
    I am currently studying computing at As-level (in England) and have been given a dulled down version of this project to do. I am able to do it all except I cannot work out how you draw the hangman to screen. Do you find it using google or physically write it yourself? I would be very grateful if you could help me out with this aspect of it, thank-you.
    Jacob

    • gieseanw Says:

      Hey Jacob, if I recall correctly, this is modified ASCII art from something I found on Google 4 years ago. Feel free to use mine.

      What I did is I think I actually defined the ascii art into a .txt file and read it in to display the title screen. It’s just a fancy use of keyboard characters and newline characters. You could just as easily hardcode it.

    • gieseanw Says:

      Hi Jacob (and anybody else visiting the site),
      The Hangman ASCII art can be found on my GitHub page, which I link to in the opening paragraph.

      -Andy

  6. TimWoo Says:

    Hey Andy,

    Was looking for an ascii representation of hangman and found it :). Hope you don’t mind me using it for the cover of a little document I did for a school assignment of programming hangman for java phones.

  7. TimWoo Says:

    For clarification: I made a screen clipping of the full hangman.

    Cheers,
    TimWoo

  8. Bruno Says:

    Hey!
    wow, this game is fantastic… im a computer science student from brazil… and i’d like to see how the source code works… would you mindo to send me the source?? im practicing on python…;) thx!

    • gieseanw Says:

      Hey Bruno. I’ll see if I can dig them up! In fact, I should work harder at putting all my source in GitHub…

      -Andy

    • gieseanw Says:

      Hey Bruno! I just found my old source code…. and it’s a mess! Like I mentioned in the article, I was completely unfamiliar with the STL at the time. I think I’m going to clean my data structures up, though perhaps keep all the string parsing as-is, and then upload it to my GitHub account and link to it from this blog post.

      -Andy

    • gieseanw Says:

      Actually, scratch that. I think leaving the data structures as-is would be fine (although I will clean up the syntax a bit). It makes for a good demonstration of what someone with 2 semesters of programming should be able to do.

    • gieseanw Says:

      Hey Bruno,
      I’ve uploaded all the source code onto GitHub. Hope this helps!

  9. Satrio Kamaludin Says:

    I’ve tried compiling your source code using codeblocks 12.11 but I get an error syste was not declared in the scope. I’ve tried adding cstdlib.h but it didn’t work……

    • gieseanw Says:

      Hi Satrio, The system header file is for Windows machines. You can safely remove this header, so long as you also remove other system calls like system(“cls”), which I used for clearing the screen. Please forgive my then-sophomore self for using system calls (especially platform-dependent ones). I didn’t know any better. You can replace the calls to clear-screen with a one that prints bunch of newline characters instead.

      -Andy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: