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

in #hive-1635212 years ago

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.

Sort:  

i cant give you my opinion as developer because y develop in Js i have never use python but it is looks awesome and can keep growing that proyect

I know what's going on here and will create a post (as it could be long which may be better read as a whole post instead of a big-arse comment block) which I'll mention you in, to explain what's going on and some coding best practices that I notice could be useful to you.

Thanks, I look forward to it!

Just published! If anything I talk about doesn't make sense please let me know and I'd be happy to go into a deeper explanation.

https://peakd.com/hive-155735/@oblivioncubed/fixing-a-game-you-cannot-lose-some-programming-tips-for-beginners

Oh, I know such a game! As a child, we used to play it, but we just called it the gallows.

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.