vendredi 13 août 2021

I have been trying to get my code to calculate my score in the end of my quiz, without any break through.. HELP please?

class FragmentQuestions : Fragment() { lateinit var binding: FragmentQuestionsBinding lateinit var currentQuestion: Questions private var questionIndex = 0 private val maxNumberOfQuestions = 6 lateinit var answers: ArrayList lateinit var selectedAnswer: String

private var questions = arrayListOf(
    Questions(
        "Who was the former king of pirates ?",
        arrayListOf("Roger", "White beard", "Xebec")
    ),
    Questions(
        "What is known to be the symbol of the ancient kingdom ?",
        ArrayList("The sun", "The moon", "The Bell")
    ),
    Questions(
        "When where we introduced to conquerors haki ?",
        arrayListOf("Foosha Village", "Marine Ford", "Amazon lily")
    ),
    Questions(
        "What character do we know used observation haki first ?",
        arrayListOf("Zoro", "Ussop", "Enel")
    ),
    Questions(
        "What are the winged people in Skypie called ?",
        arrayListOf("Birkas", "Sky people", "Angels")
    ),
    Questions(
        "What was joy boys promise to the fishemen ?",
        ArrayList("To bring them to the surface", "To show them the Forrest", "To destroy their home"
        )
    ),
    Questions(
        "How many kingdoms banded together to declare war to the ancient kingdom ?",
        arrayListOf("20", "40", "50")
    ),
    Questions(
        "What 3 weapons did the ancient kingdom create to battle there enemy's ?",
        arrayListOf("Poseidon,Pluton,Uranos", "Zeus,Pluton,Poseidon", "Armes,Zeus,Pluton")
    ),
    Questions(
        "Who stopped the marine ford war ?",
        arrayListOf("Shanks", "Black Beard", "Kaido")
    ),
    Questions(
        "What arc did we see Zoro accidentally use conquerors haki ?",
        ArrayList("Whisky Peak", "Sabaody", "Punk Hazard")
    ),
    Questions(
        "What fictional comic book is beating One piece for number one spot ?",
        arrayListOf("Super Man", "Demon Slayer", "Attack on titan")
    ),
    Questions(
        "Where did Sanji learn to create fire from his leg ?",
        arrayListOf("Wanze", "Bon Kurei", "Kuroobi")
    ),
    Questions(
        "How many one piece movies have been released do far ?",
        arrayListOf("17", "20", "10")
    ),
    Questions(
        "How many people know what happen to Zoro after the kuma situation ?",
        arrayListOf("4", "2", "3")
    ),
    Questions(
        "How many islands have the strawHats been to ?",
        arrayListOf("20+", "10+", "15+")
    ),
    Questions(
        "What is the name of the man that trained ussop during the 2 year time skip ?",
        ArrayList("Heracles", "Loki", "Moses")
    ),
    Questions(
        "How many shichibukai are there in one piece ?",
        arrayListOf("0", "7", "12")
    ),
    Questions(
        "What did white Beard say to the world, before he died ?",
        ArrayList("one-piece is real", "Roger wasn't waiting for you", "its a sin to die before me")
    ),
    Questions(
        "What character sacrificed them selfs so Luffy could save his brother ?",
        ArrayList("Bon Kurei", "White Beard", "Little oars")
    ),
    Questions(
        "Who is the weakest pirate in the new world ? ",
        arrayListOf("Buggy", "Ussop", "Chopper")
    )


)

var score = 0
private fun setQuestion() {
    currentQuestion = questions[questionIndex]
    answers = ArrayList(currentQuestion.theAnswer)
    answers.shuffle()
    Log.d("ANSWERGROUP", answers[0] + " " + answers[1] + " " + answers[2])
    Log.d("ANSWERREAL", currentQuestion.theAnswer[0])
}

private fun randomQuestion() {
    questions.shuffle()
    setQuestion()
}

//When the answer is checked from the quiz, the score will increase independently.
//Next question will com up
private fun checkTheAnswer(answers: String) {
    if (answers == currentQuestion.theAnswer[0]) {
        score + 1
    }
    questionIndex++
    if (questionIndex<maxNumberOfQuestions) {
        setQuestion()
        binding.invalidateAll()
    } else {
        getTheScore()
    }
}

// adds up my score in the end through the toast method
private fun getTheScore() = if (score >= 3){
    Toast.makeText(activity, "WIN", Toast.LENGTH_SHORT).show()
}else {
    Toast.makeText(activity, "LOST", Toast.LENGTH_SHORT).show()
}



override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = DataBindingUtil.inflate(inflater, R.layout.fragment_questions, container, false)
    randomQuestion()
    binding.quiz = this
    return binding.root
}

// Buttons for the texViews in the XML
override fun onActivityCreated( savedInstanceState: Bundle?) {
    super.onActivityCreated( savedInstanceState)
    binding.Answer1.setOnClickListener {
        checkTheAnswer(toString())
    }

    binding.Answer2.setOnClickListener {
        checkTheAnswer(toString())
    }

    binding.Answer3.setOnClickListener {
        checkTheAnswer(toString())
    }

}


override fun onDestroyView() {
    super.onDestroyView()
    binding
}

}

I am trying to get the toast to work properly, it just keeps showing either win, if I'm wrong or right, or lost if I'm wrong or right after i change the greater symbol

Aucun commentaire:

Enregistrer un commentaire