Advertisement
Guest User

Kotlin : Start crash

a guest
Apr 12th, 2020
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 10.76 KB | None | 0 0
  1. // Application package
  2. package com.madijason.quizzer
  3.  
  4. // Import all resources
  5. import android.app.Dialog
  6. import android.content.DialogInterface
  7. import android.content.SharedPreferences
  8. import android.icu.text.CaseMap
  9. import android.support.v7.app.AppCompatActivity
  10. import android.os.Bundle
  11. import android.widget.EditText
  12. import android.widget.Toast
  13. import kotlinx.android.synthetic.main.activity_main.*
  14. import kotlin.random.Random
  15.  
  16. // Beginning of the MainActivity class
  17. class MainActivity : AppCompatActivity() {
  18.  
  19.     // This function is called at the creation of the activity
  20.     override fun onCreate(savedInstanceState: Bundle?) {
  21.         super.onCreate(savedInstanceState)
  22.         setContentView(R.layout.activity_main)
  23.         Toast.makeText(applicationContext, "Welcome on Quizzer !", Toast.LENGTH_SHORT).show()
  24.         setLevel()
  25.         checkAnswer()
  26.     }
  27.  
  28.     // Variables declared
  29.     private val questions = arrayOf<String>("Who is Steve Job ?", "What is Kotlin ?", "What is WHO ?")
  30.     private val firstQuestionAnswer = arrayOf<String>("The creator of Apple", "The creator of Microsoft", "A random guy", "A serial killer")
  31.     private val secondQuestionAnswer = arrayOf<String>("A company", "The best hacker", "The creator of Microsoft", "A thief")
  32.     private val thirdQuestionAnswer = arrayOf<String>("World Humanity Organisation", "A word", "World Health Organisation", "World Heart Ocean")
  33.     private var currentQuestion = 0
  34.     private var userPoint = 0
  35.     private var first_done = false
  36.  
  37.     // This function is called in the "onCreate" for setup the questions
  38.     private fun setLevel() {
  39.         // Take a random number
  40.         val randomNumber = (1..3).random()
  41.         // Check the random number and setup the question
  42.         if (randomNumber == 1) {
  43.             currentQuestion = 1
  44.             question.text = questions[1]
  45.             first_answer.text = firstQuestionAnswer[1]
  46.             second_answer.text = firstQuestionAnswer[2]
  47.             third_answer.text = firstQuestionAnswer[3]
  48.             fourth_answer.text = firstQuestionAnswer[4]
  49.         } else if (randomNumber == 2) {
  50.             currentQuestion = 2
  51.             question.text = questions[2]
  52.             first_answer.text = secondQuestionAnswer[1]
  53.             second_answer.text = secondQuestionAnswer[2]
  54.             third_answer.text = secondQuestionAnswer[3]
  55.             fourth_answer.text = secondQuestionAnswer[4]
  56.         } else if (randomNumber == 3) {
  57.             currentQuestion = 3
  58.             question.text = questions[3]
  59.             first_answer.text = thirdQuestionAnswer[1]
  60.             second_answer.text = thirdQuestionAnswer[2]
  61.             third_answer.text = thirdQuestionAnswer[3]
  62.             fourth_answer.text = thirdQuestionAnswer[4]
  63.         }
  64.     }
  65.  
  66.     // This function is called in the "onCreate" for setup the questions
  67.     // Check answer and change level
  68.     private fun checkAnswer() {
  69.         first_answer.setOnClickListener {
  70.             if (currentQuestion == 1) {
  71.                 userPoint += 1
  72.                 Toast.makeText(applicationContext, "Bravo, you win one coin", Toast.LENGTH_SHORT).show()
  73.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  74.                 // Next level
  75.                 currentQuestion = 2
  76.                 question.text = questions[2]
  77.                 first_answer.text = secondQuestionAnswer[1]
  78.                 second_answer.text = secondQuestionAnswer[2]
  79.                 third_answer.text = secondQuestionAnswer[3]
  80.                 fourth_answer.text = secondQuestionAnswer[4]
  81.                 first_done = true
  82.             } else if (currentQuestion == 2) {
  83.                 userPoint -= 1
  84.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  85.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  86.                 currentQuestion = 3
  87.                 question.text = questions[3]
  88.                 first_answer.text = thirdQuestionAnswer[1]
  89.                 second_answer.text = thirdQuestionAnswer[2]
  90.                 third_answer.text = thirdQuestionAnswer[3]
  91.                 fourth_answer.text = thirdQuestionAnswer[4]
  92.             } else if (currentQuestion == 3) {
  93.                 userPoint -= 1
  94.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  95.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  96.                 if (first_done == false)
  97.                     question.text = questions[1]
  98.                     first_answer.text = firstQuestionAnswer[1]
  99.                     second_answer.text = firstQuestionAnswer[2]
  100.                     third_answer.text = firstQuestionAnswer[3]
  101.                     fourth_answer.text = firstQuestionAnswer[4]
  102.             }
  103.         }
  104.         second_answer.setOnClickListener {
  105.             if (currentQuestion == 1) {
  106.                 userPoint -= 1
  107.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  108.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  109.                 currentQuestion = 2
  110.                 question.text = questions[2]
  111.                 first_answer.text = secondQuestionAnswer[1]
  112.                 second_answer.text = secondQuestionAnswer[2]
  113.                 third_answer.text = secondQuestionAnswer[3]
  114.                 fourth_answer.text = secondQuestionAnswer[4]
  115.             } else if (currentQuestion == 2) {
  116.                 userPoint -= 1
  117.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  118.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  119.                 currentQuestion = 3
  120.                 question.text = questions[3]
  121.                 first_answer.text = thirdQuestionAnswer[1]
  122.                 second_answer.text = thirdQuestionAnswer[2]
  123.                 third_answer.text = thirdQuestionAnswer[3]
  124.                 fourth_answer.text = thirdQuestionAnswer[4]
  125.             } else if (currentQuestion == 3) {
  126.                 userPoint -= 1
  127.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  128.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  129.                 if (first_done == false)
  130.                     question.text = questions[1]
  131.                     first_answer.text = firstQuestionAnswer[1]
  132.                     second_answer.text = firstQuestionAnswer[2]
  133.                     third_answer.text = firstQuestionAnswer[3]
  134.                     fourth_answer.text = firstQuestionAnswer[4]
  135.             }
  136.         }
  137.         third_answer.setOnClickListener {
  138.             if (currentQuestion == 2) {
  139.                 userPoint += 1
  140.                 Toast.makeText(applicationContext, "Bravo, you win one coin", Toast.LENGTH_SHORT).show()
  141.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  142.                 currentQuestion = 3
  143.                 question.text = questions[3]
  144.                 first_answer.text = thirdQuestionAnswer[1]
  145.                 second_answer.text = thirdQuestionAnswer[2]
  146.                 third_answer.text = thirdQuestionAnswer[3]
  147.                 fourth_answer.text = thirdQuestionAnswer[4]
  148.             } else if (currentQuestion == 1) {
  149.                 userPoint -= 1
  150.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  151.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  152.                 currentQuestion = 2
  153.                 question.text = questions[2]
  154.                 first_answer.text = secondQuestionAnswer[1]
  155.                 second_answer.text = secondQuestionAnswer[2]
  156.                 third_answer.text = secondQuestionAnswer[3]
  157.                 fourth_answer.text = secondQuestionAnswer[4]
  158.             } else if (currentQuestion == 3) {
  159.                 userPoint -= 1
  160.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  161.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  162.                 if (first_done == false)
  163.                     question.text = questions[1]
  164.                     first_answer.text = firstQuestionAnswer[1]
  165.                     second_answer.text = firstQuestionAnswer[2]
  166.                     third_answer.text = firstQuestionAnswer[3]
  167.                     fourth_answer.text = firstQuestionAnswer[4]
  168.             }
  169.         }
  170.         fourth_answer.setOnClickListener {
  171.             if (currentQuestion == 1) {
  172.                 userPoint -= 1
  173.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  174.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  175.                 currentQuestion = 2
  176.                 question.text = questions[2]
  177.                 first_answer.text = secondQuestionAnswer[1]
  178.                 second_answer.text = secondQuestionAnswer[2]
  179.                 third_answer.text = secondQuestionAnswer[3]
  180.                 fourth_answer.text = secondQuestionAnswer[4]
  181.             } else if (currentQuestion == 2) {
  182.                 userPoint -= 1
  183.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  184.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  185.                 currentQuestion = 3
  186.                 question.text = questions[3]
  187.                 first_answer.text = thirdQuestionAnswer[1]
  188.                 second_answer.text = thirdQuestionAnswer[2]
  189.                 third_answer.text = thirdQuestionAnswer[3]
  190.                 fourth_answer.text = thirdQuestionAnswer[4]
  191.             } else if (currentQuestion == 3) {
  192.                 userPoint -= 1
  193.                 Toast.makeText(applicationContext, "Wrong, you lose one coin", Toast.LENGTH_SHORT).show()
  194.                 Toast.makeText(applicationContext, "Now, you have : ${userPoint.toString()}", Toast.LENGTH_SHORT).show()
  195.                 if (first_done == false)
  196.                     question.text = questions[1]
  197.                     first_answer.text = firstQuestionAnswer[1]
  198.                     second_answer.text = firstQuestionAnswer[2]
  199.                     third_answer.text = firstQuestionAnswer[3]
  200.                     fourth_answer.text = firstQuestionAnswer[4]
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement