Skip to content

Commit

Permalink
Add vocabulary quizz
Browse files Browse the repository at this point in the history
  • Loading branch information
Xwilarg committed Jul 11, 2020
1 parent b3faab1 commit 24d05b4
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/xwilarg/yousei/HiraganaLearning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, String>>() {}.type)
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/xwilarg/yousei/KanjiLearning.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<KanjiInfo>::class.java)
}

Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/xwilarg/yousei/LearningType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ package com.xwilarg.yousei

enum class LearningType {
HIRAGANA,
KANJI
KANJI,
VOCABULARY
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/xwilarg/yousei/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/xwilarg/yousei/QuizzCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
}
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/java/com/xwilarg/yousei/VocabularyInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.xwilarg.yousei

data class VocabularyInfo (
val word: String,
val meaning: Array<String>,
val reading: String
)
55 changes: 55 additions & 0 deletions app/src/main/java/com/xwilarg/yousei/VocabularyLearning.kt
Original file line number Diff line number Diff line change
@@ -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<VocabularyInfo>::class.java)
}

override fun getQuestion() : Pair<String, String> {
currentWord = words[Random.nextInt(0, words.size)]
return Pair(currentWord.word, currentWord.reading)
}

override fun checkAnswer(myAnswer: String) : Pair<IsCorrect, String> {
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<String> {
var choices = arrayListOf<String>()
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<VocabularyInfo>
lateinit var currentWord: VocabularyInfo
}
43 changes: 38 additions & 5 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" />

<TextView
android:id="@+id/textTitleVocabulary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vocabulary Learning (JLPT 5)"
android:textSize="25sp"
app:layout_constraintBottom_toTopOf="@+id/guidelineTitle2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down Expand Up @@ -84,17 +95,39 @@
android:layout_height="wrap_content"
android:onClick="startGameFreeKanji"
android:text="Free Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@+id/guidelineTitle2"
app:layout_constraintEnd_toStartOf="@+id/guidelineVertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal" />
app:layout_constraintTop_toTopOf="@+id/guidelineTitle1" />

<Button
android:id="@+id/launchButtonChoicesHiragana2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startGameChoicesKanji"
android:text="Choices"
app:layout_constraintBottom_toTopOf="@+id/guidelineTitle2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guidelineVertical"
app:layout_constraintTop_toTopOf="@+id/guidelineTitle1" />

<Button
android:id="@+id/launchButtonFreeVocabulary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startGameFreeVocabulary"
android:text="Free Text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guidelineVertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guidelineHorizontal" />

<Button
android:id="@+id/launchButtonChoicesVocabulary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startGameChoicesVocabulary"
android:text="Choices"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guidelineVertical"
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions app/src/main/res/raw/vocabulary_jlpt5.json

Large diffs are not rendered by default.

0 comments on commit 24d05b4

Please sign in to comment.