SHARE
TWEET

Untitled

a guest Oct 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.kinderapp.fragments
  2.  
  3. import android.content.Intent
  4. import android.os.CountDownTimer
  5. import android.preference.PreferenceManager
  6. import android.view.View
  7. import android.widget.TextView
  8. import android.widget.Toast
  9. import com.example.kinderapp.R
  10. import kotlinx.android.synthetic.main.fragment_math.*
  11. import java.util.*
  12. import kotlin.collections.ArrayList
  13.  
  14. class MathFragment:BaseFragment(R.layout.fragment_math) {
  15.  
  16.     private var Question: TextView? = null
  17.     private var textViewTimer: TextView? = null
  18.     private var textViewScore: TextView? = null
  19.     private var textViewOpinion0: TextView? = null
  20.     private var textViewOpinion1: TextView? = null
  21.     private var textViewOpinion2: TextView? = null
  22.     private var textViewOpinion3: TextView? = null
  23.     private val options = ArrayList<TextView>()
  24.  
  25.     private var question: String? = null
  26.     private var rightAnswer: Int = 0
  27.     private var rightAnswerPosition: Int = 0
  28.     private var isPositive: Boolean = false
  29.     private val min = 5
  30.     private val max = 30
  31.     private var countOfQuestions = 0
  32.     private var countOfRightAnswers = 0
  33.     private var gameOver = false
  34.  
  35.  
  36.     override fun onCreate() {
  37.          textViewTimer = timer
  38.         textViewOpinion0 = textViewOpinion0
  39.         textViewOpinion1 = textViewOpinion1
  40.         textViewOpinion2 = textViewOpinion2
  41.         textViewOpinion3 = textViewOpinion3
  42.         Question = questions
  43.         textViewScore = score
  44.         playNext()
  45.         val timer = object : CountDownTimer(60000, 1000) {
  46.             override fun onTick(millisUntilFinished: Long) {
  47.                 textViewTimer!!.text = getTime(millisUntilFinished)
  48.                 if (millisUntilFinished < 10000) {
  49.                     textViewTimer!!.setTextColor(resources.getColor(android.R.color.holo_red_light))
  50.                 }
  51.             }
  52.  
  53.             override fun onFinish() {
  54.                 gameOver = true
  55.                 val preferences = PreferenceManager.getDefaultSharedPreferences(view?.context)
  56.                 val max = preferences.getInt("max", 0)
  57.                 if (countOfRightAnswers >= max) {
  58.                     preferences.edit().putInt("max", countOfRightAnswers).apply()
  59.                 }
  60.                 val intent : Intent?=null
  61.                 intent!!.putExtra("result", countOfRightAnswers)
  62.                 startActivity(intent)
  63.             }
  64.         }
  65.         timer.start()
  66.     }
  67.  
  68.     private fun playNext() {
  69.         generateQuestion()
  70.         for (i in options.indices) {
  71.             if (i == rightAnswerPosition) {
  72.                 options[i].text = Integer.toString(rightAnswer)
  73.             } else {
  74.                 options[i].text = Integer.toString(generateWrongAnswer())
  75.             }
  76.         }
  77.         val score = String.format("%s / %s", countOfRightAnswers, countOfQuestions)
  78.         textViewScore!!.setText(score)
  79.     }
  80.  
  81.     private fun generateQuestion() {
  82.         val a = (Math.random() * (max - min + 1) + min).toInt()
  83.         val b = (Math.random() * (max - min + 1) + min).toInt()
  84.         val mark = (Math.random() * 2).toInt()
  85.         isPositive = mark == 1
  86.         if (isPositive) {
  87.             rightAnswer = a + b
  88.             question = String.format("%s + %s", a, b)
  89.         } else {
  90.             rightAnswer = a - b
  91.             question = String.format("%s - %s", a, b)
  92.         }
  93.         Question!!.text = question
  94.         rightAnswerPosition = (Math.random() * 4).toInt()
  95.     }
  96.  
  97.     private fun generateWrongAnswer(): Int {
  98.         var result: Int
  99.         do {
  100.             result = (Math.random() * max.toDouble() * 2.0 + 1).toInt() - (max - min)
  101.         } while (result == rightAnswer)
  102.         return result
  103.     }
  104.  
  105.     private fun getTime(millis: Long): String {
  106.         var seconds = (millis / 1000).toInt()
  107.         val minutes = seconds / 60
  108.         seconds = seconds % 60
  109.         return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)
  110.     }
  111.  
  112.     fun onClickAnswer(view: View) {
  113.         if (!gameOver) {
  114.             val textView = view as TextView
  115.             val answer = textView.text.toString()
  116.             val chosenAnswer = Integer.parseInt(answer)
  117.             if (chosenAnswer == rightAnswer) {
  118.                 countOfRightAnswers++
  119.                 Toast.makeText(view.context, "Верно", Toast.LENGTH_SHORT).show()
  120.             } else {
  121.                 Toast.makeText(view.context, "Неверно", Toast.LENGTH_SHORT).show()
  122.             }
  123.             countOfQuestions++
  124.             playNext()
  125.         }
  126.     }
  127.  
  128.  
  129.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top