UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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"

2      

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

3      

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

3      

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!

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.