Errors in the code of word guessing app

62 views Asked by At

I have tried to make a word guessing game app in iOS, but the app is only able to check the first character of the word. I haven't written the else part as I am stuck in the else part.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var lblTitlleLabelOUTLET: UILabel!
    @IBOutlet weak var lblHintLabelOUTLET: UILabel!
    @IBOutlet weak var lblGuessWordsOUTLET: UILabel!
    @IBOutlet weak var txtTextTakerOUTLET: UITextField!
    @IBOutlet weak var lblGuessedLetterOUTLET: UILabel!
    @IBOutlet weak var lblWordProgressOUTLET: UILabel!

    var listOfHint:[String] = ["The president ",
                         "Yellow fruit",
                         "Extinct animal"]
    var listOfAnswer:[String] = ["donald",
                                 "lemon",
                                "Dinosaur"]

    var listOfWords: [[Character]] = [["d", "o", "n", "a", "l", "d"],
                                     ["l", "e", "m", "o", "n"],
                                     ["d", "i", "n", "o", "s", "a", "u", "r"]]


    var listOfCount: [Int] = [6,
                             5,
                             8]

    var wordProgressShower: [String] = []
    var randomNumber: Int?
    var previousRandomNumber = 4
    var charac :[Character] = []
    var score: Int = 0


    override func viewDidLoad() {
        super.viewDidLoad()
        txtTextTakerOUTLET.delegate = self
        hintGenerator()

    }

    @IBAction func btnNewWordButtonACTION(sender: UIButton) {
        txtTextTakerOUTLET.text?.removeAll()
        score = 0
        hintGenerator()
        }

    func textFieldDidEndEditing(textField: UITextField) {
        let letter = Character(txtTextTakerOUTLET.text!)
        textFie(letter)
        txtTextTakerOUTLET.text?.removeAll()
        }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        txtTextTakerOUTLET.resignFirstResponder()
        txtTextTakerOUTLET.becomeFirstResponder()
        return true

    }


    func hintGenerator(){


        randomNumber = Int(arc4random_uniform(3))
        if(randomNumber == previousRandomNumber){
            hintGenerator()
        }

        previousRandomNumber = randomNumber!
        lblTitlleLabelOUTLET.text = String (randomNumber!)

        lblHintLabelOUTLET.text = listOfHint[randomNumber!] +  " , " + String(listOfCount[randomNumber!]) + " letters"

        for _ in 0..<(listOfCount[randomNumber!]){
            wordProgressShower.append("_ ")
        }
        lblWordProgressOUTLET.text = String(wordProgressShower)

    }

    func textFie(letterGuess: Character ){

        for index in 0..<(listOfCount[randomNumber!]){

            if(listOfWords[randomNumber!][index] == letterGuess){
                score += 1
                wordProgressShower[index] = String(letterGuess)
                lblWordProgressOUTLET.text = String(wordProgressShower)
                lblTitlleLabelOUTLET.text = String(score)

            } // end of if loop
            else {
                break

            }

        } // end of for loop
    } // end of the function
}
0

There are 0 answers