Commit 0888956b by likorn

Duplicate numbers are not allowed in the answer set

parent 23de08e3
...@@ -22,11 +22,11 @@ import org.junit.Before ...@@ -22,11 +22,11 @@ import org.junit.Before
class MainActivityTest { class MainActivityTest {
@get:Rule @get:Rule
val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java) val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var radioGroup: RadioGroup // private lateinit var radioGroup: RadioGroup
@Before @Before
fun init() { fun init() {
radioGroup = testRule.activity.radio_group // radioGroup = testRule.activity.radio_group
} }
@Test @Test
......
...@@ -3,21 +3,26 @@ package com.example.quickmax ...@@ -3,21 +3,26 @@ package com.example.quickmax
class AnswerSet(numDigits: Int): Iterable<Answer> { class AnswerSet(numDigits: Int): Iterable<Answer> {
private val buttonIds = val buttonIds = listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom)
listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom) private val numAnswers = 4
private val numOptions = 4 private lateinit var correctAnswer: Answer
val numbers: MutableList<Answer> =
MutableList(numOptions) { i -> Answer(buttonIds[i], generateRandom(numDigits)) }
val answers: MutableList<Answer> = mutableListOf()
init { init {
val correctAnswer = findSecondMax(numbers.map { n -> n.value }) val randomNumbers = generateNRandomNumbers(numDigits, numAnswers)
numbers.forEach { n -> for (i in 0 until numAnswers)
n.correct = n.value == correctAnswer answers.add(Answer(buttonIds[i], randomNumbers.elementAt(i)))
val correctAnswerNum = findSecondMax(answers.map { answer -> answer.value })
answers.forEach { n ->
n.correct = n.value == correctAnswerNum
if (n.correct)
correctAnswer = n
} }
} }
override fun iterator(): Iterator<Answer> { override fun iterator(): Iterator<Answer> {
return numbers.iterator() return answers.iterator()
} }
} }
\ No newline at end of file
...@@ -8,6 +8,14 @@ fun generateRandom(numDigits: Int): Int { ...@@ -8,6 +8,14 @@ fun generateRandom(numDigits: Int): Int {
return (start..end).shuffled().first() return (start..end).shuffled().first()
} }
fun generateNRandomNumbers(numDigits: Int, n: Int): Set<Int> {
val randomNumbers = setOf<Int>()
while (randomNumbers.size < n) {
randomNumbers.plus(generateRandom(numDigits))
}
return randomNumbers
}
fun findSecondMax(numbers: List<Int>): Int { fun findSecondMax(numbers: List<Int>): Int {
var firstMax = 0 var firstMax = 0
var secondMax = 0 var secondMax = 0
......
...@@ -12,13 +12,13 @@ class AnswerSetTest { ...@@ -12,13 +12,13 @@ class AnswerSetTest {
@Test @Test
fun test_constructor() { fun test_constructor() {
assertEquals(numOptions, numberSet.numbers.size) assertEquals(numOptions, numberSet.answers.size)
numberSet.numbers.forEach { n -> n in (100..999) } numberSet.answers.forEach { n -> n in (100..999) }
} }
@Test @Test
fun isCorrect() { fun isCorrect() {
val secondMax = findSecondMax(numberSet.numbers) val secondMax = findSecondMax(numberSet.answers)
assertTrue(numberSet.isCorrect(secondMax)) assertTrue(numberSet.isCorrect(secondMax))
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment