Skip to content

Commit

Permalink
Experiment with dynamic year ranges.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrant committed Jan 10, 2025
1 parent 3fa5fc3 commit b755358
Showing 1 changed file with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import java.time.LocalDate
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.random.Random

Expand Down Expand Up @@ -67,16 +68,20 @@ class OnThisDayGameViewModel(bundle: Bundle) : ViewModel() {
allEvents.shuffle(Random(currentMonth * 100 + currentDay))

events.clear()
// Take an event from the list, and find another event that is the closest by year.
// Take an event from the list, and find another event that is within a certain range
for (i in 0 until MAX_QUESTIONS) {
val event1 = allEvents.removeAt(0)
var event2: OnThisDay.Event? = null
var minDiff = Int.MAX_VALUE
for (event in allEvents) {
val diff = abs(event1.year - event.year)
if (diff < minDiff) {
minDiff = diff
event2 = event
var yearSpread = max((390 - (0.19043 * event1.year)).toInt(), 5)
event2 = allEvents.find { abs(event1.year - it.year) <= yearSpread}
if (event2 == null) {
var minDiff = Int.MAX_VALUE
for (event in allEvents) {
val diff = abs(event1.year - event.year)
if (diff < minDiff) {
minDiff = diff
event2 = event
}
}
}
event2?.let {
Expand Down

0 comments on commit b755358

Please sign in to comment.