Commit 0888956b by likorn

Duplicate numbers are not allowed in the answer set

parent 23de08e3
......@@ -22,11 +22,11 @@ import org.junit.Before
class MainActivityTest {
@get:Rule
val testRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
private lateinit var radioGroup: RadioGroup
// private lateinit var radioGroup: RadioGroup
@Before
fun init() {
radioGroup = testRule.activity.radio_group
// radioGroup = testRule.activity.radio_group
}
@Test
......
......@@ -3,21 +3,26 @@ package com.example.quickmax
class AnswerSet(numDigits: Int): Iterable<Answer> {
private val buttonIds =
listOf(R.id.btn_left_top, R.id.btn_right_top, R.id.btn_left_bottom, R.id.btn_right_bottom)
private val numOptions = 4
val numbers: MutableList<Answer> =
MutableList(numOptions) { i -> Answer(buttonIds[i], generateRandom(numDigits)) }
val buttonIds = 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 lateinit var correctAnswer: Answer
val answers: MutableList<Answer> = mutableListOf()
init {
val correctAnswer = findSecondMax(numbers.map { n -> n.value })
numbers.forEach { n ->
n.correct = n.value == correctAnswer
val randomNumbers = generateNRandomNumbers(numDigits, numAnswers)
for (i in 0 until numAnswers)
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> {
return numbers.iterator()
return answers.iterator()
}
}
\ No newline at end of file
......@@ -8,6 +8,14 @@ fun generateRandom(numDigits: Int): Int {
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 {
var firstMax = 0
var secondMax = 0
......
......@@ -12,13 +12,13 @@ class AnswerSetTest {
@Test
fun test_constructor() {
assertEquals(numOptions, numberSet.numbers.size)
numberSet.numbers.forEach { n -> n in (100..999) }
assertEquals(numOptions, numberSet.answers.size)
numberSet.answers.forEach { n -> n in (100..999) }
}
@Test
fun isCorrect() {
val secondMax = findSecondMax(numberSet.numbers)
val secondMax = findSecondMax(numberSet.answers)
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