Logic Question. How to determine if a string has changed since I last updated it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I am making a hangman game to learn programming.

WHAT IS WORKING

Assume the hidden word is APPLE.

I have it so there is a hidden word in a variable. Also a display string on screen that defaults to ----- (The length of the hidden word, in -'s)

I have a grid of buttons that represent the alphabet.
When one of those buttons is pressed, I have a small loop that compares if the button pressed actually exists in the hidden word. for example, if I press P button the display string will be -PP--

displayWord = ""
		if not word.empty():
			for i in range(word.length()):
				if charactersPressed.has(word[i]):
					displayWord += word[i]
				else:
					displayWord += "_"

Now all that is working just great. I can click buttons, see the word appear. It’s excellent. The above is to get an idea of where I’m going which leads to…

THE PROBLEM

I need to do a check, to see if the user progressed (matched a missing letter) or if they didn’t (they clicked a button that doesn’t match a letter). If they don’t match a letter, the man needs to draw another body part.

I’m trying to wrap my head around how to do this. I created a system like the following:

wordCheckPrev = displayWord
			if wordCheckPrev == get_node("wordLabel").get_text():
failedAttempt += 1

It SORT of works, but if I hit the “restart” button enough times, failedAttempt sometimes increments due to this code according to the debugger. I know something is wrong but I just can’t bend my head that far yet. I am VERY new to programming having only done a codecademy Javascript and Python course.

Any help on this concept would be appreciated. I’m not chasing a code copy as I enjoy learning, more a guide on the principal of how to do this.

Thank you.

:bust_in_silhouette: Reply From: rustyStriker

I couldn’t understand too much what you did, but i did understand that you made it in a really weird way instead of comparing characters.
you can simply use some string manipulations and compare every letter to the letter the player clicked and replace every letter that matches with the _ in the right location.
with it if you are using a loop you can use “break;” to exit the loop and use the loop counter to check if the word has ended without a “break” activated and draw the man.

hope i helped but that is how i believe is the easiest way to make a hangman

Thanks for your answer.

you can simply use some string manipulations and compare every letter
to the letter the player clicked and replace every letter that matches
with the _ in the right location.

That’s what I’ve done here exactly and that works well, which is great.

displayWord = ""
        if not word.empty():
            for i in range(word.length()):
                if charactersPressed.has(word[i]):
                    displayWord += word[i]
                else:
                    displayWord += "_"

What I’m having issues with though is determining if the word has changed since the user last made a guess.

For example:
APPLE is the word
----- is the display word.
User presses A
word has changed (to A----)
User presses X
word does not change (stays at A----)

So that’s the logic I’m looking at trying to solve. How to tell if the word has changed since the last button pressed. If it has NOT changed, then we play out the hanging animations.

I’m guessing I’ll need to have a variable. Maybe called wordCheckPrev for example. If wordCheckPrev doesn’t match the currently displayed word, then the user has progressed. If it does match the current word, then the user must have guessed and not progressed.

This is what I’m struggling with.

Robster | 2016-09-25 02:24

OK I think I have it.

Immediately after button is pressed (A…Z), make wordCheckPrev the value of the current word. EG A----.

THEN redraw the string, then afterwards check to see if the string and wordCheckPrev match or not. Seems so obvious. I’ll place code here once I get it working.

Robster | 2016-09-25 07:14