Skip to content
  • P
    Projects
  • G
    Groups
  • S
    Snippets
  • Help

likorn / quick_max

  • This project
    • Loading...
  • Sign in
Go to a project
  • Project
  • Repository
  • Issues 0
  • Merge Requests 0
  • Pipelines
  • Wiki
  • Snippets
  • Members
  • Activity
  • Graph
  • Charts
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
  • Files
  • Commits
  • Branches
  • Tags
  • Contributors
  • Graph
  • Compare
  • Charts
Commit 0888956b authored 5 years ago by likorn's avatar likorn
Browse files
Options
  • Browse Files
  • Download
  • Email Patches
  • Plain Diff

Duplicate numbers are not allowed in the answer set

parent 23de08e3 master
Show whitespace changes
Inline Side-by-side
Showing with 30 additions and 15 deletions
  • app/src/androidTest/java/com/example/quickmax/MainActivityTest.kt
  • app/src/main/java/com/example/quickmax/AnswerSet.kt
  • app/src/main/java/com/example/quickmax/Calculator.kt
  • app/src/test/java/com/example/quickmax/AnswerSetTest.kt
app/src/androidTest/java/com/example/quickmax/MainActivityTest.kt
View file @ 0888956b
......@@ -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
......
This diff is collapsed. Click to expand it.
app/src/main/java/com/example/quickmax/AnswerSet.kt
View file @ 0888956b
......@@ -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
This diff is collapsed. Click to expand it.
app/src/main/java/com/example/quickmax/Calculator.kt
View file @ 0888956b
......@@ -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
......
This diff is collapsed. Click to expand it.
app/src/test/java/com/example/quickmax/AnswerSetTest.kt
View file @ 0888956b
......@@ -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
This diff is collapsed. Click to expand it.
  • Write
  • Preview
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