Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Application package
- package com.madijason.quizzer
- // Import all resources
- import android.app.Dialog
- import android.content.DialogInterface
- import android.content.SharedPreferences
- import android.icu.text.CaseMap
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.widget.EditText
- import android.widget.Toast
- import kotlinx.android.synthetic.main.activity_main.*
- import kotlin.random.Random
- // Beginning of the MainActivity class
- class MainActivity : AppCompatActivity() {
- // This function is called at the creation of the activity
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- Toast.makeText(applicationContext, "Welcome on Quizzer !", Toast.LENGTH_SHORT).show()
- setLevel()
- checkAnswer()
- }
- // Variables declared
- private val questions = arrayOf<String>("Who is Steve Job ?", "What is Kotlin ?", "What is WHO ?")
- private val firstQuestionAnswer = arrayOf<String>("The creator of Apple", "The creator of Microsoft", "A random guy", "A serial killer")
- private val secondQuestionAnswer = arrayOf<String>("A company", "The best hacker", "The creator of Microsoft", "A thief")
- private val thirdQuestionAnswer = arrayOf<String>("World Humanity Organisation", "A word", "World Health Organisation", "World Heart Ocean")
- private var currentQuestion = 0
- private var userPoint = 0
- private var first_done = false
- // This function is called in the "onCreate" for setup the questions
- private fun setLevel() {
- // Take a random number
- val randomNumber = (1..3).random()
- // Check the random number and setup the question
- if (randomNumber == 1) {
- currentQuestion = 1
- question.text = questions[1]
- first_answer.text = firstQuestionAnswer[1]
- second_answer.text = firstQuestionAnswer[2]
- third_answer.text = firstQuestionAnswer[3]
- fourth_answer.text = firstQuestionAnswer[4]
- } else if (randomNumber == 2) {
- currentQuestion = 2
- question.text = questions[2]
- first_answer.text = secondQuestionAnswer[1]
- second_answer.text = secondQuestionAnswer[2]
- third_answer.text = secondQuestionAnswer[3]
- fourth_answer.text = secondQuestionAnswer[4]
- } else if (randomNumber == 3) {
- currentQuestion = 3
- question.text = questions[3]
- first_answer.text = thirdQuestionAnswer[1]
- second_answer.text = thirdQuestionAnswer[2]
- third_answer.text = thirdQuestionAnswer[3]
- fourth_answer.text = thirdQuestionAnswer[4]
- }
- }
- // This function is called in the "onCreate" for setup the questions
- // Check answer and change level
- private fun checkAnswer() {
- first_answer.setOnClickListener {
- if (currentQuestion == 1) {
- userPoint += 1
- Toast.makeText(applicationContext, "Bravo, you win one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- // Next level
- currentQuestion = 2
- question.text = questions[2]
- first_answer.text = secondQuestionAnswer[1]
- second_answer.text = secondQuestionAnswer[2]
- third_answer.text = secondQuestionAnswer[3]
- fourth_answer.text = secondQuestionAnswer[4]
- first_done = true
- } else if (currentQuestion == 2) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 3
- question.text = questions[3]
- first_answer.text = thirdQuestionAnswer[1]
- second_answer.text = thirdQuestionAnswer[2]
- third_answer.text = thirdQuestionAnswer[3]
- fourth_answer.text = thirdQuestionAnswer[4]
- } else if (currentQuestion == 3) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- if (first_done == false)
- question.text = questions[1]
- first_answer.text = firstQuestionAnswer[1]
- second_answer.text = firstQuestionAnswer[2]
- third_answer.text = firstQuestionAnswer[3]
- fourth_answer.text = firstQuestionAnswer[4]
- }
- }
- second_answer.setOnClickListener {
- if (currentQuestion == 1) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 2
- question.text = questions[2]
- first_answer.text = secondQuestionAnswer[1]
- second_answer.text = secondQuestionAnswer[2]
- third_answer.text = secondQuestionAnswer[3]
- fourth_answer.text = secondQuestionAnswer[4]
- } else if (currentQuestion == 2) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 3
- question.text = questions[3]
- first_answer.text = thirdQuestionAnswer[1]
- second_answer.text = thirdQuestionAnswer[2]
- third_answer.text = thirdQuestionAnswer[3]
- fourth_answer.text = thirdQuestionAnswer[4]
- } else if (currentQuestion == 3) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- if (first_done == false)
- question.text = questions[1]
- first_answer.text = firstQuestionAnswer[1]
- second_answer.text = firstQuestionAnswer[2]
- third_answer.text = firstQuestionAnswer[3]
- fourth_answer.text = firstQuestionAnswer[4]
- }
- }
- third_answer.setOnClickListener {
- if (currentQuestion == 2) {
- userPoint += 1
- Toast.makeText(applicationContext, "Bravo, you win one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 3
- question.text = questions[3]
- first_answer.text = thirdQuestionAnswer[1]
- second_answer.text = thirdQuestionAnswer[2]
- third_answer.text = thirdQuestionAnswer[3]
- fourth_answer.text = thirdQuestionAnswer[4]
- } else if (currentQuestion == 1) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 2
- question.text = questions[2]
- first_answer.text = secondQuestionAnswer[1]
- second_answer.text = secondQuestionAnswer[2]
- third_answer.text = secondQuestionAnswer[3]
- fourth_answer.text = secondQuestionAnswer[4]
- } else if (currentQuestion == 3) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- if (first_done == false)
- question.text = questions[1]
- first_answer.text = firstQuestionAnswer[1]
- second_answer.text = firstQuestionAnswer[2]
- third_answer.text = firstQuestionAnswer[3]
- fourth_answer.text = firstQuestionAnswer[4]
- }
- }
- fourth_answer.setOnClickListener {
- if (currentQuestion == 1) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 2
- question.text = questions[2]
- first_answer.text = secondQuestionAnswer[1]
- second_answer.text = secondQuestionAnswer[2]
- third_answer.text = secondQuestionAnswer[3]
- fourth_answer.text = secondQuestionAnswer[4]
- } else if (currentQuestion == 2) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- currentQuestion = 3
- question.text = questions[3]
- first_answer.text = thirdQuestionAnswer[1]
- second_answer.text = thirdQuestionAnswer[2]
- third_answer.text = thirdQuestionAnswer[3]
- fourth_answer.text = thirdQuestionAnswer[4]
- } else if (currentQuestion == 3) {
- userPoint -= 1
- Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
- if (first_done == false)
- question.text = questions[1]
- first_answer.text = firstQuestionAnswer[1]
- second_answer.text = firstQuestionAnswer[2]
- third_answer.text = firstQuestionAnswer[3]
- fourth_answer.text = firstQuestionAnswer[4]
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement