From 24d05b47e3e1c1728dc9b34d5772908426f542a0 Mon Sep 17 00:00:00 2001 From: Xwilarg Date: Sat, 11 Jul 2020 02:47:32 +0200 Subject: [PATCH] Add vocabulary quizz --- .../com/xwilarg/yousei/HiraganaLearning.kt | 4 +- .../java/com/xwilarg/yousei/KanjiLearning.kt | 4 +- .../java/com/xwilarg/yousei/LearningType.kt | 3 +- .../java/com/xwilarg/yousei/MainActivity.kt | 12 ++++ .../java/com/xwilarg/yousei/QuizzCommon.kt | 4 +- .../java/com/xwilarg/yousei/VocabularyInfo.kt | 7 +++ .../com/xwilarg/yousei/VocabularyLearning.kt | 55 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 43 +++++++++++++-- .../res/raw/{jlpt5.json => kanji_jlpt5.json} | 0 app/src/main/res/raw/vocabulary_jlpt5.json | 1 + 10 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/xwilarg/yousei/VocabularyInfo.kt create mode 100644 app/src/main/java/com/xwilarg/yousei/VocabularyLearning.kt rename app/src/main/res/raw/{jlpt5.json => kanji_jlpt5.json} (100%) create mode 100644 app/src/main/res/raw/vocabulary_jlpt5.json diff --git a/app/src/main/java/com/xwilarg/yousei/HiraganaLearning.kt b/app/src/main/java/com/xwilarg/yousei/HiraganaLearning.kt index e5a9af7..23e6251 100644 --- a/app/src/main/java/com/xwilarg/yousei/HiraganaLearning.kt +++ b/app/src/main/java/com/xwilarg/yousei/HiraganaLearning.kt @@ -4,9 +4,9 @@ import com.google.gson.Gson import kotlin.random.Random import com.google.gson.reflect.TypeToken -class HiraganaLearning() : ILearning { +class HiraganaLearning : ILearning { - constructor(content: String) : this() { + constructor(content: String) { hiraganas = Gson().fromJson(content, object : TypeToken>() {}.type) } diff --git a/app/src/main/java/com/xwilarg/yousei/KanjiLearning.kt b/app/src/main/java/com/xwilarg/yousei/KanjiLearning.kt index 5eaa00c..774d2b2 100644 --- a/app/src/main/java/com/xwilarg/yousei/KanjiLearning.kt +++ b/app/src/main/java/com/xwilarg/yousei/KanjiLearning.kt @@ -3,9 +3,9 @@ package com.xwilarg.yousei import com.google.gson.Gson import kotlin.random.Random -class KanjiLearning() : ILearning { +class KanjiLearning : ILearning { - constructor(content: String) : this() { + constructor(content: String) { kanjis = Gson().fromJson(content, Array::class.java) } diff --git a/app/src/main/java/com/xwilarg/yousei/LearningType.kt b/app/src/main/java/com/xwilarg/yousei/LearningType.kt index 20cb382..b9f70f7 100644 --- a/app/src/main/java/com/xwilarg/yousei/LearningType.kt +++ b/app/src/main/java/com/xwilarg/yousei/LearningType.kt @@ -2,5 +2,6 @@ package com.xwilarg.yousei enum class LearningType { HIRAGANA, - KANJI + KANJI, + VOCABULARY } \ No newline at end of file diff --git a/app/src/main/java/com/xwilarg/yousei/MainActivity.kt b/app/src/main/java/com/xwilarg/yousei/MainActivity.kt index e88ff6a..29ae0bf 100644 --- a/app/src/main/java/com/xwilarg/yousei/MainActivity.kt +++ b/app/src/main/java/com/xwilarg/yousei/MainActivity.kt @@ -34,4 +34,16 @@ class MainActivity : AppCompatActivity() { intent.putExtra("LEARNING_TYPE", LearningType.KANJI) startActivity(intent) } + + fun startGameFreeVocabulary(view: View) { + var intent = Intent(this, QuizzNormalActivity::class.java) + intent.putExtra("LEARNING_TYPE", LearningType.VOCABULARY) + startActivity(intent) + } + + fun startGameChoicesVocabulary(view: View) { + var intent = Intent(this, QuizzChoicesActivity::class.java) + intent.putExtra("LEARNING_TYPE", LearningType.VOCABULARY) + startActivity(intent) + } } \ No newline at end of file diff --git a/app/src/main/java/com/xwilarg/yousei/QuizzCommon.kt b/app/src/main/java/com/xwilarg/yousei/QuizzCommon.kt index 2f60710..4b596cd 100644 --- a/app/src/main/java/com/xwilarg/yousei/QuizzCommon.kt +++ b/app/src/main/java/com/xwilarg/yousei/QuizzCommon.kt @@ -15,7 +15,9 @@ open class QuizzCommon : AppCompatActivity() { fun preload() { learning = if (intent.getSerializableExtra("LEARNING_TYPE") == LearningType.KANJI) { - KanjiLearning(this.resources.openRawResource(R.raw.jlpt5).bufferedReader().use { it.readText() }) + KanjiLearning(this.resources.openRawResource(R.raw.kanji_jlpt5).bufferedReader().use { it.readText() }) + } else if (intent.getSerializableExtra("LEARNING_TYPE") == LearningType.VOCABULARY) { + VocabularyLearning(this.resources.openRawResource(R.raw.vocabulary_jlpt5).bufferedReader().use { it.readText() }) } else { HiraganaLearning(this.resources.openRawResource(R.raw.hiragana).bufferedReader().use { it.readText() }) } diff --git a/app/src/main/java/com/xwilarg/yousei/VocabularyInfo.kt b/app/src/main/java/com/xwilarg/yousei/VocabularyInfo.kt new file mode 100644 index 0000000..9b8d0ad --- /dev/null +++ b/app/src/main/java/com/xwilarg/yousei/VocabularyInfo.kt @@ -0,0 +1,7 @@ +package com.xwilarg.yousei + +data class VocabularyInfo ( + val word: String, + val meaning: Array, + val reading: String +) \ No newline at end of file diff --git a/app/src/main/java/com/xwilarg/yousei/VocabularyLearning.kt b/app/src/main/java/com/xwilarg/yousei/VocabularyLearning.kt new file mode 100644 index 0000000..c0e0711 --- /dev/null +++ b/app/src/main/java/com/xwilarg/yousei/VocabularyLearning.kt @@ -0,0 +1,55 @@ +package com.xwilarg.yousei; + +import com.google.gson.Gson +import kotlin.random.Random + +class VocabularyLearning : ILearning { + + constructor(content: String) { + words = Gson().fromJson(content, Array::class.java) + } + + override fun getQuestion() : Pair { + currentWord = words[Random.nextInt(0, words.size)] + return Pair(currentWord.word, currentWord.reading) + } + + override fun checkAnswer(myAnswer: String) : Pair { + var isCorrect = false + var closestAnswer : String? = null + if (!myAnswer.isNullOrBlank()) { + for (m in currentWord .meaning) { + if (myAnswer == m) { + return Pair(IsCorrect.YES, m) + } + if (closestAnswer == null && (myAnswer.contains(m) || m.contains(myAnswer))) { + closestAnswer = m + } + } + } + if (closestAnswer != null) { + return Pair(IsCorrect.PARTIAL, closestAnswer) + } + return Pair(IsCorrect.NO, currentWord.meaning[0]) + } + + override fun getCurrent(): String { + return currentWord.word + } + + override fun getRandomChoices(): ArrayList { + var choices = arrayListOf() + choices.add(currentWord.meaning[0]) + while (choices.size < 4) { + var randomChoice = words[Random.nextInt(0, words.size)].meaning[0] + if (!choices.contains(randomChoice)) { + choices.add(randomChoice) + } + } + choices.shuffle() + return choices + } + + lateinit var words: Array + lateinit var currentWord: VocabularyInfo +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e5d7f3a..f24e301 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -11,7 +11,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hiragana Learning" - android:textSize="30sp" + android:textSize="25sp" app:layout_constraintBottom_toTopOf="@+id/guidelineTitle1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" @@ -21,8 +21,19 @@ android:id="@+id/textTitleKanji" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="Kanji Learning" - android:textSize="30sp" + android:text="Kanji Learning (JLPT 5)" + android:textSize="25sp" + app:layout_constraintBottom_toTopOf="@+id/guidelineHorizontal" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="@+id/guidelineTitle1" /> + + + app:layout_constraintTop_toTopOf="@+id/guidelineTitle1" />