NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

Help with the logic of my hangman game...

Forums > Swift

In theory the game is quite simple... something like the user presses "G" button, then I take that "G" and convert it to Character so I can compare it (using a for loop) with the current word he is trying to guess.

Now this is the part where I am struggling. How do I make that exact letter ("G") to show up in a UILabel, at the exact part of the string this letter belongs?

Do I need to use just one UILabel for the words to be guessed? Do I need to use an array of UILabels to display each letter of the word? (This would have to be dynamic and I found a way to do that but it doesn't look right, it does look out of place and I can't make it look good inside my stackview).

So, in short... how do I compare letter ''G'' with the letters of the word "BRING" and make the "G" to show up in the last position inside my UILabel like " G"

   

Hello,

there are certainly a number of ways how to achieve this...

But I would definitely go for one UILabel and just construct the string after each guess. You can store the correct word in a variable and then when building the string to display check already guessed letters and if letter is not present then display _ in its place.

Let me know if you want more hints/help.

Good luck

1      

Have you done Project 5 Wordscramble which does a number of thing to check that word is possible.

    func isPossible(word: String) -> Bool {
        guard var tempWord = title?.lowercased() else { return false }

        for letter in word {
            if let position = tempWord.firstIndex(of: letter) {
                tempWord.remove(at: position)
            } else {
                return false
            }
        }
        return true
    }

this may help you

1      

Thank you for both response guys, I hadn't seen this Projects page before (found word scramble and swifty words).

I think I will watch/read them to get a better understanding of what I am doing and will definetely keep both answers in my mind when I tackle building my app!

Thanks for mentioning the Project 5!

   

Hacking with Swift is sponsored by Paw

SPONSORED Use Paw to build, test and describe web APIs. Paw has a lightning fast native macOS interface to compose requests, collaborate in real-time on API specs, and generate client code for your applications. You can import and export API definitions.

Discover Paw for Mac

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.