Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.kinderapp.fragments
- import android.content.Intent
- import android.os.CountDownTimer
- import android.preference.PreferenceManager
- import android.view.View
- import android.widget.TextView
- import android.widget.Toast
- import com.example.kinderapp.R
- import kotlinx.android.synthetic.main.fragment_math.*
- import java.util.*
- import kotlin.collections.ArrayList
- class MathFragment:BaseFragment(R.layout.fragment_math) {
- private var Question: TextView? = null
- private var textViewTimer: TextView? = null
- private var textViewScore: TextView? = null
- private var textViewOpinion0: TextView? = null
- private var textViewOpinion1: TextView? = null
- private var textViewOpinion2: TextView? = null
- private var textViewOpinion3: TextView? = null
- private val options = ArrayList<TextView>()
- private var question: String? = null
- private var rightAnswer: Int = 0
- private var rightAnswerPosition: Int = 0
- private var isPositive: Boolean = false
- private val min = 5
- private val max = 30
- private var countOfQuestions = 0
- private var countOfRightAnswers = 0
- private var gameOver = false
- override fun onCreate() {
- textViewTimer = timer
- textViewOpinion0 = textViewOpinion0
- textViewOpinion1 = textViewOpinion1
- textViewOpinion2 = textViewOpinion2
- textViewOpinion3 = textViewOpinion3
- Question = questions
- textViewScore = score
- playNext()
- val timer = object : CountDownTimer(60000, 1000) {
- override fun onTick(millisUntilFinished: Long) {
- textViewTimer!!.text = getTime(millisUntilFinished)
- if (millisUntilFinished < 10000) {
- textViewTimer!!.setTextColor(resources.getColor(android.R.color.holo_red_light))
- }
- }
- override fun onFinish() {
- gameOver = true
- val preferences = PreferenceManager.getDefaultSharedPreferences(view?.context)
- val max = preferences.getInt("max", 0)
- if (countOfRightAnswers >= max) {
- preferences.edit().putInt("max", countOfRightAnswers).apply()
- }
- val intent : Intent?=null
- intent!!.putExtra("result", countOfRightAnswers)
- startActivity(intent)
- }
- }
- timer.start()
- }
- private fun playNext() {
- generateQuestion()
- for (i in options.indices) {
- if (i == rightAnswerPosition) {
- options[i].text = Integer.toString(rightAnswer)
- } else {
- options[i].text = Integer.toString(generateWrongAnswer())
- }
- }
- val score = String.format("%s / %s", countOfRightAnswers, countOfQuestions)
- textViewScore!!.setText(score)
- }
- private fun generateQuestion() {
- val a = (Math.random() * (max - min + 1) + min).toInt()
- val b = (Math.random() * (max - min + 1) + min).toInt()
- val mark = (Math.random() * 2).toInt()
- isPositive = mark == 1
- if (isPositive) {
- rightAnswer = a + b
- question = String.format("%s + %s", a, b)
- } else {
- rightAnswer = a - b
- question = String.format("%s - %s", a, b)
- }
- Question!!.text = question
- rightAnswerPosition = (Math.random() * 4).toInt()
- }
- private fun generateWrongAnswer(): Int {
- var result: Int
- do {
- result = (Math.random() * max.toDouble() * 2.0 + 1).toInt() - (max - min)
- } while (result == rightAnswer)
- return result
- }
- private fun getTime(millis: Long): String {
- var seconds = (millis / 1000).toInt()
- val minutes = seconds / 60
- seconds = seconds % 60
- return String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds)
- }
- fun onClickAnswer(view: View) {
- if (!gameOver) {
- val textView = view as TextView
- val answer = textView.text.toString()
- val chosenAnswer = Integer.parseInt(answer)
- if (chosenAnswer == rightAnswer) {
- countOfRightAnswers++
- Toast.makeText(view.context, "Верно", Toast.LENGTH_SHORT).show()
- } else {
- Toast.makeText(view.context, "Неверно", Toast.LENGTH_SHORT).show()
- }
- countOfQuestions++
- playNext()
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement