Kaja Blackwing avatar

A Noob's Adventures in Coding: A Game You Cannot Lose

steampunkkaja

Published: 25 Feb 2023 › Updated: 25 Feb 2023A Noob's Adventures in Coding: A Game You Cannot Lose

A Noob's Adventures in Coding: A Game You Cannot Lose

Well, the first part of my programming course has concluded, and for my final project, I submitted an attempt to program a computer to play hangman. For those of you who don't know, this is a game in which one person thinks of a word, and another must guess letters until there are enough letters to guess what the word is. For every incorrect guess, the person who thought up the word draws a single line of a stick figure on the gallows, hence the name of the game. If the entire drawing is completed (usually in six strokes), the game ends.

To set this game up, we must first import the random module, and also a list of words to choose from. For simplicity's sake, I typed them in myself.

image.png

The reason for multiple word libraries is so as to easily add groups of words to the selection pool. The full library can then be modified to exclude certain smaller libraries. For example, if you want to play this game with a small child, I would recommend excluding libraryX from the list for reasons that ought to be obvious. If, on the other hand, you want a challenge, you could exclude libraries of shorter words. The possibilities are endless.

Unfortunately, sometime after this, I made a mistake somewhere. Mine is not a class of perfectionists, and most of my classmates didn't get even half this far (or chose much simpler projects), so I'll fix this at a later date at my leisure. Nonetheless, most of this code functions exactly as intended, but perhaps someone who is more experienced with Python will be able to spot my mistake.

image.png

There were supposed to be only two functions defined here, "guess_letter" and "guess_word." Unfortunately, I couldn't get the the number of guesses to below 5 (from a hard-coded default of 6), it will not set it 0 and break the loop even when I re-define the variable in another function. Thus, the game not only never ends, it is impossible to lose. Everything else functions entirely as intended. I took part of the guess_letter function out and re-defined it as "hang," just to see if there was something I could do to break the gameplay loop. Oddly enough, the portion of the code that I thought would be more difficult works perfectly.

image.png

When the program chooses a word, it will display it as a series of blanks, like so:

image.png

The program will allow the player to enter ‘1’ to guess a single letter, or ‘2’ to guess the entire word. Currently, the game is case-sensitive.

If the letter is in the word, it will be displayed in the correct place. If there is more than one of that letter, all of them will be displayed. Continuing with my test example:

image.png

Next guess:

image.png

Right, at this point it's obvious that the word is "butte," which if you don't already know, is a type of geological formation that looks vaguely like a giant tree stump... and some people apparently think that they literally are, but let's not get into that right now!

image.png

Now the problem, of course, is that the loop is supposed to be broken, and the game ended. It isn't. It gets even worse if I guess letters incorrectly. Here's an example of that:

image.png

Thus, the game runs forever, and you can guess as many times as you want! I tried everything I could think of to fix this, including referencing some of my earlier projects in which similar loops (including countdown loops) ran flawlessly. I'm sick of looking at it, maybe the solution will come to me in a dream, and I'll fix it in five minutes while I'm having my morning coffee tomorrow (stranger things have happened). If that happens, I'll post my solution in a comment below, as I did with the last coding adventure.

Leave A Noob's Adventures in Coding: A Game You Cannot Lose to:

Written by

2D and 3D artist, toymaker, engineer, history buff, scifi/fantasy writer, chicken farmer, arachnophile and all-around nature lover, hunter of deer and mushrooms.

Read more #coding posts


Best Posts From Kaja Blackwing

We have not curated any of steampunkkaja's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From Kaja Blackwing